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