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