added in_pitch_min and in_pitch_max cvars to limit pitch (default: -90 to +90)
[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 // GL_ARB_multitexture
24 void (GLAPIENTRY *qglMultiTexCoord2f) (GLenum, GLfloat, GLfloat);
25 void (GLAPIENTRY *qglActiveTexture) (GLenum);
26 void (GLAPIENTRY *qglClientActiveTexture) (GLenum);
27
28 // GL_EXT_compiled_vertex_array
29 void (GLAPIENTRY *qglLockArraysEXT) (GLint first, GLint count);
30 void (GLAPIENTRY *qglUnlockArraysEXT) (void);
31
32 typedef struct
33 {
34         char *name;
35         void **funcvariable;
36 }
37 gl_extensionfunctionlist_t;
38
39 typedef struct
40 {
41         char *name;
42         gl_extensionfunctionlist_t *funcs;
43         int *enablevariable;
44         char *disableparm;
45 }
46 gl_extensioninfo_t;
47
48 static gl_extensionfunctionlist_t multitexturefuncs[] =
49 {
50         {"glMultiTexCoord2fARB", (void **) &qglMultiTexCoord2f},
51         {"glActiveTextureARB", (void **) &qglActiveTexture},
52         {"glClientActiveTextureARB", (void **) &qglClientActiveTexture},
53         {NULL, NULL}
54 };
55
56 static gl_extensionfunctionlist_t compiledvertexarrayfuncs[] =
57 {
58         {"glLockArraysEXT", (void **) &qglLockArraysEXT},
59         {"glUnlockArraysEXT", (void **) &qglUnlockArraysEXT},
60         {NULL, NULL}
61 };
62
63 #ifndef WIN32
64 #include <dlfcn.h>
65 #endif
66
67 static void *prjobj = NULL;
68
69 static void gl_getfuncs_begin(void)
70 {
71 #ifndef WIN32
72         if (prjobj)
73                 dlclose(prjobj);
74
75         prjobj = dlopen(NULL, RTLD_LAZY);
76         if (prjobj == NULL)
77         {
78                 Con_Printf("Unable to open symbol list for main program.\n");
79                 return;
80         }
81 #endif
82 }
83
84 static void gl_getfuncs_end(void)
85 {
86 #ifndef WIN32
87         if (prjobj)
88         {
89                 dlclose(prjobj);
90                 prjobj = NULL;
91         }
92 #endif
93 }
94
95 static void *gl_getfuncaddress(char *name)
96 {
97 #ifdef WIN32
98         return (void *) wglGetProcAddress(name);
99 #else
100         return (void *) dlsym(prjobj, name);
101 #endif
102 }
103
104 static int gl_checkextension(char *name, gl_extensionfunctionlist_t *funcs, char *disableparm)
105 {
106         gl_extensionfunctionlist_t *func;
107
108         Con_Printf("checking for %s...  ", name);
109
110         for (func = funcs;func && func->name;func++)
111                 *func->funcvariable = NULL;
112
113         if (disableparm && COM_CheckParm(disableparm))
114         {
115                 Con_Printf("disabled by commandline\n");
116                 return false;
117         }
118
119         if (strstr(gl_extensions, name))
120         {
121                 for (func = funcs;func && func->name != NULL;func++)
122                 {
123                         if (!(*func->funcvariable = (void *) gl_getfuncaddress(func->name)))
124                         {
125                                 Con_Printf("missing function \"%s\"!\n", func->name);
126                                 return false;
127                         }
128                 }
129                 Con_Printf("enabled\n");
130                 return true;
131         }
132         else
133         {
134                 Con_Printf("not detected\n");
135                 return false;
136         }
137 }
138
139 void VID_CheckExtensions(void)
140 {
141         Con_Printf("Checking OpenGL extensions...\n");
142
143         gl_getfuncs_begin();
144
145         gl_combine_extension = false;
146         gl_supportslockarrays = false;
147         gl_textureunits = 1;
148
149         if (gl_checkextension("GL_ARB_multitexture", multitexturefuncs, "-nomtex"))
150         {
151                 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gl_textureunits);
152                 if (gl_textureunits > 1)
153                         gl_combine_extension = gl_checkextension("GL_ARB_texture_env_combine", NULL, "-nocombine") || gl_checkextension("GL_EXT_texture_env_combine", NULL, "-nocombine");
154                 else
155                         gl_textureunits = 1; // for sanity sake, make sure it's not 0
156         }
157
158         gl_supportslockarrays = gl_checkextension("GL_EXT_compiled_vertex_array", compiledvertexarrayfuncs, "-nocva");
159
160         gl_getfuncs_end();
161 }
162
163 void Force_CenterView_f (void)
164 {
165         cl.viewangles[PITCH] = 0;
166 }
167
168 void VID_InitCvars(void)
169 {
170         Cvar_RegisterVariable(&vid_mode);
171         Cvar_RegisterVariable(&vid_mouse);
172         Cvar_RegisterVariable(&vid_fullscreen);
173         Cvar_RegisterVariable(&gl_combine);
174         Cvar_RegisterVariable(&in_pitch_min);
175         Cvar_RegisterVariable(&in_pitch_max);
176         Cmd_AddCommand("force_centerview", Force_CenterView_f);
177 }