]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_backend.c
the beginnings of a cgGL rendering path experiment, does not work yet
[xonotic/darkplaces.git] / gl_backend.c
index adf0ad1c30d156ef18d4ac0ea7c23bb2eb2ab5ba..ecc972d94951293c17857d73713a89684d1782c8 100644 (file)
@@ -22,6 +22,16 @@ cvar_t gl_fbo = {CVAR_SAVE, "gl_fbo", "1", "make use of GL_ARB_framebuffer_objec
 cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
 qboolean v_flipped_state = false;
 
+r_viewport_t gl_viewport;
+matrix4x4_t gl_modelmatrix;
+matrix4x4_t gl_viewmatrix;
+matrix4x4_t gl_modelviewmatrix;
+matrix4x4_t gl_projectionmatrix;
+matrix4x4_t gl_modelviewprojectionmatrix;
+float gl_modelview16f[16];
+float gl_modelviewprojection16f[16];
+qboolean gl_modelmatrixchanged;
+
 int gl_maxdrawrangeelementsvertices;
 int gl_maxdrawrangeelementsindices;
 
@@ -141,10 +151,6 @@ typedef struct gl_state_s
 
        memexpandablearray_t bufferobjectinfoarray;
 
-       r_viewport_t viewport;
-       matrix4x4_t modelmatrix;
-       matrix4x4_t modelviewmatrix;
-
        qboolean active;
 }
 gl_state_t;
@@ -306,10 +312,10 @@ void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t
        out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
 }
 
-static void R_Viewport_ApplyNearClipPlane(r_viewport_t *v, double normalx, double normaly, double normalz, double dist)
+static void R_Viewport_ApplyNearClipPlane(r_viewport_t *v, float normalx, float normaly, float normalz, float dist)
 {
-       double q[4];
-       double d;
+       float q[4];
+       float d;
        float clipPlane[4], v3[3], v4[3];
        float normal[3];
 
@@ -327,7 +333,7 @@ static void R_Viewport_ApplyNearClipPlane(r_viewport_t *v, double normalx, doubl
        // testing code for comparing results
        float clipPlane2[4];
        VectorCopy4(clipPlane, clipPlane2);
-       R_Mesh_Matrix(&identitymatrix);
+       R_EntityMatrix(&identitymatrix);
        VectorSet(q, normal[0], normal[1], normal[2], -dist);
        qglClipPlane(GL_CLIP_PLANE0, q);
        qglGetClipPlane(GL_CLIP_PLANE0, q);
@@ -354,7 +360,7 @@ static void R_Viewport_ApplyNearClipPlane(r_viewport_t *v, double normalx, doubl
        v->m[14] = clipPlane[3] * d;
 }
 
-void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double x1, double y1, double x2, double y2, double nearclip, double farclip, const double *nearplane)
+void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float x1, float y1, float x2, float y2, float nearclip, float farclip, const float *nearplane)
 {
        float left = x1, right = x2, bottom = y2, top = y1, zNear = nearclip, zFar = farclip;
        memset(v, 0, sizeof(*v));
@@ -393,7 +399,7 @@ void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int
 #endif
 }
 
