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