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