Moved an extern to the proper header.
[xonotic/darkplaces.git] / vid_shared.c
1
2 #include "quakedef.h"
3
4 // Tell startup code that we have a client
5 int cl_available = true;
6
7 // global video state
8 viddef_t vid;
9
10 // LordHavoc: these are only set in wgl
11 qboolean isG200 = false; // LordHavoc: the Matrox G200 can't do per pixel alpha, and it uses a D3D driver for GL... ugh...
12 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.
13
14 // AK FIXME -> input_dest
15 qboolean in_client_mouse = true;
16
17 // AK where should it be placed ?
18 float in_mouse_x, in_mouse_y;
19
20 // GL_ARB_multitexture
21 int gl_textureunits = 0;
22 // GL_ARB_texture_env_combine or GL_EXT_texture_env_combine
23 int gl_combine_extension = false;
24 // GL_EXT_compiled_vertex_array
25 int gl_supportslockarrays = false;
26 // GLX_SGI_video_sync or WGL_EXT_swap_control
27 int gl_videosyncavailable = false;
28 // stencil available
29 int gl_stencil = false;
30 // 3D textures available
31 int gl_texture3d = false;
32 // GL_ARB_texture_cubemap
33 int gl_texturecubemap = false;
34 // GL_ARB_texture_env_dot3
35 int gl_dot3arb = false;
36 // GL_SGIS_texture_edge_clamp
37 int gl_support_clamptoedge = false;
38 // GL_NV_vertex_array_range
39 int gl_support_var = false;
40 // GL_NV_vertex_array_range2
41 int gl_support_var2 = false;
42 // GL_EXT_texture_filter_anisotropic
43 int gl_support_anisotropy = false;
44 // GL_NV_texture_shader
45 int gl_textureshader = 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 int GL_CheckExtension(const char *name, const dllfunction_t *funcs, const char *disableparm, int silent)
238 {
239         int failed = false;
240         const dllfunction_t *func;
241
242         Con_DPrintf("checking for %s...  ", name);
243
244         for (func = funcs;func && func->name;func++)
245                 *func->funcvariable = NULL;
246
247         if (disableparm && COM_CheckParm(disableparm))
248         {
249                 Con_DPrintf("disabled by commandline\n");
250                 return false;
251         }
252
253         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)))
254         {
255                 for (func = funcs;func && func->name != NULL;func++)
256                 {
257                         // functions are cleared before all the extensions are evaluated
258                         if (!(*func->funcvariable = (void *) GL_GetProcAddress(func->name)))
259                         {
260                                 if (!silent)
261                                         Con_Printf("OpenGL extension \"%s\" is missing function \"%s\" - broken driver!\n", name, func->name);
262                                 failed = true;
263                         }
264                 }
265                 // delay the return so it prints all missing functions
266                 if (failed)
267                         return false;
268                 Con_DPrintf("enabled\n");
269                 return true;
270         }
271         else
272         {
273                 Con_DPrintf("not detected\n");
274                 return false;
275         }
276 }
277
278 static dllfunction_t opengl110funcs[] =
279 {
280         {"glClearColor", (void **) &qglClearColor},
281         {"glClear", (void **) &qglClear},
282 //      {"glAlphaFunc", (void **) &qglAlphaFunc},
283         {"glBlendFunc", (void **) &qglBlendFunc},
284         {"glCullFace", (void **) &qglCullFace},
285 //      {"glDrawBuffer", (void **) &qglDrawBuffer},
286 //      {"glReadBuffer", (void **) &qglReadBuffer},
287         {"glEnable", (void **) &qglEnable},
288         {"glDisable", (void **) &qglDisable},
289         {"glIsEnabled", (void **) &qglIsEnabled},
290         {"glEnableClientState", (void **) &qglEnableClientState},
291         {"glDisableClientState", (void **) &qglDisableClientState},
292 //      {"glGetBooleanv", (void **) &qglGetBooleanv},
293 //      {"glGetDoublev", (void **) &qglGetDoublev},
294 //      {"glGetFloatv", (void **) &qglGetFloatv},
295         {"glGetIntegerv", (void **) &qglGetIntegerv},
296         {"glGetError", (void **) &qglGetError},
297         {"glGetString", (void **) &qglGetString},
298         {"glFinish", (void **) &qglFinish},
299         {"glFlush", (void **) &qglFlush},
300         {"glClearDepth", (void **) &qglClearDepth},
301         {"glDepthFunc", (void **) &qglDepthFunc},
302         {"glDepthMask", (void **) &qglDepthMask},
303         {"glDepthRange", (void **) &qglDepthRange},
304         {"glDrawElements", (void **) &qglDrawElements},
305         {"glColorMask", (void **) &qglColorMask},
306         {"glVertexPointer", (void **) &qglVertexPointer},
307         {"glNormalPointer", (void **) &qglNormalPointer},
308         {"glColorPointer", (void **) &qglColorPointer},
309         {"glTexCoordPointer", (void **) &qglTexCoordPointer},
310         {"glArrayElement", (void **) &qglArrayElement},
311         {"glColor4f", (void **) &qglColor4f},
312         {"glTexCoord2f", (void **) &qglTexCoord2f},
313         {"glTexCoord3f", (void **) &qglTexCoord3f},
314         {"glVertex2f", (void **) &qglVertex2f},
315         {"glVertex3f", (void **) &qglVertex3f},
316         {"glBegin", (void **) &qglBegin},
317         {"glEnd", (void **) &qglEnd},
318         {"glMatrixMode", (void **) &qglMatrixMode},
319         {"glOrtho", (void **) &qglOrtho},
320         {"glFrustum", (void **) &qglFrustum},
321         {"glViewport", (void **) &qglViewport},
322 //      {"glPushMatrix", (void **) &qglPushMatrix},
323 //      {"glPopMatrix", (void **) &qglPopMatrix},
324         {"glLoadIdentity", (void **) &qglLoadIdentity},
325 //      {"glLoadMatrixd", (void **) &qglLoadMatrixd},
326         {"glLoadMatrixf", (void **) &qglLoadMatrixf},
327 //      {"glMultMatrixd", (void **) &qglMultMatrixd},
328 //      {"glMultMatrixf", (void **) &qglMultMatrixf},
329 //      {"glRotated", (void **) &qglRotated},
330 //      {"glRotatef", (void **) &qglRotatef},
331 //      {"glScaled", (void **) &qglScaled},
332 //      {"glScalef", (void **) &qglScalef},
333 //      {"glTranslated", (void **) &qglTranslated},
334 //      {"glTranslatef", (void **) &qglTranslatef},
335         {"glReadPixels", (void **) &qglReadPixels},
336         {"glStencilFunc", (void **) &qglStencilFunc},
337         {"glStencilMask", (void **) &qglStencilMask},
338         {"glStencilOp", (void **) &qglStencilOp},
339         {"glClearStencil", (void **) &qglClearStencil},
340 //      {"glTexEnvf", (void **) &qglTexEnvf},
341         {"glTexEnvfv", (void **) &qglTexEnvfv},
342         {"glTexEnvi", (void **) &qglTexEnvi},
343         {"glTexParameterf", (void **) &qglTexParameterf},
344 //      {"glTexParameterfv", (void **) &qglTexParameterfv},
345         {"glTexParameteri", (void **) &qglTexParameteri},
346 //      {"glPixelStoref", (void **) &qglPixelStoref},
347         {"glPixelStorei", (void **) &qglPixelStorei},
348         {"glGenTextures", (void **) &qglGenTextures},
349         {"glDeleteTextures", (void **) &qglDeleteTextures},
350         {"glBindTexture", (void **) &qglBindTexture},
351 //      {"glPrioritizeTextures", (void **) &qglPrioritizeTextures},
352 //      {"glAreTexturesResident", (void **) &qglAreTexturesResident},
353         {"glIsTexture", (void **) &qglIsTexture},
354         {"glTexImage1D", (void **) &qglTexImage1D},
355         {"glTexImage2D", (void **) &qglTexImage2D},
356         {"glTexSubImage1D", (void **) &qglTexSubImage1D},
357         {"glTexSubImage2D", (void **) &qglTexSubImage2D},
358         {"glCopyTexImage1D", (void **) &qglCopyTexImage1D},
359         {"glCopyTexImage2D", (void **) &qglCopyTexImage2D},
360         {"glCopyTexSubImage1D", (void **) &qglCopyTexSubImage1D},
361         {"glCopyTexSubImage2D", (void **) &qglCopyTexSubImage2D},
362         {"glScissor", (void **) &qglScissor},
363         {"glPolygonOffset", (void **) &qglPolygonOffset},
364         {NULL, NULL}
365 };
366
367 static dllfunction_t drawrangeelementsfuncs[] =
368 {
369         {"glDrawRangeElements", (void **) &qglDrawRangeElements},
370         {NULL, NULL}
371 };
372
373 static dllfunction_t drawrangeelementsextfuncs[] =
374 {
375         {"glDrawRangeElementsEXT", (void **) &qglDrawRangeElementsEXT},
376         {NULL, NULL}
377 };
378
379 static dllfunction_t multitexturefuncs[] =
380 {
381         {"glMultiTexCoord2fARB", (void **) &qglMultiTexCoord2f},
382         {"glMultiTexCoord3fARB", (void **) &qglMultiTexCoord3f},
383         {"glActiveTextureARB", (void **) &qglActiveTexture},
384         {"glClientActiveTextureARB", (void **) &qglClientActiveTexture},
385         {NULL, NULL}
386 };
387
388 static dllfunction_t compiledvertexarrayfuncs[] =
389 {
390         {"glLockArraysEXT", (void **) &qglLockArraysEXT},
391         {"glUnlockArraysEXT", (void **) &qglUnlockArraysEXT},
392         {NULL, NULL}
393 };
394
395 static dllfunction_t texture3dextfuncs[] =
396 {
397         {"glTexImage3DEXT", (void **) &qglTexImage3D},
398         {"glTexSubImage3DEXT", (void **) &qglTexSubImage3D},
399         {"glCopyTexSubImage3DEXT", (void **) &qglCopyTexSubImage3D},
400         {NULL, NULL}
401 };
402
403 static dllfunction_t glxvarfuncs[] =
404 {
405         {"glXAllocateMemoryNV", (void **) &qglAllocateMemoryNV},
406         {"glXFreeMemoryNV", (void **) &qglFreeMemoryNV},
407         {"glVertexArrayRangeNV", (void **) &qglVertexArrayRangeNV},
408         {"glFlushVertexArrayRangeNV", (void **) &qglFlushVertexArrayRangeNV},
409         {NULL, NULL}
410 };
411
412 static dllfunction_t wglvarfuncs[] =
413 {
414         {"wglAllocateMemoryNV", (void **) &qglAllocateMemoryNV},
415         {"wglFreeMemoryNV", (void **) &qglFreeMemoryNV},
416         {"glVertexArrayRangeNV", (void **) &qglVertexArrayRangeNV},
417         {"glFlushVertexArrayRangeNV", (void **) &qglFlushVertexArrayRangeNV},
418         {NULL, NULL}
419 };
420
421
422 void VID_CheckExtensions(void)
423 {
424         gl_stencil = vid_bitsperpixel.integer == 32;
425         gl_combine_extension = false;
426         gl_dot3arb = false;
427         gl_supportslockarrays = false;
428         gl_textureunits = 1;
429         gl_support_clamptoedge = false;
430         gl_support_var = false;
431         gl_support_var2 = false;
432
433         if (!GL_CheckExtension("OpenGL 1.1.0", opengl110funcs, NULL, false))
434                 Sys_Error("OpenGL 1.1.0 functions not found\n");
435
436         Con_DPrintf ("GL_VENDOR: %s\n", gl_vendor);
437         Con_DPrintf ("GL_RENDERER: %s\n", gl_renderer);
438         Con_DPrintf ("GL_VERSION: %s\n", gl_version);
439         Con_DPrintf ("GL_EXTENSIONS: %s\n", gl_extensions);
440         Con_DPrintf ("%s_EXTENSIONS: %s\n", gl_platform, gl_platformextensions);
441
442         Con_DPrintf("Checking OpenGL extensions...\n");
443
444         if (!GL_CheckExtension("glDrawRangeElements", drawrangeelementsfuncs, "-nodrawrangeelements", true))
445                 GL_CheckExtension("GL_EXT_draw_range_elements", drawrangeelementsextfuncs, "-nodrawrangeelements", false);
446
447         if (GL_CheckExtension("GL_ARB_multitexture", multitexturefuncs, "-nomtex", false))
448         {
449                 qglGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_textureunits);
450                 gl_combine_extension = GL_CheckExtension("GL_ARB_texture_env_combine", NULL, "-nocombine", false) || GL_CheckExtension("GL_EXT_texture_env_combine", NULL, "-nocombine", false);
451                 if (gl_combine_extension)
452                         gl_dot3arb = GL_CheckExtension("GL_ARB_texture_env_dot3", NULL, "-nodot3", false);
453         }
454
455         gl_texture3d = GL_CheckExtension("GL_EXT_texture3D", texture3dextfuncs, "-notexture3d", false);
456         gl_texturecubemap = GL_CheckExtension("GL_ARB_texture_cube_map", NULL, "-nocubemap", false);
457         gl_supportslockarrays = GL_CheckExtension("GL_EXT_compiled_vertex_array", compiledvertexarrayfuncs, "-nocva", false);
458         gl_support_clamptoedge = GL_CheckExtension("GL_EXT_texture_edge_clamp", NULL, "-noedgeclamp", false) || GL_CheckExtension("GL_SGIS_texture_edge_clamp", NULL, "-noedgeclamp", false);
459
460         if (!strcmp(gl_platform, "GLX"))
461                 gl_support_var = GL_CheckExtension("GL_NV_vertex_array_range", glxvarfuncs, "-novar", false);
462         else if (!strcmp(gl_platform, "WGL"))
463                 gl_support_var = GL_CheckExtension("GL_NV_vertex_array_range", wglvarfuncs, "-novar", false);
464         if (gl_support_var)
465                 gl_support_var2 = GL_CheckExtension("GL_NV_vertex_array_range2", NULL, "-novar2", false);
466
467         gl_support_anisotropy = GL_CheckExtension("GL_EXT_texture_filter_anisotropic", NULL, "-noanisotropy", false);
468
469         gl_textureshader = GL_CheckExtension("GL_NV_texture_shader", NULL, "-notextureshader", false);
470
471         // we don't care if it's an extension or not, they are identical functions, so keep it simple in the rendering code
472         if (qglDrawRangeElements == NULL)
473                 qglDrawRangeElements = qglDrawRangeElementsEXT;
474 }
475
476 int vid_vertexarrays_are_var = false;
477 void *VID_AllocVertexArrays(mempool_t *pool, int size, int fast, float readfrequency, float writefrequency, float priority)
478 {
479         void *m;
480         vid_vertexarrays_are_var = false;
481         if (fast && qglAllocateMemoryNV)
482         {
483                 CHECKGLERROR
484                 m = qglAllocateMemoryNV(size, readfrequency, writefrequency, priority);
485                 CHECKGLERROR
486                 if (m)
487                 {
488                         vid_vertexarrays_are_var = true;
489                         return m;
490                 }
491         }
492         return Mem_Alloc(pool, size);
493 }
494
495 void VID_FreeVertexArrays(void *pointer)
496 {
497         if (vid_vertexarrays_are_var)
498         {
499                 CHECKGLERROR
500                 qglFreeMemoryNV(pointer);
501                 CHECKGLERROR
502         }
503         else
504                 Mem_Free(pointer);
505         vid_vertexarrays_are_var = false;
506 }
507
508 void Force_CenterView_f (void)
509 {
510         cl.viewangles[PITCH] = 0;
511 }
512
513 void IN_PreMove(void)
514 {
515 }
516
517 void CL_AdjustAngles(void);
518 void IN_PostMove(void)
519 {
520         // clamp after the move as well to prevent messed up rendering angles
521         CL_AdjustAngles();
522 }
523
524 /*
525 ===========
526 IN_DoMove
527 ===========
528 */
529 void IN_ProcessMove(usercmd_t *cmd)
530 {
531         // get basic movement from keyboard
532         CL_BaseMove(cmd);
533
534         // OS independent code
535         IN_PreMove();
536
537         // allow mice or other external controllers to add to the move
538         IN_Move(cmd);
539
540         // OS independent code
541         IN_PostMove();
542 }
543
544
545 void IN_Mouse(usercmd_t *cmd, float mx, float my)
546 {
547         int mouselook = (in_mlook.state & 1) || freelook.integer;
548         float mouse_x, mouse_y;
549         static float old_mouse_x = 0, old_mouse_y = 0;
550
551         if (m_filter.integer)
552         {
553                 mouse_x = (mx + old_mouse_x) * 0.5;
554                 mouse_y = (my + old_mouse_y) * 0.5;
555         }
556         else
557         {
558                 mouse_x = mx;
559                 mouse_y = my;
560         }
561
562         old_mouse_x = mx;
563         old_mouse_y = my;
564
565         in_mouse_x = (float) mouse_x * vid.conwidth / vid.realwidth;
566         in_mouse_y = (float) mouse_y * vid.conheight / vid.realheight;
567
568         // AK: eveything else is client stuff
569         // BTW, this should be seperated somewhen
570         if(!in_client_mouse)
571                 return;
572
573         // LordHavoc: viewzoom affects mouse sensitivity for sniping
574         mouse_x *= sensitivity.value * cl.viewzoom;
575         mouse_y *= sensitivity.value * cl.viewzoom;
576
577         // Add mouse X/Y movement to cmd
578         if ((in_strafe.state & 1) || (lookstrafe.integer && mouselook))
579                 cmd->sidemove += m_side.value * mouse_x;
580         else
581                 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
582
583         if (mouselook)
584                 V_StopPitchDrift();
585
586         if (mouselook && !(in_strafe.state & 1))
587                 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
588         else
589         {
590                 if ((in_strafe.state & 1) && noclip_anglehack)
591                         cmd->upmove -= m_forward.value * mouse_y;
592                 else
593                         cmd->forwardmove -= m_forward.value * mouse_y;
594         }
595 }
596
597 static float cachegamma, cachebrightness, cachecontrast, cacheblack[3], cachegrey[3], cachewhite[3];
598 static int cachecolorenable, cachehwgamma;
599 #define BOUNDCVAR(cvar, m1, m2) c = &(cvar);f = bound(m1, c->value, m2);if (c->value != f) Cvar_SetValueQuick(c, f);
600 void VID_UpdateGamma(qboolean force)
601 {
602         cvar_t *c;
603         float f;
604         static int forcenextframe = false;
605
606         // LordHavoc: don't mess with gamma tables if running dedicated
607         if (cls.state == ca_dedicated)
608                 return;
609
610         if (!force
611          && !forcenextframe
612          && !v_psycho.integer
613          && vid_usinghwgamma == (vid_activewindow && v_hwgamma.integer)
614          && v_gamma.value == cachegamma
615          && v_contrast.value == cachecontrast
616          && v_brightness.value == cachebrightness
617          && cachecolorenable == v_color_enable.integer
618          && cacheblack[0] == v_color_black_r.value
619          && cacheblack[1] == v_color_black_g.value
620          && cacheblack[2] == v_color_black_b.value
621          && cachegrey[0] == v_color_grey_r.value
622          && cachegrey[1] == v_color_grey_g.value
623          && cachegrey[2] == v_color_grey_b.value
624          && cachewhite[0] == v_color_white_r.value
625          && cachewhite[1] == v_color_white_g.value
626          && cachewhite[2] == v_color_white_b.value)
627                 return;
628                 
629         forcenextframe = false;
630
631         if (vid_activewindow && v_hwgamma.integer)
632         {
633                 if (!vid_usinghwgamma)
634                 {
635                         vid_usinghwgamma = true;
636                         Cvar_SetValueQuick(&vid_hardwaregammasupported, VID_GetGamma(vid_systemgammaramps));
637                 }
638
639                 BOUNDCVAR(v_gamma, 0.1, 5);cachegamma = v_gamma.value;
640                 BOUNDCVAR(v_contrast, 1, 5);cachecontrast = v_contrast.value;
641                 BOUNDCVAR(v_brightness, 0, 0.8);cachebrightness = v_brightness.value;
642                 BOUNDCVAR(v_color_black_r, 0, 0.8);cacheblack[0] = v_color_black_r.value;
643                 BOUNDCVAR(v_color_black_g, 0, 0.8);cacheblack[1] = v_color_black_g.value;
644                 BOUNDCVAR(v_color_black_b, 0, 0.8);cacheblack[2] = v_color_black_b.value;
645                 BOUNDCVAR(v_color_grey_r, 0, 0.95);cachegrey[0] = v_color_grey_r.value;
646                 BOUNDCVAR(v_color_grey_g, 0, 0.95);cachegrey[1] = v_color_grey_g.value;
647                 BOUNDCVAR(v_color_grey_b, 0, 0.95);cachegrey[2] = v_color_grey_b.value;
648                 BOUNDCVAR(v_color_white_r, 1, 5);cachewhite[0] = v_color_white_r.value;
649                 BOUNDCVAR(v_color_white_g, 1, 5);cachewhite[1] = v_color_white_g.value;
650                 BOUNDCVAR(v_color_white_b, 1, 5);cachewhite[2] = v_color_white_b.value;
651                 cachecolorenable = v_color_enable.integer;
652                 cachehwgamma = v_hwgamma.integer;
653
654                 if (cachecolorenable)
655                 {
656                         BuildGammaTable16(1.0f, invpow(0.5, 1 - cachegrey[0]), cachewhite[0], cacheblack[0], vid_gammaramps);
657                         BuildGammaTable16(1.0f, invpow(0.5, 1 - cachegrey[1]), cachewhite[1], cacheblack[1], vid_gammaramps + 256);
658                         BuildGammaTable16(1.0f, invpow(0.5, 1 - cachegrey[2]), cachewhite[2], cacheblack[2], vid_gammaramps + 512);
659                 }
660                 else
661                 {
662                         BuildGammaTable16(1.0f, cachegamma, cachecontrast, cachebrightness, vid_gammaramps);
663                         BuildGammaTable16(1.0f, cachegamma, cachecontrast, cachebrightness, vid_gammaramps + 256);
664                         BuildGammaTable16(1.0f, cachegamma, cachecontrast, cachebrightness, vid_gammaramps + 512);
665                 }
666
667                 // LordHavoc: this code came from Ben Winslow and Zinx Verituse, I have
668                 // immensely butchered it to work with variable framerates and fit in with
669                 // the rest of darkplaces.
670                 if (v_psycho.integer)
671                 {
672                         int x, y;
673                         float t;
674                         static float n[3], nd[3], nt[3];
675                         static int init = true;
676                         unsigned short *ramp;
677                         forcenextframe = true;
678                         if (init)
679                         {
680                                 init = false;
681                                 for (x = 0;x < 3;x++)
682                                 {
683                                         n[x] = lhrandom(0, 1);
684                                         nd[x] = (rand()&1)?-0.25:0.25;
685                                         nt[x] = lhrandom(1, 8.2);
686                                 }
687                         }
688                         
689                         for (x = 0;x < 3;x++)
690                         {
691                                 nt[x] -= host_realframetime;
692                                 if (nt[x] < 0)
693                                 {
694                                         nd[x] = -nd[x];
695                                         nt[x] += lhrandom(1, 8.2);
696                                 }
697                                 n[x] += nd[x] * host_realframetime;
698                                 n[x] -= floor(n[x]);
699                         }
700                 
701                         for (x = 0, ramp = vid_gammaramps;x < 3;x++)
702                                 for (y = 0, t = n[x] - 0.75f;y < 256;y++, t += 0.75f * (2.0f / 256.0f))
703                                         *ramp++ = cos(t*(M_PI*2.0)) * 32767.0f + 32767.0f;
704                 }
705
706                 Cvar_SetValueQuick(&vid_hardwaregammasupported, VID_SetGamma(vid_gammaramps));
707         }
708         else
709         {
710                 if (vid_usinghwgamma)
711                 {
712                         vid_usinghwgamma = false;
713                         Cvar_SetValueQuick(&vid_hardwaregammasupported, VID_SetGamma(vid_systemgammaramps));
714                 }
715         }
716 }
717
718 void VID_RestoreSystemGamma(void)
719 {
720         if (vid_usinghwgamma)
721         {
722                 vid_usinghwgamma = false;
723                 VID_SetGamma(vid_systemgammaramps);
724         }
725 }
726
727 void VID_Shared_Init(void)
728 {
729         Cvar_RegisterVariable(&vid_hardwaregammasupported);
730         Cvar_RegisterVariable(&v_gamma);
731         Cvar_RegisterVariable(&v_brightness);
732         Cvar_RegisterVariable(&v_contrast);
733
734         Cvar_RegisterVariable(&v_color_enable);
735         Cvar_RegisterVariable(&v_color_black_r);
736         Cvar_RegisterVariable(&v_color_black_g);
737         Cvar_RegisterVariable(&v_color_black_b);
738         Cvar_RegisterVariable(&v_color_grey_r);
739         Cvar_RegisterVariable(&v_color_grey_g);
740         Cvar_RegisterVariable(&v_color_grey_b);
741         Cvar_RegisterVariable(&v_color_white_r);
742         Cvar_RegisterVariable(&v_color_white_g);
743         Cvar_RegisterVariable(&v_color_white_b);
744
745         Cvar_RegisterVariable(&v_hwgamma);
746         
747         Cvar_RegisterVariable(&v_psycho);
748
749         Cvar_RegisterVariable(&vid_fullscreen);
750         Cvar_RegisterVariable(&vid_width);
751         Cvar_RegisterVariable(&vid_height);
752         Cvar_RegisterVariable(&vid_bitsperpixel);
753         Cvar_RegisterVariable(&vid_mouse);
754         Cvar_RegisterVariable(&gl_combine);
755         Cvar_RegisterVariable(&in_pitch_min);
756         Cvar_RegisterVariable(&in_pitch_max);
757         Cvar_RegisterVariable(&m_filter);
758         Cmd_AddCommand("force_centerview", Force_CenterView_f);
759         Cmd_AddCommand("vid_restart", VID_Restart_f);
760         if (gamemode == GAME_GOODVSBAD2)
761                 Cvar_Set("gl_combine", "0");
762 }
763
764 int current_vid_fullscreen;
765 int current_vid_width;
766 int current_vid_height;
767 int current_vid_bitsperpixel;
768 int VID_Mode(int fullscreen, int width, int height, int bpp)
769 {
770         Con_Printf("Video: %s %dx%dx%d\n", fullscreen ? "fullscreen" : "window", width, height, bpp);
771         if (VID_InitMode(fullscreen, width, height, bpp))
772         {
773                 current_vid_fullscreen = fullscreen;
774                 current_vid_width = width;
775                 current_vid_height = height;
776                 current_vid_bitsperpixel = bpp;
777                 Cvar_SetValueQuick(&vid_fullscreen, fullscreen);
778                 Cvar_SetValueQuick(&vid_width, width);
779                 Cvar_SetValueQuick(&vid_height, height);
780                 Cvar_SetValueQuick(&vid_bitsperpixel, bpp);
781                 return true;
782         }
783         else
784                 return false;
785 }
786
787 static void VID_OpenSystems(void)
788 {
789         R_Modules_Start();
790         S_Startup();
791         CDAudio_Startup();
792 }
793
794 static void VID_CloseSystems(void)
795 {
796         CDAudio_Shutdown();
797         S_Shutdown();
798         R_Modules_Shutdown();
799 }
800
801 int vid_commandlinecheck = true;
802
803 void VID_Restart_f(void)
804 {
805         // don't crash if video hasn't started yet
806         if (vid_commandlinecheck)
807                 return;
808
809         Con_Printf("VID_Restart: changing from %s %dx%dx%dbpp, to %s %dx%dx%dbpp.\n",
810                 current_vid_fullscreen ? "fullscreen" : "window", current_vid_width, current_vid_height, current_vid_bitsperpixel,
811                 vid_fullscreen.integer ? "fullscreen" : "window", vid_width.integer, vid_height.integer, vid_bitsperpixel.integer);
812         VID_Close();
813         if (!VID_Mode(vid_fullscreen.integer, vid_width.integer, vid_height.integer, vid_bitsperpixel.integer))
814         {
815                 Con_Printf("Video mode change failed\n");
816                 if (!VID_Mode(current_vid_fullscreen, current_vid_width, current_vid_height, current_vid_bitsperpixel))
817                         Sys_Error("Unable to restore to last working video mode\n");
818         }
819         VID_OpenSystems();
820 }
821
822 void VID_Open(void)
823 {
824         int i, width, height;
825         if (vid_commandlinecheck)
826         {
827                 // interpret command-line parameters
828                 vid_commandlinecheck = false;
829                 if ((i = COM_CheckParm("-window")) != 0)
830                         Cvar_SetValueQuick(&vid_fullscreen, false);
831                 if ((i = COM_CheckParm("-fullscreen")) != 0)
832                         Cvar_SetValueQuick(&vid_fullscreen, true);
833                 width = 0;
834                 height = 0;
835                 if ((i = COM_CheckParm("-width")) != 0)
836                         width = atoi(com_argv[i+1]);
837                 if ((i = COM_CheckParm("-height")) != 0)
838                         height = atoi(com_argv[i+1]);
839                 if (width == 0)
840                         width = height * 4 / 3;
841                 if (height == 0)
842                         height = width * 3 / 4;
843                 if (width)
844                         Cvar_SetValueQuick(&vid_width, width);
845                 if (height)
846                         Cvar_SetValueQuick(&vid_height, height);
847                 if ((i = COM_CheckParm("-bpp")) != 0)
848                         Cvar_SetQuick(&vid_bitsperpixel, com_argv[i+1]);
849         }
850
851         Con_DPrintf("Starting video system\n");
852         if (!VID_Mode(vid_fullscreen.integer, vid_width.integer, vid_height.integer, vid_bitsperpixel.integer))
853         {
854                 Con_Printf("Desired video mode fail, trying fallbacks...\n");
855                 if (vid_fullscreen.integer)
856                 {
857                         if (!VID_Mode(true, 640, 480, 16))
858                                 if (!VID_Mode(false, 640, 480, 16))
859                                         Sys_Error("Video modes failed\n");
860                 }
861                 else
862                         Sys_Error("Windowed video failed\n");
863         }
864         VID_OpenSystems();
865 }
866
867 void VID_Close(void)
868 {
869         VID_CloseSystems();
870         VID_Shutdown();
871 }
872