now entirely dynamic OpenGL binding
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Sat, 3 Aug 2002 20:59:29 +0000 (20:59 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Sat, 3 Aug 2002 20:59:29 +0000 (20:59 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@2181 d7cf8633-e32d-0410-b094-e92efae38249

gl_rmain.c
glquake.h
makefile
r_light.c
vid_3dfxsvga.c
vid_glx.c
vid_shared.c
vid_wgl.c

index 6c7deed..064bda6 100644 (file)
@@ -310,16 +310,6 @@ GL_Init
 extern char *ENGINE_EXTENSIONS;
 void GL_Init (void)
 {
-       gl_vendor = glGetString (GL_VENDOR);
-       Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
-       gl_renderer = glGetString (GL_RENDERER);
-       Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
-
-       gl_version = glGetString (GL_VERSION);
-       Con_Printf ("GL_VERSION: %s\n", gl_version);
-       gl_extensions = glGetString (GL_EXTENSIONS);
-       Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
-
        VID_CheckExtensions();
 
        // LordHavoc: report supported extensions
index 137cde2..690edba 100644 (file)
--- a/glquake.h
+++ b/glquake.h
@@ -35,13 +35,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #define strncasecmp strnicmp
 #endif
 
-#include <GL/gl.h>
+//#include <GL/gl.h>
 
 extern qboolean isG200;
 extern qboolean isRagePro;
 
-extern float gldepthmin, gldepthmax;
-
 //====================================================
 
 extern const char *gl_vendor;
@@ -49,6 +47,10 @@ extern const char *gl_renderer;
 extern const char *gl_version;
 extern const char *gl_extensions;
 
+void GL_OpenLibrary(void);
+void GL_CloseLibrary(void);
+void *GL_GetProcAddress(char *name);
+
 // wgl uses APIENTRY
 #ifndef APIENTRY
 #define APIENTRY
@@ -59,6 +61,150 @@ extern const char *gl_extensions;
 #define GLAPIENTRY APIENTRY
 #endif
 
+#ifndef GL_MATRIX_MODE
+typedef unsigned int GLenum;
+typedef unsigned char GLboolean;
+typedef unsigned int GLbitfield;
+typedef void GLvoid;
+// 1-byte signed
+typedef signed char GLbyte;
+// 2-byte signed
+typedef short GLshort;
+// 4-byte signed
+typedef int GLint;
+// 1-byte unsigned
+typedef unsigned char GLubyte;
+// 2-byte unsigned
+typedef unsigned short GLushort;
+// 4-byte unsigned
+typedef unsigned int GLuint;
+// 4-byte signed
+typedef int GLsizei;
+// single precision float
+typedef float GLfloat;
+// single precision float in [0,1]
+typedef float GLclampf;
+// double precision float
+typedef double GLdouble;
+// double precision float in [0,1]
+typedef double GLclampd;
+
+#define GL_MATRIX_MODE                         0x0BA0
+#define GL_MODELVIEW                           0x1700
+#define GL_PROJECTION                          0x1701
+#define GL_TEXTURE                             0x1702
+
+#define GL_DEPTH_TEST                          0x0B71
+
+#define GL_CULL_FACE                           0x0B44
+
+#define GL_BLEND                               0x0BE2
+
+#define GL_ZERO                                        0x0
+#define GL_ONE                                 0x1
+#define GL_SRC_COLOR                           0x0300
+#define GL_ONE_MINUS_SRC_COLOR                 0x0301
+#define GL_DST_COLOR                           0x0306
+#define GL_ONE_MINUS_DST_COLOR                 0x0307
+#define GL_SRC_ALPHA                           0x0302
+#define GL_ONE_MINUS_SRC_ALPHA                 0x0303
+#define GL_DST_ALPHA                           0x0304
+#define GL_ONE_MINUS_DST_ALPHA                 0x0305
+#define GL_SRC_ALPHA_SATURATE                  0x0308
+#define GL_CONSTANT_COLOR                      0x8001
+#define GL_ONE_MINUS_CONSTANT_COLOR            0x8002
+#define GL_CONSTANT_ALPHA                      0x8003
+#define GL_ONE_MINUS_CONSTANT_ALPHA            0x8004
+
+#define GL_TEXTURE_ENV                         0x2300
+#define GL_TEXTURE_ENV_MODE                    0x2200
+#define GL_TEXTURE_1D                          0x0DE0
+#define GL_TEXTURE_2D                          0x0DE1
+#define GL_TEXTURE_WRAP_S                      0x2802
+#define GL_TEXTURE_WRAP_T                      0x2803
+#define GL_TEXTURE_MAG_FILTER                  0x2800
+#define GL_TEXTURE_MIN_FILTER                  0x2801
+
+#define GL_NEAREST                             0x2600
+#define GL_LINEAR                              0x2601
+#define GL_NEAREST_MIPMAP_NEAREST              0x2700
+#define GL_NEAREST_MIPMAP_LINEAR               0x2702
+#define GL_LINEAR_MIPMAP_NEAREST               0x2701
+#define GL_LINEAR_MIPMAP_LINEAR                        0x2703
+
+#define GL_DECAL                               0x2101
+#define GL_MODULATE                            0x2100
+
+#define GL_REPEAT                              0x2901
+#define GL_CLAMP                               0x2900
+
+#define GL_TRIANGLES                           0x0004
+
+#define GL_FALSE                               0x0
+#define GL_TRUE                                        0x1
+
+#define GL_BYTE                                        0x1400
+#define GL_UNSIGNED_BYTE                       0x1401
+#define GL_SHORT                               0x1402
+#define GL_UNSIGNED_SHORT                      0x1403
+#define GL_INT                                 0x1404
+#define GL_UNSIGNED_INT                                0x1405
+#define GL_FLOAT                               0x1406
+#define GL_DOUBLE                              0x140A
+#define GL_2_BYTES                             0x1407
+#define GL_3_BYTES                             0x1408
+#define GL_4_BYTES                             0x1409
+
+#define GL_VERTEX_ARRAY                                0x8074
+#define GL_NORMAL_ARRAY                                0x8075
+#define GL_COLOR_ARRAY                         0x8076
+//#define GL_INDEX_ARRAY                               0x8077
+#define GL_TEXTURE_COORD_ARRAY                 0x8078
+//#define GL_EDGE_FLAG_ARRAY                   0x8079
+
+#define GL_FRONT                               0x0404
+#define GL_BACK                                        0x0405
+
+#define GL_VENDOR                              0x1F00
+#define GL_RENDERER                            0x1F01
+#define GL_VERSION                             0x1F02
+#define GL_EXTENSIONS                          0x1F03
+
+#define GL_NO_ERROR                            0x0
+#define GL_INVALID_VALUE                       0x0501
+#define GL_INVALID_ENUM                                0x0500
+#define GL_INVALID_OPERATION                   0x0502
+#define GL_STACK_OVERFLOW                      0x0503
+#define GL_STACK_UNDERFLOW                     0x0504
+#define GL_OUT_OF_MEMORY                       0x0505
+
+#define GL_DITHER                              0x0BD0
+#define GL_RGB                                 0x1907
+#define GL_RGBA                                        0x1908
+
+#define GL_MAX_TEXTURE_SIZE                    0x0D33
+
+#define GL_NEVER                               0x0200
+#define GL_LESS                                        0x0201
+#define GL_EQUAL                               0x0202
+#define GL_LEQUAL                              0x0203
+#define GL_GREATER                             0x0204
+#define GL_NOTEQUAL                            0x0205
+#define GL_GEQUAL                              0x0206
+#define GL_ALWAYS                              0x0207
+#define GL_DEPTH_TEST                          0x0B71
+
+#define GL_RED_SCALE                           0x0D14
+#define GL_GREEN_SCALE                         0x0D18
+#define GL_BLUE_SCALE                          0x0D1A
+#define GL_ALPHA_SCALE                         0x0D1C
+
+#define GL_DEPTH_BUFFER_BIT                    0x00000100
+#define GL_ACCUM_BUFFER_BIT                    0x00000200
+#define GL_STENCIL_BUFFER_BIT                  0x00000400
+#define GL_COLOR_BUFFER_BIT                    0x00004000
+#endif
+
 // GL_ARB_multitexture
 extern int gl_textureunits;
 extern void (GLAPIENTRY *qglMultiTexCoord2f) (GLenum, GLfloat, GLfloat);
@@ -242,7 +388,7 @@ extern BOOL (WINAPI *qwglSwapIntervalEXT)(int interval);
 #define DEBUGGL
 
 #ifdef DEBUGGL
-#define CHECKGLERROR if ((errornumber = glGetError())) GL_PrintError(errornumber, __FILE__, __LINE__);
+#define CHECKGLERROR if ((errornumber = qglGetError())) GL_PrintError(errornumber, __FILE__, __LINE__);
 extern int errornumber;
 void GL_PrintError(int errornumber, char *filename, int linenumber);
 #else
index 8ecdd87..2aacb9a 100644 (file)
--- a/makefile
+++ b/makefile
@@ -56,7 +56,8 @@ CFLAGS= -MD -Wall -Werror -I/usr/X11R6/include -ggdb $(OPTIMIZATIONS) $(PROFILEO
 
 #LordHavoc: what is XIE?  XFree 4.1.0 doesn't need it and 4.2.0 seems to be missing it entirely
 #LDFLAGS= -L/usr/X11R6/lib -lm -lX11 -lXext -lXIE -lXxf86dga -lXxf86vm -lGL -ldl $(SOUNDLIB) $(PROFILEOPTION)
-LDFLAGS= -L/usr/X11R6/lib -lm -lX11 -lXext -lXxf86dga -lXxf86vm -lGL -ldl $(SOUNDLIB) $(PROFILEOPTION)
+#LDFLAGS= -L/usr/X11R6/lib -lm -lX11 -lXext -lXxf86dga -lXxf86vm -lGL -ldl $(SOUNDLIB) $(PROFILEOPTION)
+LDFLAGS= -L/usr/X11R6/lib -lm -lX11 -lXext -lXxf86dga -lXxf86vm -ldl $(SOUNDLIB) $(PROFILEOPTION)
 
 #if you don't need the -3dfx version, use this line
 all: builddate darkplaces-glx
index 861547c..fd18b08 100644 (file)
--- a/r_light.c
+++ b/r_light.c
@@ -740,7 +740,7 @@ void R_ModelLightPoint (vec3_t color, vec3_t p, int *dlightbits)
 void R_LightModel(int numverts, float colorr, float colorg, float colorb, int worldcoords)
 {
        int i, j, nearlights = 0;
-       float color[3], basecolor[3], v[3], t, *av, *avn, *avc, a, number, f, dist2, mscale;
+       float color[3], basecolor[3], v[3], t, *av, *avn, *avc, a, f, dist2, mscale, dot;
        struct
        {
                vec3_t origin;
@@ -877,8 +877,8 @@ void R_LightModel(int numverts, float colorr, float colorg, float colorb, int wo
                        {
                                VectorSubtract(nl->origin, av, v);
                                // directional shading
-                               a = DotProduct(avn,v);
-                               if (a > 0)
+                               dot = DotProduct(avn,v);
+                               if (dot > 0)
                                {
                                        // the vertex normal faces the light
 
@@ -890,18 +890,18 @@ void R_LightModel(int numverts, float colorr, float colorg, float colorb, int wo
                                                #if SLOWMATH
                                                t = 1.0f / sqrt(dist2);
                                                #else
-                                               number = DotProduct(v, v);
-                                               *((int *)&t) = 0x5f3759df - ((* (int *) &number) >> 1);
-                                               t = t * (1.5f - (number * 0.5f * t * t));
+                                               *((int *)&t) = 0x5f3759df - ((* (int *) &dist2) >> 1);
+                                               t = t * (1.5f - (dist2 * 0.5f * t * t));
                                                #endif
-                                               // a * t is dotproduct with a normalized v.
+
+                                               // dot * t is dotproduct with a normalized v.
                                                // (the result would be -1 to +1, but we already
                                                // eliminated the <= 0 case, so it is 0 to 1)
 
                                                // the hardness variables are for backlighting/shinyness
                                                // these have been hardwired at * 0.5 + 0.5 to match
                                                // the quake map lighting utility's equations
-                                               f *= a * t * 0.5f + 0.5f;// * hardness + hardnessoffset;
+                                               f *= dot * t * 0.5f + 0.5f;// * hardness + hardnessoffset;
                                                VectorMA(color, f, nl->light, color);
                                        }
                                }
index 9ecca04..a07b8f3 100644 (file)
@@ -56,13 +56,6 @@ static int   scr_width, scr_height;
 int    VID_options_items = 0;
 
 /*-----------------------------------------------------------------------*/
-
-const char *gl_vendor;
-const char *gl_renderer;
-const char *gl_version;
-const char *gl_extensions;
-
-/*-----------------------------------------------------------------------*/
 void D_BeginDirectRect (int x, int y, qbyte *pbitmap, int width, int height)
 {
 }
@@ -77,6 +70,8 @@ void VID_Shutdown(void)
                return;
 
        fxMesaDestroyContext(fc);
+
+       GL_CloseLibrary();
 }
 
 void signal_handler(int sig)
@@ -235,6 +230,8 @@ void VID_Init(void)
        GLint attribs[32];
        int width = 640, height = 480;
 
+       GL_OpenLibrary();
+
 // set vid parameters
        attribs[0] = FXMESA_DOUBLEBUFFER;
        attribs[1] = FXMESA_ALPHA_SIZE;
index 22a7b67..07104c1 100644 (file)
--- a/vid_glx.c
+++ b/vid_glx.c
@@ -17,18 +17,18 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
 
-#include <termios.h>
-#include <sys/ioctl.h>
-#include <sys/stat.h>
-#include <sys/vt.h>
-#include <stdarg.h>
-#include <stdio.h>
+//#include <termios.h>
+//#include <sys/ioctl.h>
+//#include <sys/stat.h>
+//#include <sys/vt.h>
+//#include <stdarg.h>
+//#include <stdio.h>
 #include <signal.h>
 
-#include <dlfcn.h>
-
-#include "quakedef.h"
+//#include <dlfcn.h>
 
+#include <X11/Xlib.h>
+#include <X11/Xutil.h>
 #include <GL/glx.h>
 
 #include <X11/keysym.h>
@@ -38,6 +38,15 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include <X11/extensions/xf86dga.h>
 #include <X11/extensions/xf86vmode.h>
 
+#include "quakedef.h"
+
+XVisualInfo *(GLAPIENTRY *qglXChooseVisual)(Display *dpy, int screen, int *attribList);
+GLXContext (GLAPIENTRY *qglXCreateContext)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct);
+void (GLAPIENTRY *qglXDestroyContext)(Display *dpy, GLXContext ctx);
+Bool (GLAPIENTRY *qglXMakeCurrent)(Display *dpy, GLXDrawable drawable, GLXContext ctx);
+void (GLAPIENTRY *qglXSwapBuffers)(Display *dpy, GLXDrawable drawable);
+
+
 static Display *vidx11_display = NULL;
 static int scrnum;
 static Window win;
@@ -76,12 +85,6 @@ static Colormap vidx11_colormap;
 
 /*-----------------------------------------------------------------------*/
 
-const char *gl_vendor;
-const char *gl_renderer;
-const char *gl_version;
-const char *gl_extensions;
-
-/*-----------------------------------------------------------------------*/
 static int
 XLateKey(XKeyEvent *ev)
 {
@@ -464,6 +467,8 @@ void VID_Shutdown(void)
        vidx11_display = NULL;
        win = 0;
        ctx = NULL;
+
+       GL_CloseLibrary();
 }
 
 void signal_handler(int sig)
@@ -505,7 +510,7 @@ void VID_Finish (void)
        if (r_render.integer)
        {
                qglFinish();
-               glXSwapBuffers(vidx11_display, win);
+               qglXSwapBuffers(vidx11_display, win);
        }
 
 // handle the mouse state when windowed if that's changed
@@ -643,6 +648,8 @@ void VID_Init(void)
        qboolean fullscreen = true;
        int MajorVersion, MinorVersion;
 
+       GL_OpenLibrary();
+
        Cvar_RegisterVariable (&vid_dga);
        Cvar_RegisterVariable (&vid_dga_mouseaccel);
 
@@ -695,18 +702,24 @@ void VID_Init(void)
                vidmode_ext = true;
        }
 
+       if ((qglXChooseVisual = GL_GetProcAddress("glXChooseVisual")) == NULL
+        || (qglXCreateContext = GL_GetProcAddress("glXCreateContext")) == NULL
+        || (qglXMakeCurrent = GL_GetProcAddress("glXMakeCurrent")) == NULL
+        || (qglXSwapBuffers = GL_GetProcAddress("glXSwapBuffers")) == NULL)
+               Sys_Error("glX functions not found in libGL.so.1\n");
+
        visinfo = NULL;
 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
 #if 0
        if (!COM_CheckParm("-nogamma"))
-               visinfo = glXChooseVisual(vidx11_display, scrnum, gammaattrib);
+               visinfo = qglXChooseVisual(vidx11_display, scrnum, gammaattrib);
 #endif
        if (!visinfo)
        {
-               visinfo = glXChooseVisual(vidx11_display, scrnum, nogammaattrib);
+               visinfo = qglXChooseVisual(vidx11_display, scrnum, nogammaattrib);
                if (!visinfo)
                {
-                       fprintf(stderr, "qkHack: Error couldn't get an RGB, Double-buffered, Depth visual\n");
+                       Sys_Error("couldn't get an RGB, Double-buffered, Depth visual\n");
                        exit(1);
                }
        }
@@ -798,9 +811,9 @@ void VID_Init(void)
 
        XFlush(vidx11_display);
 
-       ctx = glXCreateContext(vidx11_display, visinfo, NULL, True);
+       ctx = qglXCreateContext(vidx11_display, visinfo, NULL, True);
 
-       glXMakeCurrent(vidx11_display, win, ctx);
+       qglXMakeCurrent(vidx11_display, win, ctx);
 
        scr_width = width;
        scr_height = height;
index 780cdbd..2a9bdfe 100644 (file)
@@ -28,6 +28,11 @@ cvar_t in_pitch_max = {0, "in_pitch_max", "90"};
 
 cvar_t m_filter = {CVAR_SAVE, "m_filter","0"};
 
+const char *gl_vendor;
+const char *gl_renderer;
+const char *gl_version;
+const char *gl_extensions;
+
 // GL_ARB_multitexture
 void (GLAPIENTRY *qglMultiTexCoord2f) (GLenum, GLfloat, GLfloat);
 void (GLAPIENTRY *qglActiveTexture) (GLenum);
@@ -289,27 +294,26 @@ static HINSTANCE gldll;
 static void *prjobj = NULL;
 #endif
 
-static void gl_getfuncs_begin(void)
+void GL_OpenLibrary(void)
 {
 #ifdef WIN32
-       gldll = LoadLibrary("opengl32.dll");
+       if (gldll)
+               FreeLibrary(gldll);
+       if (!(gldll = LoadLibrary("opengl32.dll")))
+               Sys_Error("Unable to LoadLibrary opengl32.dll\n");
 #else
        if (prjobj)
                dlclose(prjobj);
-
-       prjobj = dlopen(NULL, RTLD_LAZY);
-       if (prjobj == NULL)
-       {
-               Con_Printf("Unable to open symbol list for main program.\n");
-               return;
-       }
+       if (!(prjobj = dlopen("libGL.so.1", RTLD_LAZY)))
+               Sys_Error("Unable to open symbol list for libGL.so.1\n");
 #endif
 }
 
-static void gl_getfuncs_end(void)
+void GL_CloseLibrary(void)
 {
 #ifdef WIN32
        FreeLibrary(gldll);
+       gldll = 0;
 #else
        if (prjobj)
                dlclose(prjobj);
@@ -317,7 +321,7 @@ static void gl_getfuncs_end(void)
 #endif
 }
 
-static void *gl_getfuncaddress(char *name)
+void *GL_GetProcAddress(char *name)
 {
        void *p = NULL;
 #ifdef WIN32
@@ -352,7 +356,7 @@ static int gl_checkextension(char *name, gl_extensionfunctionlist_t *funcs, char
                for (func = funcs;func && func->name != NULL;func++)
                {
                        // functions are cleared before all the extensions are evaluated
-                       if (!(*func->funcvariable = (void *) gl_getfuncaddress(func->name)))
+                       if (!(*func->funcvariable = (void *) GL_GetProcAddress(func->name)))
                        {
                                if (!silent)
                                        Con_Printf("missing function \"%s\" - broken driver!\n", func->name);
@@ -374,9 +378,12 @@ static int gl_checkextension(char *name, gl_extensionfunctionlist_t *funcs, char
 
 void VID_CheckExtensions(void)
 {
-       Con_Printf("Checking OpenGL extensions...\n");
+       gl_vendor = NULL;
+       gl_renderer = NULL;
+       gl_version = NULL;
+       gl_extensions = NULL;
 
-       gl_getfuncs_begin();
+       Con_Printf("Opening OpenGL library to retrieve functions\n");
 
        gl_combine_extension = false;
        gl_supportslockarrays = false;
@@ -391,6 +398,18 @@ void VID_CheckExtensions(void)
        if (!gl_checkextension("OpenGL 1.1.0", opengl110funcs, NULL, false))
                Sys_Error("OpenGL 1.1.0 functions not found\n");
 
+       gl_vendor = qglGetString (GL_VENDOR);
+       gl_renderer = qglGetString (GL_RENDERER);
+       gl_version = qglGetString (GL_VERSION);
+       gl_extensions = qglGetString (GL_EXTENSIONS);
+
+       Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
+       Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
+       Con_Printf ("GL_VERSION: %s\n", gl_version);
+       Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
+
+       Con_Printf("Checking OpenGL extensions...\n");
+
        if (!gl_checkextension("glDrawRangeElements", drawrangeelementsfuncs, "-nodrawrangeelements", true))
                gl_checkextension("GL_EXT_draw_range_elements", drawrangeelementsextfuncs, "-nodrawrangeelements", true);
 
@@ -408,8 +427,6 @@ void VID_CheckExtensions(void)
 
        gl_supportslockarrays = gl_checkextension("GL_EXT_compiled_vertex_array", compiledvertexarrayfuncs, "-nocva", false);
 
-       gl_getfuncs_end();
-
        // we don't care if it's an extension or not, they are identical functions, so keep it simple in the rendering code
        if (qglDrawRangeElements == NULL)
                qglDrawRangeElements = qglDrawRangeElementsEXT;
index 12de090..4979397 100644 (file)
--- a/vid_wgl.c
+++ b/vid_wgl.c
@@ -57,11 +57,6 @@ lmode_t      lowresmodes[] = {
        {512, 384},
 };
 
-const char *gl_vendor;
-const char *gl_renderer;
-const char *gl_version;
-const char *gl_extensions;
-
 qboolean scr_skipupdate;
 
 static vmode_t modelist[MAX_MODE_LIST];
@@ -419,6 +414,8 @@ void VID_Shutdown (void)
 
                VID_RestoreSystemGamma();
        }
+
+       GL_CloseLibrary();
 }
 
 
@@ -1068,6 +1065,8 @@ void VID_Init (void)
        HDC hdc;
        DEVMODE devmode;
 
+       GL_OpenLibrary();
+
        memset(&devmode, 0, sizeof(devmode));
 
        Cmd_AddCommand ("vid_nummodes", VID_NumModes_f);