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