-void R_Viewport_InitPerspective(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double frustumx, double frustumy, double nearclip, double farclip, const double *nearplane)
+void R_Viewport_InitPerspective(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float frustumx, float frustumy, float nearclip, float farclip, const float *nearplane)
 {
        matrix4x4_t tempmatrix, basematrix;
        memset(v, 0, sizeof(*v));
@@ -428,10 +434,10 @@ void R_Viewport_InitPerspective(r_viewport_t *v, const matrix4x4_t *cameramatrix
                R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
 }
 
-void R_Viewport_InitPerspectiveInfinite(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double frustumx, double frustumy, double nearclip, const double *nearplane)
+void R_Viewport_InitPerspectiveInfinite(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float frustumx, float frustumy, float nearclip, const float *nearplane)
 {
        matrix4x4_t tempmatrix, basematrix;
-       const double nudge = 1.0 - 1.0 / (1<<23);
+       const float nudge = 1.0 - 1.0 / (1<<23);
        memset(v, 0, sizeof(*v));
 
        if(v_flipped.integer)
@@ -595,29 +601,40 @@ void R_Viewport_InitRectSideView(r_viewport_t *v, const matrix4x4_t *cameramatri
 
 void R_SetViewport(const r_viewport_t *v)
 {
-       float glmatrix[16];
-       gl_state.viewport = *v;
+       gl_viewport = *v;
 
        CHECKGLERROR
        qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
 
-       // Load the projection matrix into OpenGL
-       qglMatrixMode(GL_PROJECTION);CHECKGLERROR
-       qglLoadMatrixf(gl_state.viewport.m);CHECKGLERROR
-       qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
-
        // FIXME: v_flipped_state is evil, this probably breaks somewhere
        GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
 
-       // directly force an update of the modelview matrix
-       Matrix4x4_Concat(&gl_state.modelviewmatrix, &gl_state.viewport.viewmatrix, &gl_state.modelmatrix);
-       Matrix4x4_ToArrayFloatGL(&gl_state.modelviewmatrix, glmatrix);
-       qglLoadMatrixf(glmatrix);CHECKGLERROR
+       // copy over the matrices to our state
+       gl_viewmatrix = v->viewmatrix;
+       gl_projectionmatrix = v->projectmatrix;
+
+       switch(vid.renderpath)
+       {
+       case RENDERPATH_GL20:
+       case RENDERPATH_CGGL:
+//             break;
+       case RENDERPATH_GL13:
+       case RENDERPATH_GL11:
+               // Load the projection matrix into OpenGL
+               qglMatrixMode(GL_PROJECTION);CHECKGLERROR
+               qglLoadMatrixf(gl_viewport.m);CHECKGLERROR
+               qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
+               break;
+       }
+
+       // force an update of the derived matrices
+       gl_modelmatrixchanged = true;
+       R_EntityMatrix(&gl_modelmatrix);
 }
 
 void R_GetViewport(r_viewport_t *v)
 {
-       *v = gl_state.viewport;
+       *v = gl_viewport;
 }
 
 static void GL_BindVBO(int bufferobject)
@@ -1435,19 +1452,6 @@ void GL_Mesh_ListVBOs(qboolean printeach)
        Con_Printf("vertex buffers: %i element buffers totalling %i bytes (%.3f MB), %i vertex buffers totalling %i bytes (%.3f MB), combined %i bytes (%.3fMB)\n", (int)ebocount, (int)ebomemory, ebomemory / 1048576.0, (int)vbocount, (int)vbomemory, vbomemory / 1048576.0, (int)(ebomemory + vbomemory), (ebomemory + vbomemory) / 1048576.0);
 }
 
-void R_Mesh_Matrix(const matrix4x4_t *matrix)
-{
-       if (memcmp(matrix, &gl_state.modelmatrix, sizeof(matrix4x4_t)))
-       {
-               float glmatrix[16];
-               gl_state.modelmatrix = *matrix;
-               Matrix4x4_Concat(&gl_state.modelviewmatrix, &gl_state.viewport.viewmatrix, &gl_state.modelmatrix);
-               Matrix4x4_ToArrayFloatGL(&gl_state.modelviewmatrix, glmatrix);
-               CHECKGLERROR
-               qglLoadMatrixf(glmatrix);CHECKGLERROR
-       }
-}
-
 void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
 {
        if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
@@ -1561,6 +1565,13 @@ int R_Mesh_TexBound(unsigned int unitnum, int id)
        return 0;
 }
 
+void R_Mesh_CopyToTexture(int texnum, int tx, int ty, int sx, int sy, int width, int height)
+{
+       R_Mesh_TexBind(0, texnum);
+       GL_ActiveTexture(0);CHECKGLERROR
+       qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, tx, ty, sx, sy, width, height);CHECKGLERROR
+}
+
 void R_Mesh_TexBindAll(unsigned int unitnum, int tex2d, int tex3d, int texcubemap, int texrectangle)
 {
        gltextureunit_t *unit = gl_state.units + unitnum;
@@ -1781,6 +1792,7 @@ void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, i
        switch(vid.renderpath)
        {
        case RENDERPATH_GL20:
+       case RENDERPATH_CGGL:
                // do nothing
                break;
        case RENDERPATH_GL13:
@@ -1863,6 +1875,7 @@ void R_Mesh_ResetTextureState(void)
        switch(vid.renderpath)
        {
        case RENDERPATH_GL20:
+       case RENDERPATH_CGGL:
                for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
                {
                        gltextureunit_t *unit = gl_state.units + unitnum;