]> icculus.org git repositories - divverent/darkplaces.git/blob - vid_shared.c
independent control of gl_mesh_maxtriangles and gl_mesh_transtriangles (that is to...
[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 *qglTexImage2D)(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels);
116
117 void (GLAPIENTRY *qglBindTexture)(GLenum target, GLuint texture);
118
119 void (GLAPIENTRY *qglTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels);
120
121
122 typedef struct
123 {
124         char *name;
125         void **funcvariable;
126 }
127 gl_extensionfunctionlist_t;
128
129 typedef struct
130 {
131         char *name;
132         gl_extensionfunctionlist_t *funcs;
133         int *enablevariable;
134         char *disableparm;
135 }
136 gl_extensioninfo_t;
137
138 static gl_extensionfunctionlist_t opengl110funcs[] =
139 {
140         {"glClearColor", (void **) &qglClearColor},
141         {"glClear", (void **) &qglClear},
142 //      {"glAlphaFunc", (void **) &qglAlphaFunc},
143         {"glBlendFunc", (void **) &qglBlendFunc},
144         {"glCullFace", (void **) &qglCullFace},
145         {"glDrawBuffer", (void **) &qglDrawBuffer},
146         {"glReadBuffer", (void **) &qglReadBuffer},
147         {"glEnable", (void **) &qglEnable},
148         {"glDisable", (void **) &qglDisable},
149 //      {"glIsEnabled", (void **) &qglIsEnabled},
150         {"glEnableClientState", (void **) &qglEnableClientState},
151         {"glDisableClientState", (void **) &qglDisableClientState},
152 //      {"glGetBooleanv", (void **) &qglGetBooleanv},
153 //      {"glGetDoublev", (void **) &qglGetDoublev},
154 //      {"glGetFloatv", (void **) &qglGetFloatv},
155         {"glGetIntegerv", (void **) &qglGetIntegerv},
156         {"glGetError", (void **) &qglGetError},
157         {"glGetString", (void **) &qglGetString},
158         {"glFinish", (void **) &qglFinish},
159         {"glFlush", (void **) &qglFlush},
160         {"glClearDepth", (void **) &qglClearDepth},
161         {"glDepthFunc", (void **) &qglDepthFunc},
162         {"glDepthMask", (void **) &qglDepthMask},
163         {"glDepthRange", (void **) &qglDepthRange},
164         {"glDrawElements", (void **) &qglDrawElements},
165         {"glVertexPointer", (void **) &qglVertexPointer},
166 //      {"glNormalPointer", (void **) &qglNormalPointer},
167         {"glColorPointer", (void **) &qglColorPointer},
168         {"glTexCoordPointer", (void **) &qglTexCoordPointer},
169         {"glArrayElement", (void **) &qglArrayElement},
170         {"glColor4ub", (void **) &qglColor4ub},
171         {"glTexCoord2f", (void **) &qglTexCoord2f},
172         {"glVertex2f", (void **) &qglVertex2f},
173         {"glVertex3f", (void **) &qglVertex3f},
174         {"glBegin", (void **) &qglBegin},
175         {"glEnd", (void **) &qglEnd},
176         {"glMatrixMode", (void **) &qglMatrixMode},
177         {"glOrtho", (void **) &qglOrtho},
178         {"glFrustum", (void **) &qglFrustum},
179         {"glViewport", (void **) &qglViewport},
180 //      {"glPushMatrix", (void **) &qglPushMatrix},
181 //      {"glPopMatrix", (void **) &qglPopMatrix},
182         {"glLoadIdentity", (void **) &qglLoadIdentity},
183 //      {"glLoadMatrixd", (void **) &qglLoadMatrixd},
184 //      {"glLoadMatrixf", (void **) &qglLoadMatrixf},
185 //      {"glMultMatrixd", (void **) &qglMultMatrixd},
186 //      {"glMultMatrixf", (void **) &qglMultMatrixf},
187 //      {"glRotated", (void **) &qglRotated},
188         {"glRotatef", (void **) &qglRotatef},
189 //      {"glScaled", (void **) &qglScaled},
190 //      {"glScalef", (void **) &qglScalef},
191 //      {"glTranslated", (void **) &qglTranslated},
192         {"glTranslatef", (void **) &qglTranslatef},
193         {"glReadPixels", (void **) &qglReadPixels},
194 //      {"glStencilFunc", (void **) &qglStencilFunc},
195 //      {"glStencilMask", (void **) &qglStencilMask},
196 //      {"glStencilOp", (void **) &qglStencilOp},
197 //      {"glClearStencil", (void **) &qglClearStencil},
198 //      {"glTexEnvf", (void **) &qglTexEnvf},
199         {"glTexEnvi", (void **) &qglTexEnvi},
200 //      {"glTexParameterf", (void **) &qglTexParameterf},
201         {"glTexParameteri", (void **) &qglTexParameteri},
202         {"glTexImage2D", (void **) &qglTexImage2D},
203         {"glBindTexture", (void **) &qglBindTexture},
204         {"glTexSubImage2D", (void **) &qglTexSubImage2D},
205         {NULL, NULL}
206 };
207
208 static gl_extensionfunctionlist_t drawrangeelementsfuncs[] =
209 {
210         {"glDrawRangeElements", (void **) &qglDrawRangeElements},
211         {NULL, NULL}
212 };
213
214 static gl_extensionfunctionlist_t multitexturefuncs[] =
215 {
216         {"glMultiTexCoord2fARB", (void **) &qglMultiTexCoord2f},
217         {"glActiveTextureARB", (void **) &qglActiveTexture},
218         {"glClientActiveTextureARB", (void **) &qglClientActiveTexture},
219         {NULL, NULL}
220 };
221
222 static gl_extensionfunctionlist_t compiledvertexarrayfuncs[] =
223 {
224         {"glLockArraysEXT", (void **) &qglLockArraysEXT},
225         {"glUnlockArraysEXT", (void **) &qglUnlockArraysEXT},
226         {NULL, NULL}
227 };
228
229 #ifndef WIN32
230 #include <dlfcn.h>
231 #endif
232
233 #ifndef WIN32
234 static void *prjobj = NULL;
235 #endif
236
237 static void gl_getfuncs_begin(void)
238 {
239 #ifndef WIN32
240         if (prjobj)
241                 dlclose(prjobj);
242
243         prjobj = dlopen(NULL, RTLD_LAZY);
244         if (prjobj == NULL)
245         {
246                 Con_Printf("Unable to open symbol list for main program.\n");
247                 return;
248         }
249 #endif
250 }
251
252 static void gl_getfuncs_end(void)
253 {
254 #ifndef WIN32
255         if (prjobj)
256         {
257                 dlclose(prjobj);
258                 prjobj = NULL;
259         }
260 #endif
261 }
262
263 static void *gl_getfuncaddress(char *name)
264 {
265 #ifdef WIN32
266         return (void *) wglGetProcAddress(name);
267 #else
268         return (void *) dlsym(prjobj, name);
269 #endif
270 }
271
272 static int gl_checkextension(char *name, gl_extensionfunctionlist_t *funcs, char *disableparm)
273 {
274         gl_extensionfunctionlist_t *func;
275
276         Con_Printf("checking for %s...  ", name);
277
278         for (func = funcs;func && func->name;func++)
279                 *func->funcvariable = NULL;
280
281         if (disableparm && COM_CheckParm(disableparm))
282         {
283                 Con_Printf("disabled by commandline\n");
284                 return false;
285         }
286
287         if (!strstr(name, "GL_") || strstr(gl_extensions, name))
288         {
289                 for (func = funcs;func && func->name != NULL;func++)
290                 {
291                         if (!(*func->funcvariable = (void *) gl_getfuncaddress(func->name)))
292                         {
293                                 Con_Printf("missing function \"%s\"!\n", func->name);
294                                 return false;
295                         }
296                 }
297                 Con_Printf("enabled\n");
298                 return true;
299         }
300         else
301         {
302                 Con_Printf("not detected\n");
303                 return false;
304         }
305 }
306
307 void VID_CheckExtensions(void)
308 {
309         Con_Printf("Checking OpenGL extensions...\n");
310
311         gl_getfuncs_begin();
312
313         gl_combine_extension = false;
314         gl_supportslockarrays = false;
315         gl_textureunits = 1;
316
317         if (!gl_checkextension("OpenGL 1.1.0 or above", opengl110funcs, NULL))
318                 Sys_Error("OpenGL 1.1.0 functions not found\n");
319
320         gl_checkextension("glDrawRangeElements", drawrangeelementsfuncs, "-nodrawrangeelements");
321
322         if (gl_checkextension("GL_ARB_multitexture", multitexturefuncs, "-nomtex"))
323         {
324                 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_textureunits);
325                 if (gl_textureunits > 1)
326                         gl_combine_extension = gl_checkextension("GL_ARB_texture_env_combine", NULL, "-nocombine") || gl_checkextension("GL_EXT_texture_env_combine", NULL, "-nocombine");
327                 else
328                         gl_textureunits = 1; // for sanity sake, make sure it's not 0
329         }
330
331         gl_supportslockarrays = gl_checkextension("GL_EXT_compiled_vertex_array", compiledvertexarrayfuncs, "-nocva");
332
333         gl_getfuncs_end();
334 }
335
336 void Force_CenterView_f (void)
337 {
338         cl.viewangles[PITCH] = 0;
339 }
340
341 void IN_PreMove(void)
342 {
343 }
344
345 void CL_AdjustAngles(void);
346 void IN_PostMove(void)
347 {
348         // clamp after the move as well to prevent messed up rendering angles
349         CL_AdjustAngles();
350 }
351
352 void IN_Mouse(usercmd_t *cmd, float mx, float my)
353 {
354         int mouselook = (in_mlook.state & 1) || freelook.integer;
355         float mouse_x, mouse_y;
356         static float old_mouse_x = 0, old_mouse_y = 0;
357
358         if (m_filter.integer)
359         {
360                 mouse_x = (mx + old_mouse_x) * 0.5;
361                 mouse_y = (my + old_mouse_y) * 0.5;
362         }
363         else
364         {
365                 mouse_x = mx;
366                 mouse_y = my;
367         }
368
369         old_mouse_x = mx;
370         old_mouse_y = my;
371
372         // LordHavoc: viewzoom affects mouse sensitivity for sniping
373         mouse_x *= sensitivity.value * cl.viewzoom;
374         mouse_y *= sensitivity.value * cl.viewzoom;
375
376         // Add mouse X/Y movement to cmd
377         if ((in_strafe.state & 1) || (lookstrafe.integer && mouselook))
378                 cmd->sidemove += m_side.value * mouse_x;
379         else
380                 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
381
382         if (mouselook)
383                 V_StopPitchDrift();
384
385         if (mouselook && !(in_strafe.state & 1))
386                 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
387         else
388         {
389                 if ((in_strafe.state & 1) && noclip_anglehack)
390                         cmd->upmove -= m_forward.value * mouse_y;
391                 else
392                         cmd->forwardmove -= m_forward.value * mouse_y;
393         }
394 }
395
396 void VID_InitCvars(void)
397 {
398         Cvar_RegisterVariable(&vid_mode);
399         Cvar_RegisterVariable(&vid_mouse);
400         Cvar_RegisterVariable(&vid_fullscreen);
401         Cvar_RegisterVariable(&gl_combine);
402         Cvar_RegisterVariable(&in_pitch_min);
403         Cvar_RegisterVariable(&in_pitch_max);
404         Cvar_RegisterVariable(&m_filter);
405         Cmd_AddCommand("force_centerview", Force_CenterView_f);
406 }