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