cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
cvar_t gl_lockarrays = {0, "gl_lockarrays", "0", "enables use of glLockArraysEXT, may cause glitches with some broken drivers, and may be slower than normal"};
cvar_t gl_lockarrays_minimumvertices = {0, "gl_lockarrays_minimumvertices", "1", "minimum number of vertices required for use of glLockArraysEXT, setting this too low may reduce performance"};
-cvar_t gl_vbo = {0, "gl_vbo", "1", "make use of GL_ARB_vertex_buffer_object extension to store static geometry in video memory for faster rendering"};
+cvar_t gl_vbo = {CVAR_SAVE, "gl_vbo", "1", "make use of GL_ARB_vertex_buffer_object extension to store static geometry in video memory for faster rendering"};
-cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode"};
+cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
qboolean v_flipped_state = false;
int gl_maxdrawrangeelementsvertices;
static void gl_backend_start(void)
{
- Con_Print("OpenGL Backend starting...\n");
CHECKGLERROR
if (qglDrawRangeElements != NULL)
CHECKGLERROR
qglGetIntegerv(GL_MAX_ELEMENTS_INDICES, &gl_maxdrawrangeelementsindices);
CHECKGLERROR
- Con_Printf("glDrawRangeElements detected (max vertices %i, max indices %i)\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
+ Con_DPrintf("GL_MAX_ELEMENTS_VERTICES = %i\nGL_MAX_ELEMENTS_INDICES = %i\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
}
backendunits = bound(1, gl_textureunits, MAX_TEXTUREUNITS);
CHECKGLERROR
qglGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, (int *)&backendarrayunits);
CHECKGLERROR
- Con_Printf("GLSL shader support detected: texture units = %i texenv, %i image, %i array\n", backendunits, backendimageunits, backendarrayunits);
+ Con_DPrintf("GLSL shader support detected: texture units = %i texenv, %i image, %i array\n", backendunits, backendimageunits, backendarrayunits);
backendimageunits = bound(1, backendimageunits, MAX_TEXTUREUNITS);
backendarrayunits = bound(1, backendarrayunits, MAX_TEXTUREUNITS);
}
- else if (backendunits > 1)
- Con_Printf("multitexture detected: texture units = %i\n", backendunits);
else
- Con_Printf("singletexture\n");
+ Con_DPrintf("GL_MAX_TEXTUREUNITS = %i\n", backendunits);
GL_Backend_AllocArrays();
Mem_ExpandableArray_NewArray(&gl_bufferobjectinfoarray, r_main_mempool, sizeof(gl_bufferobjectinfo_t), 128);
- Con_Printf("OpenGL backend started.\n");
+ Con_DPrintf("OpenGL backend started.\n");
CHECKGLERROR
backendarrayunits = 0;
backendactive = false;
- Con_Print("OpenGL Backend shutting down\n");
+ Con_DPrint("OpenGL Backend shutting down\n");
Mem_ExpandableArray_FreeArray(&gl_bufferobjectinfoarray);
Cvar_RegisterVariable(&gl_mesh_testarrayelement);
Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
- Cmd_AddCommand("gl_vbostats", GL_VBOStats_f, "prints a list of all buffer objects (vertex data and triangle elements) and total video memory used by them\n");
+ Cmd_AddCommand("gl_vbostats", GL_VBOStats_f, "prints a list of all buffer objects (vertex data and triangle elements) and total video memory used by them");
R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
}
+void GL_SetMirrorState(qboolean state);
+
void GL_SetupView_Orientation_Identity (void)
{
backend_viewmatrix = identitymatrix;
- v_flipped_state = false;
+ GL_SetMirrorState(false);
memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
}
void GL_SetupView_Orientation_FromEntity(const matrix4x4_t *matrix)
{
matrix4x4_t tempmatrix, basematrix;
- Matrix4x4_Invert_Simple(&tempmatrix, matrix);
+ Matrix4x4_Invert_Full(&tempmatrix, matrix);
Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
Matrix4x4_Concat(&backend_viewmatrix, &basematrix, &tempmatrix);
- v_flipped_state = v_flipped.integer;
+ GL_SetMirrorState(v_flipped.integer);
if(v_flipped_state)
{
Matrix4x4_Transpose(&basematrix, &backend_viewmatrix);
//Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[0], 0, 1, 0);
//Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[1], 0, 0, 1);
//Matrix4x4_ConcatTranslate(&backend_viewmatrix, -origin[0], -origin[1], -origin[2]);
+
+ // force an update of the model matrix by copying it off, resetting it, and then calling the R_Mesh_Matrix function with it
+ tempmatrix = backend_modelmatrix;
memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
+ R_Mesh_Matrix(&tempmatrix);
}
void GL_SetupView_Mode_Perspective (double frustumx, double frustumy, double zNear, double zFar)
CHECKGLERROR
}
+void GL_SetupView_ApplyCustomNearClipPlane(double normalx, double normaly, double normalz, double dist)
+{
+ double matrix[16];
+ double q[4];
+ double d;
+ float clipPlane[4], v3[3], v4[3];
+ float normal[3];
+
+ // This is Olique Depth Projection from http://www.terathon.com/code/oblique.php
+ // modified to fit in this codebase.
+
+ VectorSet(normal, normalx, normaly, normalz);
+ Matrix4x4_Transform3x3(&backend_viewmatrix, normal, clipPlane);
+ VectorScale(normal, dist, v3);
+ Matrix4x4_Transform(&backend_viewmatrix, v3, v4);
+ // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
+ clipPlane[3] = -DotProduct(v4, clipPlane);
+
+#if 0
+{
+ // testing code for comparing results
+ float clipPlane2[4];
+ VectorCopy4(clipPlane, clipPlane2);
+ R_Mesh_Matrix(&identitymatrix);
+ VectorSet(q, normal[0], normal[1], normal[2], -dist);
+ qglClipPlane(GL_CLIP_PLANE0, q);
+ qglGetClipPlane(GL_CLIP_PLANE0, q);
+ VectorCopy4(q, clipPlane);
+}
+#endif
+
+ // Calculate the clip-space corner point opposite the clipping plane
+ // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
+ // transform it into camera space by multiplying it
+ // by the inverse of the projection matrix
+ Matrix4x4_ToArrayDoubleGL(&backend_projectmatrix, matrix);
+
+ q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + matrix[8]) / matrix[0];
+ q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + matrix[9]) / matrix[5];
+ q[2] = -1.0f;
+ q[3] = (1.0f + matrix[10]) / matrix[14];
+
+ // Calculate the scaled plane vector
+ d = 2.0f / DotProduct4(clipPlane, q);
+
+ // Replace the third row of the projection matrix
+ matrix[2] = clipPlane[0] * d;
+ matrix[6] = clipPlane[1] * d;
+ matrix[10] = clipPlane[2] * d + 1.0f;
+ matrix[14] = clipPlane[3] * d;
+
+ // Load it back into OpenGL
+ qglMatrixMode(GL_PROJECTION);CHECKGLERROR
+ qglLoadMatrixd(matrix);CHECKGLERROR
+ qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
+ CHECKGLERROR
+ Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, matrix);
+}
+
typedef struct gltextureunit_s
{
const void *pointer_texcoord;
int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
int depthtest;
float depthrange[2];
+ float polygonoffset[2];
int alphatest;
int scissortest;
unsigned int unit;
gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
gl_state.lockrange_first = 0;
gl_state.lockrange_count = 0;
- gl_state.cullface = GL_FRONT; // quake is backwards, this culls back faces
+ gl_state.cullface = v_flipped_state ? GL_BACK : GL_FRONT; // quake is backwards, this culls back faces
gl_state.cullfaceenable = true;
+ gl_state.polygonoffset[0] = 0;
+ gl_state.polygonoffset[1] = 0;
CHECKGLERROR
qglDepthFunc(GL_LEQUAL);CHECKGLERROR
qglEnable(GL_DEPTH_TEST);CHECKGLERROR
qglDepthMask(gl_state.depthmask);CHECKGLERROR
+ qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
if (gl_support_arb_vertex_buffer_object)
{
}
}
+void GL_PolygonOffset(float planeoffset, float depthoffset)
+{
+ if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
+ {
+ gl_state.polygonoffset[0] = planeoffset;
+ gl_state.polygonoffset[1] = depthoffset;
+ qglPolygonOffset(planeoffset, depthoffset);
+ }
+}
+
+void GL_SetMirrorState(qboolean state)
+{
+ if(!state != !v_flipped_state)
+ {
+ // change cull face mode!
+ if(gl_state.cullface == GL_BACK)
+ qglCullFace((gl_state.cullface = GL_FRONT));
+ else if(gl_state.cullface == GL_FRONT)
+ qglCullFace((gl_state.cullface = GL_BACK));
+ }
+ v_flipped_state = state;
+}
+
void GL_CullFace(int state)
{
CHECKGLERROR
Matrix4x4_Transform4 (&backend_viewmatrix, in, temp);
Matrix4x4_Transform4 (&backend_projectmatrix, temp, out);
iw = 1.0f / out[3];
- out[0] = r_view.x + (out[0] * iw + 1.0f) * r_view.width * 0.5f;
- out[1] = r_view.y + r_view.height - (out[1] * iw + 1.0f) * r_view.height * 0.5f;
- out[2] = r_view.z + (out[2] * iw + 1.0f) * r_view.depth * 0.5f;
+ out[0] = r_refdef.view.x + (out[0] * iw + 1.0f) * r_refdef.view.width * 0.5f;
+ out[1] = r_refdef.view.y + r_refdef.view.height - (out[1] * iw + 1.0f) * r_refdef.view.height * 0.5f;
+ out[2] = r_refdef.view.z + (out[2] * iw + 1.0f) * r_refdef.view.depth * 0.5f;
}
// called at beginning of frame
qglCompileShaderARB(shaderobject);CHECKGLERROR
qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
- if (compilelog[0])
- Con_DPrintf("%s shader compile log:\n%s\n", shadertype, compilelog);
+ if (compilelog[0] && developer.integer > 0)
+ {
+ int i, j, pretextlines = 0;
+ for (i = 0;i < numstrings - 1;i++)
+ for (j = 0;strings[i][j];j++)
+ if (strings[i][j] == '\n')
+ pretextlines++;
+ Con_DPrintf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
+ }
if (!shadercompiled)
{
qglDeleteObjectARB(shaderobject);CHECKGLERROR
Con_Printf("R_Mesh_Draw(%d, %d, %d, %8p, %i, %p);\n", firstvertex, numvertices, numtriangles, elements, bufferobject, (void *)bufferoffset);
return;
}
- if (!gl_vbo.integer)
+ if (gl_vbo.integer != 1)
bufferobject = 0;
CHECKGLERROR
r_refdef.stats.meshes++;