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