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