]> icculus.org git repositories - divverent/darkplaces.git/blob - vid_shared.c
change a lot of calls from gl to qgl
[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 #ifdef WIN32
234 static HINSTANCE gldll;
235 #else
236 static void *prjobj = NULL;
237 #endif
238
239 static void gl_getfuncs_begin(void)
240 {
241 #ifdef WIN32
242         gldll = LoadLibrary("opengl32.dll");
243 #else
244         if (prjobj)
245                 dlclose(prjobj);
246
247         prjobj = dlopen(NULL, RTLD_LAZY);
248         if (prjobj == NULL)
249         {
250                 Con_Printf("Unable to open symbol list for main program.\n");
251                 return;
252         }
253 #endif
254 }
255
256 static void gl_getfuncs_end(void)
257 {
258 #ifdef WIN32
259         FreeLibrary(gldll);
260 #else
261         if (prjobj)
262                 dlclose(prjobj);
263         prjobj = NULL;
264 #endif
265 }
266
267 static void *gl_getfuncaddress(char *name)
268 {
269 #ifdef WIN32
270 //      return (void *) wglGetProcAddress(name);
271         return (void *) GetProcAddress(gldll, name);
272 #else
273         return (void *) dlsym(prjobj, name);
274 #endif
275 }
276
277 static int gl_checkextension(char *name, gl_extensionfunctionlist_t *funcs, char *disableparm)
278 {
279         int failed = false;
280         gl_extensionfunctionlist_t *func;
281
282         Con_Printf("checking for %s...  ", name);
283
284         for (func = funcs;func && func->name;func++)
285                 *func->funcvariable = NULL;
286
287         if (disableparm && COM_CheckParm(disableparm))
288         {
289                 Con_Printf("disabled by commandline\n");
290                 return false;
291         }
292
293         if (!strstr(name, "GL_") || strstr(gl_extensions, name))
294         {
295                 for (func = funcs;func && func->name != NULL;func++)
296                 {
297                         if (!(*func->funcvariable = (void *) gl_getfuncaddress(func->name)))
298                         {
299                                 Con_Printf("missing function \"%s\"!\n", func->name);
300                                 failed = true;
301                         }
302                 }
303                 // delay the return so it prints all missing functions
304                 if (failed)
305                         return false;
306                 Con_Printf("enabled\n");
307                 return true;
308         }
309         else
310         {
311                 Con_Printf("not detected\n");
312                 return false;
313         }
314 }
315
316 void VID_CheckExtensions(void)
317 {
318         Con_Printf("Checking OpenGL extensions...\n");
319
320         gl_getfuncs_begin();
321
322         gl_combine_extension = false;
323         gl_supportslockarrays = false;
324         gl_textureunits = 1;
325
326         if (!gl_checkextension("OpenGL 1.1.0 or above", opengl110funcs, NULL))
327                 Sys_Error("OpenGL 1.1.0 functions not found\n");
328
329         gl_checkextension("glDrawRangeElements", drawrangeelementsfuncs, "-nodrawrangeelements");
330
331         if (gl_checkextension("GL_ARB_multitexture", multitexturefuncs, "-nomtex"))
332         {
333                 qglGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_textureunits);
334                 if (gl_textureunits > 1)
335                         gl_combine_extension = gl_checkextension("GL_ARB_texture_env_combine", NULL, "-nocombine") || gl_checkextension("GL_EXT_texture_env_combine", NULL, "-nocombine");
336                 else
337                         gl_textureunits = 1; // for sanity sake, make sure it's not 0
338         }
339
340         gl_supportslockarrays = gl_checkextension("GL_EXT_compiled_vertex_array", compiledvertexarrayfuncs, "-nocva");
341
342         gl_getfuncs_end();
343 }
344
345 void Force_CenterView_f (void)
346 {
347         cl.viewangles[PITCH] = 0;
348 }
349
350 void IN_PreMove(void)
351 {
352 }
353
354 void CL_AdjustAngles(void);
355 void IN_PostMove(void)
356 {
357         // clamp after the move as well to prevent messed up rendering angles
358         CL_AdjustAngles();
359 }
360
361 void IN_Mouse(usercmd_t *cmd, float mx, float my)
362 {
363         int mouselook = (in_mlook.state & 1) || freelook.integer;
364         float mouse_x, mouse_y;
365         static float old_mouse_x = 0, old_mouse_y = 0;
366
367         if (m_filter.integer)
368         {
369                 mouse_x = (mx + old_mouse_x) * 0.5;
370                 mouse_y = (my + old_mouse_y) * 0.5;
371         }
372         else
373         {
374                 mouse_x = mx;
375                 mouse_y = my;
376         }
377
378         old_mouse_x = mx;
379         old_mouse_y = my;
380
381         // LordHavoc: viewzoom affects mouse sensitivity for sniping
382         mouse_x *= sensitivity.value * cl.viewzoom;
383         mouse_y *= sensitivity.value * cl.viewzoom;
384
385         // Add mouse X/Y movement to cmd
386         if ((in_strafe.state & 1) || (lookstrafe.integer && mouselook))
387                 cmd->sidemove += m_side.value * mouse_x;
388         else
389                 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
390
391         if (mouselook)
392                 V_StopPitchDrift();
393
394         if (mouselook && !(in_strafe.state & 1))
395                 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
396         else
397         {
398                 if ((in_strafe.state & 1) && noclip_anglehack)
399                         cmd->upmove -= m_forward.value * mouse_y;
400                 else
401                         cmd->forwardmove -= m_forward.value * mouse_y;
402         }
403 }
404
405 void VID_InitCvars(void)
406 {
407         Cvar_RegisterVariable(&vid_mode);
408         Cvar_RegisterVariable(&vid_mouse);
409         Cvar_RegisterVariable(&vid_fullscreen);
410         Cvar_RegisterVariable(&gl_combine);
411         Cvar_RegisterVariable(&in_pitch_min);
412         Cvar_RegisterVariable(&in_pitch_max);
413         Cvar_RegisterVariable(&m_filter);
414         Cmd_AddCommand("force_centerview", Force_CenterView_f);
415 }