]> icculus.org git repositories - divverent/darkplaces.git/blob - vid_shared.c
now checks tangent vector crossproduct against surface normal to see if the tangents...
[divverent/darkplaces.git] / vid_shared.c
1
2 #include "quakedef.h"
3
4 // global video state
5 viddef_t vid;
6
7 // LordHavoc: these are only set in wgl
8 qboolean isG200 = false; // LordHavoc: the Matrox G200 can't do per pixel alpha, and it uses a D3D driver for GL... ugh...
9 qboolean isRagePro = false; // LordHavoc: the ATI Rage Pro has limitations with per pixel alpha (the color scaler does not apply to per pixel alpha images...), although not as bad as a G200.
10
11 // AK FIXME -> input_dest
12 qboolean in_client_mouse = true;
13
14 // AK where should it be placed ?
15 float in_mouse_x, in_mouse_y;
16
17 // GL_ARB_multitexture
18 int gl_textureunits = 1;
19 // GL_ARB_texture_env_combine or GL_EXT_texture_env_combine
20 int gl_combine_extension = false;
21 // GL_EXT_compiled_vertex_array
22 int gl_supportslockarrays = false;
23 // GLX_SGI_video_sync or WGL_EXT_swap_control
24 int gl_videosyncavailable = false;
25 // stencil available
26 int gl_stencil = false;
27 // 3D textures available
28 int gl_texture3d = false;
29 // GL_ARB_texture_cubemap
30 int gl_texturecubemap = false;
31 // GL_ARB_texture_env_dot3
32 int gl_dot3arb = false;
33 // GL_SGIS_texture_edge_clamp
34 int gl_support_clamptoedge = false;
35 // GL_NV_vertex_array_range
36 int gl_support_var = false;
37 // GL_NV_vertex_array_range2
38 int gl_support_var2 = false;
39 // GL_EXT_texture_filter_anisotropic
40 int gl_support_anisotropy = false;
41 int gl_max_anisotropy = 1;
42 // GL_NV_texture_shader
43 int gl_textureshader = false;
44 // GL_EXT_stencil_two_side
45 int gl_support_stenciltwoside = false;
46
47 // LordHavoc: if window is hidden, don't update screen
48 int vid_hidden = true;
49 // LordHavoc: if window is not the active window, don't hog as much CPU time,
50 // let go of the mouse, turn off sound, and restore system gamma ramps...
51 int vid_activewindow = true;
52
53 // we don't know until we try it!
54 cvar_t vid_hardwaregammasupported = {CVAR_READONLY,"vid_hardwaregammasupported","1"};
55 // whether hardware gamma ramps are currently in effect
56 int vid_usinghwgamma = false;
57
58 unsigned short vid_gammaramps[768];
59 unsigned short vid_systemgammaramps[768];
60
61 cvar_t vid_fullscreen = {CVAR_SAVE, "vid_fullscreen", "1"};
62 cvar_t vid_width = {CVAR_SAVE, "vid_width", "640"};
63 cvar_t vid_height = {CVAR_SAVE, "vid_height", "480"};
64 cvar_t vid_bitsperpixel = {CVAR_SAVE, "vid_bitsperpixel", "32"};
65
66 cvar_t vid_mouse = {CVAR_SAVE, "vid_mouse", "1"};
67 cvar_t gl_combine = {CVAR_SAVE, "gl_combine", "1"};
68 cvar_t gl_finish = {0, "gl_finish", "0"};
69
70 cvar_t in_pitch_min = {0, "in_pitch_min", "-70"};
71 cvar_t in_pitch_max = {0, "in_pitch_max", "80"};
72
73 cvar_t m_filter = {CVAR_SAVE, "m_filter","0"};
74
75 cvar_t v_gamma = {CVAR_SAVE, "v_gamma", "1"};
76 cvar_t v_contrast = {CVAR_SAVE, "v_contrast", "1"};
77 cvar_t v_brightness = {CVAR_SAVE, "v_brightness", "0"};
78 cvar_t v_color_enable = {CVAR_SAVE, "v_color_enable", "0"};
79 cvar_t v_color_black_r = {CVAR_SAVE, "v_color_black_r", "0"};
80 cvar_t v_color_black_g = {CVAR_SAVE, "v_color_black_g", "0"};
81 cvar_t v_color_black_b = {CVAR_SAVE, "v_color_black_b", "0"};
82 cvar_t v_color_grey_r = {CVAR_SAVE, "v_color_grey_r", "0.5"};
83 cvar_t v_color_grey_g = {CVAR_SAVE, "v_color_grey_g", "0.5"};
84 cvar_t v_color_grey_b = {CVAR_SAVE, "v_color_grey_b", "0.5"};
85 cvar_t v_color_white_r = {CVAR_SAVE, "v_color_white_r", "1"};
86 cvar_t v_color_white_g = {CVAR_SAVE, "v_color_white_g", "1"};
87 cvar_t v_color_white_b = {CVAR_SAVE, "v_color_white_b", "1"};
88 cvar_t v_hwgamma = {CVAR_SAVE, "v_hwgamma", "1"};
89 cvar_t v_psycho = {0, "v_psycho", "0"};
90
91 // brand of graphics chip
92 const char *gl_vendor;
93 // graphics chip model and other information
94 const char *gl_renderer;
95 // begins with 1.0.0, 1.1.0, 1.2.0, 1.2.1, 1.3.0, 1.3.1, or 1.4.0
96 const char *gl_version;
97 // extensions list, space separated
98 const char *gl_extensions;
99 // WGL, GLX, or AGL
100 const char *gl_platform;
101 // another extensions list, containing platform-specific extensions that are
102 // not in the main list
103 const char *gl_platformextensions;
104 // name of driver library (opengl32.dll, libGL.so.1, or whatever)
105 char gl_driver[256];
106
107 // GL_ARB_multitexture
108 void (GLAPIENTRY *qglMultiTexCoord2f) (GLenum, GLfloat, GLfloat);
109 void (GLAPIENTRY *qglMultiTexCoord3f) (GLenum, GLfloat, GLfloat, GLfloat);
110 void (GLAPIENTRY *qglActiveTexture) (GLenum);
111 void (GLAPIENTRY *qglClientActiveTexture) (GLenum);
112
113 // GL_EXT_compiled_vertex_array
114 void (GLAPIENTRY *qglLockArraysEXT) (GLint first, GLint count);
115 void (GLAPIENTRY *qglUnlockArraysEXT) (void);
116
117 //GL_NV_vertex_array_range
118 GLvoid *(GLAPIENTRY *qglAllocateMemoryNV)(GLsizei size, GLfloat readFrequency, GLfloat writeFrequency, GLfloat priority);
119 GLvoid (GLAPIENTRY *qglFreeMemoryNV)(GLvoid *pointer);
120 GLvoid (GLAPIENTRY *qglVertexArrayRangeNV)(GLsizei length, GLvoid *pointer);
121 GLvoid (GLAPIENTRY *qglFlushVertexArrayRangeNV)(GLvoid);
122
123 // general GL functions
124
125 void (GLAPIENTRY *qglClearColor)(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
126
127 void (GLAPIENTRY *qglClear)(GLbitfield mask);
128
129 void (GLAPIENTRY *qglAlphaFunc)(GLenum func, GLclampf ref);
130 void (GLAPIENTRY *qglBlendFunc)(GLenum sfactor, GLenum dfactor);
131 void (GLAPIENTRY *qglCullFace)(GLenum mode);
132
133 void (GLAPIENTRY *qglDrawBuffer)(GLenum mode);
134 void (GLAPIENTRY *qglReadBuffer)(GLenum mode);
135 void (GLAPIENTRY *qglEnable)(GLenum cap);
136 void (GLAPIENTRY *qglDisable)(GLenum cap);
137 GLboolean (GLAPIENTRY *qglIsEnabled)(GLenum cap);
138
139 void (GLAPIENTRY *qglEnableClientState)(GLenum cap);
140 void (GLAPIENTRY *qglDisableClientState)(GLenum cap);
141
142 void (GLAPIENTRY *qglGetBooleanv)(GLenum pname, GLboolean *params);
143 void (GLAPIENTRY *qglGetDoublev)(GLenum pname, GLdouble *params);
144 void (GLAPIENTRY *qglGetFloatv)(GLenum pname, GLfloat *params);
145 void (GLAPIENTRY *qglGetIntegerv)(GLenum pname, GLint *params);
146
147 GLenum (GLAPIENTRY *qglGetError)(void);
148 const GLubyte* (GLAPIENTRY *qglGetString)(GLenum name);
149 void (GLAPIENTRY *qglFinish)(void);
150 void (GLAPIENTRY *qglFlush)(void);
151
152 void (GLAPIENTRY *qglClearDepth)(GLclampd depth);
153 void (GLAPIENTRY *qglDepthFunc)(GLenum func);
154 void (GLAPIENTRY *qglDepthMask)(GLboolean flag);
155 void (GLAPIENTRY *qglDepthRange)(GLclampd near_val, GLclampd far_val);
156 void (GLAPIENTRY *qglColorMask)(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha);
157
158 void (GLAPIENTRY *qglDrawRangeElements)(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices);
159 void (GLAPIENTRY *qglDrawElements)(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
160 void (GLAPIENTRY *qglVertexPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
161 void (GLAPIENTRY *qglNormalPointer)(GLenum type, GLsizei stride, const GLvoid *ptr);
162 void (GLAPIENTRY *qglColorPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
163 void (GLAPIENTRY *qglTexCoordPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
164 void (GLAPIENTRY *qglArrayElement)(GLint i);
165
166 void (GLAPIENTRY *qglColor4f)(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);
167 void (GLAPIENTRY *qglTexCoord2f)(GLfloat s, GLfloat t);
168 void (GLAPIENTRY *qglTexCoord2f)(GLfloat s, GLfloat t);
169 void (GLAPIENTRY *qglTexCoord3f)(GLfloat s, GLfloat t, GLfloat r);
170 void (GLAPIENTRY *qglVertex2f)(GLfloat x, GLfloat y);
171 void (GLAPIENTRY *qglVertex3f)(GLfloat x, GLfloat y, GLfloat z);
172 void (GLAPIENTRY *qglBegin)(GLenum mode);
173 void (GLAPIENTRY *qglEnd)(void);
174
175 void (GLAPIENTRY *qglMatrixMode)(GLenum mode);
176 void (GLAPIENTRY *qglOrtho)(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val);
177 void (GLAPIENTRY *qglFrustum)(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val);
178 void (GLAPIENTRY *qglViewport)(GLint x, GLint y, GLsizei width, GLsizei height);
179 void (GLAPIENTRY *qglPushMatrix)(void);
180 void (GLAPIENTRY *qglPopMatrix)(void);
181 void (GLAPIENTRY *qglLoadIdentity)(void);
182 void (GLAPIENTRY *qglLoadMatrixd)(const GLdouble *m);
183 void (GLAPIENTRY *qglLoadMatrixf)(const GLfloat *m);
184 void (GLAPIENTRY *qglMultMatrixd)(const GLdouble *m);
185 void (GLAPIENTRY *qglMultMatrixf)(const GLfloat *m);
186 void (GLAPIENTRY *qglRotated)(GLdouble angle, GLdouble x, GLdouble y, GLdouble z);
187 void (GLAPIENTRY *qglRotatef)(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
188 void (GLAPIENTRY *qglScaled)(GLdouble x, GLdouble y, GLdouble z);
189 void (GLAPIENTRY *qglScalef)(GLfloat x, GLfloat y, GLfloat z);
190 void (GLAPIENTRY *qglTranslated)(GLdouble x, GLdouble y, GLdouble z);
191 void (GLAPIENTRY *qglTranslatef)(GLfloat x, GLfloat y, GLfloat z);
192
193 void (GLAPIENTRY *qglReadPixels)(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels);
194
195 void (GLAPIENTRY *qglStencilFunc)(GLenum func, GLint ref, GLuint mask);
196 void (GLAPIENTRY *qglStencilMask)(GLuint mask);
197 void (GLAPIENTRY *qglStencilOp)(GLenum fail, GLenum zfail, GLenum zpass);
198 void (GLAPIENTRY *qglClearStencil)(GLint s);
199
200 void (GLAPIENTRY *qglTexEnvf)(GLenum target, GLenum pname, GLfloat param);
201 void (GLAPIENTRY *qglTexEnvfv)(GLenum target, GLenum pname, const GLfloat *params);
202 void (GLAPIENTRY *qglTexEnvi)(GLenum target, GLenum pname, GLint param);
203 void (GLAPIENTRY *qglTexParameterf)(GLenum target, GLenum pname, GLfloat param);
204 void (GLAPIENTRY *qglTexParameterfv)(GLenum target, GLenum pname, GLfloat *params);
205 void (GLAPIENTRY *qglTexParameteri)(GLenum target, GLenum pname, GLint param);
206
207 void (GLAPIENTRY *qglGenTextures)(GLsizei n, GLuint *textures);
208 void (GLAPIENTRY *qglDeleteTextures)(GLsizei n, const GLuint *textures);
209 void (GLAPIENTRY *qglBindTexture)(GLenum target, GLuint texture);
210 //void (GLAPIENTRY *qglPrioritizeTextures)(GLsizei n, const GLuint *textures, const GLclampf *priorities);
211 //GLboolean (GLAPIENTRY *qglAreTexturesResident)(GLsizei n, const GLuint *textures, GLboolean *residences);
212 GLboolean (GLAPIENTRY *qglIsTexture)(GLuint texture);
213 void (GLAPIENTRY *qglPixelStoref)(GLenum pname, GLfloat param);
214 void (GLAPIENTRY *qglPixelStorei)(GLenum pname, GLint param);
215
216 void (GLAPIENTRY *qglTexImage1D)(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid *pixels);
217 void (GLAPIENTRY *qglTexImage2D)(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels);
218 void (GLAPIENTRY *qglTexSubImage1D)(GLenum target, GLint level, GLint xoffset, GLsizei width, GLenum format, GLenum type, const GLvoid *pixels);
219 void (GLAPIENTRY *qglTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels);
220 void (GLAPIENTRY *qglCopyTexImage1D)(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLint border);
221 void (GLAPIENTRY *qglCopyTexImage2D)(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border);
222 void (GLAPIENTRY *qglCopyTexSubImage1D)(GLenum target, GLint level, GLint xoffset, GLint x, GLint y, GLsizei width);
223 void (GLAPIENTRY *qglCopyTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height);
224
225
226 void (GLAPIENTRY *qglDrawRangeElementsEXT)(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices);
227
228 //void (GLAPIENTRY *qglColorTableEXT)(int, int, int, int, int, const void *);
229
230 void (GLAPIENTRY *qglTexImage3D)(GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels);
231 void (GLAPIENTRY *qglTexSubImage3D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels);
232 void (GLAPIENTRY *qglCopyTexSubImage3D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height);
233
234 void (GLAPIENTRY *qglScissor)(GLint x, GLint y, GLsizei width, GLsizei height);
235
236 void (GLAPIENTRY *qglPolygonOffset)(GLfloat factor, GLfloat units);
237
238 void (GLAPIENTRY *qglActiveStencilFaceEXT)(GLenum);
239
240 int GL_CheckExtension(const char *name, const dllfunction_t *funcs, const char *disableparm, int silent)
241 {
242         int failed = false;
243         const dllfunction_t *func;
244
245         Con_DPrintf("checking for %s...  ", name);
246
247         for (func = funcs;func && func->name;func++)
248                 *func->funcvariable = NULL;
249
250         if (disableparm && (COM_CheckParm(disableparm) || COM_CheckParm("-safe")))
251         {
252                 Con_DPrint("disabled by commandline\n");
253                 return false;
254         }
255
256         if (strstr(gl_extensions, name) || strstr(gl_platformextensions, name) || (strncmp(name, "GL_", 3) && strncmp(name, "WGL_", 4) && strncmp(name, "GLX_", 4) && strncmp(name, "AGL_", 4)))
257         {
258                 for (func = funcs;func && func->name != NULL;func++)
259                 {
260                         // functions are cleared before all the extensions are evaluated
261                         if (!(*func->funcvariable = (void *) GL_GetProcAddress(func->name)))
262                         {
263                                 if (!silent)
264                                         Con_Printf("OpenGL extension \"%s\" is missing function \"%s\" - broken driver!\n", name, func->name);
265                                 failed = true;
266                         }
267                 }
268                 // delay the return so it prints all missing functions
269                 if (failed)
270                         return false;
271                 Con_DPrint("enabled\n");
272                 return true;
273         }
274         else
275         {
276                 Con_DPrint("not detected\n");
277                 return false;
278         }
279 }
280
281 static dllfunction_t opengl110funcs[] =
282 {
283         {"glClearColor", (void **) &qglClearColor},
284         {"glClear", (void **) &qglClear},
285         {"glAlphaFunc", (void **) &qglAlphaFunc},
286         {"glBlendFunc", (void **) &qglBlendFunc},
287         {"glCullFace", (void **) &qglCullFace},
288         {"glDrawBuffer", (void **) &qglDrawBuffer},
289         {"glReadBuffer", (void **) &qglReadBuffer},
290         {"glEnable", (void **) &qglEnable},
291         {"glDisable", (void **) &qglDisable},
292         {"glIsEnabled", (void **) &qglIsEnabled},
293         {"glEnableClientState", (void **) &qglEnableClientState},
294         {"glDisableClientState", (void **) &qglDisableClientState},
295         {"glGetBooleanv", (void **) &qglGetBooleanv},
296         {"glGetDoublev", (void **) &qglGetDoublev},
297         {"glGetFloatv", (void **) &qglGetFloatv},
298         {"glGetIntegerv", (void **) &qglGetIntegerv},
299         {"glGetError", (void **) &qglGetError},
300         {"glGetString", (void **) &qglGetString},
301         {"glFinish", (void **) &qglFinish},
302         {"glFlush", (void **) &qglFlush},
303         {"glClearDepth", (void **) &qglClearDepth},
304         {"glDepthFunc", (void **) &qglDepthFunc},
305         {"glDepthMask", (void **) &qglDepthMask},
306         {"glDepthRange", (void **) &qglDepthRange},
307         {"glDrawElements", (void **) &qglDrawElements},
308         {"glColorMask", (void **) &qglColorMask},
309         {"glVertexPointer", (void **) &qglVertexPointer},
310         {"glNormalPointer", (void **) &qglNormalPointer},
311         {"glColorPointer", (void **) &qglColorPointer},
312         {"glTexCoordPointer", (void **) &qglTexCoordPointer},
313         {"glArrayElement", (void **) &qglArrayElement},
314         {"glColor4f", (void **) &qglColor4f},
315         {"glTexCoord2f", (void **) &qglTexCoord2f},
316         {"glTexCoord3f", (void **) &qglTexCoord3f},
317         {"glVertex2f", (void **) &qglVertex2f},
318         {"glVertex3f", (void **) &qglVertex3f},
319         {"glBegin", (void **) &qglBegin},
320         {"glEnd", (void **) &qglEnd},
321         {"glMatrixMode", (void **) &qglMatrixMode},
322         {"glOrtho", (void **) &qglOrtho},
323         {"glFrustum", (void **) &qglFrustum},
324         {"glViewport", (void **) &qglViewport},
325         {"glPushMatrix", (void **) &qglPushMatrix},
326         {"glPopMatrix", (void **) &qglPopMatrix},
327         {"glLoadIdentity", (void **) &qglLoadIdentity},
328         {"glLoadMatrixd", (void **) &qglLoadMatrixd},
329         {"glLoadMatrixf", (void **) &qglLoadMatrixf},
330         {"glMultMatrixd", (void **) &qglMultMatrixd},
331         {"glMultMatrixf", (void **) &qglMultMatrixf},
332         {"glRotated", (void **) &qglRotated},
333         {"glRotatef", (void **) &qglRotatef},
334         {"glScaled", (void **) &qglScaled},
335         {"glScalef", (void **) &qglScalef},
336         {"glTranslated", (void **) &qglTranslated},
337         {"glTranslatef", (void **) &qglTranslatef},
338         {"glReadPixels", (void **) &qglReadPixels},
339         {"glStencilFunc", (void **) &qglStencilFunc},
340         {"glStencilMask", (void **) &qglStencilMask},
341         {"glStencilOp", (void **) &qglStencilOp},
342         {"glClearStencil", (void **) &qglClearStencil},
343         {"glTexEnvf", (void **) &qglTexEnvf},
344         {"glTexEnvfv", (void **) &qglTexEnvfv},
345         {"glTexEnvi", (void **) &qglTexEnvi},
346         {"glTexParameterf", (void **) &qglTexParameterf},
347         {"glTexParameterfv", (void **) &qglTexParameterfv},
348         {"glTexParameteri", (void **) &qglTexParameteri},
349         {"glPixelStoref", (void **) &qglPixelStoref},
350         {"glPixelStorei", (void **) &qglPixelStorei},
351         {"glGenTextures", (void **) &qglGenTextures},
352         {"glDeleteTextures", (void **) &qglDeleteTextures},
353         {"glBindTexture", (void **) &qglBindTexture},
354 //      {"glPrioritizeTextures", (void **) &qglPrioritizeTextures},
355 //      {"glAreTexturesResident", (void **) &qglAreTexturesResident},
356         {"glIsTexture", (void **) &qglIsTexture},
357         {"glTexImage1D", (void **) &qglTexImage1D},
358         {"glTexImage2D", (void **) &qglTexImage2D},
359         {"glTexSubImage1D", (void **) &qglTexSubImage1D},
360         {"glTexSubImage2D", (void **) &qglTexSubImage2D},
361         {"glCopyTexImage1D", (void **) &qglCopyTexImage1D},
362         {"glCopyTexImage2D", (void **) &qglCopyTexImage2D},
363         {"glCopyTexSubImage1D", (void **) &qglCopyTexSubImage1D},
364         {"glCopyTexSubImage2D", (void **) &qglCopyTexSubImage2D},
365         {"glScissor", (void **) &qglScissor},
366         {"glPolygonOffset", (void **) &qglPolygonOffset},
367         {NULL, NULL}
368 };
369
370 static dllfunction_t drawrangeelementsfuncs[] =
371 {
372         {"glDrawRangeElements", (void **) &qglDrawRangeElements},
373         {NULL, NULL}
374 };
375
376 static dllfunction_t drawrangeelementsextfuncs[] =
377 {
378         {"glDrawRangeElementsEXT", (void **) &qglDrawRangeElementsEXT},
379         {NULL, NULL}
380 };
381
382 static dllfunction_t multitexturefuncs[] =
383 {
384         {"glMultiTexCoord2fARB", (void **) &qglMultiTexCoord2f},
385         {"glMultiTexCoord3fARB", (void **) &qglMultiTexCoord3f},
386         {"glActiveTextureARB", (void **) &qglActiveTexture},
387         {"glClientActiveTextureARB", (void **) &qglClientActiveTexture},
388         {NULL, NULL}
389 };
390
391 static dllfunction_t compiledvertexarrayfuncs[] =
392 {
393         {"glLockArraysEXT", (void **) &qglLockArraysEXT},
394         {"glUnlockArraysEXT", (void **) &qglUnlockArraysEXT},
395         {NULL, NULL}
396 };
397
398 static dllfunction_t texture3dextfuncs[] =
399 {
400         {"glTexImage3DEXT", (void **) &qglTexImage3D},
401         {"glTexSubImage3DEXT", (void **) &qglTexSubImage3D},
402         {"glCopyTexSubImage3DEXT", (void **) &qglCopyTexSubImage3D},
403         {NULL, NULL}
404 };
405
406 static dllfunction_t glxvarfuncs[] =
407 {
408         {"glXAllocateMemoryNV", (void **) &qglAllocateMemoryNV},
409         {"glXFreeMemoryNV", (void **) &qglFreeMemoryNV},
410         {"glVertexArrayRangeNV", (void **) &qglVertexArrayRangeNV},
411         {"glFlushVertexArrayRangeNV", (void **) &qglFlushVertexArrayRangeNV},
412         {NULL, NULL}
413 };
414
415 static dllfunction_t wglvarfuncs[] =
416 {
417         {"wglAllocateMemoryNV", (void **) &qglAllocateMemoryNV},
418         {"wglFreeMemoryNV", (void **) &qglFreeMemoryNV},
419         {"glVertexArrayRangeNV", (void **) &qglVertexArrayRangeNV},
420         {"glFlushVertexArrayRangeNV", (void **) &qglFlushVertexArrayRangeNV},
421         {NULL, NULL}
422 };
423
424 static dllfunction_t stenciltwosidefuncs[] =
425 {
426         {"glActiveStencilFaceEXT", (void **) &qglActiveStencilFaceEXT},
427         {NULL, NULL}
428 };
429
430
431 void VID_CheckExtensions(void)
432 {
433         gl_stencil = vid_bitsperpixel.integer == 32;
434
435         // reset all the gl extension variables here
436         // this should match the declarations
437         gl_textureunits = 1;
438         gl_combine_extension = false;
439         gl_supportslockarrays = false;
440         gl_videosyncavailable = false;
441         gl_texture3d = false;
442         gl_texturecubemap = false;
443         gl_dot3arb = false;
444         gl_support_clamptoedge = false;
445         gl_support_var = false;
446         gl_support_var2 = false;
447         gl_support_anisotropy = false;
448         gl_max_anisotropy = 1;
449         gl_textureshader = false;
450         gl_support_stenciltwoside = false;
451
452         if (!GL_CheckExtension("OpenGL 1.1.0", opengl110funcs, NULL, false))
453                 Sys_Error("OpenGL 1.1.0 functions not found\n");
454
455         Con_DPrintf("GL_VENDOR: %s\n", gl_vendor);
456         Con_DPrintf("GL_RENDERER: %s\n", gl_renderer);
457         Con_DPrintf("GL_VERSION: %s\n", gl_version);
458         Con_DPrintf("GL_EXTENSIONS: %s\n", gl_extensions);
459         Con_DPrintf("%s_EXTENSIONS: %s\n", gl_platform, gl_platformextensions);
460
461         Con_DPrint("Checking OpenGL extensions...\n");
462
463         if (!GL_CheckExtension("glDrawRangeElements", drawrangeelementsfuncs, "-nodrawrangeelements", true))
464                 GL_CheckExtension("GL_EXT_draw_range_elements", drawrangeelementsextfuncs, "-nodrawrangeelements", false);
465
466         if (GL_CheckExtension("GL_ARB_multitexture", multitexturefuncs, "-nomtex", false))
467         {
468                 qglGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_textureunits);
469                 gl_combine_extension = GL_CheckExtension("GL_ARB_texture_env_combine", NULL, "-nocombine", false) || GL_CheckExtension("GL_EXT_texture_env_combine", NULL, "-nocombine", false);
470                 if (gl_combine_extension)
471                         gl_dot3arb = GL_CheckExtension("GL_ARB_texture_env_dot3", NULL, "-nodot3", false);
472         }
473
474         gl_texture3d = GL_CheckExtension("GL_EXT_texture3D", texture3dextfuncs, "-notexture3d", false);
475         gl_texturecubemap = GL_CheckExtension("GL_ARB_texture_cube_map", NULL, "-nocubemap", false);
476         gl_supportslockarrays = GL_CheckExtension("GL_EXT_compiled_vertex_array", compiledvertexarrayfuncs, "-nocva", false);
477         gl_support_clamptoedge = GL_CheckExtension("GL_EXT_texture_edge_clamp", NULL, "-noedgeclamp", false) || GL_CheckExtension("GL_SGIS_texture_edge_clamp", NULL, "-noedgeclamp", false);
478
479         if (!strcmp(gl_platform, "GLX"))
480                 gl_support_var = GL_CheckExtension("GL_NV_vertex_array_range", glxvarfuncs, "-novar", false);
481         else if (!strcmp(gl_platform, "WGL"))
482                 gl_support_var = GL_CheckExtension("GL_NV_vertex_array_range", wglvarfuncs, "-novar", false);
483         if (gl_support_var)
484                 gl_support_var2 = GL_CheckExtension("GL_NV_vertex_array_range2", NULL, "-novar2", false);
485
486         if ((gl_support_anisotropy = GL_CheckExtension("GL_EXT_texture_filter_anisotropic", NULL, "-noanisotropy", false)))
487                 qglGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &gl_max_anisotropy);
488
489         gl_textureshader = GL_CheckExtension("GL_NV_texture_shader", NULL, "-notextureshader", false);
490
491         gl_support_stenciltwoside = GL_CheckExtension("GL_EXT_stencil_two_side", stenciltwosidefuncs, "-nostenciltwoside", false);
492
493         // we don't care if it's an extension or not, they are identical functions, so keep it simple in the rendering code
494         if (qglDrawRangeElements == NULL)
495                 qglDrawRangeElements = qglDrawRangeElementsEXT;
496 }
497
498 int vid_vertexarrays_are_var = false;
499 void *VID_AllocVertexArrays(mempool_t *pool, int size, int fast, float readfrequency, float writefrequency, float priority)
500 {
501         void *m;
502         vid_vertexarrays_are_var = false;
503         if (fast && qglAllocateMemoryNV)
504         {
505                 CHECKGLERROR
506                 m = qglAllocateMemoryNV(size, readfrequency, writefrequency, priority);
507                 CHECKGLERROR
508                 if (m)
509                 {
510                         vid_vertexarrays_are_var = true;
511                         return m;
512                 }
513         }
514         return Mem_Alloc(pool, size);
515 }
516
517 void VID_FreeVertexArrays(void *pointer)
518 {
519         if (vid_vertexarrays_are_var)
520         {
521                 CHECKGLERROR
522                 qglFreeMemoryNV(pointer);
523                 CHECKGLERROR
524         }
525         else
526                 Mem_Free(pointer);
527         vid_vertexarrays_are_var = false;
528 }
529
530 void Force_CenterView_f (void)
531 {
532         cl.viewangles[PITCH] = 0;
533 }
534
535 void IN_PreMove(void)
536 {
537 }
538
539 void CL_AdjustAngles(void);
540 void IN_PostMove(void)
541 {
542         // clamp after the move as well to prevent messed up rendering angles
543         CL_AdjustAngles();
544 }
545
546 /*
547 ===========
548 IN_DoMove
549 ===========
550 */
551 void IN_ProcessMove(usercmd_t *cmd)
552 {
553         // get basic movement from keyboard
554         CL_BaseMove(cmd);
555
556         // OS independent code
557         IN_PreMove();
558
559         // allow mice or other external controllers to add to the move
560         IN_Move(cmd);
561
562         // OS independent code
563         IN_PostMove();
564 }
565
566
567 void IN_Mouse(usercmd_t *cmd, float mx, float my)
568 {
569         int mouselook = (in_mlook.state & 1) || freelook.integer;
570         float mouse_x, mouse_y;
571         static float old_mouse_x = 0, old_mouse_y = 0;
572
573         if (m_filter.integer)
574         {
575                 mouse_x = (mx + old_mouse_x) * 0.5;
576                 mouse_y = (my + old_mouse_y) * 0.5;
577         }
578         else
579         {
580                 mouse_x = mx;
581                 mouse_y = my;
582         }
583
584         old_mouse_x = mx;
585         old_mouse_y = my;
586
587         in_mouse_x = (float) mouse_x * vid.conwidth / vid.realwidth;
588         in_mouse_y = (float) mouse_y * vid.conheight / vid.realheight;
589
590         // AK: eveything else is client stuff
591         // BTW, this should be seperated somewhen
592         if(!in_client_mouse)
593                 return;
594
595         // LordHavoc: viewzoom affects mouse sensitivity for sniping
596         mouse_x *= sensitivity.value * cl.viewzoom;
597         mouse_y *= sensitivity.value * cl.viewzoom;
598
599         // Add mouse X/Y movement to cmd
600         if ((in_strafe.state & 1) || (lookstrafe.integer && mouselook))
601                 cmd->sidemove += m_side.value * mouse_x;
602         else
603                 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
604
605         if (mouselook)
606                 V_StopPitchDrift();
607
608         if (mouselook && !(in_strafe.state & 1))
609                 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
610         else
611         {
612                 if ((in_strafe.state & 1) && noclip_anglehack)
613                         cmd->upmove -= m_forward.value * mouse_y;
614                 else
615                         cmd->forwardmove -= m_forward.value * mouse_y;
616         }
617 }
618
619 static float cachegamma, cachebrightness, cachecontrast, cacheblack[3], cachegrey[3], cachewhite[3];
620 static int cachecolorenable, cachehwgamma;
621 #define BOUNDCVAR(cvar, m1, m2) c = &(cvar);f = bound(m1, c->value, m2);if (c->value != f) Cvar_SetValueQuick(c, f);
622 void VID_UpdateGamma(qboolean force)
623 {
624         cvar_t *c;
625         float f;
626         static int forcenextframe = false;
627
628         // LordHavoc: don't mess with gamma tables if running dedicated
629         if (cls.state == ca_dedicated)
630                 return;
631
632         if (!force
633          && !forcenextframe
634          && !v_psycho.integer
635          && vid_usinghwgamma == (vid_activewindow && v_hwgamma.integer)
636          && v_gamma.value == cachegamma
637          && v_contrast.value == cachecontrast
638          && v_brightness.value == cachebrightness
639          && cachecolorenable == v_color_enable.integer
640          && cacheblack[0] == v_color_black_r.value
641          && cacheblack[1] == v_color_black_g.value
642          && cacheblack[2] == v_color_black_b.value
643          && cachegrey[0] == v_color_grey_r.value
644          && cachegrey[1] == v_color_grey_g.value
645          && cachegrey[2] == v_color_grey_b.value
646          && cachewhite[0] == v_color_white_r.value
647          && cachewhite[1] == v_color_white_g.value
648          && cachewhite[2] == v_color_white_b.value)
649                 return;
650                 
651         forcenextframe = false;
652
653         if (vid_activewindow && v_hwgamma.integer)
654         {
655                 if (!vid_usinghwgamma)
656                 {
657                         vid_usinghwgamma = true;
658                         Cvar_SetValueQuick(&vid_hardwaregammasupported, VID_GetGamma(vid_systemgammaramps));
659                 }
660
661                 BOUNDCVAR(v_gamma, 0.1, 5);cachegamma = v_gamma.value;
662                 BOUNDCVAR(v_contrast, 1, 5);cachecontrast = v_contrast.value;
663                 BOUNDCVAR(v_brightness, 0, 0.8);cachebrightness = v_brightness.value;
664                 BOUNDCVAR(v_color_black_r, 0, 0.8);cacheblack[0] = v_color_black_r.value;
665                 BOUNDCVAR(v_color_black_g, 0, 0.8);cacheblack[1] = v_color_black_g.value;
666                 BOUNDCVAR(v_color_black_b, 0, 0.8);cacheblack[2] = v_color_black_b.value;
667                 BOUNDCVAR(v_color_grey_r, 0, 0.95);cachegrey[0] = v_color_grey_r.value;
668                 BOUNDCVAR(v_color_grey_g, 0, 0.95);cachegrey[1] = v_color_grey_g.value;
669                 BOUNDCVAR(v_color_grey_b, 0, 0.95);cachegrey[2] = v_color_grey_b.value;
670                 BOUNDCVAR(v_color_white_r, 1, 5);cachewhite[0] = v_color_white_r.value;
671                 BOUNDCVAR(v_color_white_g, 1, 5);cachewhite[1] = v_color_white_g.value;
672                 BOUNDCVAR(v_color_white_b, 1, 5);cachewhite[2] = v_color_white_b.value;
673                 cachecolorenable = v_color_enable.integer;
674                 cachehwgamma = v_hwgamma.integer;
675
676                 if (cachecolorenable)
677                 {
678                         BuildGammaTable16(1.0f, invpow(0.5, 1 - cachegrey[0]), cachewhite[0], cacheblack[0], vid_gammaramps);
679                         BuildGammaTable16(1.0f, invpow(0.5, 1 - cachegrey[1]), cachewhite[1], cacheblack[1], vid_gammaramps + 256);
680                         BuildGammaTable16(1.0f, invpow(0.5, 1 - cachegrey[2]), cachewhite[2], cacheblack[2], vid_gammaramps + 512);
681                 }
682                 else
683                 {
684                         BuildGammaTable16(1.0f, cachegamma, cachecontrast, cachebrightness, vid_gammaramps);
685                         BuildGammaTable16(1.0f, cachegamma, cachecontrast, cachebrightness, vid_gammaramps + 256);
686                         BuildGammaTable16(1.0f, cachegamma, cachecontrast, cachebrightness, vid_gammaramps + 512);
687                 }
688
689                 // LordHavoc: this code came from Ben Winslow and Zinx Verituse, I have
690                 // immensely butchered it to work with variable framerates and fit in with
691                 // the rest of darkplaces.
692                 if (v_psycho.integer)
693                 {
694                         int x, y;
695                         float t;
696                         static float n[3], nd[3], nt[3];
697                         static int init = true;
698                         unsigned short *ramp;
699                         forcenextframe = true;
700                         if (init)
701                         {
702                                 init = false;
703                                 for (x = 0;x < 3;x++)
704                                 {
705                                         n[x] = lhrandom(0, 1);
706                                         nd[x] = (rand()&1)?-0.25:0.25;
707                                         nt[x] = lhrandom(1, 8.2);
708                                 }
709                         }
710                         
711                         for (x = 0;x < 3;x++)
712                         {
713                                 nt[x] -= host_realframetime;
714                                 if (nt[x] < 0)
715                                 {
716                                         nd[x] = -nd[x];
717                                         nt[x] += lhrandom(1, 8.2);
718                                 }
719                                 n[x] += nd[x] * host_realframetime;
720                                 n[x] -= floor(n[x]);
721                         }
722                 
723                         for (x = 0, ramp = vid_gammaramps;x < 3;x++)
724                                 for (y = 0, t = n[x] - 0.75f;y < 256;y++, t += 0.75f * (2.0f / 256.0f))
725                                         *ramp++ = cos(t*(M_PI*2.0)) * 32767.0f + 32767.0f;
726                 }
727
728                 Cvar_SetValueQuick(&vid_hardwaregammasupported, VID_SetGamma(vid_gammaramps));
729         }
730         else
731         {
732                 if (vid_usinghwgamma)
733                 {
734                         vid_usinghwgamma = false;
735                         Cvar_SetValueQuick(&vid_hardwaregammasupported, VID_SetGamma(vid_systemgammaramps));
736                 }
737         }
738 }
739
740 void VID_RestoreSystemGamma(void)
741 {
742         if (vid_usinghwgamma)
743         {
744                 vid_usinghwgamma = false;
745                 VID_SetGamma(vid_systemgammaramps);
746         }
747 }
748
749 void VID_Shared_Init(void)
750 {
751         Cvar_RegisterVariable(&vid_hardwaregammasupported);
752         Cvar_RegisterVariable(&v_gamma);
753         Cvar_RegisterVariable(&v_brightness);
754         Cvar_RegisterVariable(&v_contrast);
755
756         Cvar_RegisterVariable(&v_color_enable);
757         Cvar_RegisterVariable(&v_color_black_r);
758         Cvar_RegisterVariable(&v_color_black_g);
759         Cvar_RegisterVariable(&v_color_black_b);
760         Cvar_RegisterVariable(&v_color_grey_r);
761         Cvar_RegisterVariable(&v_color_grey_g);
762         Cvar_RegisterVariable(&v_color_grey_b);
763         Cvar_RegisterVariable(&v_color_white_r);
764         Cvar_RegisterVariable(&v_color_white_g);
765         Cvar_RegisterVariable(&v_color_white_b);
766
767         Cvar_RegisterVariable(&v_hwgamma);
768         
769         Cvar_RegisterVariable(&v_psycho);
770
771         Cvar_RegisterVariable(&vid_fullscreen);
772         Cvar_RegisterVariable(&vid_width);
773         Cvar_RegisterVariable(&vid_height);
774         Cvar_RegisterVariable(&vid_bitsperpixel);
775         Cvar_RegisterVariable(&vid_mouse);
776         Cvar_RegisterVariable(&gl_combine);
777         Cvar_RegisterVariable(&gl_finish);
778         Cvar_RegisterVariable(&in_pitch_min);
779         Cvar_RegisterVariable(&in_pitch_max);
780         Cvar_RegisterVariable(&m_filter);
781         Cmd_AddCommand("force_centerview", Force_CenterView_f);
782         Cmd_AddCommand("vid_restart", VID_Restart_f);
783         if (gamemode == GAME_GOODVSBAD2)
784                 Cvar_Set("gl_combine", "0");
785 }
786
787 int current_vid_fullscreen;
788 int current_vid_width;
789 int current_vid_height;
790 int current_vid_bitsperpixel;
791 int VID_Mode(int fullscreen, int width, int height, int bpp)
792 {
793         Con_Printf("Video: %s %dx%dx%d\n", fullscreen ? "fullscreen" : "window", width, height, bpp);
794         if (VID_InitMode(fullscreen, width, height, bpp))
795         {
796                 current_vid_fullscreen = fullscreen;
797                 current_vid_width = width;
798                 current_vid_height = height;
799                 current_vid_bitsperpixel = bpp;
800                 Cvar_SetValueQuick(&vid_fullscreen, fullscreen);
801                 Cvar_SetValueQuick(&vid_width, width);
802                 Cvar_SetValueQuick(&vid_height, height);
803                 Cvar_SetValueQuick(&vid_bitsperpixel, bpp);
804                 return true;
805         }
806         else
807                 return false;
808 }
809
810 static void VID_OpenSystems(void)
811 {
812         R_Modules_Start();
813         S_Startup();
814         CDAudio_Startup();
815 }
816
817 static void VID_CloseSystems(void)
818 {
819         CDAudio_Shutdown();
820         S_Shutdown();
821         R_Modules_Shutdown();
822 }
823
824 int vid_commandlinecheck = true;
825
826 void VID_Restart_f(void)
827 {
828         // don't crash if video hasn't started yet
829         if (vid_commandlinecheck)
830                 return;
831
832         Con_Printf("VID_Restart: changing from %s %dx%dx%dbpp, to %s %dx%dx%dbpp.\n",
833                 current_vid_fullscreen ? "fullscreen" : "window", current_vid_width, current_vid_height, current_vid_bitsperpixel,
834                 vid_fullscreen.integer ? "fullscreen" : "window", vid_width.integer, vid_height.integer, vid_bitsperpixel.integer);
835         VID_Close();
836         if (!VID_Mode(vid_fullscreen.integer, vid_width.integer, vid_height.integer, vid_bitsperpixel.integer))
837         {
838                 Con_Print("Video mode change failed\n");
839                 if (!VID_Mode(current_vid_fullscreen, current_vid_width, current_vid_height, current_vid_bitsperpixel))
840                         Sys_Error("Unable to restore to last working video mode\n");
841         }
842         VID_OpenSystems();
843 }
844
845 void VID_Open(void)
846 {
847         int i, width, height, success;
848         if (vid_commandlinecheck)
849         {
850                 // interpret command-line parameters
851                 vid_commandlinecheck = false;
852                 if (COM_CheckParm("-window") || COM_CheckParm("-safe"))
853                         Cvar_SetValueQuick(&vid_fullscreen, false);
854                 if (COM_CheckParm("-fullscreen"))
855                         Cvar_SetValueQuick(&vid_fullscreen, true);
856                 width = 0;
857                 height = 0;
858                 if ((i = COM_CheckParm("-width")) != 0)
859                         width = atoi(com_argv[i+1]);
860                 if ((i = COM_CheckParm("-height")) != 0)
861                         height = atoi(com_argv[i+1]);
862                 if (width == 0)
863                         width = height * 4 / 3;
864                 if (height == 0)
865                         height = width * 3 / 4;
866                 if (width)
867                         Cvar_SetValueQuick(&vid_width, width);
868                 if (height)
869                         Cvar_SetValueQuick(&vid_height, height);
870                 if ((i = COM_CheckParm("-bpp")) != 0)
871                         Cvar_SetQuick(&vid_bitsperpixel, com_argv[i+1]);
872         }
873
874         Con_DPrint("Starting video system\n");
875         success = VID_Mode(vid_fullscreen.integer, vid_width.integer, vid_height.integer, vid_bitsperpixel.integer);
876         if (!success)
877         {
878                 Con_Print("Desired video mode fail, trying fallbacks...\n");
879                 if (!success && vid_bitsperpixel.integer > 16)
880                         success = VID_Mode(vid_fullscreen.integer, vid_width.integer, vid_height.integer, 16);
881                 if (!success && (vid_width.integer > 640 || vid_height.integer > 480))
882                         success = VID_Mode(vid_fullscreen.integer, 640, 480, 16);
883                 if (!success && vid_fullscreen.integer)
884                         success = VID_Mode(false, 640, 480, 16);
885                 if (!success)
886                         Sys_Error("Video modes failed\n");
887         }
888         VID_OpenSystems();
889 }
890
891 void VID_Close(void)
892 {
893         VID_CloseSystems();
894         VID_Shutdown();
895 }
896