]> icculus.org git repositories - divverent/darkplaces.git/blob - vid_shared.c
optimized entity updates substantially (or rather, UpdateEnd), nice speedup
[divverent/darkplaces.git] / vid_shared.c
1
2 #include "quakedef.h"
3
4 // LordHavoc: these are only set in wgl
5 qboolean isG200 = false; // LordHavoc: the Matrox G200 can't do per pixel alpha, and it uses a D3D driver for GL... ugh...
6 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.
7
8 // LordHavoc: GL_ARB_multitexture support
9 int gl_textureunits;
10 // LordHavoc: GL_ARB_texture_env_combine or GL_EXT_texture_env_combine support
11 int gl_combine_extension = false;
12 // LordHavoc: GL_EXT_compiled_vertex_array support
13 int gl_supportslockarrays = false;
14
15 // LordHavoc: if window is hidden, don't update screen
16 int vid_hidden = false;
17 // LordHavoc: if window is not the active window, don't hog as much CPU time,
18 // let go of the mouse, turn off sound, and restore system gamma ramps...
19 int vid_activewindow = true;
20
21 cvar_t vid_mode = {0, "vid_mode", "0"};
22 cvar_t vid_mouse = {CVAR_SAVE, "vid_mouse", "1"};
23 cvar_t vid_fullscreen = {0, "vid_fullscreen", "1"};
24 cvar_t gl_combine = {0, "gl_combine", "1"};
25
26 cvar_t in_pitch_min = {0, "in_pitch_min", "-90"};
27 cvar_t in_pitch_max = {0, "in_pitch_max", "90"};
28
29 cvar_t m_filter = {CVAR_SAVE, "m_filter","0"};
30
31 // GL_ARB_multitexture
32 void (GLAPIENTRY *qglMultiTexCoord2f) (GLenum, GLfloat, GLfloat);
33 void (GLAPIENTRY *qglActiveTexture) (GLenum);
34 void (GLAPIENTRY *qglClientActiveTexture) (GLenum);
35
36 // GL_EXT_compiled_vertex_array
37 void (GLAPIENTRY *qglLockArraysEXT) (GLint first, GLint count);
38 void (GLAPIENTRY *qglUnlockArraysEXT) (void);
39
40
41 // general GL functions
42
43 void (GLAPIENTRY *qglClearColor)(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
44
45 void (GLAPIENTRY *qglClear)(GLbitfield mask);
46
47 //void (GLAPIENTRY *qglAlphaFunc)(GLenum func, GLclampf ref);
48 void (GLAPIENTRY *qglBlendFunc)(GLenum sfactor, GLenum dfactor);
49 void (GLAPIENTRY *qglCullFace)(GLenum mode);
50
51 void (GLAPIENTRY *qglDrawBuffer)(GLenum mode);
52 void (GLAPIENTRY *qglReadBuffer)(GLenum mode);
53 void (GLAPIENTRY *qglEnable)(GLenum cap);
54 void (GLAPIENTRY *qglDisable)(GLenum cap);
55 //GLboolean GLAPIENTRY *qglIsEnabled)(GLenum cap);
56
57 void (GLAPIENTRY *qglEnableClientState)(GLenum cap);
58 void (GLAPIENTRY *qglDisableClientState)(GLenum cap);
59
60 //void (GLAPIENTRY *qglGetBooleanv)(GLenum pname, GLboolean *params);
61 //void (GLAPIENTRY *qglGetDoublev)(GLenum pname, GLdouble *params);
62 //void (GLAPIENTRY *qglGetFloatv)(GLenum pname, GLfloat *params);
63 void (GLAPIENTRY *qglGetIntegerv)(GLenum pname, GLint *params);
64
65 GLenum (GLAPIENTRY *qglGetError)(void);
66 const GLubyte* (GLAPIENTRY *qglGetString)(GLenum name);
67 void (GLAPIENTRY *qglFinish)(void);
68 void (GLAPIENTRY *qglFlush)(void);
69
70 void (GLAPIENTRY *qglClearDepth)(GLclampd depth);
71 void (GLAPIENTRY *qglDepthFunc)(GLenum func);
72 void (GLAPIENTRY *qglDepthMask)(GLboolean flag);
73 void (GLAPIENTRY *qglDepthRange)(GLclampd near_val, GLclampd far_val);
74
75 void (GLAPIENTRY *qglDrawRangeElements)(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices);
76 void (GLAPIENTRY *qglDrawElements)(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
77 void (GLAPIENTRY *qglVertexPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
78 //void (GLAPIENTRY *qglNormalPointer)(GLenum type, GLsizei stride, const GLvoid *ptr);
79 void (GLAPIENTRY *qglColorPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
80 void (GLAPIENTRY *qglTexCoordPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
81 void (GLAPIENTRY *qglArrayElement)(GLint i);
82
83 void (GLAPIENTRY *qglColor4ub)(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha);
84 void (GLAPIENTRY *qglTexCoord2f)(GLfloat s, GLfloat t);
85 void (GLAPIENTRY *qglVertex2f)(GLfloat x, GLfloat y);
86 void (GLAPIENTRY *qglVertex3f)(GLfloat x, GLfloat y, GLfloat z);
87 void (GLAPIENTRY *qglBegin)(GLenum mode);
88 void (GLAPIENTRY *qglEnd)(void);
89
90 void (GLAPIENTRY *qglMatrixMode)(GLenum mode);
91 void (GLAPIENTRY *qglOrtho)(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val);
92 void (GLAPIENTRY *qglFrustum)(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val);
93 void (GLAPIENTRY *qglViewport)(GLint x, GLint y, GLsizei width, GLsizei height);
94 //void (GLAPIENTRY *qglPushMatrix)(void);
95 //void (GLAPIENTRY *qglPopMatrix)(void);
96 void (GLAPIENTRY *qglLoadIdentity)(void);
97 //void (GLAPIENTRY *qglLoadMatrixd)(const GLdouble *m);
98 //void (GLAPIENTRY *qglLoadMatrixf)(const GLfloat *m);
99 //void (GLAPIENTRY *qglMultMatrixd)(const GLdouble *m);
100 //void (GLAPIENTRY *qglMultMatrixf)(const GLfloat *m);
101 //void (GLAPIENTRY *qglRotated)(GLdouble angle, GLdouble x, GLdouble y, GLdouble z);
102 void (GLAPIENTRY *qglRotatef)(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
103 //void (GLAPIENTRY *qglScaled)(GLdouble x, GLdouble y, GLdouble z);
104 //void (GLAPIENTRY *qglScalef)(GLfloat x, GLfloat y, GLfloat z);
105 //void (GLAPIENTRY *qglTranslated)(GLdouble x, GLdouble y, GLdouble z);
106 void (GLAPIENTRY *qglTranslatef)(GLfloat x, GLfloat y, GLfloat z);
107
108 void (GLAPIENTRY *qglReadPixels)(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels);
109
110 //void (GLAPIENTRY *qglStencilFunc)(GLenum func, GLint ref, GLuint mask);
111 //void (GLAPIENTRY *qglStencilMask)(GLuint mask);
112 //void (GLAPIENTRY *qglStencilOp)(GLenum fail, GLenum zfail, GLenum zpass);
113 //void (GLAPIENTRY *qglClearStencil)(GLint s);
114
115 //void (GLAPIENTRY *qglTexEnvf)(GLenum target, GLenum pname, GLfloat param);
116 void (GLAPIENTRY *qglTexEnvi)(GLenum target, GLenum pname, GLint param);
117
118 //void (GLAPIENTRY *qglTexParameterf)(GLenum target, GLenum pname, GLfloat param);
119 void (GLAPIENTRY *qglTexParameteri)(GLenum target, GLenum pname, GLint param);
120
121 void (GLAPIENTRY *qglBindTexture)(GLenum target, GLuint texture);
122 void (GLAPIENTRY *qglTexImage2D)(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels);
123 void (GLAPIENTRY *qglTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels);
124 void (GLAPIENTRY *qglDeleteTextures)(GLsizei n, const GLuint *textures);
125
126 void (GLAPIENTRY *qglDrawRangeElementsEXT)(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices);
127
128 //void (GLAPIENTRY *qglColorTableEXT)(int, int, int, int, int, const void *);
129
130 #if WIN32
131 int (WINAPI *qwglChoosePixelFormat)(HDC, CONST PIXELFORMATDESCRIPTOR *);
132 int (WINAPI *qwglDescribePixelFormat)(HDC, int, UINT, LPPIXELFORMATDESCRIPTOR);
133 //int (WINAPI *qwglGetPixelFormat)(HDC);
134 BOOL (WINAPI *qwglSetPixelFormat)(HDC, int, CONST PIXELFORMATDESCRIPTOR *);
135 BOOL (WINAPI *qwglSwapBuffers)(HDC);
136 HGLRC (WINAPI *qwglCreateContext)(HDC);
137 BOOL (WINAPI *qwglDeleteContext)(HGLRC);
138 PROC (WINAPI *qwglGetProcAddress)(LPCSTR);
139 BOOL (WINAPI *qwglMakeCurrent)(HDC, HGLRC);
140 //BOOL (WINAPI *qwglSwapIntervalEXT)(int interval);
141 #endif
142
143
144 typedef struct
145 {
146         char *name;
147         void **funcvariable;
148 }
149 gl_extensionfunctionlist_t;
150
151 typedef struct
152 {
153         char *name;
154         gl_extensionfunctionlist_t *funcs;
155         int *enablevariable;
156         char *disableparm;
157 }
158 gl_extensioninfo_t;
159
160 #if WIN32
161 static gl_extensionfunctionlist_t wglfuncs[] =
162 {
163         {"wglChoosePixelFormat", (void **) &qwglChoosePixelFormat},
164         {"wglDescribePixelFormat", (void **) &qwglDescribePixelFormat},
165 //      {"wglGetPixelFormat", (void **) &qwglGetPixelFormat},
166         {"wglSetPixelFormat", (void **) &qwglSetPixelFormat},
167         {"wglSwapBuffers", (void **) &qwglSwapBuffers},
168         {"wglCreateContext", (void **) &qwglCreateContext},
169         {"wglDeleteContext", (void **) &qwglDeleteContext},
170         {"wglGetProcAddress", (void **) &qwglGetProcAddress},
171         {"wglMakeCurrent", (void **) &qwglMakeCurrent},
172         {NULL, NULL}
173 };
174
175 /*
176 static gl_extensionfunctionlist_t wglswapintervalfuncs[] =
177 {
178         {"wglSwapIntervalEXT", (void **) &qwglSwapIntervalEXT},
179         {NULL, NULL}
180 };
181 */
182 #endif
183
184 static gl_extensionfunctionlist_t opengl110funcs[] =
185 {
186         {"glClearColor", (void **) &qglClearColor},
187         {"glClear", (void **) &qglClear},
188 //      {"glAlphaFunc", (void **) &qglAlphaFunc},
189         {"glBlendFunc", (void **) &qglBlendFunc},
190         {"glCullFace", (void **) &qglCullFace},
191         {"glDrawBuffer", (void **) &qglDrawBuffer},
192         {"glReadBuffer", (void **) &qglReadBuffer},
193         {"glEnable", (void **) &qglEnable},
194         {"glDisable", (void **) &qglDisable},
195 //      {"glIsEnabled", (void **) &qglIsEnabled},
196         {"glEnableClientState", (void **) &qglEnableClientState},
197         {"glDisableClientState", (void **) &qglDisableClientState},
198 //      {"glGetBooleanv", (void **) &qglGetBooleanv},
199 //      {"glGetDoublev", (void **) &qglGetDoublev},
200 //      {"glGetFloatv", (void **) &qglGetFloatv},
201         {"glGetIntegerv", (void **) &qglGetIntegerv},
202         {"glGetError", (void **) &qglGetError},
203         {"glGetString", (void **) &qglGetString},
204         {"glFinish", (void **) &qglFinish},
205         {"glFlush", (void **) &qglFlush},
206         {"glClearDepth", (void **) &qglClearDepth},
207         {"glDepthFunc", (void **) &qglDepthFunc},
208         {"glDepthMask", (void **) &qglDepthMask},
209         {"glDepthRange", (void **) &qglDepthRange},
210         {"glDrawElements", (void **) &qglDrawElements},
211         {"glVertexPointer", (void **) &qglVertexPointer},
212 //      {"glNormalPointer", (void **) &qglNormalPointer},
213         {"glColorPointer", (void **) &qglColorPointer},
214         {"glTexCoordPointer", (void **) &qglTexCoordPointer},
215         {"glArrayElement", (void **) &qglArrayElement},
216         {"glColor4ub", (void **) &qglColor4ub},
217         {"glTexCoord2f", (void **) &qglTexCoord2f},
218         {"glVertex2f", (void **) &qglVertex2f},
219         {"glVertex3f", (void **) &qglVertex3f},
220         {"glBegin", (void **) &qglBegin},
221         {"glEnd", (void **) &qglEnd},
222         {"glMatrixMode", (void **) &qglMatrixMode},
223         {"glOrtho", (void **) &qglOrtho},
224         {"glFrustum", (void **) &qglFrustum},
225         {"glViewport", (void **) &qglViewport},
226 //      {"glPushMatrix", (void **) &qglPushMatrix},
227 //      {"glPopMatrix", (void **) &qglPopMatrix},
228         {"glLoadIdentity", (void **) &qglLoadIdentity},
229 //      {"glLoadMatrixd", (void **) &qglLoadMatrixd},
230 //      {"glLoadMatrixf", (void **) &qglLoadMatrixf},
231 //      {"glMultMatrixd", (void **) &qglMultMatrixd},
232 //      {"glMultMatrixf", (void **) &qglMultMatrixf},
233 //      {"glRotated", (void **) &qglRotated},
234         {"glRotatef", (void **) &qglRotatef},
235 //      {"glScaled", (void **) &qglScaled},
236 //      {"glScalef", (void **) &qglScalef},
237 //      {"glTranslated", (void **) &qglTranslated},
238         {"glTranslatef", (void **) &qglTranslatef},
239         {"glReadPixels", (void **) &qglReadPixels},
240 //      {"glStencilFunc", (void **) &qglStencilFunc},
241 //      {"glStencilMask", (void **) &qglStencilMask},
242 //      {"glStencilOp", (void **) &qglStencilOp},
243 //      {"glClearStencil", (void **) &qglClearStencil},
244 //      {"glTexEnvf", (void **) &qglTexEnvf},
245         {"glTexEnvi", (void **) &qglTexEnvi},
246 //      {"glTexParameterf", (void **) &qglTexParameterf},
247         {"glTexParameteri", (void **) &qglTexParameteri},
248         {"glBindTexture", (void **) &qglBindTexture},
249         {"glTexImage2D", (void **) &qglTexImage2D},
250         {"glTexSubImage2D", (void **) &qglTexSubImage2D},
251         {"glDeleteTextures", (void **) &qglDeleteTextures},
252         {NULL, NULL}
253 };
254
255 static gl_extensionfunctionlist_t drawrangeelementsfuncs[] =
256 {
257         {"glDrawRangeElements", (void **) &qglDrawRangeElements},
258         {NULL, NULL}
259 };
260
261 static gl_extensionfunctionlist_t drawrangeelementsextfuncs[] =
262 {
263         {"glDrawRangeElementsEXT", (void **) &qglDrawRangeElementsEXT},
264         {NULL, NULL}
265 };
266
267 static gl_extensionfunctionlist_t multitexturefuncs[] =
268 {
269         {"glMultiTexCoord2fARB", (void **) &qglMultiTexCoord2f},
270         {"glActiveTextureARB", (void **) &qglActiveTexture},
271         {"glClientActiveTextureARB", (void **) &qglClientActiveTexture},
272         {NULL, NULL}
273 };
274
275 static gl_extensionfunctionlist_t compiledvertexarrayfuncs[] =
276 {
277         {"glLockArraysEXT", (void **) &qglLockArraysEXT},
278         {"glUnlockArraysEXT", (void **) &qglUnlockArraysEXT},
279         {NULL, NULL}
280 };
281
282 #ifndef WIN32
283 #include <dlfcn.h>
284 #endif
285
286 #ifdef WIN32
287 static HINSTANCE gldll;
288 #else
289 static void *prjobj = NULL;
290 #endif
291
292 static void gl_getfuncs_begin(void)
293 {
294 #ifdef WIN32
295         gldll = LoadLibrary("opengl32.dll");
296 #else
297         if (prjobj)
298                 dlclose(prjobj);
299
300         prjobj = dlopen(NULL, RTLD_LAZY);
301         if (prjobj == NULL)
302         {
303                 Con_Printf("Unable to open symbol list for main program.\n");
304                 return;
305         }
306 #endif
307 }
308
309 static void gl_getfuncs_end(void)
310 {
311 #ifdef WIN32
312         FreeLibrary(gldll);
313 #else
314         if (prjobj)
315                 dlclose(prjobj);
316         prjobj = NULL;
317 #endif
318 }
319
320 static void *gl_getfuncaddress(char *name)
321 {
322         void *p = NULL;
323 #ifdef WIN32
324         if (qwglGetProcAddress != NULL)
325                 p = (void *) qwglGetProcAddress(name);
326         if (p == NULL)
327                 p = (void *) GetProcAddress(gldll, name);
328 #else
329         p = (void *) dlsym(prjobj, name);
330 #endif
331         return p;
332 }
333
334 static int gl_checkextension(char *name, gl_extensionfunctionlist_t *funcs, char *disableparm, int silent)
335 {
336         int failed = false;
337         gl_extensionfunctionlist_t *func;
338
339         Con_Printf("checking for %s...  ", name);
340
341         for (func = funcs;func && func->name;func++)
342                 *func->funcvariable = NULL;
343
344         if (disableparm && COM_CheckParm(disableparm))
345         {
346                 Con_Printf("disabled by commandline\n");
347                 return false;
348         }
349
350         if (strncmp(name, "GL_", 3) || strstr(gl_extensions, name))
351         {
352                 for (func = funcs;func && func->name != NULL;func++)
353                 {
354                         // functions are cleared before all the extensions are evaluated
355                         if (!(*func->funcvariable = (void *) gl_getfuncaddress(func->name)))
356                         {
357                                 if (!silent)
358                                         Con_Printf("missing function \"%s\" - broken driver!\n", func->name);
359                                 failed = true;
360                         }
361                 }
362                 // delay the return so it prints all missing functions
363                 if (failed)
364                         return false;
365                 Con_Printf("enabled\n");
366                 return true;
367         }
368         else
369         {
370                 Con_Printf("not detected\n");
371                 return false;
372         }
373 }
374
375 void VID_CheckExtensions(void)
376 {
377         Con_Printf("Checking OpenGL extensions...\n");
378
379         gl_getfuncs_begin();
380
381         gl_combine_extension = false;
382         gl_supportslockarrays = false;
383         gl_textureunits = 1;
384
385 #if WIN32
386         if (!gl_checkextension("wgl", wglfuncs, NULL, false))
387                 Sys_Error("wgl functions not found\n");
388         //gl_checkextension("wglSwapIntervalEXT", wglswapintervalfuncs, NULL, false);
389 #endif
390
391         if (!gl_checkextension("OpenGL 1.1.0", opengl110funcs, NULL, false))
392                 Sys_Error("OpenGL 1.1.0 functions not found\n");
393
394         if (!gl_checkextension("glDrawRangeElements", drawrangeelementsfuncs, "-nodrawrangeelements", true))
395                 gl_checkextension("GL_EXT_draw_range_elements", drawrangeelementsextfuncs, "-nodrawrangeelements", true);
396
397         if (gl_checkextension("GL_ARB_multitexture", multitexturefuncs, "-nomtex", false))
398         {
399                 qglGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_textureunits);
400                 if (gl_textureunits > 1)
401                         gl_combine_extension = gl_checkextension("GL_ARB_texture_env_combine", NULL, "-nocombine", false) || gl_checkextension("GL_EXT_texture_env_combine", NULL, "-nocombine", false);
402                 else
403                 {
404                         Con_Printf("GL_ARB_multitexture with less than 2 units? - BROKEN DRIVER!\n");
405                         gl_textureunits = 1; // for sanity sake, make sure it's not 0
406                 }
407         }
408
409         gl_supportslockarrays = gl_checkextension("GL_EXT_compiled_vertex_array", compiledvertexarrayfuncs, "-nocva", false);
410
411         gl_getfuncs_end();
412
413         // we don't care if it's an extension or not, they are identical functions, so keep it simple in the rendering code
414         if (qglDrawRangeElements == NULL)
415                 qglDrawRangeElements = qglDrawRangeElementsEXT;
416 }
417
418 void Force_CenterView_f (void)
419 {
420         cl.viewangles[PITCH] = 0;
421 }
422
423 void IN_PreMove(void)
424 {
425 }
426
427 void CL_AdjustAngles(void);
428 void IN_PostMove(void)
429 {
430         // clamp after the move as well to prevent messed up rendering angles
431         CL_AdjustAngles();
432 }
433
434 void IN_Mouse(usercmd_t *cmd, float mx, float my)
435 {
436         int mouselook = (in_mlook.state & 1) || freelook.integer;
437         float mouse_x, mouse_y;
438         static float old_mouse_x = 0, old_mouse_y = 0;
439
440         if (m_filter.integer)
441         {
442                 mouse_x = (mx + old_mouse_x) * 0.5;
443                 mouse_y = (my + old_mouse_y) * 0.5;
444         }
445         else
446         {
447                 mouse_x = mx;
448                 mouse_y = my;
449         }
450
451         old_mouse_x = mx;
452         old_mouse_y = my;
453
454         // LordHavoc: viewzoom affects mouse sensitivity for sniping
455         mouse_x *= sensitivity.value * cl.viewzoom;
456         mouse_y *= sensitivity.value * cl.viewzoom;
457
458         // Add mouse X/Y movement to cmd
459         if ((in_strafe.state & 1) || (lookstrafe.integer && mouselook))
460                 cmd->sidemove += m_side.value * mouse_x;
461         else
462                 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
463
464         if (mouselook)
465                 V_StopPitchDrift();
466
467         if (mouselook && !(in_strafe.state & 1))
468                 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
469         else
470         {
471                 if ((in_strafe.state & 1) && noclip_anglehack)
472                         cmd->upmove -= m_forward.value * mouse_y;
473                 else
474                         cmd->forwardmove -= m_forward.value * mouse_y;
475         }
476 }
477
478 void VID_InitCvars(void)
479 {
480         Cvar_RegisterVariable(&vid_mode);
481         Cvar_RegisterVariable(&vid_mouse);
482         Cvar_RegisterVariable(&vid_fullscreen);
483         Cvar_RegisterVariable(&gl_combine);
484         Cvar_RegisterVariable(&in_pitch_min);
485         Cvar_RegisterVariable(&in_pitch_max);
486         Cvar_RegisterVariable(&m_filter);
487         Cmd_AddCommand("force_centerview", Force_CenterView_f);
488 }
489