]> icculus.org git repositories - divverent/darkplaces.git/blob - vid_shared.c
fixed.
[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 // LordHavoc: GL_ARB_multitexture support
12 int gl_textureunits = 0;
13 // LordHavoc: GL_ARB_texture_env_combine or GL_EXT_texture_env_combine support
14 int gl_combine_extension = false;
15 // LordHavoc: GL_EXT_compiled_vertex_array support
16 int gl_supportslockarrays = false;
17 // LordHavoc: GLX_SGI_video_sync and WGL_EXT_swap_control
18 int gl_videosyncavailable = false;
19
20 // LordHavoc: if window is hidden, don't update screen
21 int vid_hidden = false;
22 // LordHavoc: if window is not the active window, don't hog as much CPU time,
23 // let go of the mouse, turn off sound, and restore system gamma ramps...
24 int vid_activewindow = true;
25
26 cvar_t vid_fullscreen = {0, "vid_fullscreen", "1"};
27 cvar_t vid_width = {0, "vid_width", "640"};
28 cvar_t vid_height = {0, "vid_height", "480"};
29 cvar_t vid_bitsperpixel = {0, "vid_bitsperpixel", "16"};
30
31 cvar_t vid_mouse = {CVAR_SAVE, "vid_mouse", "1"};
32 cvar_t gl_combine = {0, "gl_combine", "1"};
33
34 cvar_t in_pitch_min = {0, "in_pitch_min", "-90"};
35 cvar_t in_pitch_max = {0, "in_pitch_max", "90"};
36
37 cvar_t m_filter = {CVAR_SAVE, "m_filter","0"};
38
39 // brand of graphics chip
40 const char *gl_vendor;
41 // graphics chip model and other information
42 const char *gl_renderer;
43 // begins with 1.0.0, 1.1.0, 1.2.0, 1.2.1, 1.3.0, 1.3.1, or 1.4.0
44 const char *gl_version;
45 // extensions list, space separated
46 const char *gl_extensions;
47 // WGL, GLX, or AGL
48 const char *gl_platform;
49 // another extensions list, containing platform-specific extensions that are
50 // not in the main list
51 const char *gl_platformextensions;
52 // name of driver library (opengl32.dll, libGL.so.1, or whatever)
53 char gl_driver[256];
54
55 // GL_ARB_multitexture
56 void (GLAPIENTRY *qglMultiTexCoord2f) (GLenum, GLfloat, GLfloat);
57 void (GLAPIENTRY *qglActiveTexture) (GLenum);
58 void (GLAPIENTRY *qglClientActiveTexture) (GLenum);
59
60 // GL_EXT_compiled_vertex_array
61 void (GLAPIENTRY *qglLockArraysEXT) (GLint first, GLint count);
62 void (GLAPIENTRY *qglUnlockArraysEXT) (void);
63
64
65 // general GL functions
66
67 void (GLAPIENTRY *qglClearColor)(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
68
69 void (GLAPIENTRY *qglClear)(GLbitfield mask);
70
71 //void (GLAPIENTRY *qglAlphaFunc)(GLenum func, GLclampf ref);
72 void (GLAPIENTRY *qglBlendFunc)(GLenum sfactor, GLenum dfactor);
73 void (GLAPIENTRY *qglCullFace)(GLenum mode);
74
75 void (GLAPIENTRY *qglDrawBuffer)(GLenum mode);
76 void (GLAPIENTRY *qglReadBuffer)(GLenum mode);
77 void (GLAPIENTRY *qglEnable)(GLenum cap);
78 void (GLAPIENTRY *qglDisable)(GLenum cap);
79 //GLboolean GLAPIENTRY *qglIsEnabled)(GLenum cap);
80
81 void (GLAPIENTRY *qglEnableClientState)(GLenum cap);
82 void (GLAPIENTRY *qglDisableClientState)(GLenum cap);
83
84 void (GLAPIENTRY *qglGetBooleanv)(GLenum pname, GLboolean *params);
85 void (GLAPIENTRY *qglGetDoublev)(GLenum pname, GLdouble *params);
86 void (GLAPIENTRY *qglGetFloatv)(GLenum pname, GLfloat *params);
87 void (GLAPIENTRY *qglGetIntegerv)(GLenum pname, GLint *params);
88
89 GLenum (GLAPIENTRY *qglGetError)(void);
90 const GLubyte* (GLAPIENTRY *qglGetString)(GLenum name);
91 void (GLAPIENTRY *qglFinish)(void);
92 void (GLAPIENTRY *qglFlush)(void);
93
94 void (GLAPIENTRY *qglClearDepth)(GLclampd depth);
95 void (GLAPIENTRY *qglDepthFunc)(GLenum func);
96 void (GLAPIENTRY *qglDepthMask)(GLboolean flag);
97 void (GLAPIENTRY *qglDepthRange)(GLclampd near_val, GLclampd far_val);
98
99 void (GLAPIENTRY *qglDrawRangeElements)(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices);
100 void (GLAPIENTRY *qglDrawElements)(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
101 void (GLAPIENTRY *qglVertexPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
102 //void (GLAPIENTRY *qglNormalPointer)(GLenum type, GLsizei stride, const GLvoid *ptr);
103 void (GLAPIENTRY *qglColorPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
104 void (GLAPIENTRY *qglTexCoordPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
105 void (GLAPIENTRY *qglArrayElement)(GLint i);
106
107 void (GLAPIENTRY *qglColor4ub)(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha);
108 void (GLAPIENTRY *qglTexCoord2f)(GLfloat s, GLfloat t);
109 void (GLAPIENTRY *qglVertex2f)(GLfloat x, GLfloat y);
110 void (GLAPIENTRY *qglVertex3f)(GLfloat x, GLfloat y, GLfloat z);
111 void (GLAPIENTRY *qglBegin)(GLenum mode);
112 void (GLAPIENTRY *qglEnd)(void);
113
114 void (GLAPIENTRY *qglMatrixMode)(GLenum mode);
115 void (GLAPIENTRY *qglOrtho)(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val);
116 void (GLAPIENTRY *qglFrustum)(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val);
117 void (GLAPIENTRY *qglViewport)(GLint x, GLint y, GLsizei width, GLsizei height);
118 //void (GLAPIENTRY *qglPushMatrix)(void);
119 //void (GLAPIENTRY *qglPopMatrix)(void);
120 void (GLAPIENTRY *qglLoadIdentity)(void);
121 void (GLAPIENTRY *qglLoadMatrixd)(const GLdouble *m);
122 void (GLAPIENTRY *qglLoadMatrixf)(const GLfloat *m);
123 //void (GLAPIENTRY *qglMultMatrixd)(const GLdouble *m);
124 //void (GLAPIENTRY *qglMultMatrixf)(const GLfloat *m);
125 //void (GLAPIENTRY *qglRotated)(GLdouble angle, GLdouble x, GLdouble y, GLdouble z);
126 void (GLAPIENTRY *qglRotatef)(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
127 //void (GLAPIENTRY *qglScaled)(GLdouble x, GLdouble y, GLdouble z);
128 //void (GLAPIENTRY *qglScalef)(GLfloat x, GLfloat y, GLfloat z);
129 //void (GLAPIENTRY *qglTranslated)(GLdouble x, GLdouble y, GLdouble z);
130 void (GLAPIENTRY *qglTranslatef)(GLfloat x, GLfloat y, GLfloat z);
131
132 void (GLAPIENTRY *qglReadPixels)(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels);
133
134 //void (GLAPIENTRY *qglStencilFunc)(GLenum func, GLint ref, GLuint mask);
135 //void (GLAPIENTRY *qglStencilMask)(GLuint mask);
136 //void (GLAPIENTRY *qglStencilOp)(GLenum fail, GLenum zfail, GLenum zpass);
137 //void (GLAPIENTRY *qglClearStencil)(GLint s);
138
139 //void (GLAPIENTRY *qglTexEnvf)(GLenum target, GLenum pname, GLfloat param);
140 void (GLAPIENTRY *qglTexEnvi)(GLenum target, GLenum pname, GLint param);
141
142 //void (GLAPIENTRY *qglTexParameterf)(GLenum target, GLenum pname, GLfloat param);
143 void (GLAPIENTRY *qglTexParameteri)(GLenum target, GLenum pname, GLint param);
144
145 void (GLAPIENTRY *qglBindTexture)(GLenum target, GLuint texture);
146 void (GLAPIENTRY *qglTexImage2D)(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels);
147 void (GLAPIENTRY *qglTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels);
148 void (GLAPIENTRY *qglDeleteTextures)(GLsizei n, const GLuint *textures);
149 void (GLAPIENTRY *qglPixelStoref)(GLenum pname, GLfloat param);
150 void (GLAPIENTRY *qglPixelStorei)(GLenum pname, GLint param);
151
152 void (GLAPIENTRY *qglDrawRangeElementsEXT)(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices);
153
154 //void (GLAPIENTRY *qglColorTableEXT)(int, int, int, int, int, const void *);
155
156 int GL_CheckExtension(const char *name, const gl_extensionfunctionlist_t *funcs, const char *disableparm, int silent)
157 {
158         int failed = false;
159         const gl_extensionfunctionlist_t *func;
160
161         Con_Printf("checking for %s...  ", name);
162
163         for (func = funcs;func && func->name;func++)
164                 *func->funcvariable = NULL;
165
166         if (disableparm && COM_CheckParm(disableparm))
167         {
168                 Con_Printf("disabled by commandline\n");
169                 return false;
170         }
171
172         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)))
173         {
174                 for (func = funcs;func && func->name != NULL;func++)
175                 {
176                         // functions are cleared before all the extensions are evaluated
177                         if (!(*func->funcvariable = (void *) GL_GetProcAddress(func->name)))
178                         {
179                                 if (!silent)
180                                         Con_Printf("missing function \"%s\" - broken driver!\n", func->name);
181                                 failed = true;
182                         }
183                 }
184                 // delay the return so it prints all missing functions
185                 if (failed)
186                         return false;
187                 Con_Printf("enabled\n");
188                 return true;
189         }
190         else
191         {
192                 Con_Printf("not detected\n");
193                 return false;
194         }
195 }
196
197 static gl_extensionfunctionlist_t opengl110funcs[] =
198 {
199         {"glClearColor", (void **) &qglClearColor},
200         {"glClear", (void **) &qglClear},
201 //      {"glAlphaFunc", (void **) &qglAlphaFunc},
202         {"glBlendFunc", (void **) &qglBlendFunc},
203         {"glCullFace", (void **) &qglCullFace},
204         {"glDrawBuffer", (void **) &qglDrawBuffer},
205         {"glReadBuffer", (void **) &qglReadBuffer},
206         {"glEnable", (void **) &qglEnable},
207         {"glDisable", (void **) &qglDisable},
208 //      {"glIsEnabled", (void **) &qglIsEnabled},
209         {"glEnableClientState", (void **) &qglEnableClientState},
210         {"glDisableClientState", (void **) &qglDisableClientState},
211         {"glGetBooleanv", (void **) &qglGetBooleanv},
212         {"glGetDoublev", (void **) &qglGetDoublev},
213         {"glGetFloatv", (void **) &qglGetFloatv},
214         {"glGetIntegerv", (void **) &qglGetIntegerv},
215         {"glGetError", (void **) &qglGetError},
216         {"glGetString", (void **) &qglGetString},
217         {"glFinish", (void **) &qglFinish},
218         {"glFlush", (void **) &qglFlush},
219         {"glClearDepth", (void **) &qglClearDepth},
220         {"glDepthFunc", (void **) &qglDepthFunc},
221         {"glDepthMask", (void **) &qglDepthMask},
222         {"glDepthRange", (void **) &qglDepthRange},
223         {"glDrawElements", (void **) &qglDrawElements},
224         {"glVertexPointer", (void **) &qglVertexPointer},
225 //      {"glNormalPointer", (void **) &qglNormalPointer},
226         {"glColorPointer", (void **) &qglColorPointer},
227         {"glTexCoordPointer", (void **) &qglTexCoordPointer},
228         {"glArrayElement", (void **) &qglArrayElement},
229         {"glColor4ub", (void **) &qglColor4ub},
230         {"glTexCoord2f", (void **) &qglTexCoord2f},
231         {"glVertex2f", (void **) &qglVertex2f},
232         {"glVertex3f", (void **) &qglVertex3f},
233         {"glBegin", (void **) &qglBegin},
234         {"glEnd", (void **) &qglEnd},
235         {"glMatrixMode", (void **) &qglMatrixMode},
236         {"glOrtho", (void **) &qglOrtho},
237         {"glFrustum", (void **) &qglFrustum},
238         {"glViewport", (void **) &qglViewport},
239 //      {"glPushMatrix", (void **) &qglPushMatrix},
240 //      {"glPopMatrix", (void **) &qglPopMatrix},
241         {"glLoadIdentity", (void **) &qglLoadIdentity},
242         {"glLoadMatrixd", (void **) &qglLoadMatrixd},
243         {"glLoadMatrixf", (void **) &qglLoadMatrixf},
244 //      {"glMultMatrixd", (void **) &qglMultMatrixd},
245 //      {"glMultMatrixf", (void **) &qglMultMatrixf},
246 //      {"glRotated", (void **) &qglRotated},
247         {"glRotatef", (void **) &qglRotatef},
248 //      {"glScaled", (void **) &qglScaled},
249 //      {"glScalef", (void **) &qglScalef},
250 //      {"glTranslated", (void **) &qglTranslated},
251         {"glTranslatef", (void **) &qglTranslatef},
252         {"glReadPixels", (void **) &qglReadPixels},
253 //      {"glStencilFunc", (void **) &qglStencilFunc},
254 //      {"glStencilMask", (void **) &qglStencilMask},
255 //      {"glStencilOp", (void **) &qglStencilOp},
256 //      {"glClearStencil", (void **) &qglClearStencil},
257 //      {"glTexEnvf", (void **) &qglTexEnvf},
258         {"glTexEnvi", (void **) &qglTexEnvi},
259 //      {"glTexParameterf", (void **) &qglTexParameterf},
260         {"glTexParameteri", (void **) &qglTexParameteri},
261         {"glBindTexture", (void **) &qglBindTexture},
262         {"glTexImage2D", (void **) &qglTexImage2D},
263         {"glTexSubImage2D", (void **) &qglTexSubImage2D},
264         {"glDeleteTextures", (void **) &qglDeleteTextures},
265         {"glPixelStoref", (void **) &qglPixelStoref},
266         {"glPixelStorei", (void **) &qglPixelStorei},
267         {NULL, NULL}
268 };
269
270 static gl_extensionfunctionlist_t drawrangeelementsfuncs[] =
271 {
272         {"glDrawRangeElements", (void **) &qglDrawRangeElements},
273         {NULL, NULL}
274 };
275
276 static gl_extensionfunctionlist_t drawrangeelementsextfuncs[] =
277 {
278         {"glDrawRangeElementsEXT", (void **) &qglDrawRangeElementsEXT},
279         {NULL, NULL}
280 };
281
282 static gl_extensionfunctionlist_t multitexturefuncs[] =
283 {
284         {"glMultiTexCoord2fARB", (void **) &qglMultiTexCoord2f},
285         {"glActiveTextureARB", (void **) &qglActiveTexture},
286         {"glClientActiveTextureARB", (void **) &qglClientActiveTexture},
287         {NULL, NULL}
288 };
289
290 static gl_extensionfunctionlist_t compiledvertexarrayfuncs[] =
291 {
292         {"glLockArraysEXT", (void **) &qglLockArraysEXT},
293         {"glUnlockArraysEXT", (void **) &qglUnlockArraysEXT},
294         {NULL, NULL}
295 };
296
297 void VID_CheckExtensions(void)
298 {
299         gl_combine_extension = false;
300         gl_supportslockarrays = false;
301         gl_textureunits = 1;
302
303         if (!GL_CheckExtension("OpenGL 1.1.0", opengl110funcs, NULL, false))
304                 Sys_Error("OpenGL 1.1.0 functions not found\n");
305
306         Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
307         Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
308         Con_Printf ("GL_VERSION: %s\n", gl_version);
309         Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
310         Con_Printf ("%s_EXTENSIONS: %s\n", gl_platform, gl_platformextensions);
311
312         Con_Printf("Checking OpenGL extensions...\n");
313
314         if (!GL_CheckExtension("glDrawRangeElements", drawrangeelementsfuncs, "-nodrawrangeelements", true))
315                 GL_CheckExtension("GL_EXT_draw_range_elements", drawrangeelementsextfuncs, "-nodrawrangeelements", false);
316
317         if (GL_CheckExtension("GL_ARB_multitexture", multitexturefuncs, "-nomtex", false))
318         {
319                 qglGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_textureunits);
320                 if (gl_textureunits > 1)
321                         gl_combine_extension = GL_CheckExtension("GL_ARB_texture_env_combine", NULL, "-nocombine", false) || GL_CheckExtension("GL_EXT_texture_env_combine", NULL, "-nocombine", false);
322                 else
323                 {
324                         Con_Printf("GL_ARB_multitexture with less than 2 units? - BROKEN DRIVER!\n");
325                         gl_textureunits = 1; // for sanity sake, make sure it's not 0
326                 }
327         }
328
329         gl_supportslockarrays = GL_CheckExtension("GL_EXT_compiled_vertex_array", compiledvertexarrayfuncs, "-nocva", false);
330
331         // we don't care if it's an extension or not, they are identical functions, so keep it simple in the rendering code
332         if (qglDrawRangeElements == NULL)
333                 qglDrawRangeElements = qglDrawRangeElementsEXT;
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         int i;
399
400         Cvar_RegisterVariable(&vid_fullscreen);
401         Cvar_RegisterVariable(&vid_width);
402         Cvar_RegisterVariable(&vid_height);
403         Cvar_RegisterVariable(&vid_bitsperpixel);
404         Cvar_RegisterVariable(&vid_mouse);
405         Cvar_RegisterVariable(&gl_combine);
406         Cvar_RegisterVariable(&in_pitch_min);
407         Cvar_RegisterVariable(&in_pitch_max);
408         Cvar_RegisterVariable(&m_filter);
409         Cmd_AddCommand("force_centerview", Force_CenterView_f);
410
411 // interpret command-line parameters
412         if ((i = COM_CheckParm("-window")) != 0)
413                 Cvar_SetValueQuick(&vid_fullscreen, false);
414         if ((i = COM_CheckParm("-fullscreen")) != 0)
415                 Cvar_SetValueQuick(&vid_fullscreen, true);
416         if ((i = COM_CheckParm("-width")) != 0)
417                 Cvar_SetQuick(&vid_width, com_argv[i+1]);
418         if ((i = COM_CheckParm("-height")) != 0)
419                 Cvar_SetQuick(&vid_height, com_argv[i+1]);
420         if ((i = COM_CheckParm("-bpp")) != 0)
421                 Cvar_SetQuick(&vid_bitsperpixel, com_argv[i+1]);
422 }
423
424 extern int VID_InitMode (int fullscreen, int width, int height, int bpp);
425 int VID_Mode(int fullscreen, int width, int height, int bpp)
426 {
427         if (fullscreen)
428                 Con_Printf("Video: %dx%dx%d fullscreen\n", width, height, bpp);
429         else
430                 Con_Printf("Video: %dx%d windowed\n", width, height);
431         if (VID_InitMode(fullscreen, width, height, bpp))
432                 return true;
433         else
434                 return false;
435 }