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