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