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