diff --git a/reaction/Makefile b/reaction/Makefile index 541c9ff6..f1df8277 100644 --- a/reaction/Makefile +++ b/reaction/Makefile @@ -1431,8 +1431,10 @@ Q3OBJ = \ $(B)/client/tr_bsp.o \ $(B)/client/tr_cmds.o \ $(B)/client/tr_curve.o \ + $(B)/client/tr_extramath.o \ $(B)/client/tr_flares.o \ $(B)/client/tr_font.o \ + $(B)/client/tr_glsl.o \ $(B)/client/tr_image.o \ $(B)/client/tr_image_png.o \ $(B)/client/tr_image_jpg.o \ @@ -1454,6 +1456,7 @@ Q3OBJ = \ $(B)/client/tr_sky.o \ $(B)/client/tr_surface.o \ $(B)/client/tr_world.o \ + $(B)/client/tr_vbo.o \ \ $(B)/client/sdl_gamma.o \ $(B)/client/sdl_input.o \ diff --git a/reaction/code/qcommon/q_math.c b/reaction/code/qcommon/q_math.c index 3423ce8c..97e6efbc 100644 --- a/reaction/code/qcommon/q_math.c +++ b/reaction/code/qcommon/q_math.c @@ -652,7 +652,7 @@ Returns 1, 2, or 1 + 2 int BoxOnPlaneSide(vec3_t emins, vec3_t emaxs, struct cplane_s *p) { float dist[2]; - int sides, b, i; + int sides; //, b, i; // fast axial cases if (p->type < 3) @@ -665,6 +665,7 @@ int BoxOnPlaneSide(vec3_t emins, vec3_t emaxs, struct cplane_s *p) } // general case +#if 0 dist[0] = dist[1] = 0; if (p->signbits < 8) // >= 8: default case is original code (dist[0]=dist[1]=0) { @@ -675,6 +676,47 @@ int BoxOnPlaneSide(vec3_t emins, vec3_t emaxs, struct cplane_s *p) dist[!b] += p->normal[i]*emins[i]; } } +#else + // general case + switch (p->signbits) + { + case 0: + dist[0] = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]; + dist[1] = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]; + break; + case 1: + dist[0] = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]; + dist[1] = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]; + break; + case 2: + dist[0] = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]; + dist[1] = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]; + break; + case 3: + dist[0] = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]; + dist[1] = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]; + break; + case 4: + dist[0] = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]; + dist[1] = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]; + break; + case 5: + dist[0] = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]; + dist[1] = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]; + break; + case 6: + dist[0] = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]; + dist[1] = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]; + break; + case 7: + dist[0] = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]; + dist[1] = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]; + break; + default: + dist[0] = dist[1] = 0; // shut up compiler + break; + } +#endif sides = 0; if (dist[0] >= p->dist) diff --git a/reaction/code/qcommon/qfiles.h b/reaction/code/qcommon/qfiles.h index 7f1ef965..8a596041 100644 --- a/reaction/code/qcommon/qfiles.h +++ b/reaction/code/qcommon/qfiles.h @@ -37,6 +37,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA // surface geometry should not exceed these limits #define SHADER_MAX_VERTEXES 1000 #define SHADER_MAX_INDEXES (6*SHADER_MAX_VERTEXES) +#define SHADER_MAX_TRIANGLES (SHADER_MAX_INDEXES / 3) // the maximum size of game relative pathnames diff --git a/reaction/code/renderer/qgl.h b/reaction/code/renderer/qgl.h index 52961937..91447d75 100644 --- a/reaction/code/renderer/qgl.h +++ b/reaction/code/renderer/qgl.h @@ -39,6 +39,108 @@ extern void (APIENTRYP qglMultiTexCoord2fARB) (GLenum target, GLfloat s, GLfloat extern void (APIENTRYP qglLockArraysEXT) (GLint first, GLsizei count); extern void (APIENTRYP qglUnlockArraysEXT) (void); +// GL_EXT_multi_draw_arrays +extern void (APIENTRY * qglMultiDrawArraysEXT) (GLenum, GLint *, GLsizei *, GLsizei); +extern void (APIENTRY * qglMultiDrawElementsEXT) (GLenum, const GLsizei *, GLenum, const GLvoid **, GLsizei); + +// GL_ARB_vertex_program +extern void (APIENTRY * qglVertexAttrib4fARB) (GLuint, GLfloat, GLfloat, GLfloat, GLfloat); +extern void (APIENTRY * qglVertexAttrib4fvARB) (GLuint, const GLfloat *); +extern void (APIENTRY * qglVertexAttribPointerARB) (GLuint index, GLint size, GLenum type, GLboolean normalized, + GLsizei stride, const GLvoid * pointer); +extern void (APIENTRY * qglEnableVertexAttribArrayARB) (GLuint index); +extern void (APIENTRY * qglDisableVertexAttribArrayARB) (GLuint index); + +// GL_ARB_vertex_buffer_object +extern void (APIENTRY * qglBindBufferARB) (GLenum target, GLuint buffer); +extern void (APIENTRY * qglDeleteBuffersARB) (GLsizei n, const GLuint * buffers); +extern void (APIENTRY * qglGenBuffersARB) (GLsizei n, GLuint * buffers); +extern GLboolean(APIENTRY * qglIsBufferARB) (GLuint buffer); +extern void (APIENTRY * qglBufferDataARB) (GLenum target, GLsizeiptrARB size, const GLvoid * data, GLenum usage); +extern void (APIENTRY * qglBufferSubDataARB) (GLenum target, GLintptrARB offset, GLsizeiptrARB size, const GLvoid * data); +extern void (APIENTRY * qglGetBufferSubDataARB) (GLenum target, GLintptrARB offset, GLsizeiptrARB size, GLvoid * data); +extern void (APIENTRY * qglGetBufferParameterivARB) (GLenum target, GLenum pname, GLint * params); +extern void (APIENTRY * qglGetBufferPointervARB) (GLenum target, GLenum pname, GLvoid * *params); + +// GL_ARB_shader_objects +extern void (APIENTRY * qglDeleteObjectARB) (GLhandleARB obj); +extern GLhandleARB(APIENTRY * qglGetHandleARB) (GLenum pname); +extern void (APIENTRY * qglDetachObjectARB) (GLhandleARB containerObj, GLhandleARB attachedObj); +extern GLhandleARB(APIENTRY * qglCreateShaderObjectARB) (GLenum shaderType); +extern void (APIENTRY * qglShaderSourceARB) (GLhandleARB shaderObj, GLsizei count, const GLcharARB * *string, + const GLint * length); +extern void (APIENTRY * qglCompileShaderARB) (GLhandleARB shaderObj); +extern GLhandleARB(APIENTRY * qglCreateProgramObjectARB) (void); +extern void (APIENTRY * qglAttachObjectARB) (GLhandleARB containerObj, GLhandleARB obj); +extern void (APIENTRY * qglLinkProgramARB) (GLhandleARB programObj); +extern void (APIENTRY * qglUseProgramObjectARB) (GLhandleARB programObj); +extern void (APIENTRY * qglValidateProgramARB) (GLhandleARB programObj); +extern void (APIENTRY * qglUniform1fARB) (GLint location, GLfloat v0); +extern void (APIENTRY * qglUniform2fARB) (GLint location, GLfloat v0, GLfloat v1); +extern void (APIENTRY * qglUniform3fARB) (GLint location, GLfloat v0, GLfloat v1, GLfloat v2); +extern void (APIENTRY * qglUniform4fARB) (GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3); +extern void (APIENTRY * qglUniform1iARB) (GLint location, GLint v0); +extern void (APIENTRY * qglUniform2iARB) (GLint location, GLint v0, GLint v1); +extern void (APIENTRY * qglUniform3iARB) (GLint location, GLint v0, GLint v1, GLint v2); +extern void (APIENTRY * qglUniform4iARB) (GLint location, GLint v0, GLint v1, GLint v2, GLint v3); +extern void (APIENTRY * qglUniform2fvARB) (GLint location, GLsizei count, const GLfloat * value); +extern void (APIENTRY * qglUniform3fvARB) (GLint location, GLsizei count, const GLfloat * value); +extern void (APIENTRY * qglUniform4fvARB) (GLint location, GLsizei count, const GLfloat * value); +extern void (APIENTRY * qglUniform2ivARB) (GLint location, GLsizei count, const GLint * value); +extern void (APIENTRY * qglUniform3ivARB) (GLint location, GLsizei count, const GLint * value); +extern void (APIENTRY * qglUniform4ivARB) (GLint location, GLsizei count, const GLint * value); +extern void (APIENTRY * qglUniformMatrix2fvARB) (GLint location, GLsizei count, GLboolean transpose, const GLfloat * value); +extern void (APIENTRY * qglUniformMatrix3fvARB) (GLint location, GLsizei count, GLboolean transpose, const GLfloat * value); +extern void (APIENTRY * qglUniformMatrix4fvARB) (GLint location, GLsizei count, GLboolean transpose, const GLfloat * value); +extern void (APIENTRY * qglGetObjectParameterfvARB) (GLhandleARB obj, GLenum pname, GLfloat * params); +extern void (APIENTRY * qglGetObjectParameterivARB) (GLhandleARB obj, GLenum pname, GLint * params); +extern void (APIENTRY * qglGetInfoLogARB) (GLhandleARB obj, GLsizei maxLength, GLsizei * length, GLcharARB * infoLog); +extern void (APIENTRY * qglGetAttachedObjectsARB) (GLhandleARB containerObj, GLsizei maxCount, GLsizei * count, + GLhandleARB * obj); +extern GLint(APIENTRY * qglGetUniformLocationARB) (GLhandleARB programObj, const GLcharARB * name); +extern void (APIENTRY * qglGetActiveUniformARB) (GLhandleARB programObj, GLuint index, GLsizei maxIndex, GLsizei * length, + GLint * size, GLenum * type, GLcharARB * name); +extern void (APIENTRY * qglGetUniformfvARB) (GLhandleARB programObj, GLint location, GLfloat * params); +extern void (APIENTRY * qglGetUniformivARB) (GLhandleARB programObj, GLint location, GLint * params); +extern void (APIENTRY * qglGetShaderSourceARB) (GLhandleARB obj, GLsizei maxLength, GLsizei * length, GLcharARB * source); + +// GL_ARB_vertex_shader +extern void (APIENTRY * qglBindAttribLocationARB) (GLhandleARB programObj, GLuint index, const GLcharARB * name); +extern void (APIENTRY * qglGetActiveAttribARB) (GLhandleARB programObj, GLuint index, GLsizei maxLength, GLsizei * length, + GLint * size, GLenum * type, GLcharARB * name); +extern GLint(APIENTRY * qglGetAttribLocationARB) (GLhandleARB programObj, const GLcharARB * name); + +#if defined(WIN32) +// WGL_ARB_create_context +#ifndef WGL_ARB_create_context +#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091 +#define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092 +#define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093 +#define WGL_CONTEXT_FLAGS_ARB 0x2094 +#define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126 +#define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001 +#define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002 +#define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001 +#define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002 +#define ERROR_INVALID_VERSION_ARB 0x2095 +#define ERROR_INVALID_PROFILE_ARB 0x2096 +#endif + +extern HGLRC(APIENTRY * qwglCreateContextAttribsARB) (HDC hdC, HGLRC hShareContext, const int *attribList); +#endif + +#if 0 //defined(__linux__) +// GLX_ARB_create_context +#ifndef GLX_ARB_create_context +#define GLX_CONTEXT_DEBUG_BIT_ARB 0x00000001 +#define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002 +#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091 +#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092 +#define GLX_CONTEXT_FLAGS_ARB 0x2094 +#endif + +extern GLXContext (APIENTRY * qglXCreateContextAttribsARB) (Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list); +#endif //=========================================================================== diff --git a/reaction/code/renderer/tr_backend.c b/reaction/code/renderer/tr_backend.c index 30c93685..02ec5f1e 100644 --- a/reaction/code/renderer/tr_backend.c +++ b/reaction/code/renderer/tr_backend.c @@ -73,15 +73,21 @@ void GL_SelectTexture( int unit ) { qglActiveTextureARB( GL_TEXTURE0_ARB ); GLimp_LogComment( "glActiveTextureARB( GL_TEXTURE0_ARB )\n" ); - qglClientActiveTextureARB( GL_TEXTURE0_ARB ); - GLimp_LogComment( "glClientActiveTextureARB( GL_TEXTURE0_ARB )\n" ); + if (!(glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer && glRefConfig.glsl && r_arb_shader_objects->integer)) + { + qglClientActiveTextureARB( GL_TEXTURE0_ARB ); + GLimp_LogComment( "glClientActiveTextureARB( GL_TEXTURE0_ARB )\n" ); + } } else if ( unit == 1 ) { qglActiveTextureARB( GL_TEXTURE1_ARB ); GLimp_LogComment( "glActiveTextureARB( GL_TEXTURE1_ARB )\n" ); - qglClientActiveTextureARB( GL_TEXTURE1_ARB ); - GLimp_LogComment( "glClientActiveTextureARB( GL_TEXTURE1_ARB )\n" ); + if (!(glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer && glRefConfig.glsl && r_arb_shader_objects->integer)) + { + qglClientActiveTextureARB( GL_TEXTURE1_ARB ); + GLimp_LogComment( "glClientActiveTextureARB( GL_TEXTURE1_ARB )\n" ); + } } else { ri.Error( ERR_DROP, "GL_SelectTexture: unit = %i", unit ); } @@ -117,6 +123,25 @@ void GL_BindMultitexture( image_t *image0, GLuint env0, image_t *image1, GLuint } } +/* +** GL_BindToTMU +*/ +void GL_BindToTMU( image_t *image, int tmu ) +{ + int texnum; + int oldtmu = glState.currenttmu; + + texnum = image->texnum; + + if ( glState.currenttextures[tmu] != texnum ) { + GL_SelectTexture( tmu ); + image->frameUsed = tr.frameCount; + glState.currenttextures[tmu] = texnum; + qglBindTexture( GL_TEXTURE_2D, texnum ); + GL_SelectTexture( oldtmu ); + } +} + /* ** GL_Cull @@ -386,6 +411,33 @@ void GL_State( unsigned long stateBits ) } +void GL_SetProjectionMatrix(matrix_t matrix) +{ + Matrix16Copy(matrix, glState.projection); + Matrix16Multiply(glState.projection, glState.modelview, glState.modelviewProjection); + + if (!(glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer && + glRefConfig.glsl && r_arb_shader_objects->integer)) + { + qglMatrixMode(GL_PROJECTION); + qglLoadMatrixf(matrix); + qglMatrixMode(GL_MODELVIEW); + } +} + + +void GL_SetModelviewMatrix(matrix_t matrix) +{ + Matrix16Copy(matrix, glState.modelview); + Matrix16Multiply(glState.projection, glState.modelview, glState.modelviewProjection); + + if (!(glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer && + glRefConfig.glsl && r_arb_shader_objects->integer)) + { + qglLoadMatrixf(matrix); + } +} + /* ================ @@ -410,9 +462,7 @@ static void RB_Hyperspace( void ) { static void SetViewportAndScissor( void ) { - qglMatrixMode(GL_PROJECTION); - qglLoadMatrixf( backEnd.viewParms.projectionMatrix ); - qglMatrixMode(GL_MODELVIEW); + GL_SetProjectionMatrix( backEnd.viewParms.projectionMatrix ); // set the window clipping qglViewport( backEnd.viewParms.viewportX, backEnd.viewParms.viewportY, @@ -500,17 +550,27 @@ void RB_BeginDrawingView (void) { plane2[2] = DotProduct (backEnd.viewParms.or.axis[2], plane); plane2[3] = DotProduct (plane, backEnd.viewParms.or.origin) - plane[3]; - qglLoadMatrixf( s_flipMatrix ); - qglClipPlane (GL_CLIP_PLANE0, plane2); - qglEnable (GL_CLIP_PLANE0); + GL_SetModelviewMatrix( s_flipMatrix ); + + if (!(glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer && + glRefConfig.glsl && r_arb_shader_objects->integer)) + { + qglClipPlane (GL_CLIP_PLANE0, plane2); + qglEnable (GL_CLIP_PLANE0); + } } else { - qglDisable (GL_CLIP_PLANE0); + if (!(glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer && + glRefConfig.glsl && r_arb_shader_objects->integer)) + { + qglDisable (GL_CLIP_PLANE0); + } } } #define MAC_EVENT_PUMP_MSEC 5 + /* ================== RB_RenderDrawSurfList @@ -565,6 +625,7 @@ void RB_RenderDrawSurfList( drawSurf_t *drawSurfs, int numDrawSurfs ) { RB_EndSurface(); } RB_BeginSurface( shader, fogNum ); + backEnd.pc.c_surfBatches++; oldShader = shader; oldFogNum = fogNum; oldDlighted = dlighted; @@ -609,7 +670,7 @@ void RB_RenderDrawSurfList( drawSurf_t *drawSurfs, int numDrawSurfs ) { R_TransformDlights( backEnd.refdef.num_dlights, backEnd.refdef.dlights, &backEnd.or ); } - qglLoadMatrixf( backEnd.or.modelMatrix ); + GL_SetModelviewMatrix( backEnd.or.modelMatrix ); // // change depthrange. Also change projection matrix so first person weapon does not look like coming @@ -626,9 +687,7 @@ void RB_RenderDrawSurfList( drawSurf_t *drawSurfs, int numDrawSurfs ) { if(oldDepthRange) { // was not a crosshair but now is, change back proj matrix - qglMatrixMode(GL_PROJECTION); - qglLoadMatrixf(backEnd.viewParms.projectionMatrix); - qglMatrixMode(GL_MODELVIEW); + GL_SetProjectionMatrix( backEnd.viewParms.projectionMatrix ); } } else @@ -637,9 +696,7 @@ void RB_RenderDrawSurfList( drawSurf_t *drawSurfs, int numDrawSurfs ) { R_SetupProjection(&temp, r_znear->value, qfalse); - qglMatrixMode(GL_PROJECTION); - qglLoadMatrixf(temp.projectionMatrix); - qglMatrixMode(GL_MODELVIEW); + GL_SetProjectionMatrix( temp.projectionMatrix ); } } @@ -650,9 +707,7 @@ void RB_RenderDrawSurfList( drawSurf_t *drawSurfs, int numDrawSurfs ) { { if(!wasCrosshair && backEnd.viewParms.stereoFrame != STEREO_CENTER) { - qglMatrixMode(GL_PROJECTION); - qglLoadMatrixf(backEnd.viewParms.projectionMatrix); - qglMatrixMode(GL_MODELVIEW); + GL_SetProjectionMatrix( backEnd.viewParms.projectionMatrix ); } qglDepthRange (0, 1); @@ -677,7 +732,8 @@ void RB_RenderDrawSurfList( drawSurf_t *drawSurfs, int numDrawSurfs ) { } // go back to the world modelview matrix - qglLoadMatrixf( backEnd.viewParms.world.modelMatrix ); + + GL_SetModelviewMatrix( backEnd.viewParms.world.modelMatrix ); if ( depthRange ) { qglDepthRange (0, 1); } @@ -708,16 +764,18 @@ RB_SetGL2D ================ */ void RB_SetGL2D (void) { + matrix_t matrix; + backEnd.projection2D = qtrue; // set 2D virtual screen size qglViewport( 0, 0, glConfig.vidWidth, glConfig.vidHeight ); qglScissor( 0, 0, glConfig.vidWidth, glConfig.vidHeight ); - qglMatrixMode(GL_PROJECTION); - qglLoadIdentity (); - qglOrtho (0, glConfig.vidWidth, glConfig.vidHeight, 0, 0, 1); - qglMatrixMode(GL_MODELVIEW); - qglLoadIdentity (); + + Matrix16Ortho(0, glConfig.vidWidth, glConfig.vidHeight, 0, 0, 1, matrix); + GL_SetProjectionMatrix(matrix); + Matrix16Identity(matrix); + GL_SetModelviewMatrix(matrix); GL_State( GLS_DEPTHTEST_DISABLE | GLS_SRCBLEND_SRC_ALPHA | @@ -744,6 +802,7 @@ Used for cinematics. void RE_StretchRaw (int x, int y, int w, int h, int cols, int rows, const byte *data, int client, qboolean dirty) { int i, j; int start, end; + matrix_t matrix; if ( !tr.registered ) { return; @@ -793,18 +852,113 @@ void RE_StretchRaw (int x, int y, int w, int h, int cols, int rows, const byte * RB_SetGL2D(); - qglColor3f( tr.identityLight, tr.identityLight, tr.identityLight ); + if (glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer) + { + tess.numIndexes = 0; + tess.numVertexes = 0; + tess.firstIndex = 0; - qglBegin (GL_QUADS); - qglTexCoord2f ( 0.5f / cols, 0.5f / rows ); - qglVertex2f (x, y); - qglTexCoord2f ( ( cols - 0.5f ) / cols , 0.5f / rows ); - qglVertex2f (x+w, y); - qglTexCoord2f ( ( cols - 0.5f ) / cols, ( rows - 0.5f ) / rows ); - qglVertex2f (x+w, y+h); - qglTexCoord2f ( 0.5f / cols, ( rows - 0.5f ) / rows ); - qglVertex2f (x, y+h); - qglEnd (); + tess.xyz[tess.numVertexes][0] = x; + tess.xyz[tess.numVertexes][1] = y; + tess.xyz[tess.numVertexes][2] = 0; + tess.xyz[tess.numVertexes][3] = 1; + tess.texCoords[tess.numVertexes][0][0] = 0.5f / cols; + tess.texCoords[tess.numVertexes][0][1] = 0.5f / rows; + tess.texCoords[tess.numVertexes][0][2] = 0; + tess.texCoords[tess.numVertexes][0][3] = 1; + tess.numVertexes++; + + tess.xyz[tess.numVertexes][0] = x + w; + tess.xyz[tess.numVertexes][1] = y; + tess.xyz[tess.numVertexes][2] = 0; + tess.xyz[tess.numVertexes][3] = 1; + tess.texCoords[tess.numVertexes][0][0] = (cols - 0.5f) / cols; + tess.texCoords[tess.numVertexes][0][1] = 0.5f / rows; + tess.texCoords[tess.numVertexes][0][2] = 0; + tess.texCoords[tess.numVertexes][0][3] = 1; + tess.numVertexes++; + + tess.xyz[tess.numVertexes][0] = x + w; + tess.xyz[tess.numVertexes][1] = y + h; + tess.xyz[tess.numVertexes][2] = 0; + tess.xyz[tess.numVertexes][3] = 1; + tess.texCoords[tess.numVertexes][0][0] = (cols - 0.5f) / cols; + tess.texCoords[tess.numVertexes][0][1] = (rows - 0.5f) / rows; + tess.texCoords[tess.numVertexes][0][2] = 0; + tess.texCoords[tess.numVertexes][0][3] = 1; + tess.numVertexes++; + + tess.xyz[tess.numVertexes][0] = x; + tess.xyz[tess.numVertexes][1] = y + h; + tess.xyz[tess.numVertexes][2] = 0; + tess.xyz[tess.numVertexes][3] = 1; + tess.texCoords[tess.numVertexes][0][0] = 0.5f / cols; + tess.texCoords[tess.numVertexes][0][1] = (rows - 0.5f) / rows; + tess.texCoords[tess.numVertexes][0][2] = 0; + tess.texCoords[tess.numVertexes][0][3] = 1; + tess.numVertexes++; + + tess.indexes[tess.numIndexes++] = 0; + tess.indexes[tess.numIndexes++] = 1; + tess.indexes[tess.numIndexes++] = 2; + tess.indexes[tess.numIndexes++] = 0; + tess.indexes[tess.numIndexes++] = 2; + tess.indexes[tess.numIndexes++] = 3; + + // FIXME: A lot of this can probably be removed for speed, and refactored into a more convenient function + RB_UpdateVBOs(ATTR_POSITION | ATTR_TEXCOORD); + + if (glRefConfig.glsl && r_arb_shader_objects->integer) + { + shaderProgram_t *sp = &tr.genericShader[0]; + + GLSL_VertexAttribsState(ATTR_POSITION | ATTR_TEXCOORD); + + GLSL_BindProgram(sp); + + GLSL_SetUniform_ModelViewProjectionMatrix(sp, glState.modelviewProjection); + + GLSL_SetUniform_FogAdjustColors(sp, 0); + GLSL_SetUniform_DeformGen(sp, DGEN_NONE); + GLSL_SetUniform_TCGen0(sp, TCGEN_TEXTURE); + Matrix16Identity(matrix); + GLSL_SetUniform_Texture0Matrix(sp, matrix); + GLSL_SetUniform_Texture1Env(sp, 0); + GLSL_SetUniform_ColorGen(sp, CGEN_IDENTITY); + GLSL_SetUniform_AlphaGen(sp, AGEN_IDENTITY); + } + else + { + qglEnableClientState( GL_VERTEX_ARRAY ); + qglEnableClientState( GL_TEXTURE_COORD_ARRAY ); + qglVertexPointer(3, GL_FLOAT, glState.currentVBO->stride_xyz, BUFFER_OFFSET(glState.currentVBO->ofs_xyz)); + qglTexCoordPointer( 2, GL_FLOAT, glState.currentVBO->stride_st, BUFFER_OFFSET(glState.currentVBO->ofs_st) ); + } + + qglDrawElements(GL_TRIANGLES, tess.numIndexes, GL_INDEX_TYPE, BUFFER_OFFSET(0)); + + //R_BindNullVBO(); + //R_BindNullIBO(); + + tess.numIndexes = 0; + tess.numVertexes = 0; + tess.firstIndex = 0; + } + else + { + qglColor3f( tr.identityLight, tr.identityLight, tr.identityLight ); + + qglBegin (GL_QUADS); // Alternate made + qglTexCoord2f ( 0.5f / cols, 0.5f / rows ); + qglVertex2f (x, y); + qglTexCoord2f ( ( cols - 0.5f ) / cols , 0.5f / rows ); + qglVertex2f (x+w, y); + qglTexCoord2f ( ( cols - 0.5f ) / cols, ( rows - 0.5f ) / rows ); + qglVertex2f (x+w, y+h); + qglTexCoord2f ( 0.5f / cols, ( rows - 0.5f ) / rows ); + qglVertex2f (x, y+h); + qglEnd (); + } } void RE_UploadCinematic (int w, int h, int cols, int rows, const byte *data, int client, qboolean dirty) { @@ -986,6 +1140,7 @@ void RB_ShowImages( void ) { int i; image_t *image; float x, y, w, h; + vec4_t quadVerts[4]; int start, end; if ( !backEnd.projection2D ) { @@ -1012,17 +1167,31 @@ void RB_ShowImages( void ) { h *= image->uploadHeight / 512.0f; } - GL_Bind( image ); - qglBegin (GL_QUADS); - qglTexCoord2f( 0, 0 ); - qglVertex2f( x, y ); - qglTexCoord2f( 1, 0 ); - qglVertex2f( x + w, y ); - qglTexCoord2f( 1, 1 ); - qglVertex2f( x + w, y + h ); - qglTexCoord2f( 0, 1 ); - qglVertex2f( x, y + h ); - qglEnd(); + if (glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer) + { + GL_Bind(image); + + VectorSet4(quadVerts[0], x, y, 0, 1); + VectorSet4(quadVerts[1], x + w, y, 0, 1); + VectorSet4(quadVerts[2], x + w, y + h, 0, 1); + VectorSet4(quadVerts[3], x, y + h, 0, 1); + + RB_InstantQuad(quadVerts); + } + else + { + GL_Bind( image ); // Alternate made + qglBegin (GL_QUADS); + qglTexCoord2f( 0, 0 ); + qglVertex2f( x, y ); + qglTexCoord2f( 1, 0 ); + qglVertex2f( x + w, y ); + qglTexCoord2f( 1, 1 ); + qglVertex2f( x + w, y + h ); + qglTexCoord2f( 0, 1 ); + qglVertex2f( x, y + h ); + qglEnd(); + } } qglFinish(); @@ -1207,3 +1376,4 @@ void RB_RenderThread( void ) { } } + diff --git a/reaction/code/renderer/tr_bsp.c b/reaction/code/renderer/tr_bsp.c index 45406a65..9be4014f 100644 --- a/reaction/code/renderer/tr_bsp.c +++ b/reaction/code/renderer/tr_bsp.c @@ -125,6 +125,41 @@ static void R_ColorShiftLightingBytes( byte in[4], byte out[4] ) { out[3] = in[3]; } +/* +=============== +R_ColorShiftLightingFloats +=============== +*/ +static void R_ColorShiftLightingFloats(const vec4_t in, vec4_t out) +{ + int shift, r, g, b; + + // shift the color data based on overbright range + shift = r_mapOverBrightBits->integer - tr.overbrightBits; + + // shift the data based on overbright range + r = ((byte)(in[0] * 255)) << shift; + g = ((byte)(in[1] * 255)) << shift; + b = ((byte)(in[2] * 255)) << shift; + + // normalize by color instead of saturating to white + if((r | g | b) > 255) + { + int max; + + max = r > g ? r : g; + max = max > b ? max : b; + r = r * 255 / max; + g = g * 255 / max; + b = b * 255 / max; + } + + out[0] = r * (1.0f / 255.0f); + out[1] = g * (1.0f / 255.0f); + out[2] = b * (1.0f / 255.0f); + out[3] = in[3]; +} + /* =============== R_LoadLightmaps @@ -133,6 +168,7 @@ R_LoadLightmaps */ #define LIGHTMAP_SIZE 128 static void R_LoadLightmaps( lump_t *l ) { +#if 0 byte *buf, *buf_p; int len; byte image[LIGHTMAP_SIZE*LIGHTMAP_SIZE*4]; @@ -209,9 +245,161 @@ static void R_LoadLightmaps( lump_t *l ) { if ( r_lightmap->integer == 2 ) { ri.Printf( PRINT_ALL, "Brightest lightmap value: %d\n", ( int ) ( maxIntensity * 255 ) ); } +#else + int len; + int numLightmaps; + int i; + byte *buf, *buf_p; + + //int BIGSIZE=2048; + //int BIGNUM=16; + + byte *fatbuffer; + int xoff, yoff, x, y; + //float scale = 0.9f; + + tr.fatLightmapSize = 4096; + tr.fatLightmapStep = 32; + + len = l->filelen; + if(!len) + { + return; + } + buf = fileBase + l->fileofs; + + // we are about to upload textures + R_SyncRenderThread(); + + // create all the lightmaps + numLightmaps = len / (LIGHTMAP_SIZE * LIGHTMAP_SIZE * 3); + if(numLightmaps == 1) + { + //FIXME: HACK: maps with only one lightmap turn up fullbright for some reason. + //this avoids this, but isn't the correct solution. + numLightmaps++; + } + else if(numLightmaps >= tr.fatLightmapStep * tr.fatLightmapStep ) + { + // FIXME: fat light maps don't support more than 1024 light maps + ri.Printf(PRINT_WARNING, "WARNING: number of lightmaps > %d\n", tr.fatLightmapStep * tr.fatLightmapStep); + numLightmaps = tr.fatLightmapStep * tr.fatLightmapStep; + } + + // use a fat lightmap of an appropriate size + if(numLightmaps < 65) + { + tr.fatLightmapSize = 1024; + tr.fatLightmapStep = 8; + } + else if(numLightmaps < 256) + { + tr.fatLightmapSize = 2048; + tr.fatLightmapStep = 16; + } + + // allocate one fat lightmap + tr.numLightmaps = 1; + tr.lightmaps = ri.Hunk_Alloc( tr.numLightmaps * sizeof(image_t *), h_low ); + + // This is going to be huge (4, 16, or 64MB), so don't use ri.Malloc() + fatbuffer = ri.Hunk_AllocateTempMemory(sizeof(byte) * tr.fatLightmapSize * tr.fatLightmapSize * 4); + + Com_Memset(fatbuffer, 128, tr.fatLightmapSize * tr.fatLightmapSize * 4); + for(i = 0; i < numLightmaps; i++) + { + // expand the 24 bit on-disk to 32 bit + buf_p = buf + i * LIGHTMAP_SIZE * LIGHTMAP_SIZE * 3; + + xoff = i % tr.fatLightmapStep; + yoff = i / tr.fatLightmapStep; + + //if (tr.radbumping==qfalse) + if(1) + { + for(y = 0; y < LIGHTMAP_SIZE; y++) + { + for(x = 0; x < LIGHTMAP_SIZE; x++) + { + int index = + (x + (y * tr.fatLightmapSize)) + ((xoff * LIGHTMAP_SIZE) + (yoff * tr.fatLightmapSize * LIGHTMAP_SIZE)); + fatbuffer[(index * 4) + 0] = buf_p[((x + (y * LIGHTMAP_SIZE)) * 3) + 0]; + fatbuffer[(index * 4) + 1] = buf_p[((x + (y * LIGHTMAP_SIZE)) * 3) + 1]; + fatbuffer[(index * 4) + 2] = buf_p[((x + (y * LIGHTMAP_SIZE)) * 3) + 2]; + fatbuffer[(index * 4) + 3] = 255; + + R_ColorShiftLightingBytes(&fatbuffer[(index * 4) + 0], &fatbuffer[(index * 4) + 0]); + } + } + } + /*else + { + //We need to darken the lightmaps a little bit when mixing radbump and fallback path rendering + //because radbump will be darker due to the error introduced by using 3 basis vector probes for lighting instead of surf normal. + for ( y = 0 ; y < LIGHTMAP_SIZE ; y++ ) + { + for ( x = 0 ; x < LIGHTMAP_SIZE ; x++ ) + { + int index = (x+(y*tr.fatLightmapSize))+((xoff*LIGHTMAP_SIZE)+(yoff*tr.fatLightmapSize*LIGHTMAP_SIZE)); + fatbuffer[(index*4)+0 ]=(byte)(((float)buf_p[((x+(y*LIGHTMAP_SIZE))*3)+0])*scale); + fatbuffer[(index*4)+1 ]=(byte)(((float)buf_p[((x+(y*LIGHTMAP_SIZE))*3)+1])*scale); + fatbuffer[(index*4)+2 ]=(byte)(((float)buf_p[((x+(y*LIGHTMAP_SIZE))*3)+2])*scale); + fatbuffer[(index*4)+3 ]=255; + } + } + + } */ + + + } + //memset(fatbuffer,128,tr.fatLightmapSize*tr.fatLightmapSize*4); + + tr.fatLightmap = R_CreateImage(va("_fatlightmap%d", 0), fatbuffer, tr.fatLightmapSize, tr.fatLightmapSize, qfalse, qfalse, GL_CLAMP_TO_EDGE ); + tr.lightmaps[0] = tr.fatLightmap; + + ri.Hunk_FreeTempMemory(fatbuffer); +#endif } +static float FatPackU(float input, int lightmapnum) +{ + if(tr.fatLightmapSize > 0) + { + int x = lightmapnum % tr.fatLightmapStep; + + return (input / ((float)tr.fatLightmapStep)) + ((1.0 / ((float)tr.fatLightmapStep)) * (float)x); + } + + return input; +} + +static float FatPackV(float input, int lightmapnum) +{ + if(tr.fatLightmapSize > 0) + { + int y = lightmapnum / ((float)tr.fatLightmapStep); + + return (input / ((float)tr.fatLightmapStep)) + ((1.0 / ((float)tr.fatLightmapStep)) * (float)y); + } + + return input; +} + + +static int FatLightmap(int lightmapnum) +{ + if (tr.fatLightmapSize > 0) + { + if (lightmapnum < 0) + return lightmapnum; + + return 0; + } + + return lightmapnum; +} + /* ================= RE_SetWorldVisData @@ -304,63 +492,85 @@ ParseFace static void ParseFace( dsurface_t *ds, drawVert_t *verts, msurface_t *surf, int *indexes ) { int i, j; srfSurfaceFace_t *cv; - int numPoints, numIndexes; - int lightmapNum; - int sfaceSize, ofsIndexes; + srfTriangle_t *tri; + int numVerts, numTriangles; + int realLightmapNum; - lightmapNum = LittleLong( ds->lightmapNum ); + realLightmapNum = LittleLong( ds->lightmapNum ); // get fog volume surf->fogIndex = LittleLong( ds->fogNum ) + 1; // get shader value - surf->shader = ShaderForShaderNum( ds->shaderNum, lightmapNum ); + surf->shader = ShaderForShaderNum( ds->shaderNum, FatLightmap(realLightmapNum) ); if ( r_singleShader->integer && !surf->shader->isSky ) { surf->shader = tr.defaultShader; } - numPoints = LittleLong( ds->numVerts ); - if (numPoints > MAX_FACE_POINTS) { - ri.Printf( PRINT_WARNING, "WARNING: MAX_FACE_POINTS exceeded: %i\n", numPoints); - numPoints = MAX_FACE_POINTS; + numVerts = LittleLong(ds->numVerts); + if (numVerts > MAX_FACE_POINTS) { + ri.Printf( PRINT_WARNING, "WARNING: MAX_FACE_POINTS exceeded: %i\n", numVerts); + numVerts = MAX_FACE_POINTS; surf->shader = tr.defaultShader; } - numIndexes = LittleLong( ds->numIndexes ); + numTriangles = LittleLong(ds->numIndexes) / 3; - // create the srfSurfaceFace_t - sfaceSize = ( size_t ) &((srfSurfaceFace_t *)0)->points[numPoints]; - ofsIndexes = sfaceSize; - sfaceSize += sizeof( int ) * numIndexes; - - cv = ri.Hunk_Alloc( sfaceSize, h_low ); + //cv = ri.Hunk_Alloc(sizeof(*cv), h_low); + cv = (void *)surf->data; cv->surfaceType = SF_FACE; - cv->numPoints = numPoints; - cv->numIndices = numIndexes; - cv->ofsIndices = ofsIndexes; - verts += LittleLong( ds->firstVert ); - for ( i = 0 ; i < numPoints ; i++ ) { - for ( j = 0 ; j < 3 ; j++ ) { - cv->points[i][j] = LittleFloat( verts[i].xyz[j] ); + cv->numTriangles = numTriangles; + cv->triangles = ri.Hunk_Alloc(numTriangles * sizeof(cv->triangles[0]), h_low); + + cv->numVerts = numVerts; + cv->verts = ri.Hunk_Alloc(numVerts * sizeof(cv->verts[0]), h_low); + + // copy vertexes + ClearBounds(cv->bounds[0], cv->bounds[1]); + verts += LittleLong(ds->firstVert); + for(i = 0; i < numVerts; i++) + { + for(j = 0; j < 3; j++) + { + cv->verts[i].xyz[j] = LittleFloat(verts[i].xyz[j]); + cv->verts[i].normal[j] = LittleFloat(verts[i].normal[j]); } - for ( j = 0 ; j < 2 ; j++ ) { - cv->points[i][3+j] = LittleFloat( verts[i].st[j] ); - cv->points[i][5+j] = LittleFloat( verts[i].lightmap[j] ); + AddPointToBounds(cv->verts[i].xyz, cv->bounds[0], cv->bounds[1]); + for(j = 0; j < 2; j++) + { + cv->verts[i].st[j] = LittleFloat(verts[i].st[j]); + //cv->verts[i].lightmap[j] = LittleFloat(verts[i].lightmap[j]); } - R_ColorShiftLightingBytes( verts[i].color, (byte *)&cv->points[i][7] ); + cv->verts[i].lightmap[0] = FatPackU(LittleFloat(verts[i].lightmap[0]), realLightmapNum); + cv->verts[i].lightmap[1] = FatPackV(LittleFloat(verts[i].lightmap[1]), realLightmapNum); + + R_ColorShiftLightingBytes( verts[i].color, cv->verts[i].vertexColors ); } - indexes += LittleLong( ds->firstIndex ); - for ( i = 0 ; i < numIndexes ; i++ ) { - ((int *)((byte *)cv + cv->ofsIndices ))[i] = LittleLong( indexes[ i ] ); + // copy triangles + indexes += LittleLong(ds->firstIndex); + for(i = 0, tri = cv->triangles; i < numTriangles; i++, tri++) + { + for(j = 0; j < 3; j++) + { + tri->indexes[j] = LittleLong(indexes[i * 3 + j]); + + if(tri->indexes[j] < 0 || tri->indexes[j] >= numVerts) + { + ri.Error(ERR_DROP, "Bad index in face surface"); + } + } } + //R_CalcSurfaceTriangleNeighbors(numTriangles, cv->triangles); + //R_CalcSurfaceTrianglePlanes(numTriangles, cv->triangles, cv->verts); + // take the plane information from the lightmap vector for ( i = 0 ; i < 3 ; i++ ) { cv->plane.normal[i] = LittleFloat( ds->lightmapVecs[2][i] ); } - cv->plane.dist = DotProduct( cv->points[0], cv->plane.normal ); + cv->plane.dist = DotProduct( cv->verts[0].xyz, cv->plane.normal ); SetPlaneSignbits( &cv->plane ); cv->plane.type = PlaneTypeForNormal( cv->plane.normal ); @@ -377,19 +587,19 @@ static void ParseMesh ( dsurface_t *ds, drawVert_t *verts, msurface_t *surf ) { srfGridMesh_t *grid; int i, j; int width, height, numPoints; - drawVert_t points[MAX_PATCH_SIZE*MAX_PATCH_SIZE]; - int lightmapNum; + srfVert_t points[MAX_PATCH_SIZE*MAX_PATCH_SIZE]; vec3_t bounds[2]; vec3_t tmpVec; static surfaceType_t skipData = SF_SKIP; + int realLightmapNum; - lightmapNum = LittleLong( ds->lightmapNum ); + realLightmapNum = LittleLong( ds->lightmapNum ); // get fog volume surf->fogIndex = LittleLong( ds->fogNum ) + 1; // get shader value - surf->shader = ShaderForShaderNum( ds->shaderNum, lightmapNum ); + surf->shader = ShaderForShaderNum( ds->shaderNum, FatLightmap(realLightmapNum) ); if ( r_singleShader->integer && !surf->shader->isSky ) { surf->shader = tr.defaultShader; } @@ -404,18 +614,28 @@ static void ParseMesh ( dsurface_t *ds, drawVert_t *verts, msurface_t *surf ) { width = LittleLong( ds->patchWidth ); height = LittleLong( ds->patchHeight ); + if(width < 0 || width > MAX_PATCH_SIZE || height < 0 || height > MAX_PATCH_SIZE) + ri.Error(ERR_DROP, "ParseMesh: bad size"); + verts += LittleLong( ds->firstVert ); numPoints = width * height; - for ( i = 0 ; i < numPoints ; i++ ) { - for ( j = 0 ; j < 3 ; j++ ) { - points[i].xyz[j] = LittleFloat( verts[i].xyz[j] ); - points[i].normal[j] = LittleFloat( verts[i].normal[j] ); + for(i = 0; i < numPoints; i++) + { + for(j = 0; j < 3; j++) + { + points[i].xyz[j] = LittleFloat(verts[i].xyz[j]); + points[i].normal[j] = LittleFloat(verts[i].normal[j]); } - for ( j = 0 ; j < 2 ; j++ ) { - points[i].st[j] = LittleFloat( verts[i].st[j] ); - points[i].lightmap[j] = LittleFloat( verts[i].lightmap[j] ); + + for(j = 0; j < 2; j++) + { + points[i].st[j] = LittleFloat(verts[i].st[j]); + //points[i].lightmap[j] = LittleFloat(verts[i].lightmap[j]); } - R_ColorShiftLightingBytes( verts[i].color, points[i].color ); + points[i].lightmap[0] = FatPackU(LittleFloat(verts[i].lightmap[0]), realLightmapNum); + points[i].lightmap[1] = FatPackV(LittleFloat(verts[i].lightmap[1]), realLightmapNum); + + R_ColorShiftLightingBytes( verts[i].color, points[i].vertexColors ); } // pre-tesseleate @@ -441,9 +661,10 @@ ParseTriSurf =============== */ static void ParseTriSurf( dsurface_t *ds, drawVert_t *verts, msurface_t *surf, int *indexes ) { - srfTriangles_t *tri; - int i, j; - int numVerts, numIndexes; + srfTriangles_t *cv; + srfTriangle_t *tri; + int i, j; + int numVerts, numTriangles; // get fog volume surf->fogIndex = LittleLong( ds->fogNum ) + 1; @@ -454,45 +675,58 @@ static void ParseTriSurf( dsurface_t *ds, drawVert_t *verts, msurface_t *surf, i surf->shader = tr.defaultShader; } - numVerts = LittleLong( ds->numVerts ); - numIndexes = LittleLong( ds->numIndexes ); + numVerts = LittleLong(ds->numVerts); + numTriangles = LittleLong(ds->numIndexes) / 3; - tri = ri.Hunk_Alloc( sizeof( *tri ) + numVerts * sizeof( tri->verts[0] ) - + numIndexes * sizeof( tri->indexes[0] ), h_low ); - tri->surfaceType = SF_TRIANGLES; - tri->numVerts = numVerts; - tri->numIndexes = numIndexes; - tri->verts = (drawVert_t *)(tri + 1); - tri->indexes = (int *)(tri->verts + tri->numVerts ); + //cv = ri.Hunk_Alloc(sizeof(*cv), h_low); + cv = (void *)surf->data; + cv->surfaceType = SF_TRIANGLES; - surf->data = (surfaceType_t *)tri; + cv->numTriangles = numTriangles; + cv->triangles = ri.Hunk_Alloc(numTriangles * sizeof(cv->triangles[0]), h_low); + + cv->numVerts = numVerts; + cv->verts = ri.Hunk_Alloc(numVerts * sizeof(cv->verts[0]), h_low); + + surf->data = (surfaceType_t *) cv; // copy vertexes - ClearBounds( tri->bounds[0], tri->bounds[1] ); - verts += LittleLong( ds->firstVert ); - for ( i = 0 ; i < numVerts ; i++ ) { - for ( j = 0 ; j < 3 ; j++ ) { - tri->verts[i].xyz[j] = LittleFloat( verts[i].xyz[j] ); - tri->verts[i].normal[j] = LittleFloat( verts[i].normal[j] ); - } - AddPointToBounds( tri->verts[i].xyz, tri->bounds[0], tri->bounds[1] ); - for ( j = 0 ; j < 2 ; j++ ) { - tri->verts[i].st[j] = LittleFloat( verts[i].st[j] ); - tri->verts[i].lightmap[j] = LittleFloat( verts[i].lightmap[j] ); + ClearBounds(cv->bounds[0], cv->bounds[1]); + verts += LittleLong(ds->firstVert); + for(i = 0; i < numVerts; i++) + { + for(j = 0; j < 3; j++) + { + cv->verts[i].xyz[j] = LittleFloat(verts[i].xyz[j]); + cv->verts[i].normal[j] = LittleFloat(verts[i].normal[j]); } - R_ColorShiftLightingBytes( verts[i].color, tri->verts[i].color ); + AddPointToBounds( cv->verts[i].xyz, cv->bounds[0], cv->bounds[1] ); + + for(j = 0; j < 2; j++) + { + cv->verts[i].st[j] = LittleFloat(verts[i].st[j]); + cv->verts[i].lightmap[j] = LittleFloat(verts[i].lightmap[j]); + } + + R_ColorShiftLightingBytes( verts[i].color, cv->verts[i].vertexColors ); + } - // copy indexes - indexes += LittleLong( ds->firstIndex ); - for ( i = 0 ; i < numIndexes ; i++ ) { - tri->indexes[i] = LittleLong( indexes[i] ); - if ( tri->indexes[i] < 0 || tri->indexes[i] >= numVerts ) { - ri.Error( ERR_DROP, "Bad index in triangle surface" ); + // copy triangles + indexes += LittleLong(ds->firstIndex); + for(i = 0, tri = cv->triangles; i < numTriangles; i++, tri++) + { + for(j = 0; j < 3; j++) + { + tri->indexes[j] = LittleLong(indexes[i * 3 + j]); + + if(tri->indexes[j] < 0 || tri->indexes[j] >= numVerts) + { + ri.Error(ERR_DROP, "Bad index in face surface"); + } } - } -} + }} /* =============== @@ -512,7 +746,8 @@ static void ParseFlare( dsurface_t *ds, drawVert_t *verts, msurface_t *surf, int surf->shader = tr.defaultShader; } - flare = ri.Hunk_Alloc( sizeof( *flare ), h_low ); + //flare = ri.Hunk_Alloc( sizeof( *flare ), h_low ); + flare = (void *)surf->data; flare->surfaceType = SF_FLARE; surf->data = (surfaceType_t *)flare; @@ -1204,8 +1439,8 @@ void R_MovePatchSurfacesToHunk(void) { if ( grid->surfaceType != SF_GRID ) continue; // - size = (grid->width * grid->height - 1) * sizeof( drawVert_t ) + sizeof( *grid ); - hunkgrid = ri.Hunk_Alloc( size, h_low ); + size = sizeof(*grid); + hunkgrid = ri.Hunk_Alloc(size, h_low); Com_Memcpy(hunkgrid, grid, size); hunkgrid->widthLodError = ri.Hunk_Alloc( grid->width * 4, h_low ); @@ -1214,12 +1449,424 @@ void R_MovePatchSurfacesToHunk(void) { hunkgrid->heightLodError = ri.Hunk_Alloc( grid->height * 4, h_low ); Com_Memcpy( hunkgrid->heightLodError, grid->heightLodError, grid->height * 4 ); + hunkgrid->numTriangles = grid->numTriangles; + hunkgrid->triangles = ri.Hunk_Alloc(grid->numTriangles * sizeof(srfTriangle_t), h_low); + Com_Memcpy(hunkgrid->triangles, grid->triangles, grid->numTriangles * sizeof(srfTriangle_t)); + + hunkgrid->numVerts = grid->numVerts; + hunkgrid->verts = ri.Hunk_Alloc(grid->numVerts * sizeof(srfVert_t), h_low); + Com_Memcpy(hunkgrid->verts, grid->verts, grid->numVerts * sizeof(srfVert_t)); + R_FreeSurfaceGridMesh( grid ); s_worldData.surfaces[i].data = (void *) hunkgrid; } } + +/* +================= +BSPSurfaceCompare +compare function for qsort() +================= +*/ +static int BSPSurfaceCompare(const void *a, const void *b) +{ + msurface_t *aa, *bb; + + aa = *(msurface_t **) a; + bb = *(msurface_t **) b; + + // shader first + if(aa->shader->sortedIndex < bb->shader->sortedIndex) + return -1; + + else if(aa->shader->sortedIndex > bb->shader->sortedIndex) + return 1; + + // by fogIndex + if(aa->fogIndex < bb->fogIndex) + return -1; + + else if(aa->fogIndex > bb->fogIndex) + return 1; + + return 0; +} + + +static void CopyVert(const srfVert_t * in, srfVert_t * out) +{ +#if 0 + memcpy(out->xyz, in->xyz, sizeof(in->xyz)); + memcpy(out->normal, in->normal, sizeof(in->normal)); + memcpy(out->st, in->st, sizeof(in->st)); + memcpy(out->lightmap, in->lightmap, sizeof(in->lightmap)); + memcpy(out->vertexColors, in->vertexColors, sizeof(in->vertexColors)); +#else + int j; + + for(j = 0; j < 3; j++) + { + out->xyz[j] = in->xyz[j]; + //out->tangent[j] = in->tangent[j]; + //out->binormal[j] = in->binormal[j]; + out->normal[j] = in->normal[j]; + //out->lightDirection[j] = in->lightDirection[j]; + } + + for(j = 0; j < 2; j++) + { + out->st[j] = in->st[j]; + out->lightmap[j] = in->lightmap[j]; + } + + for(j = 0; j < 4; j++) + { + //out->paintColor[j] = in->paintColor[j]; + //out->lightColor[j] = in->lightColor[j]; + out->vertexColors[j] = in->vertexColors[j]; + } + +#if DEBUG_OPTIMIZEVERTICES + out->id = in->id; +#endif +#endif +} + + +/* +=============== +R_CreateWorldVBO +=============== +*/ +static void R_CreateWorldVBO() +{ + int i, j, k; + + int numVerts; + srfVert_t *verts; + +// srfVert_t *optimizedVerts; + + int numTriangles; + srfTriangle_t *triangles; + + int numSurfaces; + msurface_t *surface; + msurface_t **surfacesSorted; + +// trRefLight_t *light; + + int startTime, endTime; + + startTime = ri.Milliseconds(); + + numVerts = 0; + numTriangles = 0; + numSurfaces = 0; + for(k = 0, surface = &s_worldData.surfaces[0]; k < s_worldData.numsurfaces /* s_worldData.numWorldSurfaces */; k++, surface++) + { + if(*surface->data == SF_FACE) + { + srfSurfaceFace_t *face = (srfSurfaceFace_t *) surface->data; + + if(face->numVerts) + numVerts += face->numVerts; + + if(face->numTriangles) + numTriangles += face->numTriangles; + + numSurfaces++; + } + else if(*surface->data == SF_GRID) + { + srfGridMesh_t *grid = (srfGridMesh_t *) surface->data; + + if(grid->numVerts) + numVerts += grid->numVerts; + + if(grid->numTriangles) + numTriangles += grid->numTriangles; + + numSurfaces++; + } + else if(*surface->data == SF_TRIANGLES) + { + srfTriangles_t *tri = (srfTriangles_t *) surface->data; + + if(tri->numVerts) + numVerts += tri->numVerts; + + if(tri->numTriangles) + numTriangles += tri->numTriangles; + + numSurfaces++; + } + } + + if(!numVerts || !numTriangles) + return; + + ri.Printf(PRINT_ALL, "...calculating world VBO ( %i verts %i tris )\n", numVerts, numTriangles); + + // create arrays + + s_worldData.numVerts = numVerts; + s_worldData.verts = verts = ri.Hunk_Alloc(numVerts * sizeof(srfVert_t), h_low); + //optimizedVerts = ri.Hunk_AllocateTempMemory(numVerts * sizeof(srfVert_t)); + + s_worldData.numTriangles = numTriangles; + s_worldData.triangles = triangles = ri.Hunk_Alloc(numTriangles * sizeof(srfTriangle_t), h_low); + + + // presort surfaces + surfacesSorted = ri.Malloc(numSurfaces * sizeof(*surfacesSorted)); + + j = 0; + for(k = 0, surface = &s_worldData.surfaces[0]; k < s_worldData.numsurfaces /* s_worldData.numWorldSurfaces */; k++, surface++) + { + if(*surface->data == SF_FACE || *surface->data == SF_GRID || *surface->data == SF_TRIANGLES) + { + surfacesSorted[j++] = surface; + } + } + + qsort(surfacesSorted, numSurfaces, sizeof(*surfacesSorted), BSPSurfaceCompare); + + // set up triangle indices + numVerts = 0; + numTriangles = 0; + for(k = 0, surface = surfacesSorted[k]; k < numSurfaces; k++, surface = surfacesSorted[k]) + { + if(*surface->data == SF_FACE) + { + srfSurfaceFace_t *srf = (srfSurfaceFace_t *) surface->data; + + srf->firstIndex = numTriangles * 3; + + if(srf->numTriangles) + { + srfTriangle_t *tri; + + for(i = 0, tri = srf->triangles; i < srf->numTriangles; i++, tri++) + { + for(j = 0; j < 3; j++) + { + triangles[numTriangles + i].indexes[j] = numVerts + tri->indexes[j]; + } + } + + numTriangles += srf->numTriangles; + } + + if(srf->numVerts) + numVerts += srf->numVerts; + } + else if(*surface->data == SF_GRID) + { + srfGridMesh_t *srf = (srfGridMesh_t *) surface->data; + + srf->firstIndex = numTriangles * 3; + + if(srf->numTriangles) + { + srfTriangle_t *tri; + + for(i = 0, tri = srf->triangles; i < srf->numTriangles; i++, tri++) + { + for(j = 0; j < 3; j++) + { + triangles[numTriangles + i].indexes[j] = numVerts + tri->indexes[j]; + } + } + + numTriangles += srf->numTriangles; + } + + if(srf->numVerts) + numVerts += srf->numVerts; + } + else if(*surface->data == SF_TRIANGLES) + { + srfTriangles_t *srf = (srfTriangles_t *) surface->data; + + srf->firstIndex = numTriangles * 3; + + if(srf->numTriangles) + { + srfTriangle_t *tri; + + for(i = 0, tri = srf->triangles; i < srf->numTriangles; i++, tri++) + { + for(j = 0; j < 3; j++) + { + triangles[numTriangles + i].indexes[j] = numVerts + tri->indexes[j]; + } + } + + numTriangles += srf->numTriangles; + } + + if(srf->numVerts) + numVerts += srf->numVerts; + } + } + + // build vertices + numVerts = 0; + for(k = 0, surface = surfacesSorted[k]; k < numSurfaces; k++, surface = surfacesSorted[k]) + { + if(*surface->data == SF_FACE) + { + srfSurfaceFace_t *srf = (srfSurfaceFace_t *) surface->data; + + srf->firstVert = numVerts; + + if(srf->numVerts) + { + for(i = 0; i < srf->numVerts; i++) + { + CopyVert(&srf->verts[i], &verts[numVerts + i]); + } + + numVerts += srf->numVerts; + } + } + else if(*surface->data == SF_GRID) + { + srfGridMesh_t *srf = (srfGridMesh_t *) surface->data; + + srf->firstVert = numVerts; + + if(srf->numVerts) + { + for(i = 0; i < srf->numVerts; i++) + { + CopyVert(&srf->verts[i], &verts[numVerts + i]); + } + + numVerts += srf->numVerts; + } + } + else if(*surface->data == SF_TRIANGLES) + { + srfTriangles_t *srf = (srfTriangles_t *) surface->data; + + srf->firstVert = numVerts; + + if(srf->numVerts) + { + for(i = 0; i < srf->numVerts; i++) + { + CopyVert(&srf->verts[i], &verts[numVerts + i]); + } + + numVerts += srf->numVerts; + } + } + } + +#if 0 + numVerts = OptimizeVertices(numVerts, verts, numTriangles, triangles, optimizedVerts, CompareWorldVert); + if(c_redundantVertexes) + { + ri.Printf(PRINT_DEVELOPER, + "...removed %i redundant vertices from staticWorldMesh %i ( %s, %i verts %i tris )\n", + c_redundantVertexes, vboSurfaces.currentElements, shader->name, numVerts, numTriangles); + } + + s_worldData.vbo = R_CreateVBO2(va("bspModelMesh_vertices %i", 0), numVerts, optimizedVerts, + ATTR_POSITION | ATTR_TEXCOORD | ATTR_LIGHTCOORD | ATTR_TANGENT | ATTR_BINORMAL | + ATTR_NORMAL | ATTR_COLOR | GLCS_LIGHTCOLOR | ATTR_LIGHTDIRECTION); +#else + s_worldData.vbo = R_CreateVBO2(va("staticBspModel0_VBO %i", 0), numVerts, verts, + ATTR_POSITION | ATTR_TEXCOORD | ATTR_LIGHTCOORD | ATTR_NORMAL | ATTR_COLOR, + VBO_USAGE_STATIC); +#endif + + s_worldData.ibo = R_CreateIBO2(va("staticBspModel0_IBO %i", 0), numTriangles, triangles, VBO_USAGE_STATIC); + + endTime = ri.Milliseconds(); + ri.Printf(PRINT_ALL, "world VBO calculation time = %5.2f seconds\n", (endTime - startTime) / 1000.0); + + // point triangle surfaces to world VBO + for(k = 0, surface = surfacesSorted[k]; k < numSurfaces; k++, surface = surfacesSorted[k]) + { + if(*surface->data == SF_FACE) + { + srfSurfaceFace_t *srf = (srfSurfaceFace_t *) surface->data; + + if( srf->numVerts && srf->numTriangles) + { + srf->vbo = s_worldData.vbo; + srf->ibo = s_worldData.ibo; + } + } + else if(*surface->data == SF_GRID) + { + srfGridMesh_t *srf = (srfGridMesh_t *) surface->data; + + if( srf->numVerts && srf->numTriangles) + { + srf->vbo = s_worldData.vbo; + srf->ibo = s_worldData.ibo; + } + } + else if(*surface->data == SF_TRIANGLES) + { + srfTriangles_t *srf = (srfTriangles_t *) surface->data; + + if( srf->numVerts && srf->numTriangles) + { + srf->vbo = s_worldData.vbo; + srf->ibo = s_worldData.ibo; + } + } + } + + + startTime = ri.Milliseconds(); + + // Tr3B: FIXME move this to somewhere else? +#if CALC_REDUNDANT_SHADOWVERTS + s_worldData.redundantVertsCalculationNeeded = 0; + for(i = 0; i < s_worldData.numLights; i++) + { + light = &s_worldData.lights[i]; + + if((r_precomputedLighting->integer || r_vertexLighting->integer) && !light->noRadiosity) + continue; + + s_worldData.redundantVertsCalculationNeeded++; + } + + if(s_worldData.redundantVertsCalculationNeeded) + { + ri.Printf(PRINT_ALL, "...calculating redundant world vertices ( %i verts )\n", numVerts); + + s_worldData.redundantLightVerts = ri.Hunk_Alloc(numVerts * sizeof(int), h_low); + BuildRedundantIndices(numVerts, verts, s_worldData.redundantLightVerts, CompareLightVert); + + s_worldData.redundantShadowVerts = ri.Hunk_Alloc(numVerts * sizeof(int), h_low); + BuildRedundantIndices(numVerts, verts, s_worldData.redundantShadowVerts, CompareShadowVert); + + s_worldData.redundantShadowAlphaTestVerts = ri.Hunk_Alloc(numVerts * sizeof(int), h_low); + BuildRedundantIndices(numVerts, verts, s_worldData.redundantShadowAlphaTestVerts, CompareShadowVertAlphaTest); + } + + endTime = ri.Milliseconds(); + ri.Printf(PRINT_ALL, "redundant world vertices calculation time = %5.2f seconds\n", (endTime - startTime) / 1000.0); +#endif + + ri.Free(surfacesSorted); + + //ri.Hunk_FreeTempMemory(triangles); + //ri.Hunk_FreeTempMemory(optimizedVerts); + //ri.Hunk_FreeTempMemory(verts); +} + + /* =============== R_LoadSurfaces @@ -1256,23 +1903,79 @@ static void R_LoadSurfaces( lump_t *surfs, lump_t *verts, lump_t *indexLump ) { s_worldData.surfaces = out; s_worldData.numsurfaces = count; + s_worldData.surfacesViewCount = ri.Hunk_Alloc ( count * sizeof(*s_worldData.surfacesViewCount), h_low ); + s_worldData.surfacesDlightBits = ri.Hunk_Alloc ( count * sizeof(*s_worldData.surfacesDlightBits), h_low ); + //s_worldData.numWorldSurfaces = count; + // Two passes, allocate surfaces first, then load them full of data + // This ensures surfaces are close together to reduce L2 cache misses when using VBOs, + // which don't actually use the verts and tris + in = (void *)(fileBase + surfs->fileofs); + out = s_worldData.surfaces; + for ( i = 0 ; i < count ; i++, in++, out++ ) { + switch ( LittleLong( in->surfaceType ) ) { + case MST_PATCH: + // FIXME: do this + break; + case MST_TRIANGLE_SOUP: + out->data = ri.Hunk_Alloc( sizeof(srfTriangles_t), h_low); + break; + case MST_PLANAR: + out->data = ri.Hunk_Alloc( sizeof(srfSurfaceFace_t), h_low); + break; + case MST_FLARE: + out->data = ri.Hunk_Alloc( sizeof(srfFlare_t), h_low); + break; + default: + break; + } + } + + in = (void *)(fileBase + surfs->fileofs); + out = s_worldData.surfaces; for ( i = 0 ; i < count ; i++, in++, out++ ) { switch ( LittleLong( in->surfaceType ) ) { case MST_PATCH: ParseMesh ( in, dv, out ); + { + srfGridMesh_t *surface = (srfGridMesh_t *)out->data; + + out->cullinfo.type = CULLINFO_BOX | CULLINFO_SPHERE; + VectorCopy(surface->meshBounds[0], out->cullinfo.bounds[0]); + VectorCopy(surface->meshBounds[1], out->cullinfo.bounds[1]); + VectorCopy(surface->localOrigin, out->cullinfo.localOrigin); + out->cullinfo.radius = surface->meshRadius; + } numMeshes++; break; case MST_TRIANGLE_SOUP: ParseTriSurf( in, dv, out, indexes ); + { + srfTriangles_t *surface = (srfTriangles_t *)out->data; + + out->cullinfo.type = CULLINFO_BOX; + VectorCopy(surface->bounds[0], out->cullinfo.bounds[0]); + VectorCopy(surface->bounds[1], out->cullinfo.bounds[1]); + } numTriSurfs++; break; case MST_PLANAR: ParseFace( in, dv, out, indexes ); + { + srfSurfaceFace_t *surface = (srfSurfaceFace_t *)out->data; + + out->cullinfo.type = CULLINFO_PLANE; // | CULLINFO_BOX; + VectorCopy(surface->bounds[0], out->cullinfo.bounds[0]); + VectorCopy(surface->bounds[1], out->cullinfo.bounds[1]); + out->cullinfo.plane = surface->plane; + } numFaces++; break; case MST_FLARE: ParseFlare( in, dv, out, indexes ); + { + out->cullinfo.type = CULLINFO_NONE; + } numFlares++; break; default: @@ -1311,6 +2014,7 @@ static void R_LoadSubmodels( lump_t *l ) { ri.Error (ERR_DROP, "LoadMap: funny lump size in %s",s_worldData.name); count = l->filelen / sizeof(*in); + s_worldData.numBModels = count; s_worldData.bmodels = out = ri.Hunk_Alloc( count * sizeof(*out), h_low ); for ( i=0 ; ibounds[1][j] = LittleFloat (in->maxs[j]); } - out->firstSurface = s_worldData.surfaces + LittleLong( in->firstSurface ); + out->firstSurface = LittleLong( in->firstSurface ); out->numSurfaces = LittleLong( in->numSurfaces ); + + if(i == 0) + { + // Tr3B: add this for limiting VBO surface creation + s_worldData.numWorldSurfaces = out->numSurfaces; + } } } @@ -1422,8 +2132,7 @@ static void R_LoadNodesAndLeafs (lump_t *nodeLump, lump_t *leafLump) { s_worldData.numClusters = out->cluster + 1; } - out->firstmarksurface = s_worldData.marksurfaces + - LittleLong(inLeaf->firstLeafSurface); + out->firstmarksurface = LittleLong(inLeaf->firstLeafSurface); out->nummarksurfaces = LittleLong(inLeaf->numLeafSurfaces); } @@ -1469,7 +2178,7 @@ static void R_LoadMarksurfaces (lump_t *l) { int i, j, count; int *in; - msurface_t **out; + int *out; in = (void *)(fileBase + l->fileofs); if (l->filelen % sizeof(*in)) @@ -1483,7 +2192,7 @@ static void R_LoadMarksurfaces (lump_t *l) for ( i=0 ; iname, "staticWorldMesh_IBO_mergedSurfs", sizeof(ibo->name)); + + // allocate more than we need + iboIndexes = outIboIndexes = ri.Malloc(s_worldData.ibo->indexesSize); + + // mark matching surfaces + for (i = 0; i < s_worldData.numnodes - s_worldData.numDecisionNodes; i++) + { + mnode_t *leaf = s_worldData.nodes + s_worldData.numDecisionNodes + i; + + for (j = 0; j < leaf->nummarksurfaces; j++) + { + msurface_t *surf1; + shader_t *shader1; + int fogIndex1; + int surfNum1; + + surfNum1 = *(s_worldData.marksurfaces + leaf->firstmarksurface + j); + + if (s_worldData.surfacesViewCount[surfNum1] != -1) + continue; + + surf1 = s_worldData.surfaces + surfNum1; + + if ((*surf1->data != SF_GRID) && (*surf1->data != SF_TRIANGLES) && (*surf1->data != SF_FACE)) + continue; + + shader1 = surf1->shader; + + if(shader1->isSky) + continue; + + if(shader1->isPortal) + continue; + + if(ShaderRequiresCPUDeforms(shader1)) + continue; + + fogIndex1 = surf1->fogIndex; + + s_worldData.surfacesViewCount[surfNum1] = surfNum1; + + for (k = j + 1; k < leaf->nummarksurfaces; k++) + { + msurface_t *surf2; + shader_t *shader2; + int fogIndex2; + int surfNum2; + + surfNum2 = *(s_worldData.marksurfaces + leaf->firstmarksurface + k); + + if (s_worldData.surfacesViewCount[surfNum2] != -1) + continue; + + surf2 = s_worldData.surfaces + surfNum2; + + if ((*surf2->data != SF_GRID) && (*surf2->data != SF_TRIANGLES) && (*surf2->data != SF_FACE)) + continue; + + shader2 = surf2->shader; + + if (shader1 != shader2) + continue; + + fogIndex2 = surf2->fogIndex; + + if (fogIndex1 != fogIndex2) + continue; + + s_worldData.surfacesViewCount[surfNum2] = surfNum1; + } + } + } + + // count merged/unmerged surfaces + numMergedSurfaces = 0; + numUnmergedSurfaces = 0; + for (i = 0; i < numWorldSurfaces; i++) + { + if (s_worldData.surfacesViewCount[i] == i) + { + numMergedSurfaces++; + } + else if (s_worldData.surfacesViewCount[i] == -1) + { + numUnmergedSurfaces++; + } + } + + // Allocate merged surfaces + s_worldData.mergedSurfaces = ri.Hunk_Alloc(sizeof(*s_worldData.mergedSurfaces) * numMergedSurfaces, h_low); + s_worldData.mergedSurfacesViewCount = ri.Hunk_Alloc(sizeof(*s_worldData.mergedSurfacesViewCount) * numMergedSurfaces, h_low); + s_worldData.mergedSurfacesDlightBits = ri.Hunk_Alloc(sizeof(*s_worldData.mergedSurfacesDlightBits) * numMergedSurfaces, h_low); + s_worldData.numMergedSurfaces = numMergedSurfaces; + + // view surfaces are like mark surfaces, except negative ones represent merged surfaces + // -1 represents 0, -2 represents 1, and so on + s_worldData.viewSurfaces = ri.Hunk_Alloc(sizeof(*s_worldData.viewSurfaces) * s_worldData.nummarksurfaces, h_low); + + // copy view surfaces into mark surfaces + for (i = 0; i < s_worldData.nummarksurfaces; i++) + { + s_worldData.viewSurfaces[i] = s_worldData.marksurfaces[i]; + } + + // actually merge surfaces + numIboIndexes = 0; + mergedSurfIndex = 0; + mergedSurf = s_worldData.mergedSurfaces; + for (i = 0; i < numWorldSurfaces; i++) + { + msurface_t *surf1; + + vec3_t bounds[2]; + + int numSurfsToMerge; + int numTriangles; + int numVerts; + int firstIndex; + + srfVBOMesh_t *vboSurf; + + if (s_worldData.surfacesViewCount[i] != i) + continue; + + surf1 = s_worldData.surfaces + i; + + // count verts, indexes, and surfaces + numSurfsToMerge = 0; + numTriangles = 0; + numVerts = 0; + for (j = i; j < numWorldSurfaces; j++) + { + msurface_t *surf2; + + if (s_worldData.surfacesViewCount[j] != i) + continue; + + surf2 = s_worldData.surfaces + j; + + switch(*surf2->data) + { + case SF_FACE: + { + srfSurfaceFace_t *face; + + face = (srfSurfaceFace_t *) surf2->data; + numTriangles += face->numTriangles; + numVerts += face->numVerts; + } + break; + + case SF_GRID: + { + srfGridMesh_t *grid; + + grid = (srfGridMesh_t *) surf2->data; + numTriangles += grid->numTriangles; + numVerts += grid->numVerts; + } + break; + + case SF_TRIANGLES: + { + srfTriangles_t *tris; + + tris = (srfTriangles_t *) surf2->data; + numTriangles += tris->numTriangles; + numVerts += tris->numVerts; + } + break; + + default: + break; + } + + numSurfsToMerge++; + } + + if (numVerts == 0 || numTriangles == 0 || numSurfsToMerge < 2) + { + continue; + } + + // Merge surfaces (indexes) and calculate bounds + ClearBounds(bounds[0], bounds[1]); + firstIndex = numIboIndexes; + for (j = i; j < numWorldSurfaces; j++) + { + msurface_t *surf2; + + if (s_worldData.surfacesViewCount[j] != i) + continue; + + surf2 = s_worldData.surfaces + j; + + AddPointToBounds(surf2->cullinfo.bounds[0], bounds[0], bounds[1]); + AddPointToBounds(surf2->cullinfo.bounds[1], bounds[0], bounds[1]); + + switch(*surf2->data) + { + case SF_FACE: + { + srfSurfaceFace_t *face; + + face = (srfSurfaceFace_t *) surf2->data; + + for (k = 0; k < face->numTriangles; k++) + { + *outIboIndexes++ = face->triangles[k].indexes[0] + face->firstVert; + *outIboIndexes++ = face->triangles[k].indexes[1] + face->firstVert; + *outIboIndexes++ = face->triangles[k].indexes[2] + face->firstVert; + numIboIndexes += 3; + } + } + break; + + case SF_GRID: + { + srfGridMesh_t *grid; + + grid = (srfGridMesh_t *) surf2->data; + + for (k = 0; k < grid->numTriangles; k++) + { + *outIboIndexes++ = grid->triangles[k].indexes[0] + grid->firstVert; + *outIboIndexes++ = grid->triangles[k].indexes[1] + grid->firstVert; + *outIboIndexes++ = grid->triangles[k].indexes[2] + grid->firstVert; + numIboIndexes += 3; + } + } + break; + + case SF_TRIANGLES: + { + srfTriangles_t *tris; + + tris = (srfTriangles_t *) surf2->data; + + for (k = 0; k < tris->numTriangles; k++) + { + *outIboIndexes++ = tris->triangles[k].indexes[0] + tris->firstVert; + *outIboIndexes++ = tris->triangles[k].indexes[1] + tris->firstVert; + *outIboIndexes++ = tris->triangles[k].indexes[2] + tris->firstVert; + numIboIndexes += 3; + } + } + break; + } + } + + vboSurf = ri.Hunk_Alloc(sizeof(*vboSurf), h_low); + memset(vboSurf, 0, sizeof(*vboSurf)); + vboSurf->surfaceType = SF_VBO_MESH; + + vboSurf->vbo = s_worldData.vbo; + vboSurf->ibo = ibo; + + vboSurf->numIndexes = numTriangles * 3; + vboSurf->numVerts = numVerts; + vboSurf->firstIndex = firstIndex; + + vboSurf->shader = surf1->shader; + vboSurf->fogIndex = surf1->fogIndex; + + VectorCopy(bounds[0], vboSurf->bounds[0]); + VectorCopy(bounds[1], vboSurf->bounds[1]); + + VectorCopy(bounds[0], mergedSurf->cullinfo.bounds[0]); + VectorCopy(bounds[1], mergedSurf->cullinfo.bounds[1]); + + mergedSurf->cullinfo.type = CULLINFO_BOX; + mergedSurf->data = (surfaceType_t *)vboSurf; + mergedSurf->fogIndex = surf1->fogIndex; + mergedSurf->shader = surf1->shader; + + // redirect view surfaces to this surf + for (j = i; j < numWorldSurfaces; j++) + { + if (s_worldData.surfacesViewCount[j] != i) + continue; + + for (k = 0; k < s_worldData.nummarksurfaces; k++) + { + int *mark = s_worldData.marksurfaces + k; + int *view = s_worldData.viewSurfaces + k; + + if (*mark == j) + *view = -(mergedSurfIndex + 1); + } + } + + mergedSurfIndex++; + mergedSurf++; + } + + // finish up the ibo + R_SyncRenderThread(); + + qglGenBuffersARB(1, &ibo->indexesVBO); + + R_BindIBO(ibo); + + qglBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, numIboIndexes * sizeof(*iboIndexes), iboIndexes, GL_STATIC_DRAW_ARB); + + R_BindNullIBO(); + + GL_CheckErrors(); + + ri.Free(iboIndexes); + + endTime = ri.Milliseconds(); + + ri.Printf(PRINT_ALL, "Processed %d surfaces into %d merged, %d unmerged in %5.2f seconds\n", + numWorldSurfaces, numMergedSurfaces, numUnmergedSurfaces, (endTime - startTime) / 1000.0f); + + // reset viewcounts + for (i = 0; i < numWorldSurfaces; i++) + { + s_worldData.surfacesViewCount[i] = -1; + } +} + + /* ================= RE_LoadWorldMap @@ -1859,11 +2929,28 @@ void RE_LoadWorldMap( const char *name ) { R_LoadEntities( &header->lumps[LUMP_ENTITIES] ); R_LoadLightGrid( &header->lumps[LUMP_LIGHTGRID] ); + // create static VBOS from the world + if (glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer) + { + R_CreateWorldVBO(); + if (r_mergeLeafSurfaces->integer) + { + R_MergeLeafSurfaces(); + } + } + s_worldData.dataSize = (byte *)ri.Hunk_Alloc(0, h_low) - startMarker; // only set tr.world now that we know the entire level has loaded properly tr.world = &s_worldData; + // make sure the VBO glState entries are save + if (glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer) + { + R_BindNullVBO(); + R_BindNullIBO(); + } + ri.FS_FreeFile( buffer.v ); } diff --git a/reaction/code/renderer/tr_cmds.c b/reaction/code/renderer/tr_cmds.c index 108299ca..42e84357 100644 --- a/reaction/code/renderer/tr_cmds.c +++ b/reaction/code/renderer/tr_cmds.c @@ -40,8 +40,8 @@ void R_PerformanceCounters( void ) { } if (r_speeds->integer == 1) { - ri.Printf (PRINT_ALL, "%i/%i shaders/surfs %i leafs %i verts %i/%i tris %.2f mtex %.2f dc\n", - backEnd.pc.c_shaders, backEnd.pc.c_surfaces, tr.pc.c_leafs, backEnd.pc.c_vertexes, + ri.Printf (PRINT_ALL, "%i/%i/%i shaders/batches/surfs %i leafs %i verts %i/%i tris %.2f mtex %.2f dc\n", + backEnd.pc.c_shaders, backEnd.pc.c_surfBatches, backEnd.pc.c_surfaces, tr.pc.c_leafs, backEnd.pc.c_vertexes, backEnd.pc.c_indexes/3, backEnd.pc.c_totalIndexes/3, R_SumOfUsedImages()/(1000000.0f), backEnd.pc.c_overDraw / (float)(glConfig.vidWidth * glConfig.vidHeight) ); } else if (r_speeds->integer == 2) { @@ -69,6 +69,11 @@ void R_PerformanceCounters( void ) { ri.Printf( PRINT_ALL, "flare adds:%i tests:%i renders:%i\n", backEnd.pc.c_flareAdds, backEnd.pc.c_flareTests, backEnd.pc.c_flareRenders ); } + else if (r_speeds->integer == 7 ) + { + ri.Printf( PRINT_ALL, "VBO draws: static %i dynamic %i\nMultidraws: %i merged %i\n", + backEnd.pc.c_staticVboDraws, backEnd.pc.c_dynamicVboDraws, backEnd.pc.c_multidraws, backEnd.pc.c_multidrawsMerged ); + } Com_Memset( &tr.pc, 0, sizeof( tr.pc ) ); Com_Memset( &backEnd.pc, 0, sizeof( backEnd.pc ) ); diff --git a/reaction/code/renderer/tr_curve.c b/reaction/code/renderer/tr_curve.c index 7eb1b4c5..3b61424a 100644 --- a/reaction/code/renderer/tr_curve.c +++ b/reaction/code/renderer/tr_curve.c @@ -33,7 +33,7 @@ distance from the true curve. Only a single entry point: srfGridMesh_t *R_SubdividePatchToGrid( int width, int height, - drawVert_t points[MAX_PATCH_SIZE*MAX_PATCH_SIZE] ) { + srfVert_t points[MAX_PATCH_SIZE*MAX_PATCH_SIZE] ) { */ @@ -43,7 +43,7 @@ srfGridMesh_t *R_SubdividePatchToGrid( int width, int height, LerpDrawVert ============ */ -static void LerpDrawVert( drawVert_t *a, drawVert_t *b, drawVert_t *out ) { +static void LerpDrawVert( srfVert_t *a, srfVert_t *b, srfVert_t *out ) { out->xyz[0] = 0.5f * (a->xyz[0] + b->xyz[0]); out->xyz[1] = 0.5f * (a->xyz[1] + b->xyz[1]); out->xyz[2] = 0.5f * (a->xyz[2] + b->xyz[2]); @@ -54,10 +54,10 @@ static void LerpDrawVert( drawVert_t *a, drawVert_t *b, drawVert_t *out ) { out->lightmap[0] = 0.5f * (a->lightmap[0] + b->lightmap[0]); out->lightmap[1] = 0.5f * (a->lightmap[1] + b->lightmap[1]); - out->color[0] = (a->color[0] + b->color[0]) >> 1; - out->color[1] = (a->color[1] + b->color[1]) >> 1; - out->color[2] = (a->color[2] + b->color[2]) >> 1; - out->color[3] = (a->color[3] + b->color[3]) >> 1; + out->vertexColors[0] = (a->vertexColors[0] + b->vertexColors[0]) >> 1; + out->vertexColors[1] = (a->vertexColors[1] + b->vertexColors[1]) >> 1; + out->vertexColors[2] = (a->vertexColors[2] + b->vertexColors[2]) >> 1; + out->vertexColors[3] = (a->vertexColors[3] + b->vertexColors[3]) >> 1; } /* @@ -65,9 +65,9 @@ static void LerpDrawVert( drawVert_t *a, drawVert_t *b, drawVert_t *out ) { Transpose ============ */ -static void Transpose( int width, int height, drawVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE] ) { +static void Transpose( int width, int height, srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE] ) { int i, j; - drawVert_t temp; + srfVert_t temp; if ( width > height ) { for ( i = 0 ; i < height ; i++ ) { @@ -109,7 +109,7 @@ MakeMeshNormals Handles all the complicated wrapping and degenerate cases ================= */ -static void MakeMeshNormals( int width, int height, drawVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE] ) { +static void MakeMeshNormals( int width, int height, srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE] ) { int i, j, k, dist; vec3_t normal; vec3_t sum; @@ -117,7 +117,7 @@ static void MakeMeshNormals( int width, int height, drawVert_t ctrl[MAX_GRID_SIZ vec3_t base; vec3_t delta; int x, y; - drawVert_t *dv; + srfVert_t *dv; vec3_t around[8], temp; qboolean good[8]; qboolean wrapWidth, wrapHeight; @@ -214,14 +214,68 @@ static int neighbors[8][2] = { } +static int MakeMeshTriangles(int width, int height, srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE], + srfTriangle_t triangles[SHADER_MAX_TRIANGLES]) +{ + int i, j; + int numTriangles; + int w, h; + srfVert_t *dv; + static srfVert_t ctrl2[MAX_GRID_SIZE * MAX_GRID_SIZE]; + + h = height - 1; + w = width - 1; + numTriangles = 0; + for(i = 0; i < h; i++) + { + for(j = 0; j < w; j++) + { + int v1, v2, v3, v4; + + // vertex order to be reckognized as tristrips + v1 = i * width + j + 1; + v2 = v1 - 1; + v3 = v2 + width; + v4 = v3 + 1; + + triangles[numTriangles].indexes[0] = v2; + triangles[numTriangles].indexes[1] = v3; + triangles[numTriangles].indexes[2] = v1; + numTriangles++; + + triangles[numTriangles].indexes[0] = v1; + triangles[numTriangles].indexes[1] = v3; + triangles[numTriangles].indexes[2] = v4; + numTriangles++; + } + } + + R_CalcSurfaceTriangleNeighbors(numTriangles, triangles); + + // FIXME: use more elegant way + for(i = 0; i < width; i++) + { + for(j = 0; j < height; j++) + { + dv = &ctrl2[j * width + i]; + *dv = ctrl[j][i]; + } + } + + R_CalcSurfaceTrianglePlanes(numTriangles, triangles, ctrl2); + + return numTriangles; +} + + /* ============ InvertCtrl ============ */ -static void InvertCtrl( int width, int height, drawVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE] ) { +static void InvertCtrl( int width, int height, srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE] ) { int i, j; - drawVert_t temp; + srfVert_t temp; for ( i = 0 ; i < height ; i++ ) { for ( j = 0 ; j < width/2 ; j++ ) { @@ -259,10 +313,10 @@ static void InvertErrorTable( float errorTable[2][MAX_GRID_SIZE], int width, int PutPointsOnCurve ================== */ -static void PutPointsOnCurve( drawVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE], +static void PutPointsOnCurve( srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE], int width, int height ) { int i, j; - drawVert_t prev, next; + srfVert_t prev, next; for ( i = 0 ; i < width ; i++ ) { for ( j = 1 ; j < height ; j += 2 ) { @@ -288,14 +342,15 @@ R_CreateSurfaceGridMesh ================= */ srfGridMesh_t *R_CreateSurfaceGridMesh(int width, int height, - drawVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE], float errorTable[2][MAX_GRID_SIZE] ) { + srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE], float errorTable[2][MAX_GRID_SIZE], + int numTriangles, srfTriangle_t triangles[SHADER_MAX_TRIANGLES]) { int i, j, size; - drawVert_t *vert; + srfVert_t *vert; vec3_t tmpVec; srfGridMesh_t *grid; // copy the results out to a grid - size = (width * height - 1) * sizeof( drawVert_t ) + sizeof( *grid ); + size = (width * height - 1) * sizeof( srfVert_t ) + sizeof( *grid ); #ifdef PATCH_STITCHING grid = /*ri.Hunk_Alloc*/ ri.Malloc( size ); @@ -306,6 +361,13 @@ srfGridMesh_t *R_CreateSurfaceGridMesh(int width, int height, grid->heightLodError = /*ri.Hunk_Alloc*/ ri.Malloc( height * 4 ); Com_Memcpy( grid->heightLodError, errorTable[1], height * 4 ); + + grid->numTriangles = numTriangles; + grid->triangles = ri.Malloc(grid->numTriangles * sizeof(srfTriangle_t)); + Com_Memcpy(grid->triangles, triangles, numTriangles * sizeof(srfTriangle_t)); + + grid->numVerts = (width * height); + grid->verts = ri.Malloc(grid->numVerts * sizeof(srfVert_t)); #else grid = ri.Hunk_Alloc( size ); Com_Memset(grid, 0, size); @@ -315,6 +377,13 @@ srfGridMesh_t *R_CreateSurfaceGridMesh(int width, int height, grid->heightLodError = ri.Hunk_Alloc( height * 4 ); Com_Memcpy( grid->heightLodError, errorTable[1], height * 4 ); + + grid->numTriangles = numTriangles; + grid->triangles = ri.Hunk_Alloc(grid->numTriangles * sizeof(srfTriangle_t), h_low); + Com_Memcpy(grid->triangles, triangles, numTriangles * sizeof(srfTriangle_t)); + + grid->numVerts = (width * height); + grid->verts = ri.Hunk_Alloc(grid->numVerts * sizeof(srfVert_t), h_low); #endif grid->width = width; @@ -349,6 +418,8 @@ R_FreeSurfaceGridMesh void R_FreeSurfaceGridMesh( srfGridMesh_t *grid ) { ri.Free(grid->widthLodError); ri.Free(grid->heightLodError); + ri.Free(grid->triangles); + ri.Free(grid->verts); ri.Free(grid); } @@ -358,16 +429,18 @@ R_SubdividePatchToGrid ================= */ srfGridMesh_t *R_SubdividePatchToGrid( int width, int height, - drawVert_t points[MAX_PATCH_SIZE*MAX_PATCH_SIZE] ) { + srfVert_t points[MAX_PATCH_SIZE*MAX_PATCH_SIZE] ) { int i, j, k, l; - drawVert_t_cleared( prev ); - drawVert_t_cleared( next ); - drawVert_t_cleared( mid ); + srfVert_t_cleared( prev ); + srfVert_t_cleared( next ); + srfVert_t_cleared( mid ); float len, maxLen; int dir; int t; - drawVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE]; + srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE]; float errorTable[2][MAX_GRID_SIZE]; + int numTriangles; + static srfTriangle_t triangles[SHADER_MAX_TRIANGLES]; for ( i = 0 ; i < width ; i++ ) { for ( j = 0 ; j < height ; j++ ) { @@ -511,10 +584,13 @@ srfGridMesh_t *R_SubdividePatchToGrid( int width, int height, } #endif + // calculate triangles + numTriangles = MakeMeshTriangles(width, height, ctrl, triangles); + // calculate normals MakeMeshNormals( width, height, ctrl ); - return R_CreateSurfaceGridMesh( width, height, ctrl, errorTable ); + return R_CreateSurfaceGridMesh(width, height, ctrl, errorTable, numTriangles, triangles); } /* @@ -525,10 +601,12 @@ R_GridInsertColumn srfGridMesh_t *R_GridInsertColumn( srfGridMesh_t *grid, int column, int row, vec3_t point, float loderror ) { int i, j; int width, height, oldwidth; - drawVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE]; + srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE]; float errorTable[2][MAX_GRID_SIZE]; float lodRadius; vec3_t lodOrigin; + int numTriangles; + static srfTriangle_t triangles[SHADER_MAX_TRIANGLES]; oldwidth = 0; width = grid->width + 1; @@ -557,6 +635,10 @@ srfGridMesh_t *R_GridInsertColumn( srfGridMesh_t *grid, int column, int row, vec } // put all the aproximating points on the curve //PutPointsOnCurve( ctrl, width, height ); + + // calculate triangles + numTriangles = MakeMeshTriangles(width, height, ctrl, triangles); + // calculate normals MakeMeshNormals( width, height, ctrl ); @@ -565,7 +647,7 @@ srfGridMesh_t *R_GridInsertColumn( srfGridMesh_t *grid, int column, int row, vec // free the old grid R_FreeSurfaceGridMesh(grid); // create a new grid - grid = R_CreateSurfaceGridMesh( width, height, ctrl, errorTable ); + grid = R_CreateSurfaceGridMesh(width, height, ctrl, errorTable, numTriangles, triangles); grid->lodRadius = lodRadius; VectorCopy(lodOrigin, grid->lodOrigin); return grid; @@ -579,10 +661,12 @@ R_GridInsertRow srfGridMesh_t *R_GridInsertRow( srfGridMesh_t *grid, int row, int column, vec3_t point, float loderror ) { int i, j; int width, height, oldheight; - drawVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE]; + srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE]; float errorTable[2][MAX_GRID_SIZE]; float lodRadius; vec3_t lodOrigin; + int numTriangles; + static srfTriangle_t triangles[SHADER_MAX_TRIANGLES]; oldheight = 0; width = grid->width; @@ -611,6 +695,10 @@ srfGridMesh_t *R_GridInsertRow( srfGridMesh_t *grid, int row, int column, vec3_t } // put all the aproximating points on the curve //PutPointsOnCurve( ctrl, width, height ); + + // calculate triangles + numTriangles = MakeMeshTriangles(width, height, ctrl, triangles); + // calculate normals MakeMeshNormals( width, height, ctrl ); @@ -619,7 +707,7 @@ srfGridMesh_t *R_GridInsertRow( srfGridMesh_t *grid, int row, int column, vec3_t // free the old grid R_FreeSurfaceGridMesh(grid); // create a new grid - grid = R_CreateSurfaceGridMesh( width, height, ctrl, errorTable ); + grid = R_CreateSurfaceGridMesh(width, height, ctrl, errorTable, numTriangles, triangles); grid->lodRadius = lodRadius; VectorCopy(lodOrigin, grid->lodOrigin); return grid; diff --git a/reaction/code/renderer/tr_extramath.c b/reaction/code/renderer/tr_extramath.c new file mode 100644 index 00000000..26c82032 --- /dev/null +++ b/reaction/code/renderer/tr_extramath.c @@ -0,0 +1,125 @@ +/* +=========================================================================== +Copyright (C) 2010 James Canete (use.less01@gmail.com) + +This file is part of Quake III Arena source code. + +Quake III Arena source code is free software; you can redistribute it +and/or modify it under the terms of the GNU General Public License as +published by the Free Software Foundation; either version 2 of the License, +or (at your option) any later version. + +Quake III Arena source code is distributed in the hope that it will be +useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Quake III Arena source code; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +=========================================================================== +*/ +// tr_extramath.c - extra math needed by the renderer not in qmath.c + +#include "tr_local.h" + +// Some matrix helper functions +// FIXME: do these already exist in ioq3 and I don't know about them? + +void Matrix16Zero( matrix_t out ) +{ + out[ 0] = 0.0f; out[ 4] = 0.0f; out[ 8] = 0.0f; out[12] = 0.0f; + out[ 1] = 0.0f; out[ 5] = 0.0f; out[ 9] = 0.0f; out[13] = 0.0f; + out[ 2] = 0.0f; out[ 6] = 0.0f; out[10] = 0.0f; out[14] = 0.0f; + out[ 3] = 0.0f; out[ 7] = 0.0f; out[11] = 0.0f; out[15] = 0.0f; +} + +void Matrix16Identity( matrix_t out ) +{ + out[ 0] = 1.0f; out[ 4] = 0.0f; out[ 8] = 0.0f; out[12] = 0.0f; + out[ 1] = 0.0f; out[ 5] = 1.0f; out[ 9] = 0.0f; out[13] = 0.0f; + out[ 2] = 0.0f; out[ 6] = 0.0f; out[10] = 1.0f; out[14] = 0.0f; + out[ 3] = 0.0f; out[ 7] = 0.0f; out[11] = 0.0f; out[15] = 1.0f; +} + +void Matrix16Copy( const matrix_t in, matrix_t out ) +{ + out[ 0] = in[ 0]; out[ 4] = in[ 4]; out[ 8] = in[ 8]; out[12] = in[12]; + out[ 1] = in[ 1]; out[ 5] = in[ 5]; out[ 9] = in[ 9]; out[13] = in[13]; + out[ 2] = in[ 2]; out[ 6] = in[ 6]; out[10] = in[10]; out[14] = in[14]; + out[ 3] = in[ 3]; out[ 7] = in[ 7]; out[11] = in[11]; out[15] = in[15]; +} + +void Matrix16Multiply( const matrix_t in1, const matrix_t in2, matrix_t out ) +{ + out[ 0] = in1[ 0] * in2[ 0] + in1[ 4] * in2[ 1] + in1[ 8] * in2[ 2] + in1[12] * in2[ 3]; + out[ 1] = in1[ 1] * in2[ 0] + in1[ 5] * in2[ 1] + in1[ 9] * in2[ 2] + in1[13] * in2[ 3]; + out[ 2] = in1[ 2] * in2[ 0] + in1[ 6] * in2[ 1] + in1[10] * in2[ 2] + in1[14] * in2[ 3]; + out[ 3] = in1[ 3] * in2[ 0] + in1[ 7] * in2[ 1] + in1[11] * in2[ 2] + in1[15] * in2[ 3]; + + out[ 4] = in1[ 0] * in2[ 4] + in1[ 4] * in2[ 5] + in1[ 8] * in2[ 6] + in1[12] * in2[ 7]; + out[ 5] = in1[ 1] * in2[ 4] + in1[ 5] * in2[ 5] + in1[ 9] * in2[ 6] + in1[13] * in2[ 7]; + out[ 6] = in1[ 2] * in2[ 4] + in1[ 6] * in2[ 5] + in1[10] * in2[ 6] + in1[14] * in2[ 7]; + out[ 7] = in1[ 3] * in2[ 4] + in1[ 7] * in2[ 5] + in1[11] * in2[ 6] + in1[15] * in2[ 7]; + + out[ 8] = in1[ 0] * in2[ 8] + in1[ 4] * in2[ 9] + in1[ 8] * in2[10] + in1[12] * in2[11]; + out[ 9] = in1[ 1] * in2[ 8] + in1[ 5] * in2[ 9] + in1[ 9] * in2[10] + in1[13] * in2[11]; + out[10] = in1[ 2] * in2[ 8] + in1[ 6] * in2[ 9] + in1[10] * in2[10] + in1[14] * in2[11]; + out[11] = in1[ 3] * in2[ 8] + in1[ 7] * in2[ 9] + in1[11] * in2[10] + in1[15] * in2[11]; + + out[12] = in1[ 0] * in2[12] + in1[ 4] * in2[13] + in1[ 8] * in2[14] + in1[12] * in2[15]; + out[13] = in1[ 1] * in2[12] + in1[ 5] * in2[13] + in1[ 9] * in2[14] + in1[13] * in2[15]; + out[14] = in1[ 2] * in2[12] + in1[ 6] * in2[13] + in1[10] * in2[14] + in1[14] * in2[15]; + out[15] = in1[ 3] * in2[12] + in1[ 7] * in2[13] + in1[11] * in2[14] + in1[15] * in2[15]; +} + +void Matrix16Transform( const matrix_t in1, const vec4_t in2, vec4_t out ) +{ + out[ 0] = in1[ 0] * in2[ 0] + in1[ 4] * in2[ 1] + in1[ 8] * in2[ 2] + in1[12] * in2[ 3]; + out[ 1] = in1[ 1] * in2[ 0] + in1[ 5] * in2[ 1] + in1[ 9] * in2[ 2] + in1[13] * in2[ 3]; + out[ 2] = in1[ 2] * in2[ 0] + in1[ 6] * in2[ 1] + in1[10] * in2[ 2] + in1[14] * in2[ 3]; + out[ 3] = in1[ 3] * in2[ 0] + in1[ 7] * in2[ 1] + in1[11] * in2[ 2] + in1[15] * in2[ 3]; +} + +qboolean Matrix16Compare( const matrix_t a, const matrix_t b ) +{ + return (a[ 0] == b[ 0] && a[ 4] == b[ 4] && a[ 8] == b[ 8] && a[12] == b[12] && + a[ 1] == b[ 1] && a[ 5] == b[ 5] && a[ 9] == b[ 9] && a[13] == b[13] && + a[ 2] == b[ 2] && a[ 6] == b[ 6] && a[10] == b[10] && a[14] == b[14] && + a[ 3] == b[ 3] && a[ 7] == b[ 7] && a[11] == b[11] && a[15] == b[15]); +} + +void Matrix16Dump( const matrix_t in ) +{ + ri.Printf(PRINT_ALL, "%3.5f %3.5f %3.5f %3.5f\n", in[ 0], in[ 4], in[ 8], in[12]); + ri.Printf(PRINT_ALL, "%3.5f %3.5f %3.5f %3.5f\n", in[ 1], in[ 5], in[ 9], in[13]); + ri.Printf(PRINT_ALL, "%3.5f %3.5f %3.5f %3.5f\n", in[ 2], in[ 6], in[10], in[14]); + ri.Printf(PRINT_ALL, "%3.5f %3.5f %3.5f %3.5f\n", in[ 3], in[ 7], in[11], in[15]); +} + +void Matrix16Translation( vec3_t vec, matrix_t out ) +{ + out[ 0] = 1.0f; out[ 4] = 0.0f; out[ 8] = 0.0f; out[12] = vec[0]; + out[ 1] = 0.0f; out[ 5] = 1.0f; out[ 9] = 0.0f; out[13] = vec[1]; + out[ 2] = 0.0f; out[ 6] = 0.0f; out[10] = 1.0f; out[14] = vec[2]; + out[ 3] = 0.0f; out[ 7] = 0.0f; out[11] = 0.0f; out[15] = 1.0f; +} + +void Matrix16Ortho( float left, float right, float bottom, float top, float znear, float zfar, matrix_t out ) +{ + Matrix16Zero(out); + out[ 0] = 2.0f / (right - left); + out[ 5] = 2.0f / (top - bottom); + out[10] = 2.0f / (zfar - znear); + out[12] = -(right + left) / (right - left); + out[13] = -(top + bottom) / (top - bottom); + out[14] = -(zfar + znear) / (zfar - znear); + out[15] = 1.0f; +} + +void VectorLerp( vec3_t a, vec3_t b, float lerp, vec3_t c) +{ + c[0] = a[0] * (1.0f - lerp) + b[0] * lerp; + c[1] = a[1] * (1.0f - lerp) + b[1] * lerp; + c[2] = a[2] * (1.0f - lerp) + b[2] * lerp; +} \ No newline at end of file diff --git a/reaction/code/renderer/tr_extramath.h b/reaction/code/renderer/tr_extramath.h new file mode 100644 index 00000000..29ae44c1 --- /dev/null +++ b/reaction/code/renderer/tr_extramath.h @@ -0,0 +1,57 @@ +/* +=========================================================================== +Copyright (C) 2010 James Canete (use.less01@gmail.com) + +This file is part of Quake III Arena source code. + +Quake III Arena source code is free software; you can redistribute it +and/or modify it under the terms of the GNU General Public License as +published by the Free Software Foundation; either version 2 of the License, +or (at your option) any later version. + +Quake III Arena source code is distributed in the hope that it will be +useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Quake III Arena source code; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +=========================================================================== +*/ +// tr_extramath.h + +#ifndef __TR_EXTRAMATH_H__ +#define __TR_EXTRAMATH_H__ + +typedef vec_t matrix_t[16]; + +void Matrix16Zero( matrix_t out ); +void Matrix16Identity( matrix_t out ); +void Matrix16Copy( const matrix_t in, matrix_t out ); +void Matrix16Multiply( const matrix_t in1, const matrix_t in2, matrix_t out ); +void Matrix16Transform( const matrix_t in1, const vec4_t in2, vec4_t out ); +qboolean Matrix16Compare(const matrix_t a, const matrix_t b); +void Matrix16Dump( const matrix_t in ); +void Matrix16Translation( vec3_t vec, matrix_t out ); +void Matrix16Ortho( float left, float right, float bottom, float top, float znear, float zfar, matrix_t out ); + +#define VectorCopy4(a,b) ((b)[0]=(a)[0],(b)[1]=(a)[1],(b)[2]=(a)[2],(b)[3]=(a)[3]) +#define VectorSet4(v,x,y,z,w) ((v)[0]=(x),(v)[1]=(y),(v)[2]=(z),(v)[3]=(w)) +#define DotProduct4(a,b) ((a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] + (a)[3]*(b)[3]) +#define VectorScale4(a,b,c) ((c)[0]=(a)[0]*(b),(c)[1]=(a)[1]*(b),(c)[2]=(a)[2]*(b),(c)[3]=(a)[3]*(b)) + +static ID_INLINE int VectorCompare4(const vec4_t v1, const vec4_t v2) +{ + if(v1[0] != v2[0] || v1[1] != v2[1] || v1[2] != v2[2] || v1[3] != v2[3]) + { + return 0; + } + return 1; +} + +void VectorLerp( vec3_t a, vec3_t b, float lerp, vec3_t c); + +#define SGN(x) (((x) >= 0) ? !!(x) : -1) + +#endif diff --git a/reaction/code/renderer/tr_flares.c b/reaction/code/renderer/tr_flares.c index 75a76eb7..030d150f 100644 --- a/reaction/code/renderer/tr_flares.c +++ b/reaction/code/renderer/tr_flares.c @@ -444,6 +444,7 @@ void RB_RenderFlares (void) { flare_t *f; flare_t **prev; qboolean draw; + matrix_t oldmodelview, oldprojection, matrix; if ( !r_flares->integer ) { return; @@ -505,14 +506,14 @@ void RB_RenderFlares (void) { qglDisable (GL_CLIP_PLANE0); } - qglPushMatrix(); - qglLoadIdentity(); - qglMatrixMode( GL_PROJECTION ); - qglPushMatrix(); - qglLoadIdentity(); - qglOrtho( backEnd.viewParms.viewportX, backEnd.viewParms.viewportX + backEnd.viewParms.viewportWidth, - backEnd.viewParms.viewportY, backEnd.viewParms.viewportY + backEnd.viewParms.viewportHeight, - -99999, 99999 ); + Matrix16Copy(glState.projection, oldprojection); + Matrix16Copy(glState.modelview, oldmodelview); + Matrix16Identity(matrix); + GL_SetModelviewMatrix(matrix); + Matrix16Ortho( backEnd.viewParms.viewportX, backEnd.viewParms.viewportX + backEnd.viewParms.viewportWidth, + backEnd.viewParms.viewportY, backEnd.viewParms.viewportY + backEnd.viewParms.viewportHeight, + -99999, 99999, matrix ); + GL_SetProjectionMatrix(matrix); for ( f = r_activeFlares ; f ; f = f->next ) { if ( f->frameSceneNum == backEnd.viewParms.frameSceneNum @@ -522,8 +523,7 @@ void RB_RenderFlares (void) { } } - qglPopMatrix(); - qglMatrixMode( GL_MODELVIEW ); - qglPopMatrix(); + GL_SetProjectionMatrix(oldprojection); + GL_SetModelviewMatrix(oldmodelview); } diff --git a/reaction/code/renderer/tr_glsl.c b/reaction/code/renderer/tr_glsl.c new file mode 100644 index 00000000..dc324a0d --- /dev/null +++ b/reaction/code/renderer/tr_glsl.c @@ -0,0 +1,1138 @@ +/* +=========================================================================== +Copyright (C) 2006-2009 Robert Beckebans + +This file is part of XreaL source code. + +XreaL source code is free software; you can redistribute it +and/or modify it under the terms of the GNU General Public License as +published by the Free Software Foundation; either version 2 of the License, +or (at your option) any later version. + +XreaL source code is distributed in the hope that it will be +useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with XreaL source code; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +=========================================================================== +*/ +// tr_glsl.c +#include "tr_local.h" + +void GLSL_BindNullProgram(void); + +// FIXME: Do something that isn't this messy +static const char *fallbackGenericShader_vp = +"attribute vec4 attr_Position;\r\nattribute vec4 attr_TexCoord0;\r\nattrib" +"ute vec4 attr_TexCoord1;\r\nattribute vec3 attr_Normal;\r\nattribute vec4" +" attr_Color;\r\n\r\n#if defined(USE_VERTEX_ANIMATION)\r\nattribute vec4 a" +"ttr_Position2;\r\nattribute vec3 attr_Normal2;\r\n#endif\r\n\r\nuniform ma" +"t4 u_Texture0Matrix;\r\nuniform vec3 u_ViewOrigin;\r\n\r\n#if defined" +"(USE_TCGEN)\r\nuniform int u_TCGen0;\r\nuniform vec4 u_TCGen0Vector0" +";\r\nuniform vec4 u_TCGen0Vector1;\r\n#endif\r\n\r\n#if defined(USE_FOG)" +"\r\nuniform vec4 u_FogDistance;\r\nuniform vec4 u_FogDepth;\r\nunifor" +"m float u_FogEyeT;\r\nuniform int u_FogAdjustColors;\r\n#endif\r\n\r" +"\n#if defined(USE_DEFORM_VERTEXES)\r\nuniform int u_DeformGen;\r\nunifo" +"rm vec4 u_DeformWave;\r\nuniform vec3 u_DeformBulge;\r\nuniform float" +" u_DeformSpread;\r\n#endif\r\n\r\nuniform float u_Time;\r\nuniform int " +" u_ColorGen;\r\nuniform int u_AlphaGen;\r\nuniform vec4 u_Color;" +"\r\nuniform mat4 u_ModelViewProjectionMatrix;\r\nuniform vec3 u_Ambie" +"ntLight;\r\nuniform vec3 u_DirectedLight;\r\nuniform vec3 u_LightDir;" +"\r\n\r\nuniform float u_PortalRange;\r\n\r\n#if defined(USE_VERTEX_ANIMAT" +"ION)\r\nuniform float u_VertexLerp;\r\n#endif\r\n\r\nvarying vec2 var_" +"Tex1;\r\nvarying vec2 var_Tex2;\r\nvarying vec4 var_Color;\r\nvarying" +" float var_DlightMod;\r\n\r\n#if defined(USE_DEFORM_VERTEXES)\r\nfloat tr" +"iangle(float x)\r\n{\r\n\treturn max(1.0 - abs(x), 0);\r\n}\r\n\r\nfloat sa" +"wtooth(float x)\r\n{\r\n\treturn x - floor(x);\r\n}\r\n\r\nvec4 DeformPosit" +"ion(const vec4 pos, const vec3 normal, const vec2 st)\r\n{\r\n\tvec4 deform" +"ed = pos;\r\n\r\n\tif (u_DeformGen == DGEN_WAVE_SIN)\r\n\t{\r\n\t\tfloat of" +"f = (pos.x + pos.y + pos.z) * u_DeformSpread;\r\n\t\tfloat scale = u_Deform" +"Wave.x + sin((off + u_DeformWave.z + (u_Time * u_DeformWave.w)) * 2.0 * M_" +"PI) * u_DeformWave.y;\r\n\t\tvec3 offset = normal * scale;\r\n\r\n\t\tdefor" +"med.xyz += offset;\r\n\t}\r\n else if (u_DeformGen == DGEN_WAVE_SQUARE)\r" +"\n\t{\r\n\t\tfloat off = (pos.x + pos.y + pos.z) * u_DeformSpread;\r\n\t\tf" +"loat scale = u_DeformWave.x + sign(sin((off + u_DeformWave.z + (u_Time * u" +"_DeformWave.w)) * 2.0 * M_PI)) * u_DeformWave.y;\r\n\t\tvec3 offset = norma" +"l * scale;\r\n\r\n\t\tdeformed.xyz += offset;\r\n\t}\r\n else if (u_Deform" +"Gen == DGEN_WAVE_TRIANGLE)\r\n\t{\r\n\t\tfloat off = (pos.x + pos.y + pos.z" +") * u_DeformSpread;\r\n\t\tfloat scale = u_DeformWave.x + triangle(off + u" +"_DeformWave.z + (u_Time * u_DeformWave.w)) * u_DeformWave.y;\r\n\t\tvec3 of" +"fset = normal * scale;\r\n\r\n\t\tdeformed.xyz += offset;\r\n\t}\r\n else " +"if (u_DeformGen == DGEN_WAVE_SAWTOOTH)\r\n\t{\r\n\t\tfloat off = (pos.x + p" +"os.y + pos.z) * u_DeformSpread;\r\n\t\tfloat scale = u_DeformWave.x + sawt" +"ooth(off + u_DeformWave.z + (u_Time * u_DeformWave.w)) * u_DeformWave.y;\r" +"\n\t\tvec3 offset = normal * scale;\r\n\r\n\t\tdeformed.xyz += offset;\r\n" +"\t}\r\n else if (u_DeformGen == DGEN_WAVE_INVERSE_SAWTOOTH)\r\n\t{\r\n\t\t" +"float off = (pos.x + pos.y + pos.z) * u_DeformSpread;\r\n\t\tfloat scale = " +"u_DeformWave.x + (1.0 - sawtooth(off + u_DeformWave.z + (u_Time * u_DeformW" +"ave.w))) * u_DeformWave.y;\r\n\t\tvec3 offset = normal * scale;\r\n\r\n\t\t" +"deformed.xyz += offset;\r\n\t}\r\n else if (u_DeformGen == DGEN_BULGE)\r\n" +"\t{\r\n\t\tfloat bulgeWidth = u_DeformBulge.x;\r\n\t\tfloat bulgeHeight = u" +"_DeformBulge.y;\r\n\t\tfloat bulgeSpeed = u_DeformBulge.z;\r\n\r\n\t\tfloat" +" now = u_Time * bulgeSpeed;\r\n\r\n\t\tfloat off = (M_PI * 0.25) * st.x * b" +"ulgeWidth + now;\r\n\t\tfloat scale = sin(off) * bulgeHeight;\r\n\t\tvec3 o" +"ffset = normal * scale;\r\n\r\n\t\tdeformed.xyz += offset;\r\n\t}\r\n\r\n\t" +"return deformed;\r\n}\r\n#endif\r\n\r\n#if defined(USE_TCGEN)\r\nvec2 GenTe" +"xCoords(int TCGen, vec4 position, vec3 normal, mat4 texMatrix, vec4 TCGenVe" +"ctor0, vec4 TCGenVector1)\r\n{\r\n\tvec2 tex = vec2(0.0);\r\n\r\n\tif (TCGe" +"n == TCGEN_LIGHTMAP)\r\n\t{\r\n\t\ttex = attr_TexCoord1.st;\r\n\t}\r\n\tels" +"e if (TCGen == TCGEN_TEXTURE)\r\n\t{\r\n\t\ttex = attr_TexCoord0.st;\r\n\t}" +"\r\n\telse if (TCGen == TCGEN_ENVIRONMENT_MAPPED)\r\n\t{\r\n\t\tvec3 viewer" +" = normalize(u_ViewOrigin - position.xyz);\r\n\r\n\t\tfloat d = dot(normal," +" viewer);\r\n\r\n\t\tvec3 reflected = normal * 2.0 * d - viewer;\r\n\r\n\t" +"\ttex.s = 0.5 + reflected.y * 0.5;\r\n\t\ttex.t = 0.5 - reflected.z * 0.5;" +"\r\n\t}\r\n\telse if (TCGen == TCGEN_VECTOR)\r\n\t{\r\n\t\ttex.s = dot(posi" +"tion.xyz, TCGenVector0.xyz);\r\n\t\ttex.t = dot(position.xyz, TCGenVector1." +"xyz);\r\n\t}\r\n\telse if (TCGen == TCGEN_DLIGHT)\r\n\t{\r\n\t vec3 dist =" +" TCGenVector0.xyz - position.xyz;\r\n\t \r\n\t if (dot(dist, normal) < 0)" +"\r\n\t {\r\n\t var_DlightMod = 0;\r\n\t }\r\n\t else\r\n\t {\r\n\t " +" float diffz = abs(dist.z);\r\n float radius = 1.0 / TCGenVector0.a;" +"\r\n\t \r\n\t if (diffz > radius)\r\n {\r\n var_DlightMod" +" = 0;\r\n }\r\n else\r\n {\r\n tex = vec2(0.5) + dist" +".xy * TCGenVector0.a;\r\n\r\n if (diffz < radius * 0.5)\r\n {" +"\r\n var_DlightMod = 1.0;\r\n }\r\n else\r\n " +"{\r\n var_DlightMod = 2.0 * (radius - diffz) * TCGenVector0.a;\r\n" +" }\r\n }\r\n\t }\r\n\t}\r\n\t\r\n\treturn tex;\r\n}\r\n#endif" +"\r\n\r\nvoid\tmain()\r\n{\r\n\tvec4 position;\r\n\tvec3 normal;\r\n\r\n#if " +"defined(USE_VERTEX_ANIMATION)\r\n if (u_VertexLerp > 0.0)\r\n {\r\n po" +"sition = mix(attr_Position, attr_Position2, u_VertexLerp);\r\n normal = " +"mix(attr_Normal, attr_Normal2, u_VertexLerp);\r\n normal = normalize(nor" +"mal);\r\n }\r\n else\r\n#endif\r\n {\r\n position = attr_Position;\r" +"\n normal = attr_Normal;\r\n }\r\n\r\n#if defined(USE_DEFORM_VERTEXES)" +"\r\n position = DeformPosition(position, normal, attr_TexCoord0.st);\r\n#e" +"ndif\r\n\r\n\tgl_Position = u_ModelViewProjectionMatrix * position;\r\n\r\n" +" {\r\n vec4 tex = vec4(1.0, 1.0, 1.0, 0.0);\r\n\r\n#if defined(USE_TCGE" +"N)\r\n tex.st = GenTexCoords(u_TCGen0, position, normal, u_Texture0Matri" +"x, u_TCGen0Vector0, u_TCGen0Vector1);\r\n#else\r\n tex.st = attr_TexCoor" +"d0.st;\r\n#endif\r\n \r\n var_Tex1 = (u_Texture0Matrix * tex).st;\r\n" +"\r\n if (u_Texture0Matrix[3][0] != 0)\r\n {\r\n var_Tex1.s += si" +"n(((position.x + position.z) * 1.0 / 128.0 * 0.125 + u_Texture0Matrix[3][1]" +") * 2.0 * M_PI) * u_Texture0Matrix[3][0];\r\n var_Tex1.t += sin((posit" +"ion.y * 1.0 / 128.0 * 0.125 + u_Texture0Matrix[3][1]) * 2.0 * M_PI) * u_Tex" +"ture0Matrix[3][0];\r\n }\r\n }\r\n\r\n var_Tex2 = attr_TexCoord1.st;\r" +"\n\r\n if (u_ColorGen == CGEN_IDENTITY)\r\n {\r\n var_Color.rgb = vec3" +"(1.0);\r\n }\r\n\telse if (u_ColorGen == CGEN_LIGHTING_DIFFUSE)\r\n\t{\r\n" +"\t\tfloat incoming = dot(attr_Normal, u_LightDir);\r\n\r\n\t\tif (incoming " +"<= 0)\r\n\t\t{\r\n\t\t\tvar_Color.rgb = u_AmbientLight;\r\n\t\t}\r\n\t\tels" +"e\r\n\t\t{\r\n\t\t var_Color.rgb = min(u_AmbientLight + u_DirectedLight * " +"incoming, vec3(1));\r\n\t\t}\r\n\t}\r\n\telse if (u_ColorGen == CGEN_EXACT_" +"VERTEX)\r\n\t{\r\n\t\tvar_Color.rgb = attr_Color.rgb;\r\n\t}\r\n\telse if (" +"u_ColorGen == CGEN_VERTEX)\r\n\t{\r\n\t\tvar_Color.rgb = attr_Color.rgb * u" +"_Color.rgb;\r\n\t}\r\n\telse if (u_ColorGen == CGEN_ONE_MINUS_VERTEX)\r\n\t" +"{\r\n\t\tvar_Color.rgb = (vec3(1.0) - attr_Color.rgb) * u_Color.rgb;\r\n\t}" +"\r\n\telse if (u_ColorGen == CGEN_DLIGHT)\r\n\t{\r\n\t var_Color.rgb = u_C" +"olor.rgb * var_DlightMod;\r\n\t}\r\n\telse\r\n\t{\r\n\t\tvar_Color.rgb = u_" +"Color.rgb;\r\n\t}\r\n\r\n if (u_AlphaGen == AGEN_IDENTITY)\r\n {\r\n v" +"ar_Color.a = 1.0;\r\n }\r\n\telse if (u_AlphaGen == AGEN_LIGHTING_SPECULAR" +")\r\n\t{\r\n\t\tvec3 lightDir = vec3(-960.0, -1980.0, 96.0) - position.xyz;" +"\r\n\t\tlightDir = normalize(lightDir);\r\n\r\n\t\tfloat d = dot(attr_Norma" +"l, lightDir);\r\n\t\tvec3 reflected = attr_Normal * 2.0 * d - lightDir;\r\n" +"\r\n\t\tvec3 viewer = u_ViewOrigin - position.xyz;\r\n\t\tfloat ilength = 1" +".0 / length(viewer);\r\n\r\n\t\tfloat l = dot(reflected, viewer);\r\n\t\tl " +"*= ilength;\r\n\r\n\t\tif (l < 0.0)\r\n\t\t{\r\n\t\t\tvar_Color.a = 0.0;\r" +"\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tl = l*l;\r\n\t\t\tl = l*l;\r\n\t\t\tv" +"ar_Color.a = min(l, 1.0);\r\n\t\t}\r\n\t}\r\n\telse if (u_AlphaGen == AGEN_" +"VERTEX)\r\n\t{\r\n\t\tvar_Color.a = attr_Color.a;\r\n\t}\r\n\telse if (u_Al" +"phaGen == AGEN_ONE_MINUS_VERTEX)\r\n\t{\r\n\t\tvar_Color.a = 1.0 - attr_Col" +"or.a;\r\n\t}\r\n\telse if (u_AlphaGen == AGEN_PORTAL)\r\n\t{\r\n\t\tfloat l" +"en;\r\n\t\tvec3 v;\r\n\r\n\t\tv = position.xyz - u_ViewOrigin;\r\n\t\tlen =" +" length(v);\r\n\r\n\t\tlen /= u_PortalRange;\r\n\r\n\t\tvar_Color.a = clamp" +"(len, 0.0, 1.0);\r\n\t}\r\n\telse\r\n\t{\r\n\t\tvar_Color.a = u_Color.a;\r" +"\n\t}\r\n\r\n#if defined (USE_FOG)\r\n if (u_FogAdjustColors != 0) \r\n\t{" +"\r\n\t\tfloat s = dot(position.xyz, u_FogDistance.xyz) + u_FogDistance.a;\r" +"\n\t\tfloat t = dot(position.xyz, u_FogDepth.xyz) + u_FogDepth.a;\r\n\t\t\r" +"\n\t\tif (s < 0.0 || t < 0.0 || (u_FogEyeT < 0.0 && t < 1.0) )\r\n\t\t{\r\n" +"\t\t s = 0.0;\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t if (u_FogEyeT < 0.0)" +"\r\n\t\t {\r\n\t\t s *= t / (t - u_FogEyeT);\r\n\t\t }\r\n\r\n\t\t " +"s *= 8.0;\r\n\t\t \t\t \r\n\t\t s = clamp(s, 0.0, 1.0);\r\n\t\t \r\n\t" +"\t s = sqrt(s);\r\n\t\t}\r\n\t\t\r\n\t\tif (u_FogAdjustColors == 1)\r\n " +" {\r\n var_Color.xyz *= (1.0 - s);\r\n }\r\n else if (u_FogAdjus" +"tColors == 2)\r\n {\r\n var_Color.a *= (1.0 - s);\r\n }\r\n e" +"lse if (u_FogAdjustColors == 3)\r\n {\r\n var_Color *= (1.0 - s);\r" +"\n }\r\n else if (u_FogAdjustColors == 4)\r\n {\r\n var_Color" +".xyz = u_Color.xyz;\r\n var_Color.a = u_Color.a * s;\r\n }\r\n }\r" +"\n#endif\r\n}\r\n"; + +static const char *fallbackGenericShader_fp = +"uniform sampler2D u_Texture0Map;\r\nuniform sampler2D u_Texture1Map;" +"\r\nuniform int u_Texture1Env;\r\n\r\n#if defined(USE_FOG)\r\nunif" +"orm int u_FogAdjustColors;\r\n#endif\r\n\r\nvarying vec2 v" +"ar_Tex1;\r\nvarying vec2 var_Tex2;\r\nvarying vec4 var_Colo" +"r;\r\n\r\n\r\nvoid\tmain()\r\n{\r\n\tvec4 color;\r\n\r\n#if defined(USE_FOG" +")\r\n if (u_FogAdjustColors == 4)\r\n {\r\n color = var_Color;\r\n }" +"\r\n else\r\n#endif\r\n {\r\n if (u_Texture1Env != 2)\r\n {\r\n " +" color = texture2D(u_Texture0Map, var_Tex1);\r\n }\r\n \r\n if (" +"u_Texture1Env != 0)\r\n {\r\n vec4 color2 = texture2D(u_Texture1Map" +", var_Tex2);\r\n\r\n if (u_Texture1Env == 1) // GL_MODULATE\r\n {" +"\r\n color *= color2;\r\n }\r\n else if (u_Texture1Env == " +"4) // GL_ADD\r\n {\r\n color += color2;\r\n }\r\n els" +"e // if (u_Texture1Env == 2) GL_REPLACE\r\n {\r\n color = color" +"2;\r\n }\r\n }\r\n\r\n color *= var_Color;\r\n }\r\n\r\n\tgl_Fr" +"agColor = color;\r\n}\r\n"; + + +static void GLSL_PrintInfoLog(GLhandleARB object, qboolean developerOnly) +{ + char *msg; + static char msgPart[1024]; + int maxLength = 0; + int i; + + qglGetObjectParameterivARB(object, GL_OBJECT_INFO_LOG_LENGTH_ARB, &maxLength); + + msg = ri.Malloc(maxLength); + + qglGetInfoLogARB(object, maxLength, &maxLength, msg); + + if(developerOnly) + { + ri.Printf(PRINT_DEVELOPER, "compile log:\n"); + } + else + { + ri.Printf(PRINT_ALL, "compile log:\n"); + } + + for(i = 0; i < maxLength; i += 1024) + { + Q_strncpyz(msgPart, msg + i, sizeof(msgPart)); + + if(developerOnly) + ri.Printf(PRINT_DEVELOPER, "%s\n", msgPart); + else + ri.Printf(PRINT_ALL, "%s\n", msgPart); + } + + ri.Free(msg); +} + +static void GLSL_PrintShaderSource(GLhandleARB object) +{ + char *msg; + static char msgPart[1024]; + int maxLength = 0; + int i; + + qglGetObjectParameterivARB(object, GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &maxLength); + + msg = ri.Malloc(maxLength); + + qglGetShaderSourceARB(object, maxLength, &maxLength, msg); + + for(i = 0; i < maxLength; i += 1024) + { + Q_strncpyz(msgPart, msg + i, sizeof(msgPart)); + ri.Printf(PRINT_ALL, "%s\n", msgPart); + } + + ri.Free(msg); +} + +static int GLSL_CompileGPUShader(GLhandleARB program, GLhandleARB *prevShader, const GLcharARB *buffer, int size, GLenum shaderType, const GLcharARB *extra) +{ + GLint compiled; + GLhandleARB shader; + + shader = qglCreateShaderObjectARB(shaderType); + + { + static char bufferExtra[32000]; + int sizeExtra; + + char *bufferFinal = NULL; + int sizeFinal; + + float fbufWidthScale, fbufHeightScale; + + Com_Memset(bufferExtra, 0, sizeof(bufferExtra)); + + // HACK: abuse the GLSL preprocessor to turn GLSL 1.20 shaders into 1.30 ones + if(0) //(glConfig.driverType == GLDRV_OPENGL3) + { + Q_strcat(bufferExtra, sizeof(bufferExtra), "#version 130\n"); + + if(shaderType == GL_VERTEX_SHADER_ARB) + { + Q_strcat(bufferExtra, sizeof(bufferExtra), "#define attribute in\n"); + Q_strcat(bufferExtra, sizeof(bufferExtra), "#define varying out\n"); + } + else + { + Q_strcat(bufferExtra, sizeof(bufferExtra), "#define varying in\n"); + + Q_strcat(bufferExtra, sizeof(bufferExtra), "out vec4 out_Color;\n"); + Q_strcat(bufferExtra, sizeof(bufferExtra), "#define gl_FragColor out_Color\n"); + } + } + else + { + Q_strcat(bufferExtra, sizeof(bufferExtra), "#version 120\n"); + } + + // HACK: add some macros to avoid extra uniforms and save speed and code maintenance + //Q_strcat(bufferExtra, sizeof(bufferExtra), + // va("#ifndef r_SpecularExponent\n#define r_SpecularExponent %f\n#endif\n", r_specularExponent->value)); + //Q_strcat(bufferExtra, sizeof(bufferExtra), + // va("#ifndef r_SpecularScale\n#define r_SpecularScale %f\n#endif\n", r_specularScale->value)); + //Q_strcat(bufferExtra, sizeof(bufferExtra), + // va("#ifndef r_NormalScale\n#define r_NormalScale %f\n#endif\n", r_normalScale->value)); + + + Q_strcat(bufferExtra, sizeof(bufferExtra), "#ifndef M_PI\n#define M_PI 3.14159265358979323846f\n#endif\n"); + + //Q_strcat(bufferExtra, sizeof(bufferExtra), va("#ifndef MAX_SHADOWMAPS\n#define MAX_SHADOWMAPS %i\n#endif\n", MAX_SHADOWMAPS)); + + Q_strcat(bufferExtra, sizeof(bufferExtra), + va("#ifndef deformGen_t\n" + "#define deformGen_t\n" + "#define DGEN_WAVE_SIN %i\n" + "#define DGEN_WAVE_SQUARE %i\n" + "#define DGEN_WAVE_TRIANGLE %i\n" + "#define DGEN_WAVE_SAWTOOTH %i\n" + "#define DGEN_WAVE_INVERSE_SAWTOOTH %i\n" + "#define DGEN_BULGE %i\n" + "#define DGEN_MOVE %i\n" + "#endif\n", + DGEN_WAVE_SIN, + DGEN_WAVE_SQUARE, + DGEN_WAVE_TRIANGLE, + DGEN_WAVE_SAWTOOTH, + DGEN_WAVE_INVERSE_SAWTOOTH, + DGEN_BULGE, + DGEN_MOVE)); + + Q_strcat(bufferExtra, sizeof(bufferExtra), + va("#ifndef tcGen_t\n" + "#define tcGen_t\n" + "#define TCGEN_LIGHTMAP %i\n" + "#define TCGEN_TEXTURE %i\n" + "#define TCGEN_ENVIRONMENT_MAPPED %i\n" + "#define TCGEN_FOG %i\n" + "#define TCGEN_VECTOR %i\n" + "#define TCGEN_DLIGHT %i\n" + "#endif\n", + TCGEN_LIGHTMAP, + TCGEN_TEXTURE, + TCGEN_ENVIRONMENT_MAPPED, + TCGEN_FOG, + TCGEN_VECTOR, + TCGEN_DLIGHT)); + + Q_strcat(bufferExtra, sizeof(bufferExtra), + va("#ifndef colorGen_t\n" + "#define colorGen_t\n" + "#define CGEN_IDENTITY %i\n" + "#define CGEN_VERTEX %i\n" + "#define CGEN_ONE_MINUS_VERTEX %i\n" + "#define CGEN_EXACT_VERTEX %i\n" + "#define CGEN_LIGHTING_DIFFUSE %i\n" + "#define CGEN_DLIGHT %i\n" + "#endif\n", + CGEN_IDENTITY, + CGEN_VERTEX, + CGEN_ONE_MINUS_VERTEX, + CGEN_EXACT_VERTEX, + CGEN_LIGHTING_DIFFUSE, + CGEN_DLIGHT)); + + Q_strcat(bufferExtra, sizeof(bufferExtra), + va("#ifndef alphaGen_t\n" + "#define alphaGen_t\n" + "#define AGEN_IDENTITY %i\n" + "#define AGEN_VERTEX %i\n" + "#define AGEN_ONE_MINUS_VERTEX %i\n" + "#define AGEN_LIGHTING_SPECULAR %i\n" + "#define AGEN_PORTAL %i\n" + "#endif\n", + AGEN_IDENTITY, + AGEN_VERTEX, + AGEN_ONE_MINUS_VERTEX, + AGEN_LIGHTING_SPECULAR, + AGEN_PORTAL)); + + Q_strcat(bufferExtra, sizeof(bufferExtra), + va("#ifndef alphaTest_t\n" + "#define alphaTest_t\n" + "#define ATEST_GT_0 %i\n" + "#define ATEST_LT_128 %i\n" + "#define ATEST_GE_128 %i\n" + "#endif\n", + ATEST_GT_0, + ATEST_LT_128, + ATEST_GE_128)); + + fbufWidthScale = 1.0f / ((float)glConfig.vidWidth); + fbufHeightScale = 1.0f / ((float)glConfig.vidHeight); + Q_strcat(bufferExtra, sizeof(bufferExtra), + va("#ifndef r_FBufScale\n#define r_FBufScale vec2(%f, %f)\n#endif\n", fbufWidthScale, fbufHeightScale)); + + + if (extra) + { + Q_strcat(bufferExtra, sizeof(bufferExtra), extra); + } + + // OK we added a lot of stuff but if we do something bad in the GLSL shaders then we want the proper line + // so we have to reset the line counting + Q_strcat(bufferExtra, sizeof(bufferExtra), "#line 0\n"); + + sizeExtra = strlen(bufferExtra); + sizeFinal = sizeExtra + size; + + //ri.Printf(PRINT_ALL, "GLSL extra: %s\n", bufferExtra); + + bufferFinal = ri.Hunk_AllocateTempMemory(size + sizeExtra); + + strcpy(bufferFinal, bufferExtra); + Q_strcat(bufferFinal, sizeFinal, buffer); + + qglShaderSourceARB(shader, 1, (const GLcharARB **)&bufferFinal, &sizeFinal); + + ri.Hunk_FreeTempMemory(bufferFinal); + } + + // compile shader + qglCompileShaderARB(shader); + + // check if shader compiled + qglGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &compiled); + if(!compiled) + { + GLSL_PrintShaderSource(shader); + GLSL_PrintInfoLog(shader, qfalse); + ri.Error(ERR_DROP, "Couldn't compile shader"); + return 0; + } + + GLSL_PrintInfoLog(shader, qtrue); + //ri.Printf(PRINT_ALL, "%s\n", GLSL_PrintShaderSource(shader)); + + if (*prevShader) + { + qglDetachObjectARB(program, *prevShader); + qglDeleteObjectARB(*prevShader); + } + + // attach shader to program + qglAttachObjectARB(program, shader); + + *prevShader = shader; + + return 1; +} + + +static int GLSL_LoadGPUShader(GLhandleARB program, GLhandleARB *prevShader, const char *name, GLenum shaderType, const GLcharARB *extra) +{ + char filename[MAX_QPATH]; + GLcharARB *buffer = NULL; + int size; + int result; + + if(shaderType == GL_VERTEX_SHADER_ARB) + { + Com_sprintf(filename, sizeof(filename), "glsl/%s_vp.glsl", name); + } + else + { + Com_sprintf(filename, sizeof(filename), "glsl/%s_fp.glsl", name); + } + + ri.Printf(PRINT_ALL, "...loading '%s'\n", filename); + size = ri.FS_ReadFile(filename, (void **)&buffer); + if(!buffer) + { + //ri.Error(ERR_DROP, "Couldn't load %s", filename); + ri.Printf(PRINT_ALL, "Couldn't load %s, size %d\n", filename, size); + return 0; + } + +#if 0 // for dumping + { + int i, l, inc; + + ri.Printf(PRINT_ALL, "%s:\n\"", filename); + l = 0; + + for (i = 0; i < size; i++) + { + switch (buffer[i]) + { + case '\a': + case '\b': + case '\f': + case '\n': + case '\r': + case '\t': + case '\v': + case '"': + case '\\': + inc = 2; + break; + default: + inc = 1; + break; + } + + l += inc; + + if (l >= 76) + { + ri.Printf(PRINT_ALL, "\"\n\""); + l = inc; + } + + switch (buffer[i]) + { + case '\a': + ri.Printf(PRINT_ALL, "\\a"); + break; + case '\b': + ri.Printf(PRINT_ALL, "\\b"); + break; + case '\f': + ri.Printf(PRINT_ALL, "\\f"); + break; + case '\n': + ri.Printf(PRINT_ALL, "\\n"); + break; + case '\r': + ri.Printf(PRINT_ALL, "\\r"); + break; + case '\t': + ri.Printf(PRINT_ALL, "\\t"); + break; + case '\v': + ri.Printf(PRINT_ALL, "\\v"); + break; + case '"': + ri.Printf(PRINT_ALL, "\\\""); + break; + case '\\': + ri.Printf(PRINT_ALL, "\\\\"); + break; + default: + ri.Printf(PRINT_ALL, "%c", buffer[i]); + break; + } + } + ri.Printf(PRINT_ALL, "\"\n"); + } +#endif + + result = GLSL_CompileGPUShader(program, prevShader, buffer, size, shaderType, extra); + + ri.FS_FreeFile(buffer); + + return result; +} + +static void GLSL_LinkProgram(GLhandleARB program) +{ + GLint linked; + + qglLinkProgramARB(program); + + qglGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &linked); + if(!linked) + { + GLSL_PrintInfoLog(program, qfalse); + ri.Error(ERR_DROP, "\nshaders failed to link"); + } +} + +static void GLSL_ValidateProgram(GLhandleARB program) +{ + GLint validated; + + qglValidateProgramARB(program); + + qglGetObjectParameterivARB(program, GL_OBJECT_VALIDATE_STATUS_ARB, &validated); + if(!validated) + { + GLSL_PrintInfoLog(program, qfalse); + ri.Error(ERR_DROP, "\nshaders failed to validate"); + } +} + +static void GLSL_ShowProgramUniforms(GLhandleARB program) +{ + int i, count, size; + GLenum type; + char uniformName[1000]; + + // install the executables in the program object as part of current state. + qglUseProgramObjectARB(program); + + // check for GL Errors + + // query the number of active uniforms + qglGetObjectParameterivARB(program, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &count); + + // Loop over each of the active uniforms, and set their value + for(i = 0; i < count; i++) + { + qglGetActiveUniformARB(program, i, sizeof(uniformName), NULL, &size, &type, uniformName); + + ri.Printf(PRINT_DEVELOPER, "active uniform: '%s'\n", uniformName); + } + + qglUseProgramObjectARB(0); +} + +static int GLSL_InitGPUShader(shaderProgram_t * program, const char *name, int attribs, qboolean fragmentShader, const GLcharARB *extra) +{ + ri.Printf(PRINT_DEVELOPER, "------- GPU shader -------\n"); + + if(strlen(name) >= MAX_QPATH) + { + ri.Error(ERR_DROP, "GLSL_InitGPUShader: \"%s\" is too long\n", name); + } + + Q_strncpyz(program->name, name, sizeof(program->name)); + + program->program = qglCreateProgramObjectARB(); + program->attribs = attribs; + + if (!(GLSL_LoadGPUShader(program->program, &program->vertexShader, name, GL_VERTEX_SHADER_ARB, extra))) + { + ri.Printf(PRINT_ALL, "GLSL_InitGPUShader: Unable to load \"%s\" as GL_VERTEX_SHADER_ARB\n", name); + qglDeleteObjectARB(program->program); + return 0; + } + + if(fragmentShader) + GLSL_LoadGPUShader(program->program, &program->fragmentShader, name, GL_FRAGMENT_SHADER_ARB, extra); + + if(attribs & ATTR_POSITION) + qglBindAttribLocationARB(program->program, ATTR_INDEX_POSITION, "attr_Position"); + + if(attribs & ATTR_TEXCOORD) + qglBindAttribLocationARB(program->program, ATTR_INDEX_TEXCOORD0, "attr_TexCoord0"); + + if(attribs & ATTR_LIGHTCOORD) + qglBindAttribLocationARB(program->program, ATTR_INDEX_TEXCOORD1, "attr_TexCoord1"); + +// if(attribs & ATTR_TEXCOORD2) +// qglBindAttribLocationARB(program->program, ATTR_INDEX_TEXCOORD2, "attr_TexCoord2"); + +// if(attribs & ATTR_TEXCOORD3) +// qglBindAttribLocationARB(program->program, ATTR_INDEX_TEXCOORD3, "attr_TexCoord3"); + + if(attribs & ATTR_TANGENT) + qglBindAttribLocationARB(program->program, ATTR_INDEX_TANGENT, "attr_Tangent"); + + if(attribs & ATTR_BINORMAL) + qglBindAttribLocationARB(program->program, ATTR_INDEX_BINORMAL, "attr_Binormal"); + + if(attribs & ATTR_NORMAL) + qglBindAttribLocationARB(program->program, ATTR_INDEX_NORMAL, "attr_Normal"); + + if(attribs & ATTR_COLOR) + qglBindAttribLocationARB(program->program, ATTR_INDEX_COLOR, "attr_Color"); + + if(attribs & ATTR_PAINTCOLOR) + qglBindAttribLocationARB(program->program, ATTR_INDEX_PAINTCOLOR, "attr_PaintColor"); + + if(attribs & ATTR_LIGHTDIRECTION) + qglBindAttribLocationARB(program->program, ATTR_INDEX_LIGHTDIRECTION, "attr_LightDirection"); + + if(attribs & ATTR_POSITION2) + qglBindAttribLocationARB(program->program, ATTR_INDEX_POSITION2, "attr_Position2"); + + if(attribs & ATTR_NORMAL2) + qglBindAttribLocationARB(program->program, ATTR_INDEX_NORMAL2, "attr_Normal2"); + + GLSL_LinkProgram(program->program); + + return 1; +} + + +static int GLSL_InitFallbackGenericShader(shaderProgram_t * program, const char *name, int attribs, qboolean fragmentShader, const GLcharARB *extra) +{ + ri.Printf(PRINT_DEVELOPER, "------- GPU shader -------\n"); + + if(strlen(name) >= MAX_QPATH) + { + ri.Error(ERR_DROP, "GLSL_InitGPUShader: \"%s\" is too long\n", name); + } + + Q_strncpyz(program->name, name, sizeof(program->name)); + + program->program = qglCreateProgramObjectARB(); + program->attribs = attribs; + + if (!(GLSL_CompileGPUShader(program->program, &program->vertexShader, fallbackGenericShader_vp, strlen(fallbackGenericShader_vp), GL_VERTEX_SHADER_ARB, extra))) + { + ri.Printf(PRINT_ALL, "GLSL_InitFallbackGenericShader: Unable to load \"%s\" as GL_VERTEX_SHADER_ARB\n", name); + qglDeleteObjectARB(program->program); + return 0; + } + + if(fragmentShader) + GLSL_CompileGPUShader(program->program, &program->fragmentShader, fallbackGenericShader_fp, strlen(fallbackGenericShader_fp), GL_FRAGMENT_SHADER_ARB, extra); + + if(attribs & ATTR_POSITION) + qglBindAttribLocationARB(program->program, ATTR_INDEX_POSITION, "attr_Position"); + + if(attribs & ATTR_TEXCOORD) + qglBindAttribLocationARB(program->program, ATTR_INDEX_TEXCOORD0, "attr_TexCoord0"); + + if(attribs & ATTR_LIGHTCOORD) + qglBindAttribLocationARB(program->program, ATTR_INDEX_TEXCOORD1, "attr_TexCoord1"); + +// if(attribs & ATTR_TEXCOORD2) +// qglBindAttribLocationARB(program->program, ATTR_INDEX_TEXCOORD2, "attr_TexCoord2"); + +// if(attribs & ATTR_TEXCOORD3) +// qglBindAttribLocationARB(program->program, ATTR_INDEX_TEXCOORD3, "attr_TexCoord3"); + + if(attribs & ATTR_TANGENT) + qglBindAttribLocationARB(program->program, ATTR_INDEX_TANGENT, "attr_Tangent"); + + if(attribs & ATTR_BINORMAL) + qglBindAttribLocationARB(program->program, ATTR_INDEX_BINORMAL, "attr_Binormal"); + + if(attribs & ATTR_NORMAL) + qglBindAttribLocationARB(program->program, ATTR_INDEX_NORMAL, "attr_Normal"); + + if(attribs & ATTR_COLOR) + qglBindAttribLocationARB(program->program, ATTR_INDEX_COLOR, "attr_Color"); + + if(attribs & ATTR_PAINTCOLOR) + qglBindAttribLocationARB(program->program, ATTR_INDEX_PAINTCOLOR, "attr_PaintColor"); + + if(attribs & ATTR_LIGHTDIRECTION) + qglBindAttribLocationARB(program->program, ATTR_INDEX_LIGHTDIRECTION, "attr_LightDirection"); + + if(attribs & ATTR_POSITION2) + qglBindAttribLocationARB(program->program, ATTR_INDEX_POSITION2, "attr_Position2"); + + if(attribs & ATTR_NORMAL2) + qglBindAttribLocationARB(program->program, ATTR_INDEX_NORMAL2, "attr_Normal2"); + + GLSL_LinkProgram(program->program); + + return 1; +} + + +void GLSL_InitGPUShaders(void) +{ + int startTime, endTime; + int i; + char extradefines[1024]; + + ri.Printf(PRINT_ALL, "------- GLSL_InitGPUShaders -------\n"); + + // make sure the render thread is stopped + R_SyncRenderThread(); + + startTime = ri.Milliseconds(); + + for (i = 0; i < GLSLDEF_COUNT; i++) + { + int attribs = ATTR_POSITION | ATTR_TEXCOORD | ATTR_LIGHTCOORD | ATTR_NORMAL | ATTR_COLOR; + extradefines[0] = '\0'; + + if (i & GLSLDEF_USE_DEFORM_VERTEXES) + Q_strcat(extradefines, 1024, "#define USE_DEFORM_VERTEXES\n"); + + if (i & GLSLDEF_USE_TCGEN) + Q_strcat(extradefines, 1024, "#define USE_TCGEN\n"); + + if (i & GLSLDEF_USE_VERTEX_ANIMATION) + { + Q_strcat(extradefines, 1024, "#define USE_VERTEX_ANIMATION\n"); + attribs |= ATTR_POSITION2 | ATTR_NORMAL2; + } + + if (i & GLSLDEF_USE_FOG) + { + Q_strcat(extradefines, 1024, "#define USE_FOG\n"); + } + + if (!GLSL_InitGPUShader(&tr.genericShader[i], "generic", attribs, qtrue, extradefines)) + { + // Failed to load, init the fallback one + GLSL_InitFallbackGenericShader(&tr.genericShader[i], "generic", attribs, qtrue, extradefines); + } + + tr.genericShader[i].u_ModelViewProjectionMatrix = + qglGetUniformLocationARB(tr.genericShader[i].program, "u_ModelViewProjectionMatrix"); + + tr.genericShader[i].u_AlphaTest = -1; //qglGetUniformLocationARB(tr.genericShader[i].program, "u_AlphaTest"); + tr.genericShader[i].u_ColorGen = qglGetUniformLocationARB(tr.genericShader[i].program, "u_ColorGen"); + tr.genericShader[i].u_AlphaGen = qglGetUniformLocationARB(tr.genericShader[i].program, "u_AlphaGen"); + + if (i & GLSLDEF_USE_TCGEN) + { + tr.genericShader[i].u_TCGen0 = qglGetUniformLocationARB(tr.genericShader[i].program, "u_TCGen0"); + tr.genericShader[i].u_TCGen1 = -1; //qglGetUniformLocationARB(tr.genericShader[i].program, "u_TCGen1"); + tr.genericShader[i].u_TCGen0Vector0 = qglGetUniformLocationARB(tr.genericShader[i].program, "u_TCGen0Vector0"); + tr.genericShader[i].u_TCGen0Vector1 = qglGetUniformLocationARB(tr.genericShader[i].program, "u_TCGen0Vector1"); + tr.genericShader[i].u_TCGen1Vector0 = -1; //qglGetUniformLocationARB(tr.genericShader[i].program, "u_TCGen1Vector0"); + tr.genericShader[i].u_TCGen1Vector1 = -1; //qglGetUniformLocationARB(tr.genericShader[i].program, "u_TCGen1Vector1"); + } + else + { + tr.genericShader[i].u_TCGen0 = -1; + tr.genericShader[i].u_TCGen1 = -1; + tr.genericShader[i].u_TCGen0Vector0 = -1; + tr.genericShader[i].u_TCGen0Vector1 = -1; + tr.genericShader[i].u_TCGen1Vector0 = -1; + tr.genericShader[i].u_TCGen1Vector1 = -1; + } + + if (i & GLSLDEF_USE_FOG) + { + tr.genericShader[i].u_FogAdjustColors = qglGetUniformLocationARB(tr.genericShader[i].program, "u_FogAdjustColors"); + tr.genericShader[i].u_FogDistance = qglGetUniformLocationARB(tr.genericShader[i].program, "u_FogDistance"); + tr.genericShader[i].u_FogDepth = qglGetUniformLocationARB(tr.genericShader[i].program, "u_FogDepth"); + tr.genericShader[i].u_FogEyeT = qglGetUniformLocationARB(tr.genericShader[i].program, "u_FogEyeT"); + } + else + { + tr.genericShader[i].u_FogAdjustColors = -1; + tr.genericShader[i].u_FogDistance = -1; + tr.genericShader[i].u_FogDepth = -1; + tr.genericShader[i].u_FogEyeT = -1; + } + + if (i & GLSLDEF_USE_DEFORM_VERTEXES) + { + tr.genericShader[i].u_DeformGen = qglGetUniformLocationARB(tr.genericShader[i].program, "u_DeformGen"); + tr.genericShader[i].u_DeformWave = qglGetUniformLocationARB(tr.genericShader[i].program, "u_DeformWave"); + tr.genericShader[i].u_DeformBulge = qglGetUniformLocationARB(tr.genericShader[i].program, "u_DeformBulge"); + tr.genericShader[i].u_DeformSpread = qglGetUniformLocationARB(tr.genericShader[i].program, "u_DeformSpread"); + } + else + { + tr.genericShader[i].u_DeformGen = -1; + tr.genericShader[i].u_DeformWave = -1; + tr.genericShader[i].u_DeformBulge = -1; + tr.genericShader[i].u_DeformSpread = -1; + } + + tr.genericShader[i].u_Time = qglGetUniformLocationARB(tr.genericShader[i].program, "u_Time"); + tr.genericShader[i].u_Color = qglGetUniformLocationARB(tr.genericShader[i].program, "u_Color"); + tr.genericShader[i].u_AmbientLight = qglGetUniformLocationARB(tr.genericShader[i].program, "u_AmbientLight"); + tr.genericShader[i].u_DirectedLight = qglGetUniformLocationARB(tr.genericShader[i].program, "u_DirectedLight"); + tr.genericShader[i].u_LightDir = qglGetUniformLocationARB(tr.genericShader[i].program, "u_LightDir"); + tr.genericShader[i].u_ViewOrigin = qglGetUniformLocationARB(tr.genericShader[i].program, "u_ViewOrigin"); + tr.genericShader[i].u_Texture0Matrix = qglGetUniformLocationARB(tr.genericShader[i].program, "u_Texture0Matrix"); + tr.genericShader[i].u_Texture1Matrix = -1; //qglGetUniformLocationARB(tr.genericShader[i].program, "u_Texture1Matrix"); + tr.genericShader[i].u_Texture1Env = qglGetUniformLocationARB(tr.genericShader[i].program, "u_Texture1Env"); + + tr.genericShader[i].u_Texture0Map = qglGetUniformLocationARB(tr.genericShader[i].program, "u_Texture0Map"); + tr.genericShader[i].u_Texture1Map = qglGetUniformLocationARB(tr.genericShader[i].program, "u_Texture1Map"); + + tr.genericShader[i].u_PortalRange = qglGetUniformLocationARB(tr.genericShader[i].program, "u_PortalRange"); + tr.genericShader[i].u_ModelMatrix = -1; //qglGetUniformLocationARB(tr.genericShader[i].program, "u_ModelMatrix"); + + if (i & GLSLDEF_USE_VERTEX_ANIMATION) + { + tr.genericShader[i].u_VertexLerp = qglGetUniformLocationARB(tr.genericShader[i].program, "u_VertexLerp"); + } + else + { + tr.genericShader[i].u_VertexLerp = -1; + } + + qglUseProgramObjectARB(tr.genericShader[i].program); + qglUniform1iARB(tr.genericShader[i].u_Texture0Map, 0); + qglUniform1iARB(tr.genericShader[i].u_Texture1Map, 1); + qglUseProgramObjectARB(0); + + GLSL_ValidateProgram(tr.genericShader[i].program); + GLSL_ShowProgramUniforms(tr.genericShader[i].program); + GL_CheckErrors(); + } + + endTime = ri.Milliseconds(); + + ri.Printf(PRINT_ALL, "GLSL shaders load time = %5.2f seconds\n", (endTime - startTime) / 1000.0); +} + +void GLSL_ShutdownGPUShaders(void) +{ + int i; + + ri.Printf(PRINT_ALL, "------- GLSL_ShutdownGPUShaders -------\n"); + + qglDisableVertexAttribArrayARB(ATTR_INDEX_TEXCOORD0); + qglDisableVertexAttribArrayARB(ATTR_INDEX_TEXCOORD1); + qglDisableVertexAttribArrayARB(ATTR_INDEX_POSITION); + qglDisableVertexAttribArrayARB(ATTR_INDEX_NORMAL); + qglDisableVertexAttribArrayARB(ATTR_INDEX_COLOR); + GLSL_BindNullProgram(); + + for ( i = 0; i < GLSLDEF_COUNT; i++) + { + if(tr.genericShader[i].program) + { + if (tr.genericShader[i].vertexShader) + { + qglDetachObjectARB(tr.genericShader[i].program, tr.genericShader[i].vertexShader); + qglDeleteObjectARB(tr.genericShader[i].vertexShader); + } + + if (tr.genericShader[i].fragmentShader) + { + qglDetachObjectARB(tr.genericShader[i].program, tr.genericShader[i].fragmentShader); + qglDeleteObjectARB(tr.genericShader[i].fragmentShader); + } + + qglDeleteObjectARB(tr.genericShader[i].program); + Com_Memset(&tr.genericShader[i], 0, sizeof(shaderProgram_t)); + } + } + + glState.currentProgram = 0; + qglUseProgramObjectARB(0); +} + + +void GLSL_BindProgram(shaderProgram_t * program) +{ + if(!program) + { + GLSL_BindNullProgram(); + return; + } + + if(r_logFile->integer) + { + // don't just call LogComment, or we will get a call to va() every frame! + GLimp_LogComment(va("--- GL_BindProgram( %s ) ---\n", program->name)); + } + + if(glState.currentProgram != program) + { + qglUseProgramObjectARB(program->program); + glState.currentProgram = program; + } +} + +void GLSL_BindNullProgram(void) +{ + if(r_logFile->integer) + { + GLimp_LogComment("--- GL_BindNullProgram ---\n"); + } + + if(glState.currentProgram) + { + qglUseProgramObjectARB(0); + glState.currentProgram = NULL; + } +} + + +void GLSL_VertexAttribsState(uint32_t stateBits) +{ + uint32_t diff; + + GLSL_VertexAttribPointers(stateBits); + + diff = stateBits ^ glState.vertexAttribsState; + if(!diff) + { + return; + } + + if(diff & ATTR_POSITION) + { + if(stateBits & ATTR_POSITION) + { + GLimp_LogComment("qglEnableVertexAttribArrayARB( ATTR_INDEX_POSITION )\n"); + qglEnableVertexAttribArrayARB(ATTR_INDEX_POSITION); + } + else + { + GLimp_LogComment("qglDisableVertexAttribArrayARB( ATTR_INDEX_POSITION )\n"); + qglDisableVertexAttribArrayARB(ATTR_INDEX_POSITION); + } + } + + if(diff & ATTR_TEXCOORD) + { + if(stateBits & ATTR_TEXCOORD) + { + GLimp_LogComment("qglEnableVertexAttribArrayARB( ATTR_INDEX_TEXCOORD )\n"); + qglEnableVertexAttribArrayARB(ATTR_INDEX_TEXCOORD0); + } + else + { + GLimp_LogComment("qglDisableVertexAttribArrayARB( ATTR_INDEX_TEXCOORD )\n"); + qglDisableVertexAttribArrayARB(ATTR_INDEX_TEXCOORD0); + } + } + + if(diff & ATTR_LIGHTCOORD) + { + if(stateBits & ATTR_LIGHTCOORD) + { + GLimp_LogComment("qglEnableVertexAttribArrayARB( ATTR_INDEX_LIGHTCOORD )\n"); + qglEnableVertexAttribArrayARB(ATTR_INDEX_TEXCOORD1); + } + else + { + GLimp_LogComment("qglDisableVertexAttribArrayARB( ATTR_INDEX_LIGHTCOORD )\n"); + qglDisableVertexAttribArrayARB(ATTR_INDEX_TEXCOORD1); + } + } + + if(diff & ATTR_NORMAL) + { + if(stateBits & ATTR_NORMAL) + { + GLimp_LogComment("qglEnableVertexAttribArrayARB( ATTR_INDEX_NORMAL )\n"); + qglEnableVertexAttribArrayARB(ATTR_INDEX_NORMAL); + } + else + { + GLimp_LogComment("qglDisableVertexAttribArrayARB( ATTR_INDEX_NORMAL )\n"); + qglDisableVertexAttribArrayARB(ATTR_INDEX_NORMAL); + } + } + + if(diff & ATTR_COLOR) + { + if(stateBits & ATTR_COLOR) + { + GLimp_LogComment("qglEnableVertexAttribArrayARB( ATTR_INDEX_COLOR )\n"); + qglEnableVertexAttribArrayARB(ATTR_INDEX_COLOR); + } + else + { + GLimp_LogComment("qglDisableVertexAttribArrayARB( ATTR_INDEX_COLOR )\n"); + qglDisableVertexAttribArrayARB(ATTR_INDEX_COLOR); + } + } + + if(diff & ATTR_POSITION2) + { + if(stateBits & ATTR_POSITION2) + { + GLimp_LogComment("qglEnableVertexAttribArrayARB( ATTR_INDEX_POSITION2 )\n"); + qglEnableVertexAttribArrayARB(ATTR_INDEX_POSITION2); + } + else + { + GLimp_LogComment("qglDisableVertexAttribArrayARB( ATTR_INDEX_POSITION2 )\n"); + qglDisableVertexAttribArrayARB(ATTR_INDEX_POSITION2); + } + } + + if(diff & ATTR_NORMAL2) + { + if(stateBits & ATTR_NORMAL2) + { + GLimp_LogComment("qglEnableVertexAttribArrayARB( ATTR_INDEX_NORMAL2 )\n"); + qglEnableVertexAttribArrayARB(ATTR_INDEX_NORMAL2); + } + else + { + GLimp_LogComment("qglDisableVertexAttribArrayARB( ATTR_INDEX_NORMAL2 )\n"); + qglDisableVertexAttribArrayARB(ATTR_INDEX_NORMAL2); + } + } + + glState.vertexAttribsState = stateBits; +} + +void GLSL_VertexAttribPointers(uint32_t attribBits) +{ + if(!glState.currentVBO) + { + ri.Error(ERR_FATAL, "GL_VertexAttribPointers: no VBO bound"); + return; + } + + // don't just call LogComment, or we will get a call to va() every frame! + GLimp_LogComment(va("--- GL_VertexAttribPointers( %s ) ---\n", glState.currentVBO->name)); + + if((attribBits & ATTR_POSITION) && !(glState.vertexAttribPointersSet & ATTR_POSITION)) + { + GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_POSITION )\n"); + + qglVertexAttribPointerARB(ATTR_INDEX_POSITION, 3, GL_FLOAT, 0, glState.currentVBO->stride_xyz, BUFFER_OFFSET(glState.currentVBO->ofs_xyz + glState.vertexAttribsNewFrame * glState.currentVBO->size_xyz)); + glState.vertexAttribPointersSet |= ATTR_POSITION; + } + + if((attribBits & ATTR_TEXCOORD) && !(glState.vertexAttribPointersSet & ATTR_TEXCOORD)) + { + GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_TEXCOORD )\n"); + + qglVertexAttribPointerARB(ATTR_INDEX_TEXCOORD0, 2, GL_FLOAT, 0, glState.currentVBO->stride_st, BUFFER_OFFSET(glState.currentVBO->ofs_st)); + glState.vertexAttribPointersSet |= ATTR_TEXCOORD; + } + + if((attribBits & ATTR_LIGHTCOORD) && !(glState.vertexAttribPointersSet & ATTR_LIGHTCOORD)) + { + GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_LIGHTCOORD )\n"); + + qglVertexAttribPointerARB(ATTR_INDEX_TEXCOORD1, 2, GL_FLOAT, 0, glState.currentVBO->stride_lightmap, BUFFER_OFFSET(glState.currentVBO->ofs_lightmap)); + glState.vertexAttribPointersSet |= ATTR_LIGHTCOORD; + } + + if((attribBits & ATTR_NORMAL) && !(glState.vertexAttribPointersSet & ATTR_NORMAL)) + { + GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_NORMAL )\n"); + + qglVertexAttribPointerARB(ATTR_INDEX_NORMAL, 3, GL_FLOAT, 0, glState.currentVBO->stride_normal, BUFFER_OFFSET(glState.currentVBO->ofs_normal + glState.vertexAttribsNewFrame * glState.currentVBO->size_normal)); + glState.vertexAttribPointersSet |= ATTR_NORMAL; + } + + if((attribBits & ATTR_COLOR) && !(glState.vertexAttribPointersSet & ATTR_COLOR)) + { + GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_COLOR )\n"); + + qglVertexAttribPointerARB(ATTR_INDEX_COLOR, 4, GL_UNSIGNED_BYTE, 1, glState.currentVBO->stride_vertexcolor, BUFFER_OFFSET(glState.currentVBO->ofs_vertexcolor)); + glState.vertexAttribPointersSet |= ATTR_COLOR; + } + + if((attribBits & ATTR_POSITION2) && !(glState.vertexAttribPointersSet & ATTR_POSITION2)) + { + GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_POSITION2 )\n"); + + qglVertexAttribPointerARB(ATTR_INDEX_POSITION2, 3, GL_FLOAT, 0, glState.currentVBO->stride_xyz, BUFFER_OFFSET(glState.currentVBO->ofs_xyz + glState.vertexAttribsOldFrame * glState.currentVBO->size_xyz)); + glState.vertexAttribPointersSet |= ATTR_POSITION2; + } + + if((attribBits & ATTR_NORMAL2) && !(glState.vertexAttribPointersSet & ATTR_NORMAL2)) + { + GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_NORMAL2 )\n"); + + qglVertexAttribPointerARB(ATTR_INDEX_NORMAL2, 3, GL_FLOAT, 0, glState.currentVBO->stride_normal, BUFFER_OFFSET(glState.currentVBO->ofs_normal + glState.vertexAttribsOldFrame * glState.currentVBO->size_normal)); + glState.vertexAttribPointersSet |= ATTR_NORMAL2; + } + +} + +shaderProgram_t *GLSL_GetGenericShaderProgram() +{ + int shaderAttribs = 0; + + if (tess.fogNum) + { + shaderAttribs |= GLSLDEF_USE_FOG; + } + + // swapping these two out causes the worse case frame time to increase due to too many context switches + // think about doing actual checks if the sort is changed + shaderAttribs |= GLSLDEF_USE_DEFORM_VERTEXES; + shaderAttribs |= GLSLDEF_USE_TCGEN; + + if (backEnd.currentEntity && backEnd.currentEntity != &tr.worldEntity) + { + shaderAttribs |= GLSLDEF_USE_VERTEX_ANIMATION; + } + + return &tr.genericShader[shaderAttribs]; +} \ No newline at end of file diff --git a/reaction/code/renderer/tr_image.c b/reaction/code/renderer/tr_image.c index 4dd12237..7f316e94 100644 --- a/reaction/code/renderer/tr_image.c +++ b/reaction/code/renderer/tr_image.c @@ -741,16 +741,16 @@ done: if (mipmap) { - if ( textureFilterAnisotropic ) + if ( glRefConfig.textureFilterAnisotropic ) qglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, - (GLint)Com_Clamp( 1, maxAnisotropy, r_ext_max_anisotropy->integer ) ); + (GLint)Com_Clamp( 1, glRefConfig.maxAnisotropy, r_ext_max_anisotropy->integer ) ); qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min); qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max); } else { - if ( textureFilterAnisotropic ) + if ( glRefConfig.textureFilterAnisotropic ) qglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1 ); qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); diff --git a/reaction/code/renderer/tr_init.c b/reaction/code/renderer/tr_init.c index c452602e..9b719e40 100644 --- a/reaction/code/renderer/tr_init.c +++ b/reaction/code/renderer/tr_init.c @@ -24,8 +24,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "tr_local.h" glconfig_t glConfig; -qboolean textureFilterAnisotropic = qfalse; -int maxAnisotropy = 0; + +glRefConfig_t glRefConfig; + float displayAspect = 0.0f; glstate_t glState; @@ -92,6 +93,13 @@ cvar_t *r_ext_texture_env_add; cvar_t *r_ext_texture_filter_anisotropic; cvar_t *r_ext_max_anisotropy; +cvar_t *r_arb_vertex_buffer_object; +cvar_t *r_arb_shader_objects; +cvar_t *r_ext_multi_draw_arrays; + +cvar_t *r_mergeMultidraws; +cvar_t *r_mergeLeafSurfaces; + cvar_t *r_ignoreGLErrors; cvar_t *r_logFile; @@ -222,7 +230,7 @@ static void InitOpenGL( void ) GL_CheckErrors ================== */ -void GL_CheckErrors( void ) { +void GL_CheckErrs( char *file, int line ) { int err; char s[64]; @@ -257,7 +265,7 @@ void GL_CheckErrors( void ) { break; } - ri.Error( ERR_FATAL, "GL_CheckErrors: %s", s ); + ri.Error( ERR_FATAL, "GL_CheckErrors: %s in %s at line %d", s , file, line); } @@ -772,6 +780,22 @@ void GL_SetDefaultState( void ) // glState.glStateBits = GLS_DEPTHTEST_DISABLE | GLS_DEPTHMASK_TRUE; + if (glRefConfig.glsl) + { + glState.vertexAttribsState = 0; + glState.vertexAttribPointersSet = 0; + glState.currentProgram = 0; + qglUseProgramObjectARB(0); + } + + if (glRefConfig.vertexBufferObject) + { + qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); + qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); + glState.currentVBO = NULL; + glState.currentIBO = NULL; + } + qglPolygonMode (GL_FRONT_AND_BACK, GL_FILL); qglDepthMask( GL_TRUE ); qglDisable( GL_DEPTH_TEST ); @@ -891,6 +915,9 @@ void R_Register( void ) r_ext_multitexture = ri.Cvar_Get( "r_ext_multitexture", "1", CVAR_ARCHIVE | CVAR_LATCH ); r_ext_compiled_vertex_array = ri.Cvar_Get( "r_ext_compiled_vertex_array", "1", CVAR_ARCHIVE | CVAR_LATCH); r_ext_texture_env_add = ri.Cvar_Get( "r_ext_texture_env_add", "1", CVAR_ARCHIVE | CVAR_LATCH); + r_arb_vertex_buffer_object = ri.Cvar_Get( "r_arb_vertex_buffer_object", "1", CVAR_ARCHIVE | CVAR_LATCH); + r_arb_shader_objects = ri.Cvar_Get( "r_arb_shader_objects", "1", CVAR_ARCHIVE | CVAR_LATCH); + r_ext_multi_draw_arrays = ri.Cvar_Get( "r_ext_multi_draw_arrays", "1", CVAR_ARCHIVE | CVAR_LATCH); r_ext_texture_filter_anisotropic = ri.Cvar_Get( "r_ext_texture_filter_anisotropic", "0", CVAR_ARCHIVE | CVAR_LATCH ); @@ -966,6 +993,8 @@ void R_Register( void ) r_directedScale = ri.Cvar_Get( "r_directedScale", "1", CVAR_CHEAT ); r_anaglyphMode = ri.Cvar_Get("r_anaglyphMode", "0", CVAR_ARCHIVE); + r_mergeMultidraws = ri.Cvar_Get("r_mergeMultidraws", "1", CVAR_ARCHIVE); + r_mergeLeafSurfaces = ri.Cvar_Get("r_mergeLeafSurfaces", "1", CVAR_ARCHIVE); // // temporary variables that can change at any time @@ -1119,8 +1148,18 @@ void R_Init( void ) { InitOpenGL(); + if (glRefConfig.glsl) + { + GLSL_InitGPUShaders(); + } + R_InitImages(); + if (glRefConfig.vertexBufferObject) + { + R_InitVBOs(); + } + R_InitShaders(); R_InitSkins(); @@ -1161,6 +1200,14 @@ void RE_Shutdown( qboolean destroyWindow ) { R_SyncRenderThread(); R_ShutdownCommandBuffers(); R_DeleteTextures(); + if (glRefConfig.vertexBufferObject) + { + R_ShutdownVBOs(); + } + if (glRefConfig.glsl) + { + GLSL_ShutdownGPUShaders(); + } } R_DoneFreeType(); diff --git a/reaction/code/renderer/tr_light.c b/reaction/code/renderer/tr_light.c index 4cc9cf29..b7d305d8 100644 --- a/reaction/code/renderer/tr_light.c +++ b/reaction/code/renderer/tr_light.c @@ -92,7 +92,7 @@ void R_DlightBmodel( bmodel_t *bmodel ) { // set the dlight bits in all the surfaces for ( i = 0 ; i < bmodel->numSurfaces ; i++ ) { - surf = bmodel->firstSurface + i; + surf = tr.world->surfaces + bmodel->firstSurface + i; if ( *surf->data == SF_FACE ) { ((srfSurfaceFace_t *)surf->data)->dlightBits[ tr.smpFrame ] = mask; diff --git a/reaction/code/renderer/tr_local.h b/reaction/code/renderer/tr_local.h index 4a4cb8fb..759efee7 100644 --- a/reaction/code/renderer/tr_local.h +++ b/reaction/code/renderer/tr_local.h @@ -28,6 +28,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "../qcommon/qfiles.h" #include "../qcommon/qcommon.h" #include "tr_public.h" +#include "tr_extramath.h" #include "qgl.h" #define GL_INDEX_TYPE GL_UNSIGNED_INT @@ -40,6 +41,7 @@ long myftol( float f ); #define myftol(x) ((int)(x)) #endif +#define BUFFER_OFFSET(i) ((char *)NULL + (i)) // everything that is needed by the backend needs // to be double buffered to allow it to run in @@ -56,6 +58,9 @@ long myftol( float f ); // can't be increased without changing bit packing for drawsurfs +#define MAX_VISCOUNTS 5 +#define MAX_VBOS 4096 +#define MAX_IBOS 4096 typedef struct dlight_s { vec3_t origin; @@ -110,6 +115,43 @@ typedef struct image_s { struct image_s* next; } image_t; +typedef enum +{ + VBO_USAGE_STATIC, + VBO_USAGE_DYNAMIC +} vboUsage_t; + +typedef struct VBO_s +{ + char name[MAX_QPATH]; + + uint32_t vertexesVBO; + int vertexesSize; // amount of memory data allocated for all vertices in bytes + uint32_t ofs_xyz; + uint32_t ofs_normal; + uint32_t ofs_st; + uint32_t ofs_lightmap; + uint32_t ofs_vertexcolor; + uint32_t stride_xyz; + uint32_t stride_normal; + uint32_t stride_st; + uint32_t stride_lightmap; + uint32_t stride_vertexcolor; + uint32_t size_xyz; + uint32_t size_normal; + + int attribs; +} VBO_t; + +typedef struct IBO_s +{ + char name[MAX_QPATH]; + + uint32_t indexesVBO; + int indexesSize; // amount of memory data allocated for all triangles in bytes +// uint32_t ofsIndexes; +} IBO_t; + //=============================================================================== typedef enum { @@ -175,6 +217,25 @@ typedef enum { DEFORM_TEXT7 } deform_t; +// deformVertexes types that can be handled by the GPU +typedef enum +{ + // do not edit: same as genFunc_t + + DGEN_NONE, + DGEN_WAVE_SIN, + DGEN_WAVE_SQUARE, + DGEN_WAVE_TRIANGLE, + DGEN_WAVE_SAWTOOTH, + DGEN_WAVE_INVERSE_SAWTOOTH, + DGEN_WAVE_NOISE, + + // do not edit until this line + + DGEN_BULGE, + DGEN_MOVE +} deformGen_t; + typedef enum { AGEN_IDENTITY, AGEN_SKIP, @@ -200,9 +261,18 @@ typedef enum { CGEN_WAVEFORM, // programmatically generated CGEN_LIGHTING_DIFFUSE, CGEN_FOG, // standard fog - CGEN_CONST // fixed color + CGEN_CONST, // fixed color + CGEN_DLIGHT } colorGen_t; +typedef enum +{ + ATEST_NONE, + ATEST_GT_0, + ATEST_LT_128, + ATEST_GE_128 +} alphaTest_t; + typedef enum { TCGEN_BAD, TCGEN_IDENTITY, // clear to 0,0 @@ -210,7 +280,8 @@ typedef enum { TCGEN_TEXTURE, TCGEN_ENVIRONMENT_MAPPED, TCGEN_FOG, - TCGEN_VECTOR // S and T from world coordinates + TCGEN_VECTOR, // S and T from world coordinates + TCGEN_DLIGHT } texCoordGen_t; typedef enum { @@ -381,6 +452,7 @@ typedef struct shader_s { fogParms_t fogParms; float portalRange; // distance to fog out at + qboolean isPortal; int multitextureEnv; // 0, GL_MODULATE, GL_ADD (FIXME: put in stage) @@ -391,10 +463,7 @@ typedef struct shader_s { fogPass_t fogPass; // draw a blended pass, possibly with depth test equals - qboolean needsNormal; // not all shaders will need all data to be gathered - qboolean needsST1; - qboolean needsST2; - qboolean needsColor; + int vertexAttribs; // not all shaders will need all data to be gathered int numDeforms; deformStage_t deforms[MAX_SHADER_DEFORMS]; @@ -420,6 +489,29 @@ typedef struct shader_s { struct shader_s *next; } shader_t; +static ID_INLINE qboolean ShaderRequiresCPUDeforms(const shader_t * shader) +{ + if(shader->numDeforms) + { + const deformStage_t *ds = &shader->deforms[0]; + + if (shader->numDeforms > 1) + return qtrue; + + switch (ds->deformation) + { + case DEFORM_WAVE: + case DEFORM_BULGE: + return qfalse; + + default: + return qtrue; + } + } + + return qfalse; +} + typedef struct shaderState_s { char shaderName[MAX_QPATH]; // name of shader this state belongs to char name[MAX_STATE_NAME]; // name of this state @@ -428,6 +520,1000 @@ typedef struct shaderState_s { shader_t *shader; } shaderState_t; +enum +{ + ATTR_INDEX_POSITION = 0, + ATTR_INDEX_TEXCOORD0 = 1, + ATTR_INDEX_TEXCOORD1 = 2, + ATTR_INDEX_TANGENT = 3, + ATTR_INDEX_BINORMAL = 4, + ATTR_INDEX_NORMAL = 5, + ATTR_INDEX_COLOR = 6, + ATTR_INDEX_PAINTCOLOR = 7, + ATTR_INDEX_LIGHTDIRECTION = 8, + ATTR_INDEX_BONE_INDEXES = 9, + ATTR_INDEX_BONE_WEIGHTS = 10, + + // GPU vertex animations + ATTR_INDEX_POSITION2 = 11, + ATTR_INDEX_TANGENT2 = 12, + ATTR_INDEX_BINORMAL2 = 13, + ATTR_INDEX_NORMAL2 = 14 +}; + +enum +{ + GLS_SRCBLEND_ZERO = (1 << 0), + GLS_SRCBLEND_ONE = (1 << 1), + GLS_SRCBLEND_DST_COLOR = (1 << 2), + GLS_SRCBLEND_ONE_MINUS_DST_COLOR = (1 << 3), + GLS_SRCBLEND_SRC_ALPHA = (1 << 4), + GLS_SRCBLEND_ONE_MINUS_SRC_ALPHA = (1 << 5), + GLS_SRCBLEND_DST_ALPHA = (1 << 6), + GLS_SRCBLEND_ONE_MINUS_DST_ALPHA = (1 << 7), + GLS_SRCBLEND_ALPHA_SATURATE = (1 << 8), + + GLS_SRCBLEND_BITS = GLS_SRCBLEND_ZERO + | GLS_SRCBLEND_ONE + | GLS_SRCBLEND_DST_COLOR + | GLS_SRCBLEND_ONE_MINUS_DST_COLOR + | GLS_SRCBLEND_SRC_ALPHA + | GLS_SRCBLEND_ONE_MINUS_SRC_ALPHA + | GLS_SRCBLEND_DST_ALPHA + | GLS_SRCBLEND_ONE_MINUS_DST_ALPHA + | GLS_SRCBLEND_ALPHA_SATURATE, + + GLS_DSTBLEND_ZERO = (1 << 9), + GLS_DSTBLEND_ONE = (1 << 10), + GLS_DSTBLEND_SRC_COLOR = (1 << 11), + GLS_DSTBLEND_ONE_MINUS_SRC_COLOR = (1 << 12), + GLS_DSTBLEND_SRC_ALPHA = (1 << 13), + GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA = (1 << 14), + GLS_DSTBLEND_DST_ALPHA = (1 << 15), + GLS_DSTBLEND_ONE_MINUS_DST_ALPHA = (1 << 16), + + GLS_DSTBLEND_BITS = GLS_DSTBLEND_ZERO + | GLS_DSTBLEND_ONE + | GLS_DSTBLEND_SRC_COLOR + | GLS_DSTBLEND_ONE_MINUS_SRC_COLOR + | GLS_DSTBLEND_SRC_ALPHA + | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA + | GLS_DSTBLEND_DST_ALPHA + | GLS_DSTBLEND_ONE_MINUS_DST_ALPHA, + + GLS_DEPTHMASK_TRUE = (1 << 17), + + GLS_POLYMODE_LINE = (1 << 18), + + GLS_DEPTHTEST_DISABLE = (1 << 19), + + GLS_DEPTHFUNC_LESS = (1 << 20), + GLS_DEPTHFUNC_EQUAL = (1 << 21), + + GLS_DEPTHFUNC_BITS = GLS_DEPTHFUNC_LESS + | GLS_DEPTHFUNC_EQUAL, + + GLS_ATEST_GT_0 = (1 << 22), + GLS_ATEST_LT_128 = (1 << 23), + GLS_ATEST_GE_128 = (1 << 24), +// GLS_ATEST_GE_CUSTOM = (1 << 25), + + GLS_ATEST_BITS = GLS_ATEST_GT_0 + | GLS_ATEST_LT_128 + | GLS_ATEST_GE_128, +// | GLS_ATEST_GT_CUSTOM, + + GLS_REDMASK_FALSE = (1 << 26), + GLS_GREENMASK_FALSE = (1 << 27), + GLS_BLUEMASK_FALSE = (1 << 28), + GLS_ALPHAMASK_FALSE = (1 << 29), + + GLS_COLORMASK_BITS = GLS_REDMASK_FALSE + | GLS_GREENMASK_FALSE + | GLS_BLUEMASK_FALSE + | GLS_ALPHAMASK_FALSE, + + GLS_STENCILTEST_ENABLE = (1 << 30), + + GLS_DEFAULT = GLS_DEPTHMASK_TRUE +}; + +enum +{ + ATTR_POSITION = 0x0001, + ATTR_TEXCOORD = 0x0002, + ATTR_LIGHTCOORD = 0x0004, + ATTR_TANGENT = 0x0008, + ATTR_BINORMAL = 0x0010, + ATTR_NORMAL = 0x0020, + ATTR_COLOR = 0x0040, + ATTR_PAINTCOLOR = 0x0080, + ATTR_LIGHTDIRECTION = 0x0100, + ATTR_BONE_INDEXES = 0x0200, + ATTR_BONE_WEIGHTS = 0x0400, + + // for .md3 interpolation + ATTR_POSITION2 = 0x0800, + ATTR_TANGENT2 = 0x1000, + ATTR_BINORMAL2 = 0x2000, + ATTR_NORMAL2 = 0x4000, + + ATTR_DEFAULT = ATTR_POSITION, + ATTR_BITS = ATTR_POSITION | + ATTR_TEXCOORD | + ATTR_LIGHTCOORD | + ATTR_TANGENT | + ATTR_BINORMAL | + ATTR_NORMAL | + ATTR_COLOR | + ATTR_PAINTCOLOR | + ATTR_LIGHTDIRECTION | + ATTR_BONE_INDEXES | + ATTR_BONE_WEIGHTS | + ATTR_POSITION2 | + ATTR_TANGENT2 | + ATTR_BINORMAL2 | + ATTR_NORMAL2 +}; + +enum +{ + GLSLDEF_USE_DEFORM_VERTEXES = 0x0001, + GLSLDEF_USE_TCGEN = 0x0002, + GLSLDEF_USE_VERTEX_ANIMATION = 0x0004, + GLSLDEF_USE_FOG = 0x0008, + GLSLDEF_ALL = 0x000F, + GLSLDEF_COUNT = 0x0010, +}; + + +// Tr3B - shaderProgram_t represents a pair of one +// GLSL vertex and one GLSL fragment shader +typedef struct shaderProgram_s +{ + char name[MAX_QPATH]; + + GLhandleARB program; + GLhandleARB vertexShader; + GLhandleARB fragmentShader; + uint32_t attribs; // vertex array attributes + + // uniform parameters + GLint u_Texture0Map; + GLint u_Texture1Map; + GLint u_Texture2Map; + GLint u_Texture3Map; + + GLint u_PortalRange; + float t_PortalRange; + + GLint u_FogAdjustColors; + int t_FogAdjustColors; + + GLint u_FogDistance; + vec4_t t_FogDistance; + + GLint u_FogDepth; + vec4_t t_FogDepth; + + GLint u_FogEyeT; + float t_FogEyeT; + + GLint u_ModelMatrix; // model -> world + matrix_t t_ModelMatrix; + + GLint u_ModelViewProjectionMatrix; + matrix_t t_ModelViewProjectionMatrix; + + GLint u_AlphaTest; + alphaTest_t t_AlphaTest; + + GLint u_ColorGen; + colorGen_t t_ColorGen; + + GLint u_AlphaGen; + alphaGen_t t_AlphaGen; + + GLint u_TCGen0; + texCoordGen_t t_TCGen0; + + GLint u_TCGen1; + texCoordGen_t t_TCGen1; + + GLint u_TCGen0Vector0; + vec4_t t_TCGen0Vector0; + + GLint u_TCGen0Vector1; + vec4_t t_TCGen0Vector1; + + GLint u_TCGen1Vector0; + vec4_t t_TCGen1Vector0; + + GLint u_TCGen1Vector1; + vec4_t t_TCGen1Vector1; + + GLint u_DeformGen; + deformGen_t t_DeformGen; + + GLint u_DeformWave; + vec4_t t_DeformWave; + + GLint u_DeformBulge; + vec3_t t_DeformBulge; + + GLint u_DeformSpread; + float t_DeformSpread; + + GLint u_Time; + float t_Time; + + GLint u_Color; + vec4_t t_Color; + + GLint u_AmbientLight; + vec3_t t_AmbientLight; + + GLint u_DirectedLight; + vec3_t t_DirectedLight; + + GLint u_LightDir; + vec3_t t_LightDir; + + GLint u_ViewOrigin; + vec4_t t_ViewOrigin; + + GLint u_Texture0Matrix; + matrix_t t_Texture0Matrix; + + GLint u_Texture1Matrix; + matrix_t t_Texture1Matrix; + + GLint u_Texture1Env; + uint32_t t_Texture1Env; + + GLint u_Texture2Matrix; + matrix_t t_Texture2Matrix; + + GLint u_Texture3Matrix; + matrix_t t_Texture3Matrix; + + GLint u_VertexLerp; + float t_VertexLerp; + +} shaderProgram_t; + + +// +// Tr3B: these are fire wall functions to avoid expensive redundant glUniform* calls +#define USE_UNIFORM_FIREWALL 1 +//#define LOG_GLSL_UNIFORMS 1 + +#if defined(LOG_GLSL_UNIFORMS) +extern cvar_t *r_logFile; // number of frames to emit GL logs +void GLimp_LogComment(char *comment); +#define GLimp_LogUniformComment(...) GLimp_LogComment(__VA_ARGS__) +#else +#define GLimp_LogUniformComment(...) +#endif + +// *INDENT-OFF* + +static ID_INLINE void GLSL_SetUniform_Texture0Matrix(shaderProgram_t * program, const matrix_t m) +{ + if (program->u_Texture0Matrix == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(Matrix16Compare(program->t_Texture0Matrix, m)) + return; + + Matrix16Copy(m, program->t_Texture0Matrix); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_Texture0Matrix( program = %s, " + "matrix = \n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f ) ) ---\n", + program->name, + m[0], m[4], m[8], m[12], + m[1], m[5], m[9], m[13], + m[2], m[6], m[10], m[14], + m[3], m[7], m[11], m[15])); + + qglUniformMatrix4fvARB(program->u_Texture0Matrix, 1, GL_FALSE, m); +} + +static ID_INLINE void GLSL_SetUniform_Texture1Matrix(shaderProgram_t * program, const matrix_t m) +{ + if (program->u_Texture1Matrix == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(Matrix16Compare(program->t_Texture1Matrix, m)) + return; + + Matrix16Copy(m, program->t_Texture1Matrix); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_Texture1Matrix( program = %s, " + "matrix = \n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f ) ) ---\n", + program->name, + m[0], m[4], m[8], m[12], + m[1], m[5], m[9], m[13], + m[2], m[6], m[10], m[14], + m[3], m[7], m[11], m[15])); + + qglUniformMatrix4fvARB(program->u_Texture1Matrix, 1, GL_FALSE, m); +} + +static ID_INLINE void GLSL_SetUniform_Texture1Env(shaderProgram_t * program, uint32_t env) +{ + uint32_t value; + + if (program->u_Texture1Env == -1) + return; + + switch ( env ) + { + case GL_MODULATE: + value = 1; + break; + case GL_REPLACE: + value = 2; + break; + case GL_DECAL: + value = 3; + break; + case GL_ADD: + value = 4; + break; + default: + value = 0; + break; + } + +#if defined(USE_UNIFORM_FIREWALL) + if(program->t_Texture1Env == value) + return; + + program->t_Texture1Env = value; +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_Texture1Env( program = %s, value = %i ) ---\n", program->name, value)); + + qglUniform1iARB(program->u_Texture1Env, value); +} + +static ID_INLINE void GLSL_SetUniform_Texture2Matrix(shaderProgram_t * program, const matrix_t m) +{ + if (program->u_Texture2Matrix == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(Matrix16Compare(program->t_Texture2Matrix, m)) + return; + + Matrix16Copy(m, program->t_Texture2Matrix); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_Texture2Matrix( program = %s, " + "matrix = \n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f ) ) ---\n", + program->name, + m[0], m[4], m[8], m[12], + m[1], m[5], m[9], m[13], + m[2], m[6], m[10], m[14], + m[3], m[7], m[11], m[15])); + + qglUniformMatrix4fvARB(program->u_Texture2Matrix, 1, GL_FALSE, m); +} + +static ID_INLINE void GLSL_SetUniform_Texture3Matrix(shaderProgram_t * program, const matrix_t m) +{ + if (program->u_Texture3Matrix == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(Matrix16Compare(program->t_Texture3Matrix, m)) + return; + + Matrix16Copy(m, program->t_Texture3Matrix); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_Texture3Matrix( program = %s, " + "matrix = \n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f ) ) ---\n", + program->name, + m[0], m[4], m[8], m[12], + m[1], m[5], m[9], m[13], + m[2], m[6], m[10], m[14], + m[3], m[7], m[11], m[15])); + + qglUniformMatrix4fvARB(program->u_Texture3Matrix, 1, GL_FALSE, m); +} + + +static ID_INLINE void GLSL_SetUniform_AlphaTest(shaderProgram_t * program, uint32_t stateBits) +{ + alphaTest_t value; + + if (program->u_AlphaTest == -1) + return; + + switch (stateBits & GLS_ATEST_BITS) + { + case GLS_ATEST_GT_0: + value = ATEST_GT_0; + break; + + case GLS_ATEST_LT_128: + value = ATEST_LT_128; + break; + + case GLS_ATEST_GE_128: + value = ATEST_GE_128; + break; + + default: + value = ATEST_NONE; + break; + } + + // don't just call LogComment, or we will get + // a call to va() every frame! + GLimp_LogUniformComment(va("--- GLSL_SetUniformAlphaTest( program = %s, value = %i ) ---\n", program->name, value)); + +#if defined(USE_UNIFORM_FIREWALL) + if(program->t_AlphaTest == value) + return; + + program->t_AlphaTest = value; +#endif + + qglUniform1iARB(program->u_AlphaTest, value); +} + +static ID_INLINE void GLSL_SetUniform_ViewOrigin(shaderProgram_t * program, const vec3_t v) +{ + if (program->u_ViewOrigin == -1) + return; + + +#if defined(USE_UNIFORM_FIREWALL) + if(VectorCompare(program->t_ViewOrigin, v)) + return; + + VectorCopy(v, program->t_ViewOrigin); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_ViewOrigin( program = %s, viewOrigin = ( %5.3f, %5.3f, %5.3f ) ) ---\n", program->name, v[0], v[1], v[2])); + + qglUniform3fARB(program->u_ViewOrigin, v[0], v[1], v[2]); +} + +static ID_INLINE void GLSL_SetUniform_TCGen0(shaderProgram_t * program, texCoordGen_t value) +{ + if (program->u_TCGen0 == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(program->t_TCGen0 == value) + return; + + program->t_TCGen0 = value; +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_TCGen( program = %s, value = %i ) ---\n", program->name, value)); + + qglUniform1iARB(program->u_TCGen0, value); +} + +static ID_INLINE void GLSL_SetUniform_TCGen1(shaderProgram_t * program, texCoordGen_t value) +{ + if (program->u_TCGen1 == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(program->t_TCGen1 == value) + return; + + program->t_TCGen1 = value; +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_TCGen( program = %s, value = %i ) ---\n", program->name, value)); + + qglUniform1iARB(program->u_TCGen1, value); +} + +static ID_INLINE void GLSL_SetUniform_TCGen0Vector0(shaderProgram_t * program, const vec4_t v) +{ + if (program->u_TCGen0Vector0 == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(VectorCompare(program->t_TCGen0Vector0, v)) + return; + + VectorCopy(v, program->t_TCGen0Vector0); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_TCGen0Vector0( program = %s, color = ( %5.3f, %5.3f, %5.3f, %5.3f ) ) ---\n", program->name, v[0], v[1], v[2], v[3])); + + qglUniform4fARB(program->u_TCGen0Vector0, v[0], v[1], v[2], v[3]); +} + +static ID_INLINE void GLSL_SetUniform_TCGen0Vector1(shaderProgram_t * program, const vec4_t v) +{ + if (program->u_TCGen0Vector1 == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(VectorCompare(program->t_TCGen0Vector1, v)) + return; + + VectorCopy(v, program->t_TCGen0Vector1); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_TCGen0Vector1( program = %s, color = ( %5.3f, %5.3f, %5.3f, %5.3f ) ) ---\n", program->name, v[0], v[1], v[2], v[3])); + + qglUniform4fARB(program->u_TCGen0Vector1, v[0], v[1], v[2], v[3]); +} +static ID_INLINE void GLSL_SetUniform_TCGen1Vector0(shaderProgram_t * program, const vec4_t v) +{ + if (program->u_TCGen1Vector0 == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(VectorCompare(program->t_TCGen1Vector0, v)) + return; + + VectorCopy(v, program->t_TCGen1Vector0); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_TCGen1Vector0( program = %s, color = ( %5.3f, %5.3f, %5.3f, %5.3f ) ) ---\n", program->name, v[0], v[1], v[2], v[3])); + + qglUniform4fARB(program->u_TCGen1Vector0, v[0], v[1], v[2], v[3]); +} +static ID_INLINE void GLSL_SetUniform_TCGen1Vector1(shaderProgram_t * program, const vec4_t v) +{ + if (program->u_TCGen1Vector1 == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(VectorCompare(program->t_TCGen1Vector1, v)) + return; + + VectorCopy(v, program->t_TCGen1Vector1); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_TCGen1Vector1( program = %s, color = ( %5.3f, %5.3f, %5.3f, %5.3f ) ) ---\n", program->name, v[0], v[1], v[2], v[3])); + + qglUniform4fARB(program->u_TCGen1Vector1, v[0], v[1], v[2], v[3]); +} + +static ID_INLINE void GLSL_SetUniform_DeformGen(shaderProgram_t * program, deformGen_t value) +{ + if (program->u_DeformGen == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(program->t_DeformGen == value) + return; + + program->t_DeformGen = value; +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_DeformGen( program = %s, value = %i ) ---\n", program->name, value)); + + qglUniform1iARB(program->u_DeformGen, value); +} + +static ID_INLINE void GLSL_SetUniform_DeformWave(shaderProgram_t * program, const waveForm_t * wf) +{ + vec4_t v; + + if (program->u_DeformWave == -1) + return; + + VectorSet4(v, wf->base, wf->amplitude, wf->phase, wf->frequency); + +#if defined(USE_UNIFORM_FIREWALL) + if(VectorCompare4(program->t_DeformWave, v)) + return; + + VectorCopy4(v, program->t_DeformWave); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_DeformWave( program = %s, wave form = ( %5.3f, %5.3f, %5.3f, %5.3f ) ) ---\n", program->name, v[0], v[1], v[2], v[3])); + + qglUniform4fARB(program->u_DeformWave, v[0], v[1], v[2], v[3]); +} + +static ID_INLINE void GLSL_SetUniform_DeformBulge(shaderProgram_t * program, const deformStage_t * ds) +{ + vec3_t v; + + if (program->u_DeformBulge == -1) + return; + + VectorSet(v, ds->bulgeWidth, ds->bulgeHeight, ds->bulgeSpeed); + +#if defined(USE_UNIFORM_FIREWALL) + if(VectorCompare(program->t_DeformBulge, v)) + return; + + VectorCopy(v, program->t_DeformBulge); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_DeformBulge( program = %s, bulge = ( %5.3f, %5.3f, %5.3f ) ) ---\n", program->name, v[0], v[1], v[2])); + + qglUniform3fARB(program->u_DeformBulge, v[0], v[1], v[2]); +} + +static ID_INLINE void GLSL_SetUniform_DeformSpread(shaderProgram_t * program, float value) +{ + if (program->u_DeformSpread == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(program->t_DeformSpread == value) + return; + + program->t_DeformSpread = value; +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_DeformSpread( program = %s, value = %f ) ---\n", program->name, value)); + + qglUniform1fARB(program->u_DeformSpread, value); +} + +static ID_INLINE void GLSL_SetUniform_ColorGen(shaderProgram_t * program, colorGen_t value) +{ +#if 0 + float floatValue; + + switch (value) + { + case CGEN_VERTEX: + floatValue = 1.0f; + break; + + case CGEN_ONE_MINUS_VERTEX: + floatValue = -1.0f; + break; + + default: + floatValue = 0.0f; + break; + } + +#if defined(USE_UNIFORM_FIREWALL) + if(program->t_ColorGen == floatValue) + return; + + program->t_ColorGen = floatValue; +#endif + + qglUniform1fARB(program->u_ColorGen, floatValue); +#else + if (program->u_ColorGen == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(program->t_ColorGen == value) + return; + + program->t_ColorGen = value; +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_ColorGen( program = %s, value = %i ) ---\n", program->name, value)); + + qglUniform1iARB(program->u_ColorGen, value); +#endif +} + +static ID_INLINE void GLSL_SetUniform_AlphaGen(shaderProgram_t * program, alphaGen_t value) +{ +#if 0 + float floatValue; + + switch (value) + { + case AGEN_VERTEX: + floatValue = 1.0f; + break; + + case AGEN_ONE_MINUS_VERTEX: + floatValue = -1.0f; + break; + + default: + floatValue = 0.0f; + break; + } + +#if defined(USE_UNIFORM_FIREWALL) + if(program->t_AlphaGen == floatValue) + return; + + program->t_AlphaGen = floatValue; +#endif + + qglUniform1fARB(program->u_AlphaGen, floatValue); + +#else +#if defined(USE_UNIFORM_FIREWALL) + if (program->u_AlphaGen == -1) + return; + + if(program->t_AlphaGen == value) + return; + + program->t_AlphaGen = value; +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_AlphaGen( program = %s, value = %i ) ---\n", program->name, value)); + + qglUniform1iARB(program->u_AlphaGen, value); +#endif +} + +static ID_INLINE void GLSL_SetUniform_Color(shaderProgram_t * program, const vec4_t v) +{ + if (program->u_Color == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(VectorCompare4(program->t_Color, v)) + return; + + VectorCopy4(v, program->t_Color); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_Color( program = %s, color = ( %5.3f, %5.3f, %5.3f, %5.3f ) ) ---\n", program->name, v[0], v[1], v[2], v[3])); + + qglUniform4fARB(program->u_Color, v[0], v[1], v[2], v[3]); +} + + +static ID_INLINE void GLSL_SetUniform_AmbientLight(shaderProgram_t * program, const vec3_t v) +{ + if (program->u_AmbientLight == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(VectorCompare(program->t_AmbientLight, v)) + return; + + VectorCopy(v, program->t_AmbientLight); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_AmbientLight( program = %s, color = ( %5.3f, %5.3f, %5.3f ) ) ---\n", program->name, v[0], v[1], v[2])); + + qglUniform3fARB(program->u_AmbientLight, v[0], v[1], v[2]); +} + +static ID_INLINE void GLSL_SetUniform_DirectedLight(shaderProgram_t * program, const vec3_t v) +{ + if (program->u_DirectedLight == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(VectorCompare(program->t_DirectedLight, v)) + return; + + VectorCopy(v, program->t_DirectedLight); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_DirectedLight( program = %s, color = ( %5.3f, %5.3f, %5.3f ) ) ---\n", program->name, v[0], v[1], v[2])); + + qglUniform3fARB(program->u_DirectedLight, v[0], v[1], v[2]); +} + +static ID_INLINE void GLSL_SetUniform_LightDir(shaderProgram_t * program, const vec3_t v) +{ + if (program->u_LightDir == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(VectorCompare(program->t_LightDir, v)) + return; + + VectorCopy(v, program->t_LightDir); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_LightDir( program = %s, color = ( %5.3f, %5.3f, %5.3f ) ) ---\n", program->name, v[0], v[1], v[2])); + + qglUniform3fARB(program->u_LightDir, v[0], v[1], v[2]); +} + +static ID_INLINE void GLSL_SetUniform_PortalRange(shaderProgram_t * program, float value) +{ + if (program->u_PortalRange == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(program->t_PortalRange == value) + return; + + program->t_PortalRange = value; +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_PortalRange( program = %s, value = %f ) ---\n", program->name, value)); + + qglUniform1fARB(program->u_PortalRange, value); +} + +static ID_INLINE void GLSL_SetUniform_FogDistance(shaderProgram_t * program, const vec4_t v) +{ + if (program->u_FogDistance == -1) + return; + + +#if defined(USE_UNIFORM_FIREWALL) + if(VectorCompare4(program->t_FogDistance, v)) + return; + + VectorCopy(v, program->t_FogDistance); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniformFogDistance( program = %s, viewOrigin = ( %5.3f, %5.3f, %5.3f, %5.3f ) ) ---\n", program->name, v[0], v[1], v[2], v[3])); + + qglUniform4fARB(program->u_FogDistance, v[0], v[1], v[2], v[3]); +} + +static ID_INLINE void GLSL_SetUniform_FogDepth(shaderProgram_t * program, const vec4_t v) +{ + if (program->u_FogDepth == -1) + return; + + +#if defined(USE_UNIFORM_FIREWALL) + if(VectorCompare4(program->t_FogDepth, v)) + return; + + VectorCopy(v, program->t_FogDepth); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniformFogDepth( program = %s, viewOrigin = ( %5.3f, %5.3f, %5.3f, %5.3f ) ) ---\n", program->name, v[0], v[1], v[2], v[3])); + + qglUniform4fARB(program->u_FogDepth, v[0], v[1], v[2], v[3]); +} + +static ID_INLINE void GLSL_SetUniform_FogEyeT(shaderProgram_t * program, float value) +{ + if (program->u_FogEyeT == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(program->t_FogEyeT == value) + return; + + program->t_FogEyeT = value; +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_FogEyeT( program = %s, value = %f ) ---\n", program->name, value)); + + qglUniform1fARB(program->u_FogEyeT, value); +} + +static ID_INLINE void GLSL_SetUniform_FogAdjustColors(shaderProgram_t * program, int value) +{ + if (program->u_FogAdjustColors == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(program->t_FogAdjustColors == value) + return; + + program->t_FogAdjustColors = value; +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_FogAdjustColors( program = %s, value = %i ) ---\n", program->name, value)); + + qglUniform1iARB(program->u_FogAdjustColors, value); +} + +static ID_INLINE void GLSL_SetUniform_ModelMatrix(shaderProgram_t * program, const matrix_t m) +{ + if (program->u_ModelMatrix == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(Matrix16Compare(program->t_ModelMatrix, m)) + return; + + Matrix16Copy(m, program->t_ModelMatrix); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_ModelMatrix( program = %s, " + "matrix = \n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f ) ) ---\n", + program->name, + m[0], m[4], m[8], m[12], + m[1], m[5], m[9], m[13], + m[2], m[6], m[10], m[14], + m[3], m[7], m[11], m[15])); + + qglUniformMatrix4fvARB(program->u_ModelMatrix, 1, GL_FALSE, m); +} + +static ID_INLINE void GLSL_SetUniform_ModelViewProjectionMatrix(shaderProgram_t * program, const matrix_t m) +{ + if (program->u_ModelViewProjectionMatrix == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(Matrix16Compare(program->t_ModelViewProjectionMatrix, m)) + return; + + Matrix16Copy(m, program->t_ModelViewProjectionMatrix); +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_ModelViewProjectionMatrix( program = %s, " + "matrix = \n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f )\n" + "( %5.3f, %5.3f, %5.3f, %5.3f ) ) ---\n", + program->name, + m[0], m[4], m[8], m[12], + m[1], m[5], m[9], m[13], + m[2], m[6], m[10], m[14], + m[3], m[7], m[11], m[15])); + + qglUniformMatrix4fvARB(program->u_ModelViewProjectionMatrix, 1, GL_FALSE, m); +} + +static ID_INLINE void GLSL_SetUniform_Time(shaderProgram_t * program, float value) +{ + if (program->u_Time == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(program->t_Time == value) + return; + + program->t_Time = value; +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_Time( program = %s, value = %f ) ---\n", program->name, value)); + + qglUniform1fARB(program->u_Time, value); +} + +static ID_INLINE void GLSL_SetUniform_VertexLerp(shaderProgram_t * program, float value) +{ + if (program->u_VertexLerp == -1) + return; + +#if defined(USE_UNIFORM_FIREWALL) + if(program->t_VertexLerp == value) + return; + + program->t_VertexLerp = value; +#endif + + GLimp_LogUniformComment(va("--- GLSL_SetUniform_VertexLerp( program = %s, value = %f ) ---\n", program->name, value)); + + qglUniform1fARB(program->u_VertexLerp, value); +} + // trRefdef_t holds everything that comes in refdef_t, // as well as the locally generated scene information @@ -521,6 +1607,7 @@ SURFACES ============================================================================== */ +typedef byte color4ub_t[4]; // any changes in surfaceType must be mirrored in rb_surfaceTable[] typedef enum { @@ -530,7 +1617,7 @@ typedef enum { SF_GRID, SF_TRIANGLES, SF_POLY, - SF_MD3, + SF_MDV, SF_MD4, #ifdef RAVENMD4 SF_MDR, @@ -538,6 +1625,8 @@ typedef enum { SF_FLARE, SF_ENTITY, // beams, rails, lightning, etc that can be determined by entity SF_DISPLAY_LIST, + SF_VBO_MESH, + SF_VBO_MDVMESH, SF_NUM_SURFACE_TYPES, SF_MAX = 0x7fffffff // ensures that sizeof( surfaceType_t ) == sizeof( int ) @@ -576,8 +1665,34 @@ typedef struct srfFlare_s { vec3_t color; } srfFlare_t; -typedef struct srfGridMesh_s { - surfaceType_t surfaceType; +typedef struct +{ + vec3_t xyz; + vec2_t st; + vec2_t lightmap; + vec3_t normal; + color4ub_t vertexColors; + +#if DEBUG_OPTIMIZEVERTICES + unsigned int id; +#endif +} srfVert_t; + +#define srfVert_t_cleared(x) srfVert_t (x) = {{0, 0, 0}, {0, 0}, {0, 0}, {0, 0, 0}, {0, 0, 0, 0}} + +typedef struct +{ + int indexes[3]; + int neighbors[3]; + vec4_t plane; + qboolean facingLight; + qboolean degenerated; +} srfTriangle_t; + + +typedef struct srfGridMesh_s +{ + surfaceType_t surfaceType; // dynamic lighting information int dlightBits[SMP_FRAMES]; @@ -599,49 +1714,118 @@ typedef struct srfGridMesh_s { int width, height; float *widthLodError; float *heightLodError; - drawVert_t verts[1]; // variable sized + + int numTriangles; + srfTriangle_t *triangles; + + int numVerts; + srfVert_t *verts; + + // BSP VBO offsets + int firstVert; + int firstIndex; + + // static render data + VBO_t *vbo; // points to bsp model VBO + IBO_t *ibo; } srfGridMesh_t; - -#define VERTEXSIZE 8 -typedef struct { - surfaceType_t surfaceType; - cplane_t plane; +typedef struct +{ + surfaceType_t surfaceType; // dynamic lighting information int dlightBits[SMP_FRAMES]; - // triangle definitions (no normals at points) - int numPoints; - int numIndices; - int ofsIndices; - float points[1][VERTEXSIZE]; // variable sized - // there is a variable length list of indices here also + // culling information + cplane_t plane; + vec3_t bounds[2]; + + // triangle definitions + int numTriangles; + srfTriangle_t *triangles; + + int numVerts; + srfVert_t *verts; + + // BSP VBO offsets + int firstVert; + int firstIndex; + + // static render data + VBO_t *vbo; // points to bsp model VBO + IBO_t *ibo; } srfSurfaceFace_t; -// misc_models in maps are turned into direct geometry by q3map -typedef struct { - surfaceType_t surfaceType; +// misc_models in maps are turned into direct geometry by xmap +typedef struct +{ + surfaceType_t surfaceType; // dynamic lighting information - int dlightBits[SMP_FRAMES]; + int dlightBits[SMP_FRAMES]; - // culling information (FIXME: use this!) - vec3_t bounds[2]; - vec3_t localOrigin; - float radius; + // culling information + vec3_t bounds[2]; // triangle definitions - int numIndexes; - int *indexes; + int numTriangles; + srfTriangle_t *triangles; - int numVerts; - drawVert_t *verts; + int numVerts; + srfVert_t *verts; + + // BSP VBO offsets + int firstVert; + int firstIndex; + + // static render data + VBO_t *vbo; // points to bsp model VBO + IBO_t *ibo; } srfTriangles_t; +typedef struct srfVBOMesh_s +{ + surfaceType_t surfaceType; + + struct shader_s *shader; // FIXME move this to somewhere else + int fogIndex; + + // dynamic lighting information + int dlightBits[SMP_FRAMES]; + + // culling information + vec3_t bounds[2]; + + // backEnd stats + int numIndexes; + int numVerts; + int firstIndex; + + // static render data + VBO_t *vbo; + IBO_t *ibo; +} srfVBOMesh_t; + +typedef struct srfVBOMDVMesh_s +{ + surfaceType_t surfaceType; + + struct mdvModel_s *mdvModel; + struct mdvSurface_s *mdvSurface; + + // backEnd stats + int numIndexes; + int numVerts; + + // static render data + VBO_t *vbo; + IBO_t *ibo; +} srfVBOMDVMesh_t; + extern void (*rb_surfaceTable[SF_NUM_SURFACE_TYPES])(void *); /* @@ -661,21 +1845,34 @@ BRUSH MODELS #define SIDE_BACK 1 #define SIDE_ON 2 +#define CULLINFO_NONE 0 +#define CULLINFO_BOX 1 +#define CULLINFO_SPHERE 2 +#define CULLINFO_PLANE 4 + +typedef struct cullinfo_s { + int type; + vec3_t bounds[2]; + vec3_t localOrigin; + float radius; + cplane_t plane; +} cullinfo_t; + typedef struct msurface_s { - int viewCount; // if == tr.viewCount, already added + //int viewCount; // if == tr.viewCount, already added struct shader_s *shader; int fogIndex; + cullinfo_t cullinfo; surfaceType_t *data; // any of srf*_t } msurface_t; - #define CONTENTS_NODE -1 typedef struct mnode_s { // common with leaf and node int contents; // -1 for nodes, to differentiate from leafs - int visframe; // node needs to be traversed if current + int visCounts[MAX_VISCOUNTS]; // node needs to be traversed if current vec3_t mins, maxs; // for bounding box culling struct mnode_s *parent; @@ -687,13 +1884,13 @@ typedef struct mnode_s { int cluster; int area; - msurface_t **firstmarksurface; + int firstmarksurface; int nummarksurfaces; } mnode_t; typedef struct { vec3_t bounds[2]; // for culling - msurface_t *firstSurface; + int firstSurface; int numSurfaces; } bmodel_t; @@ -706,6 +1903,7 @@ typedef struct { int numShaders; dshader_t *shaders; + int numBModels; bmodel_t *bmodels; int numplanes; @@ -715,11 +1913,33 @@ typedef struct { int numDecisionNodes; mnode_t *nodes; + int numVerts; + srfVert_t *verts; + int redundantVertsCalculationNeeded; + int *redundantLightVerts; // util to optimize IBOs + int *redundantShadowVerts; + int *redundantShadowAlphaTestVerts; + VBO_t *vbo; + IBO_t *ibo; + + int numTriangles; + srfTriangle_t *triangles; + + int numWorldSurfaces; + int numsurfaces; msurface_t *surfaces; + int *surfacesViewCount; + int *surfacesDlightBits; + + int numMergedSurfaces; + msurface_t *mergedSurfaces; + int *mergedSurfacesViewCount; + int *mergedSurfacesDlightBits; int nummarksurfaces; - msurface_t **marksurfaces; + int *marksurfaces; + int *viewSurfaces; int numfogs; fog_t *fogs; @@ -741,6 +1961,79 @@ typedef struct { char *entityParsePoint; } world_t; + +/* +============================================================================== +MDV MODELS - meta format for vertex animation models like .md2, .md3, .mdc +============================================================================== +*/ +typedef struct +{ + float bounds[2][3]; + float localOrigin[3]; + float radius; +} mdvFrame_t; + +typedef struct +{ + float origin[3]; + float axis[3][3]; +} mdvTag_t; + +typedef struct +{ + char name[MAX_QPATH]; // tag name +} mdvTagName_t; + +typedef struct +{ + vec3_t xyz; + vec3_t normal; +} mdvVertex_t; + +typedef struct +{ + float st[2]; +} mdvSt_t; + +typedef struct mdvSurface_s +{ + surfaceType_t surfaceType; + + char name[MAX_QPATH]; // polyset name + + int numShaderIndexes; + int *shaderIndexes; + + int numVerts; + mdvVertex_t *verts; + mdvSt_t *st; + + int numTriangles; + srfTriangle_t *triangles; + + struct mdvModel_s *model; +} mdvSurface_t; + +typedef struct mdvModel_s +{ + int numFrames; + mdvFrame_t *frames; + + int numTags; + mdvTag_t *tags; + mdvTagName_t *tagNames; + + int numSurfaces; + mdvSurface_t *surfaces; + + int numVBOSurfaces; + srfVBOMDVMesh_t *vboSurfaces; + + int numSkins; +} mdvModel_t; + + //====================================================================== typedef enum { @@ -760,7 +2053,7 @@ typedef struct model_s { int dataSize; // just for listing purposes bmodel_t *bmodel; // only if type == MOD_BRUSH - md3Header_t *md3[MD3_MAX_LODS]; // only if type == MOD_MESH + mdvModel_t *mdv[MD3_MAX_LODS]; // only if type == MOD_MESH void *md4; // only if type == (MOD_MD4 | MOD_MDR) int numLods; @@ -840,13 +2133,52 @@ typedef struct { int texEnv[2]; int faceCulling; unsigned long glStateBits; + uint32_t vertexAttribsState; + uint32_t vertexAttribPointersSet; + uint32_t vertexAttribsNewFrame; + uint32_t vertexAttribsOldFrame; + float vertexAttribsInterpolation; + shaderProgram_t *currentProgram; + VBO_t *currentVBO; + IBO_t *currentIBO; + matrix_t modelview; + matrix_t projection; + matrix_t modelviewProjection; } glstate_t; +// We can't change glConfig_t without breaking DLL/vms compatibility, so +// store extensions we have here. +typedef struct { + qboolean textureFilterAnisotropic; + int maxAnisotropy; + qboolean multiDrawArrays; + qboolean vertexBufferObject; + + // These next three are all required for one chunk of code, so glsl is + // set if they are all true. + qboolean vertexProgram; + qboolean shaderObjects; + qboolean vertexShader; + qboolean glsl; +} glRefConfig_t; + typedef struct { int c_surfaces, c_shaders, c_vertexes, c_indexes, c_totalIndexes; + int c_surfBatches; float c_overDraw; + int c_vboVertexBuffers; + int c_vboIndexBuffers; + int c_vboVertexes; + int c_vboIndexes; + + int c_staticVboDraws; + int c_dynamicVboDraws; + + int c_multidraws; + int c_multidrawsMerged; + int c_dlightVertexes; int c_dlightIndexes; @@ -886,7 +2218,10 @@ typedef struct { typedef struct { qboolean registered; // cleared at shutdown, set at beginRegistration - int visCount; // incremented every time a new vis cluster is entered + int visIndex; + int visClusters[MAX_VISCOUNTS]; + int visCounts[MAX_VISCOUNTS]; // incremented every time a new vis cluster is entered + int frameCount; // incremented every frame int sceneCount; // incremented every scene int viewCount; // incremented every view (twice a scene if portaled) @@ -919,12 +2254,25 @@ typedef struct { int numLightmaps; image_t **lightmaps; + image_t *fatLightmap; + int fatLightmapSize; + int fatLightmapStep; + trRefEntity_t *currentEntity; trRefEntity_t worldEntity; // point currentEntity at this when rendering world int currentEntityNum; int shiftedEntityNum; // currentEntityNum << QSORT_ENTITYNUM_SHIFT model_t *currentModel; + // + // GPU shader programs + // + + shaderProgram_t genericShader[GLSLDEF_COUNT]; + + + // ----------------------------------------- + viewParms_t viewParms; float identityLight; // 1.0 / ( 1 << overbrightBits ) @@ -953,6 +2301,12 @@ typedef struct { int numImages; image_t *images[MAX_DRAWIMAGES]; + int numVBOs; + VBO_t *vbos[MAX_VBOS]; + + int numIBOs; + IBO_t *ibos[MAX_IBOS]; + // shader indexes from other modules will be looked up in tr.shaders[] // shader indexes from drawsurfs will be looked up in sortedShaders[] // lower indexed sortedShaders must be rendered first (opaque surfaces before translucent) @@ -979,8 +2333,7 @@ extern glstate_t glState; // outside of TR since it shouldn't be cleared during // These two variables should live inside glConfig but can't because of compatibility issues to the original ID vms. // If you release a stand-alone game and your mod uses tr_types.h from this build you can safely move them to // the glconfig_t struct. -extern qboolean textureFilterAnisotropic; -extern int maxAnisotropy; +extern glRefConfig_t glRefConfig; extern float displayAspect; @@ -1057,6 +2410,10 @@ extern cvar_t *r_ext_texture_env_add; extern cvar_t *r_ext_texture_filter_anisotropic; extern cvar_t *r_ext_max_anisotropy; +extern cvar_t *r_arb_vertex_buffer_object; +extern cvar_t *r_arb_shader_objects; +extern cvar_t *r_ext_multi_draw_arrays; + extern cvar_t *r_nobind; // turns off binding to appropriate textures extern cvar_t *r_singleShader; // make most world faces use default shader extern cvar_t *r_roundImagesDown; @@ -1098,6 +2455,9 @@ extern cvar_t *r_skipBackEnd; extern cvar_t *r_stereoEnabled; extern cvar_t *r_anaglyphMode; +extern cvar_t *r_mergeMultidraws; +extern cvar_t *r_mergeLeafSurfaces; + extern cvar_t *r_greyscale; extern cvar_t *r_ignoreGLErrors; @@ -1138,12 +2498,15 @@ void R_DecomposeSort( unsigned sort, int *entityNum, shader_t **shader, void R_AddDrawSurf( surfaceType_t *surface, shader_t *shader, int fogIndex, int dlightMap ); +void R_CalcSurfaceTriangleNeighbors(int numTriangles, srfTriangle_t * triangles); +void R_CalcSurfaceTrianglePlanes(int numTriangles, srfTriangle_t * triangles, srfVert_t * verts); #define CULL_IN 0 // completely unclipped #define CULL_CLIP 1 // clipped by one or more planes #define CULL_OUT 2 // completely outside the clipping planes void R_LocalNormalToWorld (vec3_t local, vec3_t world); void R_LocalPointToWorld (vec3_t local, vec3_t world); +int R_CullBox (vec3_t bounds[2]); int R_CullLocalBox (vec3_t bounds[2]); int R_CullPointAndRadius( vec3_t origin, float radius ); int R_CullLocalPointAndRadius( vec3_t origin, float radius ); @@ -1155,11 +2518,15 @@ void R_RotateForEntity( const trRefEntity_t *ent, const viewParms_t *viewParms, ** GL wrapper/helper functions */ void GL_Bind( image_t *image ); +void GL_BindToTMU( image_t *image, int tmu ); void GL_SetDefaultState (void); void GL_SelectTexture( int unit ); void GL_TextureMode( const char *string ); -void GL_CheckErrors( void ); +void GL_CheckErrs( char *file, int line ); +#define GL_CheckErrors(...) GL_CheckErrs(__FILE__, __LINE__) void GL_State( unsigned long stateVector ); +void GL_SetProjectionMatrix(matrix_t matrix); +void GL_SetModelviewMatrix(matrix_t matrix); void GL_TexEnv( int env ); void GL_Cull( int cullType ); @@ -1290,7 +2657,6 @@ TESSELATOR/SHADER DECLARATIONS ==================================================================== */ -typedef byte color4ub_t[4]; typedef struct stageVars { @@ -1298,6 +2664,7 @@ typedef struct stageVars vec2_t texcoords[NUM_TEXTURE_BUNDLES][SHADER_MAX_VERTEXES]; } stageVars_t; +#define MAX_MULTIDRAW_PRIMITIVES 16384 typedef struct shaderCommands_s { @@ -1306,7 +2673,11 @@ typedef struct shaderCommands_s vec4_t normal[SHADER_MAX_VERTEXES] QALIGN(16); vec2_t texCoords[SHADER_MAX_VERTEXES][2] QALIGN(16); color4ub_t vertexColors[SHADER_MAX_VERTEXES] QALIGN(16); - int vertexDlightBits[SHADER_MAX_VERTEXES] QALIGN(16); + //int vertexDlightBits[SHADER_MAX_VERTEXES] QALIGN(16); + + VBO_t *vbo; + IBO_t *ibo; + qboolean useInternalVBO; stageVars_t svars QALIGN(16); @@ -1318,9 +2689,15 @@ typedef struct shaderCommands_s int dlightBits; // or together of all vertexDlightBits + int firstIndex; int numIndexes; int numVertexes; + int multiDrawPrimitives; + GLsizei multiDrawNumIndexes[MAX_MULTIDRAW_PRIMITIVES]; + GLvoid * multiDrawFirstIndex[MAX_MULTIDRAW_PRIMITIVES]; + GLvoid * multiDrawLastIndex[MAX_MULTIDRAW_PRIMITIVES]; + // info extracted from current shader int numPasses; void (*currentStageIteratorFunc)( void ); @@ -1335,6 +2712,7 @@ void RB_CheckOverflow( int verts, int indexes ); #define RB_CHECKOVERFLOW(v,i) if (tess.numVertexes + (v) >= SHADER_MAX_VERTEXES || tess.numIndexes + (i) >= SHADER_MAX_INDEXES ) {RB_CheckOverflow(v,i);} void RB_StageIteratorGeneric( void ); +void RB_StageIteratorGenericVBO( void ); void RB_StageIteratorSky( void ); void RB_StageIteratorVertexLitTexture( void ); void RB_StageIteratorLightmappedMultitexture( void ); @@ -1423,7 +2801,7 @@ CURVE TESSELATION #define PATCH_STITCHING srfGridMesh_t *R_SubdividePatchToGrid( int width, int height, - drawVert_t points[MAX_PATCH_SIZE*MAX_PATCH_SIZE] ); + srfVert_t points[MAX_PATCH_SIZE*MAX_PATCH_SIZE] ); srfGridMesh_t *R_GridInsertColumn( srfGridMesh_t *grid, int column, int row, vec3_t point, float loderror ); srfGridMesh_t *R_GridInsertRow( srfGridMesh_t *grid, int row, int column, vec3_t point, float loderror ); void R_FreeSurfaceGridMesh( srfGridMesh_t *grid ); @@ -1440,6 +2818,48 @@ int R_MarkFragments( int numPoints, const vec3_t *points, const vec3_t projectio int maxPoints, vec3_t pointBuffer, int maxFragments, markFragment_t *fragmentBuffer ); +/* +============================================================ + +VERTEX BUFFER OBJECTS + +============================================================ +*/ +VBO_t *R_CreateVBO(const char *name, byte * vertexes, int vertexesSize, vboUsage_t usage); +VBO_t *R_CreateVBO2(const char *name, int numVertexes, srfVert_t * vertexes, uint32_t stateBits, vboUsage_t usage); + +IBO_t *R_CreateIBO(const char *name, byte * indexes, int indexesSize, vboUsage_t usage); +IBO_t *R_CreateIBO2(const char *name, int numTriangles, srfTriangle_t * triangles, vboUsage_t usage); + +void R_BindVBO(VBO_t * vbo); +void R_BindNullVBO(void); + +void R_BindIBO(IBO_t * ibo); +void R_BindNullIBO(void); + +void R_InitVBOs(void); +void R_ShutdownVBOs(void); +void R_VBOList_f(void); + +void RB_UpdateVBOs(unsigned int attribBits); + + +/* +============================================================ + +GLSL + +============================================================ +*/ + +void GLSL_InitGPUShaders(void); +void GLSL_ShutdownGPUShaders(void); +void GLSL_VertexAttribsState(uint32_t stateBits); +void GLSL_VertexAttribPointers(uint32_t attribBits); +void GLSL_BindProgram(shaderProgram_t * program); +void GLSL_BindNullProgram(void); +shaderProgram_t *GLSL_GetGenericShaderProgram(void); + /* ============================================================ @@ -1525,6 +2945,14 @@ void RB_CalcRotateTexCoords( float rotSpeed, float *dstTexCoords ); void RB_CalcScaleTexCoords( const float scale[2], float *dstTexCoords ); void RB_CalcTurbulentTexCoords( const waveForm_t *wf, float *dstTexCoords ); void RB_CalcTransformTexCoords( const texModInfo_t *tmi, float *dstTexCoords ); + +void RB_CalcScaleTexMatrix( const float scale[2], float *matrix ); +void RB_CalcScrollTexMatrix( const float scrollSpeed[2], float *matrix ); +void RB_CalcRotateTexMatrix( float degsPerSecond, float *matrix ); +void RB_CalcTurbulentTexMatrix( const waveForm_t *wf, matrix_t matrix ); +void RB_CalcTransformTexMatrix( const texModInfo_t *tmi, float *matrix ); +void RB_CalcStretchTexMatrix( const waveForm_t *wf, float *matrix ); + void RB_CalcModulateColorsByFog( unsigned char *dstColors ); void RB_CalcModulateAlphasByFog( unsigned char *dstColors ); void RB_CalcModulateRGBAsByFog( unsigned char *dstColors ); diff --git a/reaction/code/renderer/tr_main.c b/reaction/code/renderer/tr_main.c index e7c05424..b8dc2eea 100644 --- a/reaction/code/renderer/tr_main.c +++ b/reaction/code/renderer/tr_main.c @@ -43,6 +43,97 @@ refimport_t ri; // point at this for their sorting surface surfaceType_t entitySurface = SF_ENTITY; +/* +================= +R_FindSurfaceTriangleWithEdge +Tr3B - recoded from Q2E +================= +*/ +static int R_FindSurfaceTriangleWithEdge(int numTriangles, srfTriangle_t * triangles, int start, int end, int ignore) +{ + srfTriangle_t *tri; + int count, match; + int i; + + count = 0; + match = -1; + + for(i = 0, tri = triangles; i < numTriangles; i++, tri++) + { + if((tri->indexes[0] == start && tri->indexes[1] == end) || + (tri->indexes[1] == start && tri->indexes[2] == end) || (tri->indexes[2] == start && tri->indexes[0] == end)) + { + if(i != ignore) + { + match = i; + } + + count++; + } + else if((tri->indexes[1] == start && tri->indexes[0] == end) || + (tri->indexes[2] == start && tri->indexes[1] == end) || (tri->indexes[0] == start && tri->indexes[2] == end)) + { + count++; + } + } + + // detect edges shared by three triangles and make them seams + if(count > 2) + { + match = -1; + } + + return match; +} + + +/* +================= +R_CalcSurfaceTriangleNeighbors +Tr3B - recoded from Q2E +================= +*/ +void R_CalcSurfaceTriangleNeighbors(int numTriangles, srfTriangle_t * triangles) +{ + int i; + srfTriangle_t *tri; + + for(i = 0, tri = triangles; i < numTriangles; i++, tri++) + { + tri->neighbors[0] = R_FindSurfaceTriangleWithEdge(numTriangles, triangles, tri->indexes[1], tri->indexes[0], i); + tri->neighbors[1] = R_FindSurfaceTriangleWithEdge(numTriangles, triangles, tri->indexes[2], tri->indexes[1], i); + tri->neighbors[2] = R_FindSurfaceTriangleWithEdge(numTriangles, triangles, tri->indexes[0], tri->indexes[2], i); + } +} + +/* +================= +R_CalcSurfaceTrianglePlanes +================= +*/ +void R_CalcSurfaceTrianglePlanes(int numTriangles, srfTriangle_t * triangles, srfVert_t * verts) +{ + int i; + srfTriangle_t *tri; + + for(i = 0, tri = triangles; i < numTriangles; i++, tri++) + { + float *v1, *v2, *v3; + vec3_t d1, d2; + + v1 = verts[tri->indexes[0]].xyz; + v2 = verts[tri->indexes[1]].xyz; + v3 = verts[tri->indexes[2]].xyz; + + VectorSubtract(v2, v1, d1); + VectorSubtract(v3, v1, d2); + + CrossProduct(d2, d1, tri->plane); + tri->plane[3] = DotProduct(tri->plane, v1); + } +} + + /* ================= R_CullLocalBox @@ -50,7 +141,8 @@ R_CullLocalBox Returns CULL_IN, CULL_CLIP, or CULL_OUT ================= */ -int R_CullLocalBox (vec3_t bounds[2]) { +int R_CullLocalBox(vec3_t localBounds[2]) { +#if 0 int i, j; vec3_t transformed[8]; float dists[8]; @@ -104,6 +196,75 @@ int R_CullLocalBox (vec3_t bounds[2]) { } return CULL_CLIP; // partially clipped +#else + int j; + vec3_t transformed; + vec3_t v; + vec3_t worldBounds[2]; + + if(r_nocull->integer) + { + return CULL_CLIP; + } + + // transform into world space + ClearBounds(worldBounds[0], worldBounds[1]); + + for(j = 0; j < 8; j++) + { + v[0] = localBounds[j & 1][0]; + v[1] = localBounds[(j >> 1) & 1][1]; + v[2] = localBounds[(j >> 2) & 1][2]; + + R_LocalPointToWorld(v, transformed); + + AddPointToBounds(transformed, worldBounds[0], worldBounds[1]); + } + + return R_CullBox(worldBounds); +#endif +} + +/* +================= +R_CullBox + +Returns CULL_IN, CULL_CLIP, or CULL_OUT +================= +*/ +int R_CullBox(vec3_t worldBounds[2]) { + int i; + cplane_t *frust; + qboolean anyClip; + int r; + + // check against frustum planes + anyClip = qfalse; + for(i = 0; i < 4 /*FRUSTUM_PLANES*/; i++) + { + frust = &tr.viewParms.frustum[i]; + + r = BoxOnPlaneSide(worldBounds[0], worldBounds[1], frust); + + if(r == 2) + { + // completely outside frustum + return CULL_OUT; + } + if(r == 3) + { + anyClip = qtrue; + } + } + + if(!anyClip) + { + // completely inside frustum + return CULL_IN; + } + + // partially clipped + return CULL_CLIP; } /* @@ -570,7 +731,7 @@ void R_SetupProjectionZ(viewParms_t *dest) { float zNear, zFar, depth; - zNear = r_znear->value; + zNear = r_znear->value; zFar = dest->zFar; depth = zFar - zNear; @@ -578,6 +739,40 @@ void R_SetupProjectionZ(viewParms_t *dest) dest->projectionMatrix[6] = 0; dest->projectionMatrix[10] = -( zFar + zNear ) / depth; dest->projectionMatrix[14] = -2 * zFar * zNear / depth; + + if (dest->isPortal) + { + float plane[4]; + float plane2[4]; + vec4_t q, c; + + // transform portal plane into camera space + plane[0] = dest->portalPlane.normal[0]; + plane[1] = dest->portalPlane.normal[1]; + plane[2] = dest->portalPlane.normal[2]; + plane[3] = dest->portalPlane.dist; + + plane2[0] = -DotProduct (dest->or.axis[1], plane); + plane2[1] = DotProduct (dest->or.axis[2], plane); + plane2[2] = -DotProduct (dest->or.axis[0], plane); + plane2[3] = DotProduct (plane, dest->or.origin) - plane[3]; + + // Lengyel, Eric. “Modifying the Projection Matrix to Perform Oblique Near-plane Clipping”. + // Terathon Software 3D Graphics Library, 2004. http://www.terathon.com/code/oblique.html + q[0] = (SGN(plane2[0]) + dest->projectionMatrix[8]) / dest->projectionMatrix[0]; + q[1] = (SGN(plane2[1]) + dest->projectionMatrix[9]) / dest->projectionMatrix[5]; + q[2] = -1.0f; + q[3] = (1.0f + dest->projectionMatrix[10]) / dest->projectionMatrix[14]; + + VectorScale4(plane2, 2.0f / DotProduct4(plane2, q), c); + + dest->projectionMatrix[2] = c[0]; + dest->projectionMatrix[6] = c[1]; + dest->projectionMatrix[10] = c[2] + 1.0f; + dest->projectionMatrix[14] = c[3]; + + } + } /* @@ -622,7 +817,7 @@ R_PlaneForSurface void R_PlaneForSurface (surfaceType_t *surfType, cplane_t *plane) { srfTriangles_t *tri; srfPoly_t *poly; - drawVert_t *v1, *v2, *v3; + srfVert_t *v1, *v2, *v3; vec4_t plane4; if (!surfType) { @@ -636,9 +831,9 @@ void R_PlaneForSurface (surfaceType_t *surfType, cplane_t *plane) { return; case SF_TRIANGLES: tri = (srfTriangles_t *)surfType; - v1 = tri->verts + tri->indexes[0]; - v2 = tri->verts + tri->indexes[1]; - v3 = tri->verts + tri->indexes[2]; + v1 = tri->verts + tri->triangles[0].indexes[0]; + v2 = tri->verts + tri->triangles[0].indexes[1]; + v3 = tri->verts + tri->triangles[0].indexes[2]; PlaneFromPoints( plane4, v1->xyz, v2->xyz, v3->xyz ); VectorCopy( plane4, plane->normal ); plane->dist = plane4[3]; @@ -1314,6 +1509,8 @@ R_DebugPolygon ================ */ void R_DebugPolygon( int color, int numPoints, float *points ) { + // FIXME: implement this +#if 0 int i; GL_State( GLS_DEPTHMASK_TRUE | GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE ); @@ -1337,6 +1534,7 @@ void R_DebugPolygon( int color, int numPoints, float *points ) { } qglEnd(); qglDepthRange( 0, 1 ); +#endif } /* diff --git a/reaction/code/renderer/tr_marks.c b/reaction/code/renderer/tr_marks.c index b83c77b9..de1b1c68 100644 --- a/reaction/code/renderer/tr_marks.c +++ b/reaction/code/renderer/tr_marks.c @@ -134,7 +134,8 @@ R_BoxSurfaces_r void R_BoxSurfaces_r(mnode_t *node, vec3_t mins, vec3_t maxs, surfaceType_t **list, int listsize, int *listlength, vec3_t dir) { int s, c; - msurface_t *surf, **mark; + msurface_t *surf; + int *mark; // do the tail recursion in a loop while ( node->contents == -1 ) { @@ -150,37 +151,39 @@ void R_BoxSurfaces_r(mnode_t *node, vec3_t mins, vec3_t maxs, surfaceType_t **li } // add the individual surfaces - mark = node->firstmarksurface; + mark = tr.world->marksurfaces + node->firstmarksurface; c = node->nummarksurfaces; while (c--) { + int *surfViewCount; // if (*listlength >= listsize) break; // - surf = *mark; + surfViewCount = &tr.world->surfacesViewCount[*mark]; + surf = tr.world->surfaces + *mark; // check if the surface has NOIMPACT or NOMARKS set if ( ( surf->shader->surfaceFlags & ( SURF_NOIMPACT | SURF_NOMARKS ) ) || ( surf->shader->contentFlags & CONTENTS_FOG ) ) { - surf->viewCount = tr.viewCount; + *surfViewCount = tr.viewCount; } // extra check for surfaces to avoid list overflows else if (*(surf->data) == SF_FACE) { // the face plane should go through the box - s = BoxOnPlaneSide( mins, maxs, &(( srfSurfaceFace_t * ) surf->data)->plane ); + s = BoxOnPlaneSide( mins, maxs, &surf->cullinfo.plane ); if (s == 1 || s == 2) { - surf->viewCount = tr.viewCount; - } else if (DotProduct((( srfSurfaceFace_t * ) surf->data)->plane.normal, dir) > -0.5) { + *surfViewCount = tr.viewCount; + } else if (DotProduct(surf->cullinfo.plane.normal, dir) > -0.5) { // don't add faces that make sharp angles with the projection direction - surf->viewCount = tr.viewCount; + *surfViewCount = tr.viewCount; } } - else if (*(surfaceType_t *) (surf->data) != SF_GRID && - *(surfaceType_t *) (surf->data) != SF_TRIANGLES) - surf->viewCount = tr.viewCount; + else if (*(surf->data) != SF_GRID && + *(surf->data) != SF_TRIANGLES) + *surfViewCount = tr.viewCount; // check the viewCount because the surface may have // already been added if it spans multiple leafs - if (surf->viewCount != tr.viewCount) { - surf->viewCount = tr.viewCount; - list[*listlength] = (surfaceType_t *) surf->data; + if (*surfViewCount != tr.viewCount) { + *surfViewCount = tr.viewCount; + list[*listlength] = surf->data; (*listlength)++; } mark++; @@ -266,11 +269,11 @@ int R_MarkFragments( int numPoints, const vec3_t *points, const vec3_t projectio int numClipPoints; float *v; srfGridMesh_t *cv; - drawVert_t *dv; + srfTriangle_t *tri; + srfVert_t *dv; vec3_t normal; vec3_t projectionDir; vec3_t v1, v2; - int *indexes; //increment view count for double check prevention tr.viewCount++; @@ -407,11 +410,12 @@ int R_MarkFragments( int numPoints, const vec3_t *points, const vec3_t projectio continue; } - indexes = (int *)( (byte *)surf + surf->ofsIndices ); - for ( k = 0 ; k < surf->numIndices ; k += 3 ) { - for ( j = 0 ; j < 3 ; j++ ) { - v = surf->points[0] + VERTEXSIZE * indexes[k+j];; - VectorMA( v, MARKER_OFFSET, surf->plane.normal, clipPoints[0][j] ); + for(k = 0, tri = surf->triangles; k < surf->numTriangles; k++, tri++) + { + for(j = 0; j < 3; j++) + { + v = surf->verts[tri->indexes[j]].xyz; + VectorMA(v, MARKER_OFFSET, surf->plane.normal, clipPoints[0][j]); } // add the fragments of this face @@ -429,12 +433,12 @@ int R_MarkFragments( int numPoints, const vec3_t *points, const vec3_t projectio srfTriangles_t *surf = (srfTriangles_t *) surfaces[i]; - for (k = 0; k < surf->numIndexes; k += 3) + for(k = 0, tri = surf->triangles; k < surf->numTriangles; k++, tri++) { for(j = 0; j < 3; j++) { - v = surf->verts[surf->indexes[k + j]].xyz; - VectorMA(v, MARKER_OFFSET, surf->verts[surf->indexes[k + j]].normal, clipPoints[0][j]); + v = surf->verts[tri->indexes[j]].xyz; + VectorMA(v, MARKER_OFFSET, surf->verts[tri->indexes[j]].normal, clipPoints[0][j]); } // add the fragments of this face diff --git a/reaction/code/renderer/tr_mesh.c b/reaction/code/renderer/tr_mesh.c index 9b60348b..047de981 100644 --- a/reaction/code/renderer/tr_mesh.c +++ b/reaction/code/renderer/tr_mesh.c @@ -75,14 +75,14 @@ static float ProjectRadius( float r, vec3_t location ) R_CullModel ============= */ -static int R_CullModel( md3Header_t *header, trRefEntity_t *ent ) { +static int R_CullModel( mdvModel_t *model, trRefEntity_t *ent ) { vec3_t bounds[2]; - md3Frame_t *oldFrame, *newFrame; + mdvFrame_t *oldFrame, *newFrame; int i; // compute frame pointers - newFrame = ( md3Frame_t * ) ( ( byte * ) header + header->ofsFrames ) + ent->e.frame; - oldFrame = ( md3Frame_t * ) ( ( byte * ) header + header->ofsFrames ) + ent->e.oldframe; + newFrame = model->frames + ent->e.frame; + oldFrame = model->frames + ent->e.oldframe; // cull bounding sphere ONLY if this is not an upscaled entity if ( !ent->e.nonNormalizedAxes ) @@ -167,7 +167,7 @@ int R_ComputeLOD( trRefEntity_t *ent ) { float radius; float flod, lodscale; float projectedRadius; - md3Frame_t *frame; + mdvFrame_t *frame; #ifdef RAVENMD4 mdrHeader_t *mdr; mdrFrame_t *mdrframe; @@ -200,7 +200,8 @@ int R_ComputeLOD( trRefEntity_t *ent ) { else #endif { - frame = ( md3Frame_t * ) ( ( ( unsigned char * ) tr.currentModel->md3[0] ) + tr.currentModel->md3[0]->ofsFrames ); + //frame = ( md3Frame_t * ) ( ( ( unsigned char * ) tr.currentModel->md3[0] ) + tr.currentModel->md3[0]->ofsFrames ); + frame = tr.currentModel->mdv[0]->frames; frame += ent->e.frame; @@ -248,10 +249,10 @@ R_ComputeFogNum ================= */ -int R_ComputeFogNum( md3Header_t *header, trRefEntity_t *ent ) { +int R_ComputeFogNum( mdvModel_t *model, trRefEntity_t *ent ) { int i, j; fog_t *fog; - md3Frame_t *md3Frame; + mdvFrame_t *mdvFrame; vec3_t localOrigin; if ( tr.refdef.rdflags & RDF_NOWORLDMODEL ) { @@ -259,15 +260,15 @@ int R_ComputeFogNum( md3Header_t *header, trRefEntity_t *ent ) { } // FIXME: non-normalized axis issues - md3Frame = ( md3Frame_t * ) ( ( byte * ) header + header->ofsFrames ) + ent->e.frame; - VectorAdd( ent->e.origin, md3Frame->localOrigin, localOrigin ); + mdvFrame = model->frames + ent->e.frame; + VectorAdd( ent->e.origin, mdvFrame->localOrigin, localOrigin ); for ( i = 1 ; i < tr.world->numfogs ; i++ ) { fog = &tr.world->fogs[i]; for ( j = 0 ; j < 3 ; j++ ) { - if ( localOrigin[j] - md3Frame->radius >= fog->bounds[1][j] ) { + if ( localOrigin[j] - mdvFrame->radius >= fog->bounds[1][j] ) { break; } - if ( localOrigin[j] + md3Frame->radius <= fog->bounds[0][j] ) { + if ( localOrigin[j] + mdvFrame->radius <= fog->bounds[0][j] ) { break; } } @@ -287,9 +288,8 @@ R_AddMD3Surfaces */ void R_AddMD3Surfaces( trRefEntity_t *ent ) { int i; - md3Header_t *header = NULL; - md3Surface_t *surface = NULL; - md3Shader_t *md3Shader = NULL; + mdvModel_t *model = NULL; + mdvSurface_t *surface = NULL; shader_t *shader = NULL; int cull; int lod; @@ -300,8 +300,8 @@ void R_AddMD3Surfaces( trRefEntity_t *ent ) { personalModel = (ent->e.renderfx & RF_THIRD_PERSON) && !tr.viewParms.isPortal; if ( ent->e.renderfx & RF_WRAP_FRAMES ) { - ent->e.frame %= tr.currentModel->md3[0]->numFrames; - ent->e.oldframe %= tr.currentModel->md3[0]->numFrames; + ent->e.frame %= tr.currentModel->mdv[0]->numFrames; + ent->e.oldframe %= tr.currentModel->mdv[0]->numFrames; } // @@ -310,9 +310,9 @@ void R_AddMD3Surfaces( trRefEntity_t *ent ) { // when the surfaces are rendered, they don't need to be // range checked again. // - if ( (ent->e.frame >= tr.currentModel->md3[0]->numFrames) + if ( (ent->e.frame >= tr.currentModel->mdv[0]->numFrames) || (ent->e.frame < 0) - || (ent->e.oldframe >= tr.currentModel->md3[0]->numFrames) + || (ent->e.oldframe >= tr.currentModel->mdv[0]->numFrames) || (ent->e.oldframe < 0) ) { ri.Printf( PRINT_DEVELOPER, "R_AddMD3Surfaces: no such frame %d to %d for '%s'\n", ent->e.oldframe, ent->e.frame, @@ -326,13 +326,13 @@ void R_AddMD3Surfaces( trRefEntity_t *ent ) { // lod = R_ComputeLOD( ent ); - header = tr.currentModel->md3[lod]; + model = tr.currentModel->mdv[lod]; // // cull the entire model if merged bounding box of both frames // is outside the view frustum. // - cull = R_CullModel ( header, ent ); + cull = R_CullModel ( model, ent ); if ( cull == CULL_OUT ) { return; } @@ -347,13 +347,13 @@ void R_AddMD3Surfaces( trRefEntity_t *ent ) { // // see if we are in a fog volume // - fogNum = R_ComputeFogNum( header, ent ); + fogNum = R_ComputeFogNum( model, ent ); // // draw all surfaces // - surface = (md3Surface_t *)( (byte *)header + header->ofsSurfaces ); - for ( i = 0 ; i < header->numSurfaces ; i++ ) { + surface = model->surfaces; + for ( i = 0 ; i < model->numSurfaces ; i++ ) { if ( ent->e.customShader ) { shader = R_GetShaderByHandle( ent->e.customShader ); @@ -378,40 +378,55 @@ void R_AddMD3Surfaces( trRefEntity_t *ent ) { else if (shader->defaultShader) { ri.Printf( PRINT_DEVELOPER, "WARNING: shader %s in skin %s not found\n", shader->name, skin->name); } - } else if ( surface->numShaders <= 0 ) { - shader = tr.defaultShader; + //} else if ( surface->numShaders <= 0 ) { + //shader = tr.defaultShader; } else { - md3Shader = (md3Shader_t *) ( (byte *)surface + surface->ofsShaders ); - md3Shader += ent->e.skinNum % surface->numShaders; - shader = tr.shaders[ md3Shader->shaderIndex ]; + //md3Shader = (md3Shader_t *) ( (byte *)surface + surface->ofsShaders ); + //md3Shader += ent->e.skinNum % surface->numShaders; + //shader = tr.shaders[ md3Shader->shaderIndex ]; + shader = tr.shaders[ surface->shaderIndexes[ ent->e.skinNum % surface->numShaderIndexes ] ]; } - // we will add shadows even if the main object isn't visible in the view + if (model->numVBOSurfaces && glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer && + glRefConfig.glsl && r_arb_shader_objects->integer) + { + srfVBOMDVMesh_t *vboSurface = &model->vboSurfaces[i]; - // stencil shadows can't do personal models unless I polyhedron clip - if ( !personalModel - && r_shadows->integer == 2 - && fogNum == 0 - && !(ent->e.renderfx & ( RF_NOSHADOW | RF_DEPTHHACK ) ) - && shader->sort == SS_OPAQUE ) { - R_AddDrawSurf( (void *)surface, tr.shadowShader, 0, qfalse ); + // don't add third_person objects if not viewing through a portal + if(!personalModel) + { + R_AddDrawSurf((void *)vboSurface, shader, fogNum, qfalse ); + } + } + else + { + // we will add shadows even if the main object isn't visible in the view + + // stencil shadows can't do personal models unless I polyhedron clip + if ( !personalModel + && r_shadows->integer == 2 + && fogNum == 0 + && !(ent->e.renderfx & ( RF_NOSHADOW | RF_DEPTHHACK ) ) + && shader->sort == SS_OPAQUE ) { + R_AddDrawSurf( (void *)surface, tr.shadowShader, 0, qfalse ); + } + + // projection shadows work fine with personal models + if ( r_shadows->integer == 3 + && fogNum == 0 + && (ent->e.renderfx & RF_SHADOW_PLANE ) + && shader->sort == SS_OPAQUE ) { + R_AddDrawSurf( (void *)surface, tr.projectionShadowShader, 0, qfalse ); + } + + // don't add third_person objects if not viewing through a portal + if ( !personalModel ) { + R_AddDrawSurf( (void *)surface, shader, fogNum, qfalse ); + } } - // projection shadows work fine with personal models - if ( r_shadows->integer == 3 - && fogNum == 0 - && (ent->e.renderfx & RF_SHADOW_PLANE ) - && shader->sort == SS_OPAQUE ) { - R_AddDrawSurf( (void *)surface, tr.projectionShadowShader, 0, qfalse ); - } - - // don't add third_person objects if not viewing through a portal - if ( !personalModel ) { - R_AddDrawSurf( (void *)surface, shader, fogNum, qfalse ); - } - - surface = (md3Surface_t *)( (byte *)surface + surface->ofsEnd ); + surface++; } } diff --git a/reaction/code/renderer/tr_model.c b/reaction/code/renderer/tr_model.c index 8356ca24..10fb911d 100644 --- a/reaction/code/renderer/tr_model.c +++ b/reaction/code/renderer/tr_model.c @@ -25,7 +25,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #define LL(x) x=LittleLong(x) -static qboolean R_LoadMD3 (model_t *mod, int lod, void *buffer, const char *name ); +static qboolean R_LoadMD3 (model_t *mod, int lod, void *buffer, int bufferSize, const char *modName ); static qboolean R_LoadMD4 (model_t *mod, void *buffer, const char *name ); #ifdef RAVENMD4 static qboolean R_LoadMDR (model_t *mod, void *buffer, int filesize, const char *name ); @@ -182,12 +182,14 @@ qhandle_t RE_RegisterModel( const char *name ) { fext = defex; for ( lod = MD3_MAX_LODS - 1 ; lod >= 0 ; lod-- ) { + int bufferSize; + if ( lod ) Com_sprintf(namebuf, sizeof(namebuf), "%s_%d.%s", filename, lod, fext); else Com_sprintf(namebuf, sizeof(namebuf), "%s.%s", filename, fext); - ri.FS_ReadFile( namebuf, &buf.v ); + bufferSize = ri.FS_ReadFile( namebuf, &buf.v ); if ( !buf.u ) { continue; } @@ -203,7 +205,7 @@ qhandle_t RE_RegisterModel( const char *name ) { goto fail; } - loaded = R_LoadMD3( mod, lod, buf.u, name ); + loaded = R_LoadMD3( mod, lod, buf.u, bufferSize, name ); } ri.FS_FreeFile (buf.v); @@ -231,7 +233,7 @@ qhandle_t RE_RegisterModel( const char *name ) { // loaded, in case the user changes r_lodbias on the fly for ( lod-- ; lod >= 0 ; lod-- ) { mod->numLods++; - mod->md3[lod] = mod->md3[lod+1]; + mod->mdv[lod] = mod->mdv[lod+1]; } return mod->index; @@ -255,154 +257,309 @@ fail: R_LoadMD3 ================= */ -static qboolean R_LoadMD3 (model_t *mod, int lod, void *buffer, const char *mod_name ) { - int i, j; - md3Header_t *pinmodel; - md3Frame_t *frame; - md3Surface_t *surf; - md3Shader_t *shader; - md3Triangle_t *tri; - md3St_t *st; - md3XyzNormal_t *xyz; - md3Tag_t *tag; - int version; - int size; +static qboolean R_LoadMD3(model_t * mod, int lod, void *buffer, int bufferSize, const char *modName) +{ + int i, j; - pinmodel = (md3Header_t *)buffer; + md3Header_t *md3Model; + md3Frame_t *md3Frame; + md3Surface_t *md3Surf; + md3Shader_t *md3Shader; + md3Triangle_t *md3Tri; + md3St_t *md3st; + md3XyzNormal_t *md3xyz; + md3Tag_t *md3Tag; - version = LittleLong (pinmodel->version); - if (version != MD3_VERSION) { - ri.Printf( PRINT_WARNING, "R_LoadMD3: %s has wrong version (%i should be %i)\n", - mod_name, version, MD3_VERSION); + mdvModel_t *mdvModel; + mdvFrame_t *frame; + mdvSurface_t *surf;//, *surface; + int *shaderIndex; + srfTriangle_t *tri; + mdvVertex_t *v; + mdvSt_t *st; + mdvTag_t *tag; + mdvTagName_t *tagName; + + int version; + int size; + + md3Model = (md3Header_t *) buffer; + + version = LittleLong(md3Model->version); + if(version != MD3_VERSION) + { + ri.Printf(PRINT_WARNING, "R_LoadMD3: %s has wrong version (%i should be %i)\n", modName, version, MD3_VERSION); return qfalse; } mod->type = MOD_MESH; - size = LittleLong(pinmodel->ofsEnd); + size = LittleLong(md3Model->ofsEnd); mod->dataSize += size; - mod->md3[lod] = ri.Hunk_Alloc( size, h_low ); + mdvModel = mod->mdv[lod] = ri.Hunk_Alloc(sizeof(mdvModel_t), h_low); - Com_Memcpy (mod->md3[lod], buffer, LittleLong(pinmodel->ofsEnd) ); +// Com_Memcpy(mod->md3[lod], buffer, LittleLong(md3Model->ofsEnd)); - LL(mod->md3[lod]->ident); - LL(mod->md3[lod]->version); - LL(mod->md3[lod]->numFrames); - LL(mod->md3[lod]->numTags); - LL(mod->md3[lod]->numSurfaces); - LL(mod->md3[lod]->ofsFrames); - LL(mod->md3[lod]->ofsTags); - LL(mod->md3[lod]->ofsSurfaces); - LL(mod->md3[lod]->ofsEnd); + LL(md3Model->ident); + LL(md3Model->version); + LL(md3Model->numFrames); + LL(md3Model->numTags); + LL(md3Model->numSurfaces); + LL(md3Model->ofsFrames); + LL(md3Model->ofsTags); + LL(md3Model->ofsSurfaces); + LL(md3Model->ofsEnd); - if ( mod->md3[lod]->numFrames < 1 ) { - ri.Printf( PRINT_WARNING, "R_LoadMD3: %s has no frames\n", mod_name ); + if(md3Model->numFrames < 1) + { + ri.Printf(PRINT_WARNING, "R_LoadMD3: %s has no frames\n", modName); return qfalse; } - + // swap all the frames - frame = (md3Frame_t *) ( (byte *)mod->md3[lod] + mod->md3[lod]->ofsFrames ); - for ( i = 0 ; i < mod->md3[lod]->numFrames ; i++, frame++) { - frame->radius = LittleFloat( frame->radius ); - for ( j = 0 ; j < 3 ; j++ ) { - frame->bounds[0][j] = LittleFloat( frame->bounds[0][j] ); - frame->bounds[1][j] = LittleFloat( frame->bounds[1][j] ); - frame->localOrigin[j] = LittleFloat( frame->localOrigin[j] ); - } + mdvModel->numFrames = md3Model->numFrames; + mdvModel->frames = frame = ri.Hunk_Alloc(sizeof(*frame) * md3Model->numFrames, h_low); + + md3Frame = (md3Frame_t *) ((byte *) md3Model + md3Model->ofsFrames); + for(i = 0; i < md3Model->numFrames; i++, frame++, md3Frame++) + { + frame->radius = LittleFloat(md3Frame->radius); + for(j = 0; j < 3; j++) + { + frame->bounds[0][j] = LittleFloat(md3Frame->bounds[0][j]); + frame->bounds[1][j] = LittleFloat(md3Frame->bounds[1][j]); + frame->localOrigin[j] = LittleFloat(md3Frame->localOrigin[j]); + } } // swap all the tags - tag = (md3Tag_t *) ( (byte *)mod->md3[lod] + mod->md3[lod]->ofsTags ); - for ( i = 0 ; i < mod->md3[lod]->numTags * mod->md3[lod]->numFrames ; i++, tag++) { - for ( j = 0 ; j < 3 ; j++ ) { - tag->origin[j] = LittleFloat( tag->origin[j] ); - tag->axis[0][j] = LittleFloat( tag->axis[0][j] ); - tag->axis[1][j] = LittleFloat( tag->axis[1][j] ); - tag->axis[2][j] = LittleFloat( tag->axis[2][j] ); - } + mdvModel->numTags = md3Model->numTags; + mdvModel->tags = tag = ri.Hunk_Alloc(sizeof(*tag) * (md3Model->numTags * md3Model->numFrames), h_low); + + md3Tag = (md3Tag_t *) ((byte *) md3Model + md3Model->ofsTags); + for(i = 0; i < md3Model->numTags * md3Model->numFrames; i++, tag++, md3Tag++) + { + for(j = 0; j < 3; j++) + { + tag->origin[j] = LittleFloat(md3Tag->origin[j]); + tag->axis[0][j] = LittleFloat(md3Tag->axis[0][j]); + tag->axis[1][j] = LittleFloat(md3Tag->axis[1][j]); + tag->axis[2][j] = LittleFloat(md3Tag->axis[2][j]); + } + } + + + mdvModel->tagNames = tagName = ri.Hunk_Alloc(sizeof(*tagName) * (md3Model->numTags), h_low); + + md3Tag = (md3Tag_t *) ((byte *) md3Model + md3Model->ofsTags); + for(i = 0; i < md3Model->numTags; i++, tagName++, md3Tag++) + { + Q_strncpyz(tagName->name, md3Tag->name, sizeof(tagName->name)); } // swap all the surfaces - surf = (md3Surface_t *) ( (byte *)mod->md3[lod] + mod->md3[lod]->ofsSurfaces ); - for ( i = 0 ; i < mod->md3[lod]->numSurfaces ; i++) { + mdvModel->numSurfaces = md3Model->numSurfaces; + mdvModel->surfaces = surf = ri.Hunk_Alloc(sizeof(*surf) * md3Model->numSurfaces, h_low); - LL(surf->ident); - LL(surf->flags); - LL(surf->numFrames); - LL(surf->numShaders); - LL(surf->numTriangles); - LL(surf->ofsTriangles); - LL(surf->numVerts); - LL(surf->ofsShaders); - LL(surf->ofsSt); - LL(surf->ofsXyzNormals); - LL(surf->ofsEnd); - - if ( surf->numVerts > SHADER_MAX_VERTEXES ) { - ri.Error (ERR_DROP, "R_LoadMD3: %s has more than %i verts on a surface (%i)", - mod_name, SHADER_MAX_VERTEXES, surf->numVerts ); + md3Surf = (md3Surface_t *) ((byte *) md3Model + md3Model->ofsSurfaces); + for(i = 0; i < md3Model->numSurfaces; i++) + { + LL(md3Surf->ident); + LL(md3Surf->flags); + LL(md3Surf->numFrames); + LL(md3Surf->numShaders); + LL(md3Surf->numTriangles); + LL(md3Surf->ofsTriangles); + LL(md3Surf->numVerts); + LL(md3Surf->ofsShaders); + LL(md3Surf->ofsSt); + LL(md3Surf->ofsXyzNormals); + LL(md3Surf->ofsEnd); + + if(md3Surf->numVerts > SHADER_MAX_VERTEXES) + { + ri.Error(ERR_DROP, "R_LoadMD3: %s has more than %i verts on a surface (%i)", + modName, SHADER_MAX_VERTEXES, md3Surf->numVerts); } - if ( surf->numTriangles*3 > SHADER_MAX_INDEXES ) { - ri.Error (ERR_DROP, "R_LoadMD3: %s has more than %i triangles on a surface (%i)", - mod_name, SHADER_MAX_INDEXES / 3, surf->numTriangles ); + if(md3Surf->numTriangles * 3 > SHADER_MAX_INDEXES) + { + ri.Error(ERR_DROP, "R_LoadMD3: %s has more than %i triangles on a surface (%i)", + modName, SHADER_MAX_INDEXES / 3, md3Surf->numTriangles); } - + // change to surface identifier - surf->ident = SF_MD3; + surf->surfaceType = SF_MDV; + + // give pointer to model for Tess_SurfaceMDX + surf->model = mdvModel; + + // copy surface name + Q_strncpyz(surf->name, md3Surf->name, sizeof(surf->name)); // lowercase the surface name so skin compares are faster - Q_strlwr( surf->name ); + Q_strlwr(surf->name); // strip off a trailing _1 or _2 // this is a crutch for q3data being a mess - j = strlen( surf->name ); - if ( j > 2 && surf->name[j-2] == '_' ) { - surf->name[j-2] = 0; + j = strlen(surf->name); + if(j > 2 && surf->name[j - 2] == '_') + { + surf->name[j - 2] = 0; } - // register the shaders - shader = (md3Shader_t *) ( (byte *)surf + surf->ofsShaders ); - for ( j = 0 ; j < surf->numShaders ; j++, shader++ ) { - shader_t *sh; + // register the shaders + surf->numShaderIndexes = md3Surf->numShaders; + surf->shaderIndexes = shaderIndex = ri.Hunk_Alloc(sizeof(*shaderIndex) * md3Surf->numShaders, h_low); - sh = R_FindShader( shader->name, LIGHTMAP_NONE, qtrue ); - if ( sh->defaultShader ) { - shader->shaderIndex = 0; - } else { - shader->shaderIndex = sh->index; + md3Shader = (md3Shader_t *) ((byte *) md3Surf + md3Surf->ofsShaders); + for(j = 0; j < md3Surf->numShaders; j++, shaderIndex++, md3Shader++) + { + shader_t *sh; + + sh = R_FindShader(md3Shader->name, LIGHTMAP_NONE, qtrue); + if(sh->defaultShader) + { + *shaderIndex = 0; } - } + else + { + *shaderIndex = sh->index; + } + } // swap all the triangles - tri = (md3Triangle_t *) ( (byte *)surf + surf->ofsTriangles ); - for ( j = 0 ; j < surf->numTriangles ; j++, tri++ ) { - LL(tri->indexes[0]); - LL(tri->indexes[1]); - LL(tri->indexes[2]); + surf->numTriangles = md3Surf->numTriangles; + surf->triangles = tri = ri.Hunk_Alloc(sizeof(*tri) * md3Surf->numTriangles, h_low); + + md3Tri = (md3Triangle_t *) ((byte *) md3Surf + md3Surf->ofsTriangles); + for(j = 0; j < md3Surf->numTriangles; j++, tri++, md3Tri++) + { + tri->indexes[0] = LittleLong(md3Tri->indexes[0]); + tri->indexes[1] = LittleLong(md3Tri->indexes[1]); + tri->indexes[2] = LittleLong(md3Tri->indexes[2]); + } + + R_CalcSurfaceTriangleNeighbors(surf->numTriangles, surf->triangles); + + // swap all the XyzNormals + surf->numVerts = md3Surf->numVerts; + surf->verts = v = ri.Hunk_Alloc(sizeof(*v) * (md3Surf->numVerts * md3Surf->numFrames), h_low); + + md3xyz = (md3XyzNormal_t *) ((byte *) md3Surf + md3Surf->ofsXyzNormals); + for(j = 0; j < md3Surf->numVerts * md3Surf->numFrames; j++, md3xyz++, v++) + { + unsigned lat, lng; + unsigned short normal; + + v->xyz[0] = LittleShort(md3xyz->xyz[0]) * MD3_XYZ_SCALE; + v->xyz[1] = LittleShort(md3xyz->xyz[1]) * MD3_XYZ_SCALE; + v->xyz[2] = LittleShort(md3xyz->xyz[2]) * MD3_XYZ_SCALE; + + normal = LittleShort(md3xyz->normal); + + lat = ( normal >> 8 ) & 0xff; + lng = ( normal & 0xff ); + lat *= (FUNCTABLE_SIZE/256); + lng *= (FUNCTABLE_SIZE/256); + + // decode X as cos( lat ) * sin( long ) + // decode Y as sin( lat ) * sin( long ) + // decode Z as cos( long ) + + v->normal[0] = tr.sinTable[(lat+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK] * tr.sinTable[lng]; + v->normal[1] = tr.sinTable[lat] * tr.sinTable[lng]; + v->normal[2] = tr.sinTable[(lng+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK]; } // swap all the ST - st = (md3St_t *) ( (byte *)surf + surf->ofsSt ); - for ( j = 0 ; j < surf->numVerts ; j++, st++ ) { - st->st[0] = LittleFloat( st->st[0] ); - st->st[1] = LittleFloat( st->st[1] ); - } + surf->st = st = ri.Hunk_Alloc(sizeof(*st) * md3Surf->numVerts, h_low); - // swap all the XyzNormals - xyz = (md3XyzNormal_t *) ( (byte *)surf + surf->ofsXyzNormals ); - for ( j = 0 ; j < surf->numVerts * surf->numFrames ; j++, xyz++ ) + md3st = (md3St_t *) ((byte *) md3Surf + md3Surf->ofsSt); + for(j = 0; j < md3Surf->numVerts; j++, md3st++, st++) { - xyz->xyz[0] = LittleShort( xyz->xyz[0] ); - xyz->xyz[1] = LittleShort( xyz->xyz[1] ); - xyz->xyz[2] = LittleShort( xyz->xyz[2] ); - - xyz->normal = LittleShort( xyz->normal ); - } - + st->st[0] = LittleFloat(md3st->st[0]); + st->st[1] = LittleFloat(md3st->st[1]); + } // find the next surface - surf = (md3Surface_t *)( (byte *)surf + surf->ofsEnd ); + md3Surf = (md3Surface_t *) ((byte *) md3Surf + md3Surf->ofsEnd); + surf++; } - + + if (glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer && + glRefConfig.glsl && r_arb_shader_objects->integer) + { + srfVBOMDVMesh_t *vboSurf; + + mdvModel->numVBOSurfaces = mdvModel->numSurfaces; + mdvModel->vboSurfaces = ri.Hunk_Alloc(sizeof(*mdvModel->vboSurfaces) * mdvModel->numSurfaces, h_low); + + vboSurf = mdvModel->vboSurfaces; + surf = mdvModel->surfaces; + for (i = 0; i < mdvModel->numSurfaces; i++, vboSurf++, surf++) + { + vec3_t *verts; + vec3_t *normals; + vec2_t *texcoords; + + byte *data; + int dataSize; + + int ofs_xyz, ofs_normal, ofs_st; + + dataSize = 0; + + ofs_xyz = dataSize; + dataSize += surf->numVerts * mdvModel->numFrames * sizeof(*verts); + + ofs_normal = dataSize; + dataSize += surf->numVerts * mdvModel->numFrames * sizeof(*verts); + + ofs_st = dataSize; + dataSize += surf->numVerts * sizeof(*texcoords); + + data = ri.Malloc(dataSize); + + verts = (void *)(data + ofs_xyz); + normals = (void *)(data + ofs_normal); + texcoords = (void *)(data + ofs_st); + + v = surf->verts; + for ( j = 0; j < surf->numVerts * mdvModel->numFrames ; j++, v++ ) + { + VectorCopy(v->xyz, verts[j]); + VectorCopy(v->normal, normals[j]); + } + + st = surf->st; + for ( j = 0 ; j < surf->numVerts ; j++, st++ ) { + texcoords[j][0] = st->st[0]; + texcoords[j][1] = st->st[1]; + } + + vboSurf->surfaceType = SF_VBO_MDVMESH; + vboSurf->mdvModel = mdvModel; + vboSurf->mdvSurface = surf; + vboSurf->numIndexes = surf->numTriangles * 3; + vboSurf->numVerts = surf->numVerts; + vboSurf->vbo = R_CreateVBO(va("staticMD3Mesh_VBO '%s'", surf->name), data, dataSize, VBO_USAGE_STATIC); + + vboSurf->vbo->ofs_xyz = ofs_xyz; + vboSurf->vbo->ofs_normal = ofs_normal; + vboSurf->vbo->ofs_st = ofs_st; + + vboSurf->vbo->stride_xyz = sizeof(*verts); + vboSurf->vbo->stride_normal = sizeof(*normals); + vboSurf->vbo->stride_st = sizeof(*st); + + vboSurf->vbo->size_xyz = sizeof(*verts) * surf->numVerts; + vboSurf->vbo->size_normal = sizeof(*normals) * surf->numVerts; + + ri.Free(data); + + vboSurf->ibo = R_CreateIBO2(va("staticMD3Mesh_IBO %s", surf->name), surf->numTriangles, surf->triangles, VBO_USAGE_STATIC); + } + } + return qtrue; } @@ -915,7 +1072,9 @@ void RE_BeginRegistration( glconfig_t *glconfigOut ) { R_SyncRenderThread(); - tr.viewCluster = -1; // force markleafs to regenerate + tr.visIndex = 0; + memset(tr.visClusters, -2, sizeof(tr.visClusters)); // force markleafs to regenerate + R_ClearFlares(); RE_ClearScene(); @@ -961,7 +1120,7 @@ void R_Modellist_f( void ) { mod = tr.models[i]; lods = 1; for ( j = 1 ; j < MD3_MAX_LODS ; j++ ) { - if ( mod->md3[j] && mod->md3[j] != mod->md3[j-1] ) { + if ( mod->mdv[j] && mod->mdv[j] != mod->mdv[j-1] ) { lods++; } } @@ -986,19 +1145,23 @@ void R_Modellist_f( void ) { R_GetTag ================ */ -static md3Tag_t *R_GetTag( md3Header_t *mod, int frame, const char *tagName ) { - md3Tag_t *tag; - int i; +static mdvTag_t *R_GetTag( mdvModel_t *mod, int frame, const char *_tagName ) { + int i; + mdvTag_t *tag; + mdvTagName_t *tagName; if ( frame >= mod->numFrames ) { // it is possible to have a bad frame while changing models, so don't error frame = mod->numFrames - 1; } - tag = (md3Tag_t *)((byte *)mod + mod->ofsTags) + frame * mod->numTags; - for ( i = 0 ; i < mod->numTags ; i++, tag++ ) { - if ( !strcmp( tag->name, tagName ) ) { - return tag; // found it + tag = mod->tags + frame * mod->numTags; + tagName = mod->tagNames; + for(i = 0; i < mod->numTags; i++, tag++, tagName++) + { + if(!strcmp(tagName->name, _tagName)) + { + return tag; } } @@ -1058,7 +1221,7 @@ R_LerpTag */ int R_LerpTag( orientation_t *tag, qhandle_t handle, int startFrame, int endFrame, float frac, const char *tagName ) { - md3Tag_t *start, *end; + mdvTag_t *start, *end; #ifdef RAVENMD4 md3Tag_t start_space, end_space; #endif @@ -1067,7 +1230,7 @@ int R_LerpTag( orientation_t *tag, qhandle_t handle, int startFrame, int endFram model_t *model; model = R_GetModelByHandle( handle ); - if ( !model->md3[0] ) + if ( !model->mdv[0] ) { #ifdef RAVENMD4 if(model->md4) @@ -1089,8 +1252,8 @@ int R_LerpTag( orientation_t *tag, qhandle_t handle, int startFrame, int endFram } else { - start = R_GetTag( model->md3[0], startFrame, tagName ); - end = R_GetTag( model->md3[0], endFrame, tagName ); + start = R_GetTag( model->mdv[0], startFrame, tagName ); + end = R_GetTag( model->mdv[0], endFrame, tagName ); if ( !start || !end ) { AxisClear( tag->axis ); VectorClear( tag->origin ); @@ -1121,8 +1284,8 @@ R_ModelBounds */ void R_ModelBounds( qhandle_t handle, vec3_t mins, vec3_t maxs ) { model_t *model; - md3Header_t *header; - md3Frame_t *frame; + mdvModel_t *mdvModel; + mdvFrame_t *frame; model = R_GetModelByHandle( handle ); @@ -1132,15 +1295,15 @@ void R_ModelBounds( qhandle_t handle, vec3_t mins, vec3_t maxs ) { return; } - if ( !model->md3[0] ) { + if ( !model->mdv[0] ) { VectorClear( mins ); VectorClear( maxs ); return; } - header = model->md3[0]; + mdvModel = model->mdv[0]; - frame = (md3Frame_t *)( (byte *)header + header->ofsFrames ); + frame = mdvModel->frames; VectorCopy( frame->bounds[0], mins ); VectorCopy( frame->bounds[1], maxs ); diff --git a/reaction/code/renderer/tr_scene.c b/reaction/code/renderer/tr_scene.c index 3b79d8a4..41b49019 100644 --- a/reaction/code/renderer/tr_scene.c +++ b/reaction/code/renderer/tr_scene.c @@ -130,8 +130,10 @@ void RE_AddPolyToScene( qhandle_t hShader, int numVerts, const polyVert_t *verts } if ( !hShader ) { - ri.Printf( PRINT_WARNING, "WARNING: RE_AddPolyToScene: NULL poly shader\n"); - return; + // This isn't a useful warning, and an hShader of zero isn't a null shader, it's + // the default shader. + //ri.Printf( PRINT_WARNING, "WARNING: RE_AddPolyToScene: NULL poly shader\n"); + //return; } for ( j = 0; j < numPolys; j++ ) { diff --git a/reaction/code/renderer/tr_shade.c b/reaction/code/renderer/tr_shade.c index 2a53c97f..13a43286 100644 --- a/reaction/code/renderer/tr_shade.c +++ b/reaction/code/renderer/tr_shade.c @@ -200,6 +200,31 @@ static void R_DrawElements( int numIndexes, const glIndex_t *indexes ) { } +static void R_DrawElementsVBO( int numIndexes, int firstIndex ) +{ + qglDrawElements(GL_TRIANGLES, numIndexes, GL_INDEX_TYPE, BUFFER_OFFSET(firstIndex * sizeof(GL_INDEX_TYPE))); +} + + +static void R_DrawMultiElementsVBO( int multiDrawPrimitives, const GLvoid **multiDrawFirstIndex, GLsizei *multiDrawNumIndexes) +{ + if (glRefConfig.multiDrawArrays && r_ext_multi_draw_arrays->integer) + { + qglMultiDrawElementsEXT(GL_TRIANGLES, multiDrawNumIndexes, GL_INDEX_TYPE, multiDrawFirstIndex, multiDrawPrimitives); + } + else + { + int i; + + for (i = 0; i < multiDrawPrimitives; i++) + { + qglDrawElements(GL_TRIANGLES, multiDrawNumIndexes[i], GL_INDEX_TYPE, multiDrawFirstIndex[i]); + } + } +} + + + /* ============================================================= @@ -213,21 +238,24 @@ static qboolean setArraysOnce; /* ================= -R_BindAnimatedImage +R_BindAnimatedImageToTMU ================= */ -static void R_BindAnimatedImage( textureBundle_t *bundle ) { +static void R_BindAnimatedImageToTMU( textureBundle_t *bundle, int tmu ) { int index; if ( bundle->isVideoMap ) { + int oldtmu = glState.currenttmu; + GL_SelectTexture(tmu); ri.CIN_RunCinematic(bundle->videoMapHandle); ri.CIN_UploadCinematic(bundle->videoMapHandle); + GL_SelectTexture(oldtmu); return; } if ( bundle->numImageAnimations <= 1 ) { - GL_Bind( bundle->image[0] ); + GL_BindToTMU( bundle->image[0], tmu); return; } @@ -241,9 +269,20 @@ static void R_BindAnimatedImage( textureBundle_t *bundle ) { } index %= bundle->numImageAnimations; - GL_Bind( bundle->image[ index ] ); + GL_BindToTMU( bundle->image[ index ], tmu ); } +/* +================= +R_BindAnimatedImage + +================= +*/ +static void R_BindAnimatedImage( textureBundle_t *bundle ) { + R_BindAnimatedImageToTMU( bundle, glState.currenttmu ); +} + + /* ================ DrawTris @@ -253,26 +292,64 @@ Draws triangle outlines for debugging */ static void DrawTris (shaderCommands_t *input) { GL_Bind( tr.whiteImage ); - qglColor3f (1,1,1); GL_State( GLS_POLYMODE_LINE | GLS_DEPTHMASK_TRUE ); qglDepthRange( 0, 0 ); - qglDisableClientState (GL_COLOR_ARRAY); - qglDisableClientState (GL_TEXTURE_COORD_ARRAY); + if (glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer) + { + if (glRefConfig.glsl && r_arb_shader_objects->integer) + { + matrix_t matrix; + shaderProgram_t *sp = &tr.genericShader[0]; - qglVertexPointer (3, GL_FLOAT, 16, input->xyz); // padded for SIMD - - if (qglLockArraysEXT) { - qglLockArraysEXT(0, input->numVertexes); - GLimp_LogComment( "glLockArraysEXT\n" ); + GLSL_VertexAttribsState(ATTR_POSITION); + GLSL_BindProgram(sp); + + GLSL_SetUniform_ModelViewProjectionMatrix(sp, glState.modelviewProjection); + + //GLSL_SetUniform_DeformGen(sp, DGEN_NONE); + GLSL_SetUniform_TCGen0(sp, TCGEN_IDENTITY); + Matrix16Identity(matrix); + GLSL_SetUniform_Texture0Matrix(sp, matrix); + GLSL_SetUniform_Texture1Env(sp, 0); + GLSL_SetUniform_ColorGen(sp, CGEN_IDENTITY); + GLSL_SetUniform_AlphaGen(sp, AGEN_IDENTITY); + + if (input->multiDrawPrimitives) + { + R_DrawMultiElementsVBO(input->multiDrawPrimitives, input->multiDrawFirstIndex, input->multiDrawNumIndexes); + } + else + { + R_DrawElementsVBO(input->numIndexes, input->firstIndex); + } + } + else + { + // FIXME: implement this + } } + else + { + qglColor3f (1,1,1); - R_DrawElements( input->numIndexes, input->indexes ); + qglDisableClientState (GL_COLOR_ARRAY); + qglDisableClientState (GL_TEXTURE_COORD_ARRAY); - if (qglUnlockArraysEXT) { - qglUnlockArraysEXT(); - GLimp_LogComment( "glUnlockArraysEXT\n" ); + qglVertexPointer (3, GL_FLOAT, 16, input->xyz); // padded for SIMD + + if (qglLockArraysEXT) { + qglLockArraysEXT(0, input->numVertexes); + GLimp_LogComment( "glLockArraysEXT\n" ); + } + + R_DrawElements( input->numIndexes, input->indexes ); + + if (qglUnlockArraysEXT) { + qglUnlockArraysEXT(); + GLimp_LogComment( "glUnlockArraysEXT\n" ); + } } qglDepthRange( 0, 1 ); } @@ -286,6 +363,8 @@ Draws vertex normals for debugging ================ */ static void DrawNormals (shaderCommands_t *input) { + //FIXME: implement this +#if 0 int i; vec3_t temp; @@ -303,6 +382,7 @@ static void DrawNormals (shaderCommands_t *input) { qglEnd (); qglDepthRange( 0, 1 ); +#endif } /* @@ -319,22 +399,224 @@ void RB_BeginSurface( shader_t *shader, int fogNum ) { shader_t *state = (shader->remappedShader) ? shader->remappedShader : shader; tess.numIndexes = 0; + tess.firstIndex = 0; tess.numVertexes = 0; + tess.multiDrawPrimitives = 0; tess.shader = state; tess.fogNum = fogNum; tess.dlightBits = 0; // will be OR'd in by surface functions tess.xstages = state->stages; tess.numPasses = state->numUnfoggedPasses; tess.currentStageIteratorFunc = state->optimalStageIteratorFunc; + tess.useInternalVBO = qtrue; tess.shaderTime = backEnd.refdef.floatTime - tess.shader->timeOffset; if (tess.shader->clampTime && tess.shaderTime >= tess.shader->clampTime) { tess.shaderTime = tess.shader->clampTime; } - - } + + +extern float EvalWaveForm( const waveForm_t *wf ); +extern float EvalWaveFormClamped( const waveForm_t *wf ); + + +static void GenerateGLTexCoords( shaderStage_t *pStage, int bundleNum) +{ + vec4_t vec; + + switch(pStage->bundle[bundleNum].tcGen) + { + case TCGEN_IDENTITY: + qglDisableClientState ( GL_TEXTURE_COORD_ARRAY ); + qglEnable(GL_TEXTURE_GEN_S); + qglEnable(GL_TEXTURE_GEN_T); + + vec[0] = 0.0f; + vec[1] = 0.0f; + vec[2] = 0.0f; + vec[3] = 0.0f; + + glTexGenfv(GL_S, GL_OBJECT_PLANE, vec); + glTexGenfv(GL_T, GL_OBJECT_PLANE, vec); + break; + + case TCGEN_TEXTURE: + qglTexCoordPointer( 2, GL_FLOAT, glState.currentVBO->stride_st, BUFFER_OFFSET(glState.currentVBO->ofs_st) ); + qglEnableClientState ( GL_TEXTURE_COORD_ARRAY ); + break; + + case TCGEN_LIGHTMAP: + qglTexCoordPointer( 2, GL_FLOAT, glState.currentVBO->stride_lightmap, BUFFER_OFFSET(glState.currentVBO->ofs_lightmap) ); + qglEnableClientState ( GL_TEXTURE_COORD_ARRAY ); + break; + + case TCGEN_ENVIRONMENT_MAPPED: + //FIXME: This doesn't look anything like the original + qglDisableClientState ( GL_TEXTURE_COORD_ARRAY ); + qglEnable(GL_TEXTURE_GEN_S); + qglEnable(GL_TEXTURE_GEN_T); + glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP); + glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP); + break; + + case TCGEN_VECTOR: + qglDisableClientState ( GL_TEXTURE_COORD_ARRAY ); + qglEnable(GL_TEXTURE_GEN_S); + qglEnable(GL_TEXTURE_GEN_T); + + vec[0] = pStage->bundle[0].tcGenVectors[0][0]; + vec[1] = pStage->bundle[0].tcGenVectors[0][1]; + vec[2] = pStage->bundle[0].tcGenVectors[0][2]; + vec[3] = 0.0f; + + glTexGenfv(GL_S, GL_OBJECT_PLANE, vec); + + vec[0] = pStage->bundle[0].tcGenVectors[1][0]; + vec[1] = pStage->bundle[0].tcGenVectors[2][1]; + vec[2] = pStage->bundle[0].tcGenVectors[3][2]; + vec[3] = 0.0f; + + glTexGenfv(GL_T, GL_OBJECT_PLANE, vec); + break; + case TCGEN_FOG: + // FIXME: This doesn't look anything like the original + { + vec4_t vec; + + vec[0] = 0.0f; + vec[1] = 0.0f; + vec[2] = 1.0f; + vec[3] = 0.0f; + + qglDisableClientState ( GL_TEXTURE_COORD_ARRAY ); + qglEnable(GL_TEXTURE_GEN_S); + qglEnable(GL_TEXTURE_GEN_T); + glTexGenfv(GL_S, GL_EYE_PLANE, vec); + glTexGenfv(GL_T, GL_EYE_PLANE, vec); + } + break; + default: + // nothing else is supported, bail out + break; + } +} + + +static void UndoGLTexCoords( shaderStage_t *pStage, int bundleNum) +{ + switch(pStage->bundle[bundleNum].tcGen) + { + case TCGEN_IDENTITY: + case TCGEN_ENVIRONMENT_MAPPED: + case TCGEN_VECTOR: + case TCGEN_FOG: + qglEnableClientState ( GL_TEXTURE_COORD_ARRAY ); + qglDisable(GL_TEXTURE_GEN_S); + qglDisable(GL_TEXTURE_GEN_T); + break; + + case TCGEN_TEXTURE: + case TCGEN_LIGHTMAP: + default: + break; + } +} + + +static void ComputeTexMatrix( shaderStage_t *pStage, int bundleNum, float *outmatrix) +{ + int tm; + float matrix[16], currentmatrix[16]; + textureBundle_t *bundle = &pStage->bundle[bundleNum]; + +/* + if (pStage->bundle[bundleNum].tcGen == TCGEN_ENVIRONMENT_MAPPED) + { + Matrix16Identity(currentmatrix); + currentmatrix[0] = 1.0f; + currentmatrix[5] = -1.0f; + //currentmatrix[10] = 1.0f; + Matrix16Copy(currentmatrix, outmatrix); + } + else*/ + { + Matrix16Identity(outmatrix); + Matrix16Identity(currentmatrix); + } + + for ( tm = 0; tm < bundle->numTexMods ; tm++ ) { + switch ( bundle->texMods[tm].type ) + { + + case TMOD_NONE: + tm = TR_MAX_TEXMODS; // break out of for loop + break; + + case TMOD_TURBULENT: + RB_CalcTurbulentTexMatrix( &bundle->texMods[tm].wave, + matrix ); + currentmatrix[12] = matrix[12]; + currentmatrix[13] = matrix[13]; + break; + + case TMOD_ENTITY_TRANSLATE: + RB_CalcScrollTexMatrix( backEnd.currentEntity->e.shaderTexCoord, + matrix ); + //Matrix16Multiply(currentmatrix, matrix, outmatrix); + Matrix16Multiply(matrix, currentmatrix, outmatrix); + Matrix16Copy(outmatrix, currentmatrix); + break; + + case TMOD_SCROLL: + RB_CalcScrollTexMatrix( bundle->texMods[tm].scroll, + matrix ); + //Matrix16Multiply(currentmatrix, matrix, outmatrix); + Matrix16Multiply(matrix, currentmatrix, outmatrix); + Matrix16Copy(outmatrix, currentmatrix); + break; + + case TMOD_SCALE: + RB_CalcScaleTexMatrix( bundle->texMods[tm].scale, + matrix ); + //Matrix16Multiply(currentmatrix, matrix, outmatrix); + Matrix16Multiply(matrix, currentmatrix, outmatrix); + Matrix16Copy(outmatrix, currentmatrix); + break; + + case TMOD_STRETCH: + RB_CalcStretchTexMatrix( &bundle->texMods[tm].wave, + matrix ); + //Matrix16Multiply(currentmatrix, matrix, outmatrix); + Matrix16Multiply(matrix, currentmatrix, outmatrix); + Matrix16Copy(outmatrix, currentmatrix); + break; + + case TMOD_TRANSFORM: + RB_CalcTransformTexMatrix( &bundle->texMods[tm], + matrix ); + //Matrix16Multiply(currentmatrix, matrix, outmatrix); + Matrix16Multiply(matrix, currentmatrix, outmatrix); + Matrix16Copy(outmatrix, currentmatrix); + break; + + case TMOD_ROTATE: + RB_CalcRotateTexMatrix( bundle->texMods[tm].rotateSpeed, + matrix ); + //Matrix16Multiply(currentmatrix, matrix, outmatrix); + Matrix16Multiply(matrix, currentmatrix, outmatrix); + Matrix16Copy(outmatrix, currentmatrix); + break; + + default: + ri.Error( ERR_DROP, "ERROR: unknown texmod '%d' in shader '%s'\n", bundle->texMods[tm].type, tess.shader->name ); + break; + } + } +} + + /* =================== DrawMultitextured @@ -394,6 +676,82 @@ static void DrawMultitextured( shaderCommands_t *input, int stage ) { } +/* +=================== +DrawMultitexturedVBO + +output = t0 * t1 or t0 + t1 + +t0 = most upstream according to spec +t1 = most downstream according to spec +=================== +*/ +static void DrawMultitexturedVBO( shaderCommands_t *input, int stage ) { + shaderStage_t *pStage; + float matrix[16]; + + pStage = tess.xstages[stage]; + + GL_State( pStage->stateBits ); + + // this is an ugly hack to work around a GeForce driver + // bug with multitexture and clip planes + if ( backEnd.viewParms.isPortal ) { + qglPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); + } + + // + // base + // + GL_SelectTexture( 0 ); + GenerateGLTexCoords( pStage, 0 ); + ComputeTexMatrix( pStage, 0, matrix ); + qglMatrixMode(GL_TEXTURE); + qglLoadMatrixf(matrix); + qglMatrixMode(GL_MODELVIEW); + R_BindAnimatedImage( &pStage->bundle[0] ); + + // + // lightmap/secondary pass + // + GL_SelectTexture( 1 ); + qglEnable( GL_TEXTURE_2D ); + GenerateGLTexCoords( pStage, 1 ); + ComputeTexMatrix( pStage, 1, matrix ); + qglMatrixMode(GL_TEXTURE); + qglLoadMatrixf(matrix); + qglMatrixMode(GL_MODELVIEW); + + if ( r_lightmap->integer ) { + GL_TexEnv( GL_REPLACE ); + } else { + GL_TexEnv( tess.shader->multitextureEnv ); + } + + R_BindAnimatedImage( &pStage->bundle[1] ); + + if (input->multiDrawPrimitives) + { + R_DrawMultiElementsVBO(input->multiDrawPrimitives, input->multiDrawFirstIndex, input->multiDrawNumIndexes); + } + else + { + R_DrawElementsVBO(input->numIndexes, input->firstIndex); + } + + qglDisable( GL_TEXTURE_2D ); + UndoGLTexCoords( pStage, 1 ); + qglMatrixMode(GL_TEXTURE); + qglLoadIdentity(); + qglMatrixMode(GL_MODELVIEW); + + GL_SelectTexture( 0 ); + UndoGLTexCoords( pStage, 0 ); + qglMatrixMode(GL_TEXTURE); + qglLoadIdentity(); + qglMatrixMode(GL_MODELVIEW); +} + /* =================== @@ -748,6 +1106,79 @@ static void ProjectDlightTexture( void ) { ProjectDlightTexture_scalar(); } +static void ProjectDlightTextureVBOGLSL( void ) { + int l; + vec3_t origin; + float scale; + float radius; + + if ( !backEnd.refdef.num_dlights ) { + return; + } + + for ( l = 0 ; l < backEnd.refdef.num_dlights ; l++ ) { + dlight_t *dl; + shaderProgram_t *sp; + vec4_t vector; + matrix_t matrix; + + if ( !( tess.dlightBits & ( 1 << l ) ) ) { + continue; // this surface definately doesn't have any of this light + } + + dl = &backEnd.refdef.dlights[l]; + VectorCopy( dl->transformed, origin ); + radius = dl->radius; + scale = 1.0f / radius; + + sp = GLSL_GetGenericShaderProgram(); + + GLSL_BindProgram(sp); + + GLSL_SetUniform_TCGen0(sp, TCGEN_DLIGHT); + Matrix16Identity(matrix); + GLSL_SetUniform_Texture0Matrix(sp, matrix); + GLSL_SetUniform_Texture1Env(sp, 0); + GLSL_SetUniform_ColorGen(sp, CGEN_DLIGHT); + GLSL_SetUniform_AlphaGen(sp, AGEN_CONST); + + vector[0] = dl->color[0]; + vector[1] = dl->color[1]; + vector[2] = dl->color[2]; + vector[3] = 1.0f; + GLSL_SetUniform_Color(sp, vector); + + vector[0] = origin[0]; + vector[1] = origin[1]; + vector[2] = origin[2]; + vector[3] = scale; + GLSL_SetUniform_TCGen0Vector0(sp, vector); + + GL_Bind( tr.dlightImage ); + + // include GLS_DEPTHFUNC_EQUAL so alpha tested surfaces don't add light + // where they aren't rendered + if ( dl->additive ) { + GL_State( GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL ); + } + else { + GL_State( GLS_SRCBLEND_DST_COLOR | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL ); + } + + if (tess.multiDrawPrimitives) + { + R_DrawMultiElementsVBO(tess.multiDrawPrimitives, tess.multiDrawFirstIndex, tess.multiDrawNumIndexes); + } + else + { + R_DrawElementsVBO(tess.numIndexes, tess.firstIndex); + } + + backEnd.pc.c_totalIndexes += tess.numIndexes; + backEnd.pc.c_dlightIndexes += tess.numIndexes; + } +} + /* =================== @@ -785,6 +1216,94 @@ static void RB_FogPass( void ) { R_DrawElements( tess.numIndexes, tess.indexes ); } +/* +=================== +RB_FogPassVBOGLSL + +Blends a fog texture on top of everything else +=================== +*/ +static void RB_FogPassVBOGLSL( void ) { + fog_t *fog; + vec4_t color; + matrix_t matrix; + shaderProgram_t *sp = GLSL_GetGenericShaderProgram(); + + GLSL_BindProgram(sp); + + fog = tr.world->fogs + tess.fogNum; + + GLSL_SetUniform_FogAdjustColors(sp, 4); + GLSL_SetUniform_TCGen0(sp, TCGEN_IDENTITY); + Matrix16Identity(matrix); + GLSL_SetUniform_Texture0Matrix(sp, matrix); + GLSL_SetUniform_Texture1Env(sp, 0); + GLSL_SetUniform_ColorGen(sp, CGEN_CONST); + GLSL_SetUniform_AlphaGen(sp, AGEN_CONST); + + color[0] = ((unsigned char *)(&fog->colorInt))[0] / 255.0f; + color[1] = ((unsigned char *)(&fog->colorInt))[1] / 255.0f; + color[2] = ((unsigned char *)(&fog->colorInt))[2] / 255.0f; + color[3] = ((unsigned char *)(&fog->colorInt))[3] / 255.0f; + GLSL_SetUniform_Color(sp, color); + +#if 0 // already set + // from RB_CalcFogTexCoords() + VectorSubtract( backEnd.or.origin, backEnd.viewParms.or.origin, local ); + fogDistanceVector[0] = -backEnd.or.modelMatrix[2]; + fogDistanceVector[1] = -backEnd.or.modelMatrix[6]; + fogDistanceVector[2] = -backEnd.or.modelMatrix[10]; + fogDistanceVector[3] = DotProduct( local, backEnd.viewParms.or.axis[0] ); + + // scale the fog vectors based on the fog's thickness + fogDistanceVector[0] *= fog->tcScale; + fogDistanceVector[1] *= fog->tcScale; + fogDistanceVector[2] *= fog->tcScale; + fogDistanceVector[3] *= fog->tcScale; + + // rotate the gradient vector for this orientation + if ( fog->hasSurface ) { + fogDepthVector[0] = fog->surface[0] * backEnd.or.axis[0][0] + + fog->surface[1] * backEnd.or.axis[0][1] + fog->surface[2] * backEnd.or.axis[0][2]; + fogDepthVector[1] = fog->surface[0] * backEnd.or.axis[1][0] + + fog->surface[1] * backEnd.or.axis[1][1] + fog->surface[2] * backEnd.or.axis[1][2]; + fogDepthVector[2] = fog->surface[0] * backEnd.or.axis[2][0] + + fog->surface[1] * backEnd.or.axis[2][1] + fog->surface[2] * backEnd.or.axis[2][2]; + fogDepthVector[3] = -fog->surface[3] + DotProduct( backEnd.or.origin, fog->surface ); + + eyeT = DotProduct( backEnd.or.viewOrigin, fogDepthVector ) + fogDepthVector[3]; + } else { + eyeT = 1; // non-surface fog always has eye inside + } + + fogDistanceVector[3] += 1.0/512; + + //ri.Printf(PRINT_ALL,"eyeT %f fogDistanceVector %f %f %f %f fogDepthVector %f %f %f %f\n", eyeT, fogDistanceVector[0], fogDistanceVector[1], fogDistanceVector[2], fogDistanceVector[3], fogDepthVector[0], fogDepthVector[1], fogDepthVector[2], fogDepthVector[3]); + //ri.Printf(PRINT_ALL, "fogsurface %f %f %f %f\n", fog->surface[0], fog->surface[1], fog->surface[2], fog->surface[3]); + + GLSL_SetUniform_TCGen0Vector0(sp, fogDistanceVector); + GLSL_SetUniform_TCGen0Vector1(sp, fogDepthVector); + GLSL_SetUniform_FogEyeT(sp, eyeT); +#endif + GL_Bind( tr.fogImage ); + + if ( tess.shader->fogPass == FP_EQUAL ) { + GL_State( GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA | GLS_DEPTHFUNC_EQUAL ); + } else { + GL_State( GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA ); + } + + if (tess.multiDrawPrimitives) + { + R_DrawMultiElementsVBO(tess.multiDrawPrimitives, tess.multiDrawFirstIndex, tess.multiDrawNumIndexes); + } + else + { + R_DrawElementsVBO(tess.numIndexes, tess.firstIndex); + } +} + + /* =============== ComputeColors @@ -1002,6 +1521,521 @@ static void ComputeColors( shaderStage_t *pStage ) } } +static void ComputeHelperColor( shaderStage_t *pStage, vec4_t color) +{ + // + // rgbGen + // + switch ( pStage->rgbGen ) + { + case CGEN_IDENTITY: + color[0] = 1.0f; + color[1] = 1.0f; + color[2] = 1.0f; + color[3] = 1.0f; + break; + case CGEN_IDENTITY_LIGHTING: + color[0] = tr.identityLight; + color[1] = tr.identityLight; + color[2] = tr.identityLight; + color[3] = tr.identityLight; // FIXME: Code was like this in quake 3, is this a bug? + break; + case CGEN_LIGHTING_DIFFUSE: + // Done entirely in vertex program + break; + case CGEN_EXACT_VERTEX: + // Done entirely in vertex program + break; + case CGEN_CONST: + color[0] = pStage->constantColor[0] / 255.0f; + color[1] = pStage->constantColor[1] / 255.0f; + color[2] = pStage->constantColor[2] / 255.0f; + color[3] = pStage->constantColor[3] / 255.0f; + break; + case CGEN_VERTEX: + case CGEN_ONE_MINUS_VERTEX: + color[0] = tr.identityLight; + color[1] = tr.identityLight; + color[2] = tr.identityLight; + color[3] = 1.0f; + break; + case CGEN_FOG: + { + fog_t *fog; + + fog = tr.world->fogs + tess.fogNum; + + color[0] = ((unsigned char *)(&fog->colorInt))[0] / 255.0f; + color[1] = ((unsigned char *)(&fog->colorInt))[1] / 255.0f; + color[2] = ((unsigned char *)(&fog->colorInt))[2] / 255.0f; + color[3] = ((unsigned char *)(&fog->colorInt))[3] / 255.0f; + } + break; + case CGEN_WAVEFORM: + { + // from RB_CalcWaveColor + float glow; + waveForm_t *wf = &pStage->rgbWave; + + if ( wf->func == GF_NOISE ) { + glow = wf->base + R_NoiseGet4f( 0, 0, 0, ( tess.shaderTime + wf->phase ) * wf->frequency ) * wf->amplitude; + } else { + glow = EvalWaveForm( wf ) * tr.identityLight; + } + + if ( glow < 0 ) { + glow = 0; + } + else if ( glow > 1 ) { + glow = 1; + } + + color[0] = glow; + color[1] = glow; + color[2] = glow; + color[3] = 1.0f; + } + break; + case CGEN_ENTITY: + if (backEnd.currentEntity) + { + color[0] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[0] / 255.0f; + color[1] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[1] / 255.0f; + color[2] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[2] / 255.0f; + color[3] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[3] / 255.0f; + } + else // FIXME: does original quake3 black out vertex colors like this? + { + color[0] = 0.0f; + color[1] = 0.0f; + color[2] = 0.0f; + color[3] = 0.0f; + } + break; + case CGEN_ONE_MINUS_ENTITY: + if (backEnd.currentEntity) + { + color[0] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[0] / 255.0f; + color[1] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[1] / 255.0f; + color[2] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[2] / 255.0f; + color[3] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[3] / 255.0f; + } + else // FIXME: does original quake3 black out vertex colors like this? + { + color[0] = 0.0f; + color[1] = 0.0f; + color[2] = 0.0f; + color[3] = 0.0f; + } + break; + } + + // + // alphaGen + // + switch ( pStage->alphaGen ) + { + case AGEN_SKIP: + break; + case AGEN_IDENTITY: + if ( pStage->rgbGen != CGEN_IDENTITY ) { + if ( ( pStage->rgbGen == CGEN_VERTEX && tr.identityLight != 1 ) || + pStage->rgbGen != CGEN_VERTEX ) { + color[3] = 1.0f; + } + } + break; + case AGEN_CONST: + if ( pStage->rgbGen != CGEN_CONST ) { + color[3] = pStage->constantColor[3] / 255.0f; + } + break; + case AGEN_WAVEFORM: + // From RB_CalcWaveAlpha + { + float glow; + waveForm_t *wf = &pStage->alphaWave; + glow = EvalWaveFormClamped( wf ); + color[3] = glow; + } + break; + case AGEN_LIGHTING_SPECULAR: + // Done entirely in vertex program + // RB_CalcSpecularAlpha( ( unsigned char * ) tess.svars.colors ); + break; + case AGEN_ENTITY: + //RB_CalcAlphaFromEntity( ( unsigned char * ) tess.svars.colors ); + if (backEnd.currentEntity) + { + color[3] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[3] / 255.0f; + } + break; + case AGEN_ONE_MINUS_ENTITY: + //RB_CalcAlphaFromOneMinusEntity( ( unsigned char * ) tess.svars.colors ); + if (backEnd.currentEntity) + { + color[3] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[3] / 255.0f; + } + break; + case AGEN_VERTEX: + // Done entirely in vertex program + break; + case AGEN_ONE_MINUS_VERTEX: + // Done entirely in vertex program + break; + case AGEN_PORTAL: + // Done entirely in vertex program + break; + } + + // + // pStage->adjustColorsForFog is done in the vertex program now + // + + // FIXME: find some way to implement this. +#if 0 + // if in greyscale rendering mode turn all color values into greyscale. + if(r_greyscale->integer) + { + int scale; + + for(i = 0; i < tess.numVertexes; i++) + { + scale = (tess.svars.colors[i][0] + tess.svars.colors[i][1] + tess.svars.colors[i][2]) / 3; + tess.svars.colors[i][0] = tess.svars.colors[i][1] = tess.svars.colors[i][2] = scale; + } + } +#endif + +} + +/* +=============== +ComputeColorMatrix + +if disableVertexColors is true, outmatrix[12-15] contains a color that should be used. +=============== +*/ + +static void ComputeColorMatrix( shaderStage_t *pStage, float *outmatrix, qboolean *disableVertexColors) +{ + *disableVertexColors = qfalse; + + // + // rgbGen + // + switch ( pStage->rgbGen ) + { + case CGEN_IDENTITY: + *disableVertexColors = qtrue; + Matrix16Zero(outmatrix); + outmatrix[12] = 1.0f; + outmatrix[13] = 1.0f; + outmatrix[14] = 1.0f; + outmatrix[15] = 1.0f; + break; + default: + case CGEN_IDENTITY_LIGHTING: + *disableVertexColors = qtrue; + Matrix16Zero(outmatrix); + outmatrix[12] = tr.identityLight; + outmatrix[13] = tr.identityLight; + outmatrix[14] = tr.identityLight; + outmatrix[15] = 1.0f; // FIXME: used to just be straight tr.identityLight, is this a bug? + break; + case CGEN_LIGHTING_DIFFUSE: + // FIXME: Can't do this with just a matrix + //RB_CalcDiffuseColor( ( unsigned char * ) tess.svars.colors ); + *disableVertexColors = qtrue; + Matrix16Zero(outmatrix); + outmatrix[12] = 1.0f; + outmatrix[13] = 1.0f; + outmatrix[14] = 1.0f; + outmatrix[15] = 1.0f; + break; + case CGEN_EXACT_VERTEX: + Matrix16Identity(outmatrix); + break; + case CGEN_CONST: + *disableVertexColors = qtrue; + Matrix16Zero(outmatrix); + outmatrix[12] = pStage->constantColor[0] / 255.0f; + outmatrix[13] = pStage->constantColor[1] / 255.0f; + outmatrix[14] = pStage->constantColor[2] / 255.0f; + outmatrix[15] = pStage->constantColor[3] / 255.0f; + break; + case CGEN_VERTEX: + Matrix16Identity(outmatrix); + outmatrix[ 0] = tr.identityLight; + outmatrix[ 5] = tr.identityLight; + outmatrix[10] = tr.identityLight; + outmatrix[15] = 1.0f; + break; + case CGEN_ONE_MINUS_VERTEX: + // FIXME: Not a perfect fit, if alpha is less than 1.0f or identitylight isn't 1 then this doesn't work +#if 0 + if ( tr.identityLight == 1 ) + { + for ( i = 0; i < tess.numVertexes; i++ ) + { + tess.svars.colors[i][0] = 255 - tess.vertexColors[i][0]; + tess.svars.colors[i][1] = 255 - tess.vertexColors[i][1]; + tess.svars.colors[i][2] = 255 - tess.vertexColors[i][2]; + } + } + else + { + for ( i = 0; i < tess.numVertexes; i++ ) + { + tess.svars.colors[i][0] = ( 255 - tess.vertexColors[i][0] ) * tr.identityLight; + tess.svars.colors[i][1] = ( 255 - tess.vertexColors[i][1] ) * tr.identityLight; + tess.svars.colors[i][2] = ( 255 - tess.vertexColors[i][2] ) * tr.identityLight; + } + } +#endif + Matrix16Zero(outmatrix); + outmatrix[0] = -1.0f; + outmatrix[5] = -1.0f; + outmatrix[10] = -1.0f; + + outmatrix[12] = 1.0f; + outmatrix[13] = 1.0f; + outmatrix[14] = 1.0f; + outmatrix[15] = 1.0f; + break; + case CGEN_FOG: + *disableVertexColors = qtrue; + { + fog_t *fog; + + fog = tr.world->fogs + tess.fogNum; + + outmatrix[12] = ((unsigned char *)(&fog->colorInt))[0] / 255.0f; + outmatrix[13] = ((unsigned char *)(&fog->colorInt))[1] / 255.0f; + outmatrix[14] = ((unsigned char *)(&fog->colorInt))[2] / 255.0f; + outmatrix[15] = ((unsigned char *)(&fog->colorInt))[3] / 255.0f; + + } + break; + case CGEN_WAVEFORM: + *disableVertexColors = qtrue; + { + // from RB_CalcWaveColor + float glow; + waveForm_t *wf = &pStage->rgbWave; + + if ( wf->func == GF_NOISE ) { + glow = wf->base + R_NoiseGet4f( 0, 0, 0, ( tess.shaderTime + wf->phase ) * wf->frequency ) * wf->amplitude; + } else { + glow = EvalWaveForm( wf ) * tr.identityLight; + } + + if ( glow < 0 ) { + glow = 0; + } + else if ( glow > 1 ) { + glow = 1; + } + + outmatrix[12] = glow; + outmatrix[13] = glow; + outmatrix[14] = glow; + outmatrix[15] = 1.0f; + } + break; + case CGEN_ENTITY: + *disableVertexColors = qtrue; + if (backEnd.currentEntity) + { + outmatrix[12] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[0] / 255.0f; + outmatrix[13] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[1] / 255.0f; + outmatrix[14] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[2] / 255.0f; + outmatrix[15] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[3] / 255.0f; + } + else // FIXME: does original quake3 black out vertex colors like this? + { + outmatrix[12] = 0.0f; + outmatrix[13] = 0.0f; + outmatrix[14] = 0.0f; + outmatrix[15] = 0.0f; + } + break; + case CGEN_ONE_MINUS_ENTITY: + *disableVertexColors = qtrue; + if (backEnd.currentEntity) + { + outmatrix[12] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[0] / 255.0f; + outmatrix[13] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[1] / 255.0f; + outmatrix[14] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[2] / 255.0f; + outmatrix[15] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[3] / 255.0f; + } + else // FIXME: does original quake3 black out vertex colors like this? + { + outmatrix[12] = 0.0f; + outmatrix[13] = 0.0f; + outmatrix[14] = 0.0f; + outmatrix[15] = 0.0f; + } + break; + } + + // + // alphaGen + // + switch ( pStage->alphaGen ) + { + case AGEN_SKIP: + break; + case AGEN_IDENTITY: + if ( pStage->rgbGen != CGEN_IDENTITY ) { + if ( ( pStage->rgbGen == CGEN_VERTEX && tr.identityLight != 1 ) || + pStage->rgbGen != CGEN_VERTEX ) { + // FIXME: Not a perfect fit, if alpha is less than 1.0f and vertex colors are enabled then this doesn't work + outmatrix[15] = 1.0f; +#if 0 + for ( i = 0; i < tess.numVertexes; i++ ) { + tess.svars.colors[i][3] = 0xff; + } +#endif + } + } + break; + case AGEN_CONST: + if ( pStage->rgbGen != CGEN_CONST ) { + // FIXME: Not a perfect fit, if alpha is less than 1.0f and vertex colors are enabled then this doesn't work +#if 0 + for ( i = 0; i < tess.numVertexes; i++ ) { + tess.svars.colors[i][3] = pStage->constantColor[3]; + } +#endif + outmatrix[15] = pStage->constantColor[3] / 255.0f; + } + break; + case AGEN_WAVEFORM: + // From RB_CalcWaveAlpha + // FIXME: Not a perfect fit, if alpha is less than 1.0f and vertex colors are enabled then this doesn't work + { + float glow; + waveForm_t *wf = &pStage->alphaWave; + glow = EvalWaveFormClamped( wf ); + outmatrix[15] = glow; + } + break; + case AGEN_LIGHTING_SPECULAR: + // FIXME: Can't do this with just a matrix + // RB_CalcSpecularAlpha( ( unsigned char * ) tess.svars.colors ); + break; + case AGEN_ENTITY: + // FIXME: Doesn't work if alpha is less than 1.0f and vertex colors are enabled + //RB_CalcAlphaFromEntity( ( unsigned char * ) tess.svars.colors ); + if (backEnd.currentEntity) + { + outmatrix[15] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[3] / 255.0f; + } + break; + case AGEN_ONE_MINUS_ENTITY: + // FIXME: Doesn't work if alpha is less than 1.0f and vertex colors are enabled + //RB_CalcAlphaFromOneMinusEntity( ( unsigned char * ) tess.svars.colors ); + if (backEnd.currentEntity) + { + outmatrix[15] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[3] / 255.0f; + } + break; + case AGEN_VERTEX: + // FIXME: Doesn't work if vertex colors are disabled +#if 0 + if ( pStage->rgbGen != CGEN_VERTEX ) { + for ( i = 0; i < tess.numVertexes; i++ ) { + tess.svars.colors[i][3] = tess.vertexColors[i][3]; + } + } +#endif + outmatrix[15] = 1.0f; + break; + case AGEN_ONE_MINUS_VERTEX: + // FIXME: Doesn't work at all + outmatrix[15] = 1.0f; +#if 0 + for ( i = 0; i < tess.numVertexes; i++ ) + { + tess.svars.colors[i][3] = 255 - tess.vertexColors[i][3]; + } +#endif + break; + case AGEN_PORTAL: + // FIXME: This very doesn't work. +#if 0 + { + unsigned char alpha; + + for ( i = 0; i < tess.numVertexes; i++ ) + { + float len; + vec3_t v; + + VectorSubtract( tess.xyz[i], backEnd.viewParms.or.origin, v ); + len = VectorLength( v ); + + len /= tess.shader->portalRange; + + if ( len < 0 ) + { + alpha = 0; + } + else if ( len > 1 ) + { + alpha = 0xff; + } + else + { + alpha = len * 0xff; + } + + tess.svars.colors[i][3] = alpha; + } + } +#endif + break; + } + + // FIXME: find some way to implement this stuff. +#if 0 + // + // fog adjustment for colors to fade out as fog increases + // + if ( tess.fogNum ) + { + switch ( pStage->adjustColorsForFog ) + { + case ACFF_MODULATE_RGB: + RB_CalcModulateColorsByFog( ( unsigned char * ) tess.svars.colors ); + break; + case ACFF_MODULATE_ALPHA: + RB_CalcModulateAlphasByFog( ( unsigned char * ) tess.svars.colors ); + break; + case ACFF_MODULATE_RGBA: + RB_CalcModulateRGBAsByFog( ( unsigned char * ) tess.svars.colors ); + break; + case ACFF_NONE: + break; + } + } + + // if in greyscale rendering mode turn all color values into greyscale. + if(r_greyscale->integer) + { + int scale; + + for(i = 0; i < tess.numVertexes; i++) + { + scale = (tess.svars.colors[i][0] + tess.svars.colors[i][1] + tess.svars.colors[i][2]) / 3; + tess.svars.colors[i][0] = tess.svars.colors[i][1] = tess.svars.colors[i][2] = scale; + } + } +#endif +} + + /* =============== ComputeTexCoords @@ -1168,6 +2202,392 @@ static void RB_IterateStagesGeneric( shaderCommands_t *input ) } +/* +** RB_IterateStagesGenericVBO +*/ +static void RB_IterateStagesGenericVBO( shaderCommands_t *input ) +{ + int stage; + + qglEnableClientState( GL_VERTEX_ARRAY ); + qglVertexPointer(3, GL_FLOAT, glState.currentVBO->stride_xyz, BUFFER_OFFSET(glState.currentVBO->ofs_xyz)); + qglEnableClientState( GL_NORMAL_ARRAY ); + qglNormalPointer(GL_FLOAT, glState.currentVBO->stride_normal, BUFFER_OFFSET(glState.currentVBO->ofs_normal)); + + for ( stage = 0; stage < MAX_SHADER_STAGES; stage++ ) + { + shaderStage_t *pStage = tess.xstages[stage]; + qboolean disableVertexColors = qfalse; + float matrix[16]; + + if ( !pStage ) + { + break; + } + + ComputeColorMatrix( pStage, matrix, &disableVertexColors); + + if (!disableVertexColors) + { + qglMatrixMode(GL_COLOR); + qglLoadMatrixf(matrix); + } + else + { + qglColor4fv(&matrix[12]); + } + + qglMatrixMode(GL_MODELVIEW); + + if (!disableVertexColors) + { + qglEnableClientState( GL_COLOR_ARRAY ); + qglColorPointer(4, GL_UNSIGNED_BYTE, glState.currentVBO->stride_vertexcolor, BUFFER_OFFSET(glState.currentVBO->ofs_vertexcolor)); + } + else + { + qglDisableClientState( GL_COLOR_ARRAY ); + } + + // + // do multitexture + // + if ( pStage->bundle[1].image[0] != 0 ) + { + DrawMultitexturedVBO( input, stage ); + } + else + { + float matrix[16]; + + GenerateGLTexCoords( pStage, 0 ); + + ComputeTexMatrix( pStage, 0, matrix ); + qglMatrixMode(GL_TEXTURE); + qglLoadMatrixf(matrix); + qglMatrixMode(GL_MODELVIEW); + + // + // set state + // + if ( pStage->bundle[0].vertexLightmap && ( (r_vertexLight->integer && !r_uiFullScreen->integer) || glConfig.hardwareType == GLHW_PERMEDIA2 ) && r_lightmap->integer ) + { + GL_Bind( tr.whiteImage ); + } + else + R_BindAnimatedImage( &pStage->bundle[0] ); + + GL_State( pStage->stateBits ); + + // + // draw + // + + if (input->multiDrawPrimitives) + { + R_DrawMultiElementsVBO(input->multiDrawPrimitives, input->multiDrawFirstIndex, input->multiDrawNumIndexes); + } + else + { + R_DrawElementsVBO(input->numIndexes, input->firstIndex); + } + + UndoGLTexCoords( pStage, 0 ); + qglEnableClientState ( GL_COLOR_ARRAY ); + qglMatrixMode(GL_COLOR); + qglLoadIdentity(); + qglMatrixMode(GL_TEXTURE); + qglLoadIdentity(); + qglMatrixMode(GL_MODELVIEW); + + } + // allow skipping out to show just lightmaps during development + if ( r_lightmap->integer && ( pStage->bundle[0].isLightmap || pStage->bundle[1].isLightmap || pStage->bundle[0].vertexLightmap ) ) + { + break; + } + } +} + + +static void DrawMultitexturedVBOGLSL( shaderProgram_t *sp, shaderCommands_t *input, int stage ) { + shaderStage_t *pStage; + matrix_t matrix; + vec4_t vector; + + pStage = tess.xstages[stage]; + + // + // base + // + GLSL_SetUniform_TCGen0(sp, pStage->bundle[0].tcGen); + if (pStage->bundle[0].tcGen == TCGEN_VECTOR) + { + vector[3] = 0.0f; + VectorCopy(pStage->bundle[0].tcGenVectors[0], vector); + GLSL_SetUniform_TCGen0Vector0(sp, vector); + VectorCopy(pStage->bundle[0].tcGenVectors[1], vector); + GLSL_SetUniform_TCGen0Vector1(sp, vector); + } + + ComputeTexMatrix( pStage, 0, matrix ); + GLSL_SetUniform_Texture0Matrix(sp, matrix); + + R_BindAnimatedImageToTMU( &pStage->bundle[0], 0 ); + + // + // lightmap/secondary pass + // + if ( r_lightmap->integer ) { + GLSL_SetUniform_Texture1Env(sp, GL_REPLACE); + } else { + GLSL_SetUniform_Texture1Env(sp, tess.shader->multitextureEnv); + } + + R_BindAnimatedImageToTMU( &pStage->bundle[1], 1 ); + + if (input->multiDrawPrimitives) + { + R_DrawMultiElementsVBO(input->multiDrawPrimitives, input->multiDrawFirstIndex, input->multiDrawNumIndexes); + } + else + { + R_DrawElementsVBO(input->numIndexes, input->firstIndex); + } +} + +static unsigned int RB_CalcShaderVertexAttribs( shaderCommands_t *input ) +{ + unsigned int vertexAttribs = input->shader->vertexAttribs; + + if(glState.vertexAttribsInterpolation > 0.0f) + { + vertexAttribs |= ATTR_POSITION2; + if (vertexAttribs & ATTR_NORMAL) + { + vertexAttribs |= ATTR_NORMAL2; + } + } + + return vertexAttribs; +} + + +static void RB_IterateStagesGenericVBOGLSL( shaderCommands_t *input ) +{ + int stage; + matrix_t matrix; + shaderProgram_t *sp = GLSL_GetGenericShaderProgram(); + + GLSL_BindProgram(sp); + + GLSL_SetUniform_ModelViewProjectionMatrix(sp, glState.modelviewProjection); + GLSL_SetUniform_ViewOrigin(sp, backEnd.or.viewOrigin); + + GLSL_SetUniform_VertexLerp(sp, glState.vertexAttribsInterpolation); + + // u_DeformGen + if(input->shader->numDeforms) + { + deformStage_t *ds; + + // only support the first one + ds = &input->shader->deforms[0]; + + switch (ds->deformation) + { + case DEFORM_WAVE: + GLSL_SetUniform_DeformGen(sp, ds->deformationWave.func); + GLSL_SetUniform_DeformWave(sp, &ds->deformationWave); + GLSL_SetUniform_DeformSpread(sp, ds->deformationSpread); + GLSL_SetUniform_Time(sp, tess.shaderTime); + break; + + case DEFORM_BULGE: + GLSL_SetUniform_DeformGen(sp, DGEN_BULGE); + GLSL_SetUniform_DeformBulge(sp, ds); + GLSL_SetUniform_Time(sp, tess.shaderTime); + break; + + default: + GLSL_SetUniform_DeformGen(sp, DGEN_NONE); + break; + } + } + else + { + GLSL_SetUniform_DeformGen(sp, DGEN_NONE); + } + + if ( input->fogNum ) { + fog_t *fog; + vec3_t local; + vec4_t fogDistanceVector, fogDepthVector = {0, 0, 0, 0}; + float eyeT; + + fog = tr.world->fogs + tess.fogNum; + + VectorSubtract( backEnd.or.origin, backEnd.viewParms.or.origin, local ); + fogDistanceVector[0] = -backEnd.or.modelMatrix[2]; + fogDistanceVector[1] = -backEnd.or.modelMatrix[6]; + fogDistanceVector[2] = -backEnd.or.modelMatrix[10]; + fogDistanceVector[3] = DotProduct( local, backEnd.viewParms.or.axis[0] ); + + // scale the fog vectors based on the fog's thickness + VectorScale4(fogDistanceVector, fog->tcScale, fogDistanceVector); + + // rotate the gradient vector for this orientation + if ( fog->hasSurface ) { + fogDepthVector[0] = fog->surface[0] * backEnd.or.axis[0][0] + + fog->surface[1] * backEnd.or.axis[0][1] + fog->surface[2] * backEnd.or.axis[0][2]; + fogDepthVector[1] = fog->surface[0] * backEnd.or.axis[1][0] + + fog->surface[1] * backEnd.or.axis[1][1] + fog->surface[2] * backEnd.or.axis[1][2]; + fogDepthVector[2] = fog->surface[0] * backEnd.or.axis[2][0] + + fog->surface[1] * backEnd.or.axis[2][1] + fog->surface[2] * backEnd.or.axis[2][2]; + fogDepthVector[3] = -fog->surface[3] + DotProduct( backEnd.or.origin, fog->surface ); + + eyeT = DotProduct( backEnd.or.viewOrigin, fogDepthVector ) + fogDepthVector[3]; + } else { + eyeT = 1; // non-surface fog always has eye inside + } + + GLSL_SetUniform_FogDistance(sp, fogDistanceVector); + GLSL_SetUniform_FogDepth(sp, fogDepthVector); + GLSL_SetUniform_FogEyeT(sp, eyeT); + } + + for ( stage = 0; stage < MAX_SHADER_STAGES; stage++ ) + { + shaderStage_t *pStage = input->xstages[stage]; + qboolean setcolor = qfalse; + + if ( !pStage ) + { + break; + } + + GL_State( pStage->stateBits ); + + switch (pStage->rgbGen) + { + case CGEN_IDENTITY: + case CGEN_EXACT_VERTEX: + case CGEN_LIGHTING_DIFFUSE: + break; + default: + setcolor = qtrue; + } + + switch (pStage->alphaGen) + { + case AGEN_SKIP: + case AGEN_IDENTITY: + case AGEN_LIGHTING_SPECULAR: + case AGEN_VERTEX: + case AGEN_ONE_MINUS_VERTEX: + case AGEN_PORTAL: + break; + default: + setcolor = qtrue; + } + + if (setcolor) + { + vec4_t color; + + ComputeHelperColor (pStage, color); + GLSL_SetUniform_Color(sp, color); + } + + if (pStage->rgbGen == CGEN_LIGHTING_DIFFUSE) + { + vec3_t vec; + + VectorScale(backEnd.currentEntity->ambientLight, 1.0f / 255.0f, vec); + GLSL_SetUniform_AmbientLight(sp, vec); + + VectorScale(backEnd.currentEntity->directedLight, 1.0f / 255.0f, vec); + GLSL_SetUniform_DirectedLight(sp, vec); + + GLSL_SetUniform_LightDir(sp, backEnd.currentEntity->lightDir); + } + + if (pStage->alphaGen == AGEN_PORTAL) + { + GLSL_SetUniform_PortalRange(sp, tess.shader->portalRange); + } + + GLSL_SetUniform_ColorGen(sp, pStage->rgbGen); + GLSL_SetUniform_AlphaGen(sp, pStage->alphaGen); + + if ( input->fogNum ) + { + GLSL_SetUniform_FogAdjustColors(sp, pStage->adjustColorsForFog); + } + else + { + GLSL_SetUniform_FogAdjustColors(sp, 0); + } + + // + // do multitexture + // + if ( pStage->bundle[1].image[0] != 0 ) + { + DrawMultitexturedVBOGLSL( sp, input, stage ); + } + else + { + GLSL_SetUniform_TCGen0(sp, pStage->bundle[0].tcGen); + if (pStage->bundle[0].tcGen == TCGEN_VECTOR) + { + vec4_t vector; + + VectorCopy(pStage->bundle[0].tcGenVectors[0], vector); + vector[3] = 0.0f; + GLSL_SetUniform_TCGen0Vector0(sp, vector); + VectorCopy(pStage->bundle[0].tcGenVectors[1], vector); + GLSL_SetUniform_TCGen0Vector1(sp, vector); + } + + ComputeTexMatrix( pStage, 0, matrix ); + GLSL_SetUniform_Texture0Matrix(sp, matrix); + + // + // set state + // + if ( pStage->bundle[0].vertexLightmap && ( (r_vertexLight->integer && !r_uiFullScreen->integer) || glConfig.hardwareType == GLHW_PERMEDIA2 ) && r_lightmap->integer ) + { + GL_Bind( tr.whiteImage ); + } + else + R_BindAnimatedImage( &pStage->bundle[0] ); + + GLSL_SetUniform_Texture1Env(sp, 0); + + // + // draw + // + + if (input->multiDrawPrimitives) + { + R_DrawMultiElementsVBO(input->multiDrawPrimitives, input->multiDrawFirstIndex, input->multiDrawNumIndexes); + } + else + { + R_DrawElementsVBO(input->numIndexes, input->firstIndex); + } + } + + // allow skipping out to show just lightmaps during development + if ( r_lightmap->integer && ( pStage->bundle[0].isLightmap || pStage->bundle[1].isLightmap || pStage->bundle[0].vertexLightmap ) ) + { + break; + } + } +} + + /* ** RB_StageIteratorGeneric */ @@ -1176,6 +2596,11 @@ void RB_StageIteratorGeneric( void ) shaderCommands_t *input; input = &tess; + + if (!input->numVertexes || !input->numIndexes) + { + return; + } RB_DeformTessGeometry(); @@ -1282,6 +2707,119 @@ void RB_StageIteratorGeneric( void ) } +/* +** RB_StageIteratorGenericVBO +*/ +void RB_StageIteratorGenericVBO( void ) +{ + shaderCommands_t *input; + unsigned int vertexAttribs = 0; + + input = &tess; + + if (!input->numVertexes || !input->numIndexes) + { + return; + } + + if (tess.useInternalVBO) + { + RB_DeformTessGeometry(); + } + + vertexAttribs = RB_CalcShaderVertexAttribs( input ); + + if (tess.useInternalVBO) + { + RB_UpdateVBOs(vertexAttribs); + } + else + { + backEnd.pc.c_staticVboDraws++; + } + + // + // log this call + // + if ( r_logFile->integer ) + { + // don't just call LogComment, or we will get + // a call to va() every frame! + GLimp_LogComment( va("--- RB_StageIteratorGenericVBO( %s ) ---\n", tess.shader->name) ); + } + + // + // set face culling appropriately + // + GL_Cull( input->shader->cullType ); + + // set polygon offset if necessary + if ( input->shader->polygonOffset ) + { + qglEnable( GL_POLYGON_OFFSET_FILL ); + qglPolygonOffset( r_offsetFactor->value, r_offsetUnits->value ); + } + + // + // Set vertex attribs and pointers + // + if (glRefConfig.glsl && r_arb_shader_objects->integer) + { + GLSL_VertexAttribsState(vertexAttribs); + } + + + // + // call shader function + // + if (glRefConfig.glsl && r_arb_shader_objects->integer) + { + RB_IterateStagesGenericVBOGLSL( input ); + } + else + { + RB_IterateStagesGenericVBO( input ); + } + + // + // now do any dynamic lighting needed + // + if ( tess.dlightBits && tess.shader->sort <= SS_OPAQUE + && !(tess.shader->surfaceFlags & (SURF_NODLIGHT | SURF_SKY) ) ) { + if (glRefConfig.glsl && r_arb_shader_objects->integer) + { + ProjectDlightTextureVBOGLSL(); + } + else + { + // FIXME + } + } + + // + // now do fog + // + if ( tess.fogNum && tess.shader->fogPass ) { + if (glRefConfig.glsl && r_arb_shader_objects->integer) + { + RB_FogPassVBOGLSL(); + } + else + { + // FIXME: figure out a way to do fog without GLSL + } + } + + // + // reset polygon offset + // + if ( input->shader->polygonOffset ) + { + qglDisable( GL_POLYGON_OFFSET_FILL ); + } +} + + /* ** RB_StageIteratorVertexLitTexture */ @@ -1290,6 +2828,13 @@ void RB_StageIteratorVertexLitTexture( void ) shaderCommands_t *input; shader_t *shader; + if(glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer) + { + //R_BindNullVBO(); + //R_BindNullIBO(); + return; + } + input = &tess; shader = input->shader; @@ -1366,6 +2911,13 @@ void RB_StageIteratorVertexLitTexture( void ) void RB_StageIteratorLightmappedMultitexture( void ) { shaderCommands_t *input; + if(glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer) + { + //R_BindNullVBO(); + //R_BindNullIBO(); + return; + } + input = &tess; // @@ -1473,7 +3025,7 @@ void RB_EndSurface( void ) { input = &tess; - if (input->numIndexes == 0) { + if (input->numIndexes == 0 || input->numVertexes == 0) { return; } @@ -1516,8 +3068,18 @@ void RB_EndSurface( void ) { if ( r_shownormals->integer ) { DrawNormals (input); } + + if (glRefConfig.vertexBufferObject) + { + //R_BindNullVBO(); + //R_BindNullIBO(); + } + // clear shader so we can tell we don't have any unclosed surfaces tess.numIndexes = 0; + tess.numVertexes = 0; + tess.firstIndex = 0; + tess.multiDrawPrimitives = 0; GLimp_LogComment( "----------\n" ); } diff --git a/reaction/code/renderer/tr_shade_calc.c b/reaction/code/renderer/tr_shade_calc.c index 840c58eb..6a047d11 100644 --- a/reaction/code/renderer/tr_shade_calc.c +++ b/reaction/code/renderer/tr_shade_calc.c @@ -57,7 +57,7 @@ static float *TableForFunc( genFunc_t func ) ** ** Evaluates a given waveForm_t, referencing backEnd.refdef.time directly */ -static float EvalWaveForm( const waveForm_t *wf ) +float EvalWaveForm( const waveForm_t *wf ) { float *table; @@ -66,7 +66,7 @@ static float EvalWaveForm( const waveForm_t *wf ) return WAVEVALUE( table, wf->base, wf->amplitude, wf->phase, wf->frequency ); } -static float EvalWaveFormClamped( const waveForm_t *wf ) +float EvalWaveFormClamped( const waveForm_t *wf ) { float glow = EvalWaveForm( wf ); @@ -104,6 +104,24 @@ void RB_CalcStretchTexCoords( const waveForm_t *wf, float *st ) RB_CalcTransformTexCoords( &tmi, st ); } +void RB_CalcStretchTexMatrix( const waveForm_t *wf, float *matrix ) +{ + float p; + texModInfo_t tmi; + + p = 1.0f / EvalWaveForm( wf ); + + tmi.matrix[0][0] = p; + tmi.matrix[1][0] = 0; + tmi.translate[0] = 0.5f - 0.5f * p; + + tmi.matrix[0][1] = 0; + tmi.matrix[1][1] = p; + tmi.translate[1] = 0.5f - 0.5f * p; + + RB_CalcTransformTexMatrix( &tmi, matrix ); +} + /* ==================================================================== @@ -305,6 +323,7 @@ void DeformText( const char *text ) { // clear the shader indexes tess.numIndexes = 0; tess.numVertexes = 0; + tess.firstIndex = 0; color[0] = color[1] = color[2] = color[3] = 255; @@ -368,6 +387,7 @@ static void AutospriteDeform( void ) { oldVerts = tess.numVertexes; tess.numVertexes = 0; tess.numIndexes = 0; + tess.firstIndex = 0; if ( backEnd.currentEntity != &tr.worldEntity ) { GlobalVectorToLocal( backEnd.viewParms.or.axis[1], leftDir ); @@ -542,6 +562,15 @@ void RB_DeformTessGeometry( void ) { int i; deformStage_t *ds; + if (glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer) + { + if(!ShaderRequiresCPUDeforms(tess.shader)) + { + // we don't need the following CPU deforms + return; + } + } + for ( i = 0 ; i < tess.shader->numDeforms ; i++ ) { ds = &tess.shader->deforms[ i ]; @@ -934,6 +963,21 @@ void RB_CalcTurbulentTexCoords( const waveForm_t *wf, float *st ) } } +void RB_CalcTurbulentTexMatrix( const waveForm_t *wf, matrix_t matrix ) +{ + float now; + + now = ( wf->phase + tess.shaderTime * wf->frequency ); + + // bit of a hack here, hide amplitude and now in the matrix + // the vertex program will extract them and perform a turbulent pass last if it's nonzero + + matrix[ 0] = 1.0f; matrix[ 4] = 0.0f; matrix[ 8] = 0.0f; matrix[12] = wf->amplitude; + matrix[ 1] = 0.0f; matrix[ 5] = 1.0f; matrix[ 9] = 0.0f; matrix[13] = now; + matrix[ 2] = 0.0f; matrix[ 6] = 0.0f; matrix[10] = 1.0f; matrix[14] = 0.0f; + matrix[ 3] = 0.0f; matrix[ 7] = 0.0f; matrix[11] = 0.0f; matrix[15] = 1.0f; +} + /* ** RB_CalcScaleTexCoords */ @@ -948,6 +992,14 @@ void RB_CalcScaleTexCoords( const float scale[2], float *st ) } } +void RB_CalcScaleTexMatrix( const float scale[2], float *matrix ) +{ + matrix[ 0] = scale[0]; matrix[ 4] = 0.0f; matrix[ 8] = 0.0f; matrix[12] = 0.0f; + matrix[ 1] = 0.0f; matrix[ 5] = scale[1]; matrix[ 9] = 0.0f; matrix[13] = 0.0f; + matrix[ 2] = 0.0f; matrix[ 6] = 0.0f; matrix[10] = 1.0f; matrix[14] = 0.0f; + matrix[ 3] = 0.0f; matrix[ 7] = 0.0f; matrix[11] = 0.0f; matrix[15] = 1.0f; +} + /* ** RB_CalcScrollTexCoords */ @@ -972,6 +1024,26 @@ void RB_CalcScrollTexCoords( const float scrollSpeed[2], float *st ) } } +void RB_CalcScrollTexMatrix( const float scrollSpeed[2], float *matrix ) +{ + float timeScale = tess.shaderTime; + float adjustedScrollS, adjustedScrollT; + + adjustedScrollS = scrollSpeed[0] * timeScale; + adjustedScrollT = scrollSpeed[1] * timeScale; + + // clamp so coordinates don't continuously get larger, causing problems + // with hardware limits + adjustedScrollS = adjustedScrollS - floor( adjustedScrollS ); + adjustedScrollT = adjustedScrollT - floor( adjustedScrollT ); + + + matrix[ 0] = 1.0f; matrix[ 4] = 0.0f; matrix[ 8] = adjustedScrollS; matrix[12] = 0.0f; + matrix[ 1] = 0.0f; matrix[ 5] = 1.0f; matrix[ 9] = adjustedScrollT; matrix[13] = 0.0f; + matrix[ 2] = 0.0f; matrix[ 6] = 0.0f; matrix[10] = 1.0f; matrix[14] = 0.0f; + matrix[ 3] = 0.0f; matrix[ 7] = 0.0f; matrix[11] = 0.0f; matrix[15] = 1.0f; +} + /* ** RB_CalcTransformTexCoords */ @@ -989,6 +1061,14 @@ void RB_CalcTransformTexCoords( const texModInfo_t *tmi, float *st ) } } +void RB_CalcTransformTexMatrix( const texModInfo_t *tmi, float *matrix ) +{ + matrix[ 0] = tmi->matrix[0][0]; matrix[ 4] = tmi->matrix[1][0]; matrix[ 8] = tmi->translate[0]; matrix[12] = 0.0f; + matrix[ 1] = tmi->matrix[0][1]; matrix[ 5] = tmi->matrix[1][1]; matrix[ 9] = tmi->translate[1]; matrix[13] = 0.0f; + matrix[ 2] = 0.0f; matrix[ 6] = 0.0f; matrix[10] = 1.0f; matrix[14] = 0.0f; + matrix[ 3] = 0.0f; matrix[ 7] = 0.0f; matrix[11] = 0.0f; matrix[15] = 1.0f; +} + /* ** RB_CalcRotateTexCoords */ @@ -1017,6 +1097,31 @@ void RB_CalcRotateTexCoords( float degsPerSecond, float *st ) RB_CalcTransformTexCoords( &tmi, st ); } +void RB_CalcRotateTexMatrix( float degsPerSecond, float *matrix ) +{ + float timeScale = tess.shaderTime; + float degs; + int index; + float sinValue, cosValue; + texModInfo_t tmi; + + degs = -degsPerSecond * timeScale; + index = degs * ( FUNCTABLE_SIZE / 360.0f ); + + sinValue = tr.sinTable[ index & FUNCTABLE_MASK ]; + cosValue = tr.sinTable[ ( index + FUNCTABLE_SIZE / 4 ) & FUNCTABLE_MASK ]; + + tmi.matrix[0][0] = cosValue; + tmi.matrix[1][0] = -sinValue; + tmi.translate[0] = 0.5 - 0.5 * cosValue + 0.5 * sinValue; + + tmi.matrix[0][1] = sinValue; + tmi.matrix[1][1] = cosValue; + tmi.translate[1] = 0.5 - 0.5 * sinValue - 0.5 * cosValue; + + RB_CalcTransformTexMatrix( &tmi, matrix ); +} + diff --git a/reaction/code/renderer/tr_shader.c b/reaction/code/renderer/tr_shader.c index a88ba2bd..63daffa9 100644 --- a/reaction/code/renderer/tr_shader.c +++ b/reaction/code/renderer/tr_shader.c @@ -1551,6 +1551,7 @@ static qboolean ParseShader( char **text ) else if ( !Q_stricmp(token, "portal") ) { shader.sort = SS_PORTAL; + shader.isPortal = qtrue; continue; } // skyparms @@ -1634,6 +1635,11 @@ static void ComputeStageIteratorFunc( void ) { shader.optimalStageIteratorFunc = RB_StageIteratorGeneric; + if (glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer) + { + shader.optimalStageIteratorFunc = RB_StageIteratorGenericVBO; + } + // // see if this should go into the sky path // @@ -1648,6 +1654,12 @@ static void ComputeStageIteratorFunc( void ) return; } + if ( glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer ) + { + // VBOs don't support the fast path! + return; + } + // // see if this can go into the vertex lit fast path // @@ -1704,6 +1716,133 @@ done: return; } +/* +=================== +ComputeVertexAttribs + +Check which vertex attributes we only need, so we +don't need to submit/copy all of them. +=================== +*/ +static void ComputeVertexAttribs() +{ + int i, stage; + + shader.vertexAttribs = ATTR_POSITION; + + // portals always need normals, for SurfIsOffscreen() + if (shader.isPortal) + { + shader.vertexAttribs |= ATTR_NORMAL; + } + + if (shader.defaultShader) + { + shader.vertexAttribs |= ATTR_TEXCOORD; + return; + } + + if(shader.numDeforms) + { + for ( i = 0; i < shader.numDeforms; i++) + { + deformStage_t *ds = &shader.deforms[i]; + + switch (ds->deformation) + { + case DEFORM_BULGE: + shader.vertexAttribs |= ATTR_NORMAL | ATTR_TEXCOORD; + break; + + case DEFORM_AUTOSPRITE: + shader.vertexAttribs |= ATTR_NORMAL | ATTR_COLOR; + break; + + case DEFORM_WAVE: + case DEFORM_NORMALS: + case DEFORM_TEXT0: + case DEFORM_TEXT1: + case DEFORM_TEXT2: + case DEFORM_TEXT3: + case DEFORM_TEXT4: + case DEFORM_TEXT5: + case DEFORM_TEXT6: + case DEFORM_TEXT7: + shader.vertexAttribs |= ATTR_NORMAL; + break; + + default: + case DEFORM_NONE: + case DEFORM_MOVE: + case DEFORM_PROJECTION_SHADOW: + case DEFORM_AUTOSPRITE2: + break; + } + } + } + + for ( stage = 0; stage < MAX_SHADER_STAGES; stage++ ) + { + shaderStage_t *pStage = &stages[stage]; + + if ( !pStage->active ) + { + break; + } + + for (i = 0; i < 2; i++) + { + if (i == 1 && ( pStage->bundle[1].image[0] == 0 )) + { + break; + } + + switch(pStage->bundle[i].tcGen) + { + case TCGEN_TEXTURE: + shader.vertexAttribs |= ATTR_TEXCOORD; + break; + case TCGEN_LIGHTMAP: + shader.vertexAttribs |= ATTR_LIGHTCOORD; + break; + case TCGEN_ENVIRONMENT_MAPPED: + shader.vertexAttribs |= ATTR_NORMAL; + break; + + default: + break; + } + } + + switch(pStage->rgbGen) + { + case CGEN_EXACT_VERTEX: + case CGEN_VERTEX: + case CGEN_ONE_MINUS_VERTEX: + shader.vertexAttribs |= ATTR_COLOR; + break; + + default: + break; + } + + switch(pStage->alphaGen) + { + case AGEN_LIGHTING_SPECULAR: + shader.vertexAttribs |= ATTR_NORMAL; + break; + + case AGEN_VERTEX: + case AGEN_ONE_MINUS_VERTEX: + shader.vertexAttribs |= ATTR_COLOR; + break; + + default: + break; + } + } +} + typedef struct { int blendA; int blendB; @@ -2284,7 +2423,8 @@ static shader_t *FinishShader( void ) { ri.Printf( PRINT_DEVELOPER, "WARNING: shader '%s' has VERTEX forced lightmap!\n", shader.name ); } else { ri.Printf( PRINT_DEVELOPER, "WARNING: shader '%s' has lightmap but no lightmap stage!\n", shader.name ); - shader.lightmapIndex = LIGHTMAP_NONE; + // Don't set this, it will just add duplicate shaders to the hash + //shader.lightmapIndex = LIGHTMAP_NONE; } } @@ -2301,6 +2441,9 @@ static shader_t *FinishShader( void ) { // determine which stage iterator function is appropriate ComputeStageIteratorFunc(); + // determine which vertex attributes this shader needs + ComputeVertexAttribs(); + return GeneratePermanentShader(); } @@ -2486,12 +2629,6 @@ shader_t *R_FindShader( const char *name, int lightmapIndex, qboolean mipRawImag stages[i].bundle[0].texMods = texMods[i]; } - // FIXME: set these "need" values apropriately - shader.needsNormal = qtrue; - shader.needsST1 = qtrue; - shader.needsST2 = qtrue; - shader.needsColor = qtrue; - // // attempt to define shader from an explicit parameter file // @@ -2622,12 +2759,6 @@ qhandle_t RE_RegisterShaderFromImage(const char *name, int lightmapIndex, image_ stages[i].bundle[0].texMods = texMods[i]; } - // FIXME: set these "need" values apropriately - shader.needsNormal = qtrue; - shader.needsST1 = qtrue; - shader.needsST2 = qtrue; - shader.needsColor = qtrue; - // // create the default shading commands // @@ -2848,6 +2979,8 @@ void R_ShaderList_f (void) { if ( shader->optimalStageIteratorFunc == RB_StageIteratorGeneric ) { ri.Printf( PRINT_ALL, "gen " ); + } else if ( shader->optimalStageIteratorFunc == RB_StageIteratorGenericVBO ) { + ri.Printf( PRINT_ALL, "genv" ); } else if ( shader->optimalStageIteratorFunc == RB_StageIteratorSky ) { ri.Printf( PRINT_ALL, "sky " ); } else if ( shader->optimalStageIteratorFunc == RB_StageIteratorLightmappedMultitexture ) { diff --git a/reaction/code/renderer/tr_sky.c b/reaction/code/renderer/tr_sky.c index 02624f3d..bd8ecab2 100644 --- a/reaction/code/renderer/tr_sky.c +++ b/reaction/code/renderer/tr_sky.c @@ -384,6 +384,105 @@ static void DrawSkySide( struct image_s *image, const int mins[2], const int max } } +static void DrawSkySideVBO( struct image_s *image, const int mins[2], const int maxs[2] ) +{ + int s, t; + //int firstVertex = tess.numVertexes; + //int firstIndex = tess.numIndexes; + matrix_t matrix; + vec4_t color; + + tess.numVertexes = 0; + tess.numIndexes = 0; + tess.firstIndex = 0; + + GL_Bind( image ); + + for ( t = mins[1]+HALF_SKY_SUBDIVISIONS; t <= maxs[1]+HALF_SKY_SUBDIVISIONS; t++ ) + { + for ( s = mins[0]+HALF_SKY_SUBDIVISIONS; s <= maxs[0]+HALF_SKY_SUBDIVISIONS; s++ ) + { + tess.xyz[tess.numVertexes][0] = s_skyPoints[t][s][0]; + tess.xyz[tess.numVertexes][1] = s_skyPoints[t][s][1]; + tess.xyz[tess.numVertexes][2] = s_skyPoints[t][s][2]; + tess.xyz[tess.numVertexes][3] = 1.0; + + tess.texCoords[tess.numVertexes][0][0] = s_skyTexCoords[t][s][0]; + tess.texCoords[tess.numVertexes][0][1] = s_skyTexCoords[t][s][1]; + + tess.numVertexes++; + + if(tess.numVertexes >= SHADER_MAX_VERTEXES) + { + ri.Error(ERR_DROP, "SHADER_MAX_VERTEXES hit in DrawSkySideVBO()\n"); + } + } + } + + for ( t = 0; t < maxs[1] - mins[1]; t++ ) + { + for ( s = 0; s < maxs[0] - mins[0]; s++ ) + { + if (tess.numIndexes + 6 >= SHADER_MAX_INDEXES) + { + ri.Error(ERR_DROP, "SHADER_MAX_INDEXES hit in DrawSkySideVBO()\n"); + } + + tess.indexes[tess.numIndexes++] = s + t * (maxs[0] - mins[0] + 1); + tess.indexes[tess.numIndexes++] = s + (t + 1) * (maxs[0] - mins[0] + 1); + tess.indexes[tess.numIndexes++] = (s + 1) + t * (maxs[0] - mins[0] + 1); + + tess.indexes[tess.numIndexes++] = (s + 1) + t * (maxs[0] - mins[0] + 1); + tess.indexes[tess.numIndexes++] = s + (t + 1) * (maxs[0] - mins[0] + 1); + tess.indexes[tess.numIndexes++] = (s + 1) + (t + 1) * (maxs[0] - mins[0] + 1); + } + } + + // FIXME: A lot of this can probably be removed for speed, and refactored into a more convenient function + RB_UpdateVBOs(ATTR_POSITION | ATTR_TEXCOORD); + + if (glRefConfig.glsl && r_arb_shader_objects->integer) + { + shaderProgram_t *sp = GLSL_GetGenericShaderProgram(); + + GLSL_VertexAttribsState(ATTR_POSITION | ATTR_TEXCOORD); + GLSL_BindProgram(sp); + + GLSL_SetUniform_ModelViewProjectionMatrix(sp, glState.modelviewProjection); + + GLSL_SetUniform_FogAdjustColors(sp, 0); + GLSL_SetUniform_DeformGen(sp, DGEN_NONE); + GLSL_SetUniform_TCGen0(sp, TCGEN_TEXTURE); + Matrix16Identity(matrix); + GLSL_SetUniform_Texture0Matrix(sp, matrix); + GLSL_SetUniform_Texture1Env(sp, 0); + GLSL_SetUniform_ColorGen(sp, CGEN_CONST); + GLSL_SetUniform_AlphaGen(sp, AGEN_CONST); + + color[0] = tr.identityLight; + color[1] = tr.identityLight; + color[2] = tr.identityLight; + color[3] = 1.0f; + GLSL_SetUniform_Color(sp, color); + } + else + { + qglEnableClientState( GL_VERTEX_ARRAY ); + qglEnableClientState( GL_TEXTURE_COORD_ARRAY ); + qglVertexPointer(3, GL_FLOAT, glState.currentVBO->stride_xyz, BUFFER_OFFSET(glState.currentVBO->ofs_xyz)); + qglTexCoordPointer( 2, GL_FLOAT, glState.currentVBO->stride_st, BUFFER_OFFSET(glState.currentVBO->ofs_st) ); + } + + qglDrawElements(GL_TRIANGLES, tess.numIndexes, GL_INDEX_TYPE, BUFFER_OFFSET(tess.firstIndex)); + + //R_BindNullVBO(); + //R_BindNullIBO(); + + tess.numIndexes = 0; + tess.numVertexes = 0; + tess.firstIndex = 0; +} + static void DrawSkyBox( shader_t *shader ) { int i; @@ -447,9 +546,19 @@ static void DrawSkyBox( shader_t *shader ) } } - DrawSkySide( shader->sky.outerbox[sky_texorder[i]], - sky_mins_subd, - sky_maxs_subd ); + if (glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer) + { + DrawSkySideVBO( shader->sky.outerbox[sky_texorder[i]], + sky_mins_subd, + sky_maxs_subd ); + } + else + { + DrawSkySide( shader->sky.outerbox[sky_texorder[i]], + sky_mins_subd, + sky_maxs_subd ); + } + } } @@ -617,6 +726,7 @@ void R_BuildCloudData( shaderCommands_t *input ) // set up for drawing tess.numIndexes = 0; tess.numVertexes = 0; + tess.firstIndex = 0; if ( input->shader->sky.cloudHeight ) { @@ -708,8 +818,17 @@ void RB_DrawSun( void ) { if ( !r_drawSun->integer ) { return; } - qglLoadMatrixf( backEnd.viewParms.world.modelMatrix ); - qglTranslatef (backEnd.viewParms.or.origin[0], backEnd.viewParms.or.origin[1], backEnd.viewParms.or.origin[2]); + + //qglLoadMatrixf( backEnd.viewParms.world.modelMatrix ); + //qglTranslatef (backEnd.viewParms.or.origin[0], backEnd.viewParms.or.origin[1], backEnd.viewParms.or.origin[2]); + { + // FIXME: this could be a lot cleaner + matrix_t trans, product; + + Matrix16Translation( backEnd.viewParms.or.origin, trans ); + Matrix16Multiply( backEnd.viewParms.world.modelMatrix, trans, product ); + GL_SetModelviewMatrix( product ); + } dist = backEnd.viewParms.zFar / 1.75; // div sqrt(3) size = dist * 0.4; @@ -816,22 +935,45 @@ void RB_StageIteratorSky( void ) { // draw the outer skybox if ( tess.shader->sky.outerbox[0] && tess.shader->sky.outerbox[0] != tr.defaultImage ) { - qglColor3f( tr.identityLight, tr.identityLight, tr.identityLight ); + matrix_t oldmodelview; + + if (!(glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer && + glRefConfig.glsl && r_arb_shader_objects->integer)) + { + qglColor3f( tr.identityLight, tr.identityLight, tr.identityLight ); + } - qglPushMatrix (); GL_State( 0 ); - qglTranslatef (backEnd.viewParms.or.origin[0], backEnd.viewParms.or.origin[1], backEnd.viewParms.or.origin[2]); + //qglTranslatef (backEnd.viewParms.or.origin[0], backEnd.viewParms.or.origin[1], backEnd.viewParms.or.origin[2]); + + { + // FIXME: this could be a lot cleaner + matrix_t trans, product; + + Matrix16Copy( glState.modelview, oldmodelview ); + Matrix16Translation( backEnd.viewParms.or.origin, trans ); + Matrix16Multiply( glState.modelview, trans, product ); + GL_SetModelviewMatrix( product ); + + } DrawSkyBox( tess.shader ); - qglPopMatrix(); + GL_SetModelviewMatrix( oldmodelview ); } // generate the vertexes for all the clouds, which will be drawn // by the generic shader routine R_BuildCloudData( &tess ); - RB_StageIteratorGeneric(); + if (glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer) + { + RB_StageIteratorGenericVBO(); + } + else + { + RB_StageIteratorGeneric(); + } // draw the inner skybox diff --git a/reaction/code/renderer/tr_surface.c b/reaction/code/renderer/tr_surface.c index 846126fa..22bc06ec 100644 --- a/reaction/code/renderer/tr_surface.c +++ b/reaction/code/renderer/tr_surface.c @@ -67,6 +67,21 @@ void RB_CheckOverflow( int verts, int indexes ) { } +void RB_CheckVBOandIBO(VBO_t *vbo, IBO_t *ibo) +{ + if (!(vbo == glState.currentVBO && ibo == glState.currentIBO) || tess.multiDrawPrimitives >= MAX_MULTIDRAW_PRIMITIVES) + { + RB_EndSurface(); + RB_BeginSurface(tess.shader, tess.fogNum); + + R_BindVBO(vbo); + R_BindIBO(ibo); + } + + tess.useInternalVBO = qfalse; +} + + /* ============== RB_AddQuadStampExt @@ -148,6 +163,97 @@ void RB_AddQuadStamp( vec3_t origin, vec3_t left, vec3_t up, byte *color ) { RB_AddQuadStampExt( origin, left, up, color, 0, 0, 1, 1 ); } + +/* +============== +RB_InstantQuad + +based on Tess_InstantQuad from xreal +============== +*/ +void RB_InstantQuad(vec4_t quadVerts[4]) +{ + matrix_t matrix; + + GLimp_LogComment("--- RB_InstantQuad ---\n"); + + tess.numVertexes = 0; + tess.numIndexes = 0; + tess.firstIndex = 0; + + VectorCopy4(quadVerts[0], tess.xyz[tess.numVertexes]); + tess.texCoords[tess.numVertexes][0][0] = 0; + tess.texCoords[tess.numVertexes][0][1] = 0; + tess.texCoords[tess.numVertexes][0][2] = 0; + tess.texCoords[tess.numVertexes][0][3] = 1; + tess.numVertexes++; + + VectorCopy4(quadVerts[1], tess.xyz[tess.numVertexes]); + tess.texCoords[tess.numVertexes][0][0] = 1; + tess.texCoords[tess.numVertexes][0][1] = 0; + tess.texCoords[tess.numVertexes][0][2] = 0; + tess.texCoords[tess.numVertexes][0][3] = 1; + tess.numVertexes++; + + VectorCopy4(quadVerts[2], tess.xyz[tess.numVertexes]); + tess.texCoords[tess.numVertexes][0][0] = 1; + tess.texCoords[tess.numVertexes][0][1] = 1; + tess.texCoords[tess.numVertexes][0][2] = 0; + tess.texCoords[tess.numVertexes][0][3] = 1; + tess.numVertexes++; + + VectorCopy4(quadVerts[3], tess.xyz[tess.numVertexes]); + tess.texCoords[tess.numVertexes][0][0] = 0; + tess.texCoords[tess.numVertexes][0][1] = 1; + tess.texCoords[tess.numVertexes][0][2] = 0; + tess.texCoords[tess.numVertexes][0][3] = 1; + tess.numVertexes++; + + tess.indexes[tess.numIndexes++] = 0; + tess.indexes[tess.numIndexes++] = 1; + tess.indexes[tess.numIndexes++] = 2; + tess.indexes[tess.numIndexes++] = 0; + tess.indexes[tess.numIndexes++] = 2; + tess.indexes[tess.numIndexes++] = 3; + + // FIXME: A lot of this can probably be removed for speed + RB_UpdateVBOs(ATTR_POSITION | ATTR_TEXCOORD); + + if (glRefConfig.glsl && r_arb_shader_objects->integer) + { + shaderProgram_t *sp = &tr.genericShader[0]; + GLSL_VertexAttribsState(ATTR_POSITION | ATTR_TEXCOORD); + GLSL_BindProgram(sp); + + GLSL_SetUniform_ModelViewProjectionMatrix(sp, glState.modelviewProjection); + + GLSL_SetUniform_FogAdjustColors(sp, 0); + GLSL_SetUniform_DeformGen(sp, DGEN_NONE); + GLSL_SetUniform_TCGen0(sp, TCGEN_TEXTURE); + Matrix16Identity(matrix); + GLSL_SetUniform_Texture0Matrix(sp, matrix); + GLSL_SetUniform_Texture1Env(sp, 0); + GLSL_SetUniform_ColorGen(sp, CGEN_IDENTITY); + GLSL_SetUniform_AlphaGen(sp, AGEN_IDENTITY); + } + else + { + qglEnableClientState( GL_VERTEX_ARRAY ); + qglVertexPointer(3, GL_FLOAT, glState.currentVBO->stride_xyz, BUFFER_OFFSET(glState.currentVBO->ofs_xyz)); + qglEnableClientState( GL_NORMAL_ARRAY ); + qglNormalPointer(GL_FLOAT, glState.currentVBO->stride_normal, BUFFER_OFFSET(glState.currentVBO->ofs_normal)); + } + qglDrawElements(GL_TRIANGLES, tess.numIndexes, GL_INDEX_TYPE, BUFFER_OFFSET(0)); + + //R_BindNullVBO(); + //R_BindNullIBO(); + + tess.numIndexes = 0; + tess.numVertexes = 0; + tess.firstIndex = 0; +} + + /* ============== RB_SurfaceSprite @@ -217,40 +323,35 @@ static void RB_SurfacePolychain( srfPoly_t *p ) { tess.numVertexes = numv; } - -/* -============= -RB_SurfaceTriangles -============= -*/ -static void RB_SurfaceTriangles( srfTriangles_t *srf ) { +static void RB_SurfaceHelper( int numVerts, srfVert_t *verts, int numTriangles, srfTriangle_t *triangles, int dlightBits) +{ int i; - drawVert_t *dv; - float *xyz, *normal, *texCoords; + srfTriangle_t *tri; + srfVert_t *dv; + float *xyz, *normal, *texCoords; + glIndex_t *index; byte *color; - int dlightBits; qboolean needsNormal; - dlightBits = srf->dlightBits[backEnd.smpFrame]; - tess.dlightBits |= dlightBits; + RB_CHECKOVERFLOW( numVerts, numTriangles * 3 ); - RB_CHECKOVERFLOW( srf->numVerts, srf->numIndexes ); - - for ( i = 0 ; i < srf->numIndexes ; i += 3 ) { - tess.indexes[ tess.numIndexes + i + 0 ] = tess.numVertexes + srf->indexes[ i + 0 ]; - tess.indexes[ tess.numIndexes + i + 1 ] = tess.numVertexes + srf->indexes[ i + 1 ]; - tess.indexes[ tess.numIndexes + i + 2 ] = tess.numVertexes + srf->indexes[ i + 2 ]; + tri = triangles; + index = &tess.indexes[ tess.numIndexes ]; + for ( i = 0 ; i < numTriangles ; i++, tri++ ) { + *index++ = tess.numVertexes + tri->indexes[0]; + *index++ = tess.numVertexes + tri->indexes[1]; + *index++ = tess.numVertexes + tri->indexes[2]; } - tess.numIndexes += srf->numIndexes; + tess.numIndexes += numTriangles * 3; - dv = srf->verts; + dv = verts; xyz = tess.xyz[ tess.numVertexes ]; normal = tess.normal[ tess.numVertexes ]; texCoords = tess.texCoords[ tess.numVertexes ][0]; color = tess.vertexColors[ tess.numVertexes ]; - needsNormal = tess.shader->needsNormal; + needsNormal = tess.shader->vertexAttribs & ATTR_NORMAL; - for ( i = 0 ; i < srf->numVerts ; i++, dv++, xyz += 4, normal += 4, texCoords += 4, color += 4 ) { + for ( i = 0 ; i < numVerts ; i++, dv++, xyz += 4, normal += 4, texCoords += 4, color += 4 ) { xyz[0] = dv->xyz[0]; xyz[1] = dv->xyz[1]; xyz[2] = dv->xyz[2]; @@ -267,14 +368,127 @@ static void RB_SurfaceTriangles( srfTriangles_t *srf ) { texCoords[2] = dv->lightmap[0]; texCoords[3] = dv->lightmap[1]; - *(int *)color = *(int *)dv->color; + *(int *)color = *(int *)dv->vertexColors; } - for ( i = 0 ; i < srf->numVerts ; i++ ) { - tess.vertexDlightBits[ tess.numVertexes + i] = dlightBits; +#if 0 // nothing even uses vertex dlightbits + for ( i = 0 ; i < numVerts ; i++ ) { + tess.vertexDlightBits[ tess.numVertexes + i ] = dlightBits; + } +#endif + + tess.dlightBits |= dlightBits; + + tess.numVertexes += numVerts; +} + +static qboolean RB_SurfaceHelperVBO(VBO_t *vbo, IBO_t *ibo, int numVerts, int numIndexes, int firstIndex, int dlightBits, qboolean shaderCheck) +{ + if( glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer && vbo && ibo) + { + int i, mergeForward, mergeBack; + GLvoid *firstIndexOffset, *lastIndexOffset; + + if (shaderCheck && !(!ShaderRequiresCPUDeforms(tess.shader) && !tess.shader->isSky && !tess.shader->isPortal)) + { + return qfalse; + } + + RB_CheckVBOandIBO(vbo, ibo); + + tess.dlightBits |= dlightBits; + + // merge this into any existing multidraw primitives + mergeForward = -1; + mergeBack = -1; + firstIndexOffset = BUFFER_OFFSET(firstIndex * sizeof(GL_INDEX_TYPE)); + lastIndexOffset = BUFFER_OFFSET((firstIndex + numIndexes) * sizeof(GL_INDEX_TYPE)); + + if (r_mergeMultidraws->integer) + { + i = 0; + + if (r_mergeMultidraws->integer == 1) + { + // lazy merge, only check the last primitive + + if (tess.multiDrawPrimitives) + { + i = tess.multiDrawPrimitives - 1; + } + } + + for (; i < tess.multiDrawPrimitives; i++) + { + if (tess.multiDrawLastIndex[i] == firstIndexOffset) + { + mergeBack = i; + } + + if (lastIndexOffset == tess.multiDrawFirstIndex[i]) + { + mergeForward = i; + } + } + } + + if (mergeBack != -1 && mergeForward == -1) + { + tess.multiDrawNumIndexes[mergeBack] += numIndexes; + tess.multiDrawLastIndex[mergeBack] = (byte *)tess.multiDrawFirstIndex[mergeBack] + tess.multiDrawNumIndexes[mergeBack] * sizeof(GL_INDEX_TYPE); + backEnd.pc.c_multidrawsMerged++; + } + else if (mergeBack == -1 && mergeForward != -1) + { + tess.multiDrawNumIndexes[mergeForward] += numIndexes; + tess.multiDrawFirstIndex[mergeForward] = firstIndexOffset; + tess.multiDrawLastIndex[mergeForward] = (byte *)tess.multiDrawFirstIndex[mergeForward] + tess.multiDrawNumIndexes[mergeForward] * sizeof(GL_INDEX_TYPE); + backEnd.pc.c_multidrawsMerged++; + } + else if (mergeBack != -1 && mergeForward != -1) + { + tess.multiDrawNumIndexes[mergeBack] += numIndexes + tess.multiDrawNumIndexes[mergeForward]; + tess.multiDrawLastIndex[mergeBack] = (byte *)tess.multiDrawFirstIndex[mergeBack] + tess.multiDrawNumIndexes[mergeBack] * sizeof(GL_INDEX_TYPE); + tess.multiDrawPrimitives--; + + if (mergeForward != tess.multiDrawPrimitives) + { + tess.multiDrawNumIndexes[mergeForward] = tess.multiDrawNumIndexes[tess.multiDrawPrimitives]; + tess.multiDrawFirstIndex[mergeForward] = tess.multiDrawFirstIndex[tess.multiDrawPrimitives]; + } + backEnd.pc.c_multidrawsMerged += 2; + } + else if (mergeBack == -1 && mergeForward == -1) + { + tess.multiDrawNumIndexes[tess.multiDrawPrimitives] = numIndexes; + tess.multiDrawFirstIndex[tess.multiDrawPrimitives] = firstIndexOffset; + tess.multiDrawLastIndex[tess.multiDrawPrimitives] = lastIndexOffset; + tess.multiDrawPrimitives++; + } + + backEnd.pc.c_multidraws++; + + tess.numIndexes += numIndexes; + tess.numVertexes += numVerts; + + return qtrue; } - tess.numVertexes += srf->numVerts; + return qfalse; +} + +/* +============= +RB_SurfaceTriangles +============= +*/ +static void RB_SurfaceTriangles( srfTriangles_t *srf ) { + if( RB_SurfaceHelperVBO (srf->vbo, srf->ibo, srf->numVerts, srf->numTriangles * 3, srf->firstIndex, srf->dlightBits[backEnd.smpFrame], qtrue ) ) + { + return; + } + + RB_SurfaceHelper(srf->numVerts, srf->verts, srf->numTriangles, srf->triangles, srf->dlightBits[backEnd.smpFrame]); } @@ -293,6 +507,8 @@ static void RB_SurfaceBeam( void ) vec3_t direction, normalized_direction; vec3_t start_points[NUM_BEAM_SEGS], end_points[NUM_BEAM_SEGS]; vec3_t oldorigin, origin; + matrix_t matrix; + vec4_t color; e = &backEnd.currentEntity->e; @@ -326,14 +542,78 @@ static void RB_SurfaceBeam( void ) GL_State( GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE ); - qglColor3f( 1, 0, 0 ); + // FIXME: Quake3 doesn't use this, so I never tested it + if (glRefConfig.vertexBufferObject && r_arb_vertex_buffer_object->integer) + { + tess.numVertexes = 0; + tess.numIndexes = 0; + tess.firstIndex = 0; - qglBegin( GL_TRIANGLE_STRIP ); - for ( i = 0; i <= NUM_BEAM_SEGS; i++ ) { - qglVertex3fv( start_points[ i % NUM_BEAM_SEGS] ); - qglVertex3fv( end_points[ i % NUM_BEAM_SEGS] ); + for ( i = 0; i <= NUM_BEAM_SEGS; i++ ) { + VectorCopy(start_points[ i % NUM_BEAM_SEGS ], tess.xyz[tess.numVertexes++]); + VectorCopy(end_points [ i % NUM_BEAM_SEGS ], tess.xyz[tess.numVertexes++]); + } + + for ( i = 0; i < NUM_BEAM_SEGS; i++ ) { + tess.indexes[tess.numIndexes++] = i * 2; + tess.indexes[tess.numIndexes++] = (i + 1) * 2; + tess.indexes[tess.numIndexes++] = 1 + i * 2; + + tess.indexes[tess.numIndexes++] = 1 + i * 2; + tess.indexes[tess.numIndexes++] = (i + 1) * 2; + tess.indexes[tess.numIndexes++] = 1 + (i + 1) * 2; + } + + // FIXME: A lot of this can probably be removed for speed, and refactored into a more convenient function + RB_UpdateVBOs(ATTR_POSITION); + + if (glRefConfig.glsl && r_arb_shader_objects->integer) + { + shaderProgram_t *sp = GLSL_GetGenericShaderProgram(); + + GLSL_VertexAttribsState(ATTR_POSITION); + GLSL_BindProgram(sp); + + GLSL_SetUniform_ModelViewProjectionMatrix(sp, glState.modelviewProjection); + + GLSL_SetUniform_FogAdjustColors(sp, 0); + GLSL_SetUniform_DeformGen(sp, DGEN_NONE); + GLSL_SetUniform_TCGen0(sp, TCGEN_IDENTITY); + Matrix16Identity(matrix); + GLSL_SetUniform_Texture0Matrix(sp, matrix); + GLSL_SetUniform_Texture1Env(sp, 0); + GLSL_SetUniform_ColorGen(sp, CGEN_CONST); + GLSL_SetUniform_AlphaGen(sp, AGEN_CONST); + + color[0] = 1.0f; + color[1] = 0.0f; + color[2] = 0.0f; + color[3] = 1.0f; + GLSL_SetUniform_Color(sp, color); + } + else + { + qglEnableClientState( GL_VERTEX_ARRAY ); + qglVertexPointer(3, GL_FLOAT, glState.currentVBO->stride_xyz, BUFFER_OFFSET(glState.currentVBO->ofs_xyz)); + } + + qglDrawElements(GL_TRIANGLES, tess.numIndexes, GL_INDEX_TYPE, BUFFER_OFFSET(tess.firstIndex)); + + tess.numIndexes = 0; + tess.numVertexes = 0; + tess.firstIndex = 0; + } + else + { + qglColor3f( 1, 0, 0 ); + + qglBegin( GL_TRIANGLE_STRIP ); + for ( i = 0; i <= NUM_BEAM_SEGS; i++ ) { + qglVertex3fv( start_points[ i % NUM_BEAM_SEGS] ); + qglVertex3fv( end_points[ i % NUM_BEAM_SEGS] ); + } + qglEnd(); } - qglEnd(); } //================================================================================ @@ -740,8 +1020,9 @@ static void LerpMeshVertexes_altivec(md3Surface_t *surf, float backlerp) } #endif -static void LerpMeshVertexes_scalar(md3Surface_t *surf, float backlerp) +static void LerpMeshVertexes_scalar(mdvSurface_t *surf, float backlerp) { +#if 0 short *oldXyz, *newXyz, *oldNormals, *newNormals; float *outXyz, *outNormal; float oldXyzScale, newXyzScale; @@ -836,10 +1117,59 @@ static void LerpMeshVertexes_scalar(md3Surface_t *surf, float backlerp) } VectorArrayNormalize((vec4_t *)tess.normal[tess.numVertexes], numVerts); } +#endif + float *outXyz, *outNormal; + mdvVertex_t *newVerts; + int vertNum; + + newVerts = surf->verts + backEnd.currentEntity->e.frame * surf->numVerts; + + outXyz = tess.xyz[tess.numVertexes]; + outNormal = tess.normal[tess.numVertexes]; + + if (backlerp == 0) + { + // + // just copy the vertexes + // + + for (vertNum=0 ; vertNum < surf->numVerts ; vertNum++) + { + VectorCopy(newVerts->xyz, outXyz); + VectorCopy(newVerts->normal, outNormal); + newVerts++; + outXyz += 4; + outNormal += 4; + } + } + else + { + // + // interpolate and copy the vertex and normal + // + + mdvVertex_t *oldVerts; + + oldVerts = surf->verts + backEnd.currentEntity->e.oldframe * surf->numVerts; + + for (vertNum=0 ; vertNum < surf->numVerts ; vertNum++) + { + VectorLerp(newVerts->xyz, oldVerts->xyz, backlerp, outXyz); + VectorLerp(newVerts->normal, oldVerts->normal, backlerp, outNormal); + //VectorNormalize(outNormal); + newVerts++; + oldVerts++; + outXyz += 4; + outNormal += 4; + } + VectorArrayNormalize((vec4_t *)tess.normal[tess.numVertexes], surf->numVerts); + } + } -static void LerpMeshVertexes(md3Surface_t *surf, float backlerp) +static void LerpMeshVertexes(mdvSurface_t *surf, float backlerp) { +#if 0 #if idppc_altivec if (com_altivec->integer) { // must be in a seperate function or G3 systems will crash. @@ -847,6 +1177,7 @@ static void LerpMeshVertexes(md3Surface_t *surf, float backlerp) return; } #endif // idppc_altivec +#endif LerpMeshVertexes_scalar( surf, backlerp ); } @@ -856,11 +1187,11 @@ static void LerpMeshVertexes(md3Surface_t *surf, float backlerp) RB_SurfaceMesh ============= */ -static void RB_SurfaceMesh(md3Surface_t *surface) { +static void RB_SurfaceMesh(mdvSurface_t *surface) { int j; float backlerp; - int *triangles; - float *texCoords; + srfTriangle_t *triangles; + mdvSt_t *texCoords; int indexes; int Bob, Doug; int numVerts; @@ -875,11 +1206,11 @@ static void RB_SurfaceMesh(md3Surface_t *surface) { LerpMeshVertexes (surface, backlerp); - triangles = (int *) ((byte *)surface + surface->ofsTriangles); + triangles = surface->triangles; indexes = surface->numTriangles * 3; Bob = tess.numIndexes; Doug = tess.numVertexes; - // JBravo: Mirrored models + if (backEnd.currentEntity->mirrored) { // Makro - this should be done differently, but since we're copying // indices in a loop anyway (see below), reversing them in the same loop @@ -890,18 +1221,20 @@ static void RB_SurfaceMesh(md3Surface_t *surface) { tess.indexes[Bob + j + 2] = Doug + triangles[j + 1]; } } else { - for (j = 0 ; j < indexes ; j++) { - tess.indexes[Bob + j] = Doug + triangles[j]; + for (j = 0 ; j < surface->numTriangles ; j++) { + tess.indexes[Bob + j*3 + 0] = Doug + triangles[j].indexes[0]; + tess.indexes[Bob + j*3 + 1] = Doug + triangles[j].indexes[1]; + tess.indexes[Bob + j*3 + 2] = Doug + triangles[j].indexes[2]; } } tess.numIndexes += indexes; - texCoords = (float *) ((byte *)surface + surface->ofsSt); + texCoords = surface->st; numVerts = surface->numVerts; for ( j = 0; j < numVerts; j++ ) { - tess.texCoords[Doug + j][0][0] = texCoords[j*2+0]; - tess.texCoords[Doug + j][0][1] = texCoords[j*2+1]; + tess.texCoords[Doug + j][0][0] = texCoords[j].st[0]; + tess.texCoords[Doug + j][0][1] = texCoords[j].st[1]; // FIXME: fill in lightmapST for completeness? } @@ -915,56 +1248,13 @@ static void RB_SurfaceMesh(md3Surface_t *surface) { RB_SurfaceFace ============== */ -static void RB_SurfaceFace( srfSurfaceFace_t *surf ) { - int i; - unsigned *indices, *tessIndexes; - float *v; - float *normal; - int ndx; - int Bob; - int numPoints; - int dlightBits; - - RB_CHECKOVERFLOW( surf->numPoints, surf->numIndices ); - - dlightBits = surf->dlightBits[backEnd.smpFrame]; - tess.dlightBits |= dlightBits; - - indices = ( unsigned * ) ( ( ( char * ) surf ) + surf->ofsIndices ); - - Bob = tess.numVertexes; - tessIndexes = tess.indexes + tess.numIndexes; - for ( i = surf->numIndices-1 ; i >= 0 ; i-- ) { - tessIndexes[i] = indices[i] + Bob; +static void RB_SurfaceFace( srfSurfaceFace_t *srf ) { + if( RB_SurfaceHelperVBO (srf->vbo, srf->ibo, srf->numVerts, srf->numTriangles * 3, srf->firstIndex, srf->dlightBits[backEnd.smpFrame], qtrue ) ) + { + return; } - tess.numIndexes += surf->numIndices; - - v = surf->points[0]; - - ndx = tess.numVertexes; - - numPoints = surf->numPoints; - - if ( tess.shader->needsNormal ) { - normal = surf->plane.normal; - for ( i = 0, ndx = tess.numVertexes; i < numPoints; i++, ndx++ ) { - VectorCopy( normal, tess.normal[ndx] ); - } - } - - for ( i = 0, v = surf->points[0], ndx = tess.numVertexes; i < numPoints; i++, v += VERTEXSIZE, ndx++ ) { - VectorCopy( v, tess.xyz[ndx]); - tess.texCoords[ndx][0][0] = v[3]; - tess.texCoords[ndx][0][1] = v[4]; - tess.texCoords[ndx][1][0] = v[5]; - tess.texCoords[ndx][1][1] = v[6]; - * ( unsigned int * ) &tess.vertexColors[ndx] = * ( unsigned int * ) &v[7]; - tess.vertexDlightBits[ndx] = dlightBits; - } - - - tess.numVertexes += surf->numPoints; + RB_SurfaceHelper(srf->numVerts, srf->verts, srf->numTriangles, srf->triangles, srf->dlightBits[backEnd.smpFrame]); } @@ -1005,13 +1295,13 @@ RB_SurfaceGrid Just copy the grid of points and triangulate ============= */ -static void RB_SurfaceGrid( srfGridMesh_t *cv ) { +static void RB_SurfaceGrid( srfGridMesh_t *srf ) { int i, j; float *xyz; float *texCoords; float *normal; unsigned char *color; - drawVert_t *dv; + srfVert_t *dv; int rows, irows, vrows; int used; int widthTable[MAX_GRID_SIZE]; @@ -1020,37 +1310,42 @@ static void RB_SurfaceGrid( srfGridMesh_t *cv ) { int lodWidth, lodHeight; int numVertexes; int dlightBits; - int *vDlightBits; + //int *vDlightBits; qboolean needsNormal; - dlightBits = cv->dlightBits[backEnd.smpFrame]; + if( RB_SurfaceHelperVBO (srf->vbo, srf->ibo, srf->numVerts, srf->numTriangles * 3, srf->firstIndex, srf->dlightBits[backEnd.smpFrame], qtrue ) ) + { + return; + } + + dlightBits = srf->dlightBits[backEnd.smpFrame]; tess.dlightBits |= dlightBits; // determine the allowable discrepance - lodError = LodErrorForVolume( cv->lodOrigin, cv->lodRadius ); + lodError = LodErrorForVolume( srf->lodOrigin, srf->lodRadius ); // determine which rows and columns of the subdivision // we are actually going to use widthTable[0] = 0; lodWidth = 1; - for ( i = 1 ; i < cv->width-1 ; i++ ) { - if ( cv->widthLodError[i] <= lodError ) { + for ( i = 1 ; i < srf->width-1 ; i++ ) { + if ( srf->widthLodError[i] <= lodError ) { widthTable[lodWidth] = i; lodWidth++; } } - widthTable[lodWidth] = cv->width-1; + widthTable[lodWidth] = srf->width-1; lodWidth++; heightTable[0] = 0; lodHeight = 1; - for ( i = 1 ; i < cv->height-1 ; i++ ) { - if ( cv->heightLodError[i] <= lodError ) { + for ( i = 1 ; i < srf->height-1 ; i++ ) { + if ( srf->heightLodError[i] <= lodError ) { heightTable[lodHeight] = i; lodHeight++; } } - heightTable[lodHeight] = cv->height-1; + heightTable[lodHeight] = srf->height-1; lodHeight++; @@ -1088,12 +1383,12 @@ static void RB_SurfaceGrid( srfGridMesh_t *cv ) { normal = tess.normal[numVertexes]; texCoords = tess.texCoords[numVertexes][0]; color = ( unsigned char * ) &tess.vertexColors[numVertexes]; - vDlightBits = &tess.vertexDlightBits[numVertexes]; - needsNormal = tess.shader->needsNormal; + //vDlightBits = &tess.vertexDlightBits[numVertexes]; + needsNormal = tess.shader->vertexAttribs & ATTR_NORMAL; for ( i = 0 ; i < rows ; i++ ) { for ( j = 0 ; j < lodWidth ; j++ ) { - dv = cv->verts + heightTable[ used + i ] * cv->width + dv = srf->verts + heightTable[ used + i ] * srf->width + widthTable[ j ]; xyz[0] = dv->xyz[0]; @@ -1108,8 +1403,8 @@ static void RB_SurfaceGrid( srfGridMesh_t *cv ) { normal[1] = dv->normal[1]; normal[2] = dv->normal[2]; } - * ( unsigned int * ) color = * ( unsigned int * ) dv->color; - *vDlightBits++ = dlightBits; + * ( unsigned int * ) color = * ( unsigned int * ) dv->vertexColors; + //*vDlightBits++ = dlightBits; xyz += 4; normal += 4; texCoords += 4; @@ -1173,6 +1468,8 @@ Draws x/y/z lines from the origin for orientation debugging =================== */ static void RB_SurfaceAxis( void ) { + // FIXME: implement this +#if 0 GL_Bind( tr.whiteImage ); qglLineWidth( 3 ); qglBegin( GL_LINES ); @@ -1187,6 +1484,7 @@ static void RB_SurfaceAxis( void ) { qglVertex3f( 0,0,16 ); qglEnd(); qglLineWidth( 1 ); +#endif } //=========================================================================== @@ -1232,6 +1530,57 @@ static void RB_SurfaceFlare(srfFlare_t *surf) RB_AddFlare(surf, tess.fogNum, surf->origin, surf->color, surf->normal); } +static void RB_SurfaceVBOMesh(srfVBOMesh_t * srf) +{ + RB_SurfaceHelperVBO (srf->vbo, srf->ibo, srf->numVerts, srf->numIndexes, srf->firstIndex, srf->dlightBits[backEnd.smpFrame], qfalse ); +} + +void RB_SurfaceVBOMDVMesh(srfVBOMDVMesh_t * surface) +{ + mdvModel_t *mdvModel; + mdvSurface_t *mdvSurface; + refEntity_t *refEnt; + + GLimp_LogComment("--- RB_SurfaceVBOMDVMesh ---\n"); + + if(!surface->vbo || !surface->ibo) + return; + + //RB_CheckVBOandIBO(surface->vbo, surface->ibo); + RB_EndSurface(); + RB_BeginSurface(tess.shader, tess.fogNum); + + R_BindVBO(surface->vbo); + R_BindIBO(surface->ibo); + + tess.useInternalVBO = qfalse; + + tess.numIndexes += surface->numIndexes; + tess.numVertexes += surface->numVerts; + + mdvModel = surface->mdvModel; + mdvSurface = surface->mdvSurface; + + refEnt = &backEnd.currentEntity->e; + + if(refEnt->oldframe == refEnt->frame) + { + glState.vertexAttribsInterpolation = 0; + } + else + { + glState.vertexAttribsInterpolation = refEnt->backlerp; + } + + glState.vertexAttribsOldFrame = refEnt->oldframe; + glState.vertexAttribsNewFrame = refEnt->frame; + + RB_EndSurface(); + + // So we don't lerp surfaces that shouldn't be lerped + glState.vertexAttribsInterpolation = 0; +} + static void RB_SurfaceDisplayList( srfDisplayList_t *surf ) { // all apropriate state must be set in RB_BeginSurface // this isn't implemented yet... @@ -1249,12 +1598,14 @@ void (*rb_surfaceTable[SF_NUM_SURFACE_TYPES])( void *) = { (void(*)(void*))RB_SurfaceGrid, // SF_GRID, (void(*)(void*))RB_SurfaceTriangles, // SF_TRIANGLES, (void(*)(void*))RB_SurfacePolychain, // SF_POLY, - (void(*)(void*))RB_SurfaceMesh, // SF_MD3, + (void(*)(void*))RB_SurfaceMesh, // SF_MDV, (void(*)(void*))RB_SurfaceAnim, // SF_MD4, #ifdef RAVENMD4 (void(*)(void*))RB_MDRSurfaceAnim, // SF_MDR, #endif (void(*)(void*))RB_SurfaceFlare, // SF_FLARE, (void(*)(void*))RB_SurfaceEntity, // SF_ENTITY - (void(*)(void*))RB_SurfaceDisplayList // SF_DISPLAY_LIST + (void(*)(void*))RB_SurfaceDisplayList, // SF_DISPLAY_LIST + (void(*)(void*))RB_SurfaceVBOMesh, // SF_VBO_MESH, + (void(*)(void*))RB_SurfaceVBOMDVMesh, // SF_VBO_MDVMESH }; diff --git a/reaction/code/renderer/tr_vbo.c b/reaction/code/renderer/tr_vbo.c new file mode 100644 index 00000000..a4b0d2be --- /dev/null +++ b/reaction/code/renderer/tr_vbo.c @@ -0,0 +1,785 @@ +/* +=========================================================================== +Copyright (C) 2007-2009 Robert Beckebans + +This file is part of XreaL source code. + +XreaL source code is free software; you can redistribute it +and/or modify it under the terms of the GNU General Public License as +published by the Free Software Foundation; either version 2 of the License, +or (at your option) any later version. + +XreaL source code is distributed in the hope that it will be +useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with XreaL source code; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +=========================================================================== +*/ +// tr_vbo.c +#include "tr_local.h" + +/* +============ +R_CreateVBO +============ +*/ +VBO_t *R_CreateVBO(const char *name, byte * vertexes, int vertexesSize, vboUsage_t usage) +{ + VBO_t *vbo; + int glUsage; + + switch (usage) + { + case VBO_USAGE_STATIC: + glUsage = GL_STATIC_DRAW_ARB; + break; + + case VBO_USAGE_DYNAMIC: + glUsage = GL_DYNAMIC_DRAW_ARB; + break; + + default: + Com_Error(ERR_FATAL, "bad vboUsage_t given: %i", usage); + return NULL; + } + + if(strlen(name) >= MAX_QPATH) + { + ri.Error(ERR_DROP, "R_CreateVBO: \"%s\" is too long\n", name); + } + + if ( tr.numVBOs == MAX_VBOS ) { + ri.Error( ERR_DROP, "R_CreateVBO: MAX_VBOS hit\n"); + } + + // make sure the render thread is stopped + R_SyncRenderThread(); + + vbo = tr.vbos[tr.numVBOs] = ri.Hunk_Alloc(sizeof(*vbo), h_low); + tr.numVBOs++; + + memset(vbo, 0, sizeof(*vbo)); + + Q_strncpyz(vbo->name, name, sizeof(vbo->name)); + + vbo->vertexesSize = vertexesSize; + + qglGenBuffersARB(1, &vbo->vertexesVBO); + + qglBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo->vertexesVBO); + qglBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexesSize, vertexes, glUsage); + + qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); + + glState.currentVBO = NULL; + + GL_CheckErrors(); + + return vbo; +} + +/* +============ +R_CreateVBO2 +============ +*/ +VBO_t *R_CreateVBO2(const char *name, int numVertexes, srfVert_t * verts, unsigned int stateBits, vboUsage_t usage) +{ + VBO_t *vbo; + int i; + + byte *data; + int dataSize; + int dataOfs; + + int glUsage; + + switch (usage) + { + case VBO_USAGE_STATIC: + glUsage = GL_STATIC_DRAW_ARB; + break; + + case VBO_USAGE_DYNAMIC: + glUsage = GL_DYNAMIC_DRAW_ARB; + break; + + default: + Com_Error(ERR_FATAL, "bad vboUsage_t given: %i", usage); + return NULL; + } + + if(!numVertexes) + return NULL; + + if(strlen(name) >= MAX_QPATH) + { + ri.Error(ERR_DROP, "R_CreateVBO2: \"%s\" is too long\n", name); + } + + if ( tr.numVBOs == MAX_VBOS ) { + ri.Error( ERR_DROP, "R_CreateVBO2: MAX_VBOS hit\n"); + } + + // make sure the render thread is stopped + R_SyncRenderThread(); + + vbo = tr.vbos[tr.numVBOs] = ri.Hunk_Alloc(sizeof(*vbo), h_low); + tr.numVBOs++; + + memset(vbo, 0, sizeof(*vbo)); + + Q_strncpyz(vbo->name, name, sizeof(vbo->name)); + + if (usage == VBO_USAGE_STATIC) + { + // since these vertex attributes are never altered, interleave them + vbo->ofs_xyz = 0; + dataSize = sizeof(verts[0].xyz); + + if(stateBits & ATTR_NORMAL) + { + vbo->ofs_normal = dataSize; + dataSize += sizeof(verts[0].normal); + } + + if(stateBits & ATTR_TEXCOORD) + { + vbo->ofs_st = dataSize; + dataSize += sizeof(verts[0].st); + } + + if(stateBits & ATTR_LIGHTCOORD) + { + vbo->ofs_lightmap = dataSize; + dataSize += sizeof(verts[0].lightmap); + } + + if(stateBits & ATTR_COLOR) + { + vbo->ofs_vertexcolor = dataSize; + dataSize += sizeof(verts[0].vertexColors); + } + + vbo->stride_xyz = dataSize; + vbo->stride_normal = dataSize; + vbo->stride_st = dataSize; + vbo->stride_lightmap = dataSize; + vbo->stride_vertexcolor = dataSize; + + // create VBO + dataSize *= numVertexes; + data = ri.Hunk_AllocateTempMemory(dataSize); + dataOfs = 0; + + //ri.Printf(PRINT_ALL, "CreateVBO: %d, %d %d %d %d %d, %d %d %d %d %d\n", dataSize, vbo->ofs_xyz, vbo->ofs_normal, vbo->ofs_st, vbo->ofs_lightmap, vbo->ofs_vertexcolor, + //vbo->stride_xyz, vbo->stride_normal, vbo->stride_st, vbo->stride_lightmap, vbo->stride_vertexcolor); + + for (i = 0; i < numVertexes; i++) + { + // xyz + memcpy(data + dataOfs, &verts[i].xyz, sizeof(verts[i].xyz)); + dataOfs += sizeof(verts[i].xyz); + + // normal + if(stateBits & ATTR_NORMAL) + { + memcpy(data + dataOfs, &verts[i].normal, sizeof(verts[i].normal)); + dataOfs += sizeof(verts[i].normal); + } + + // vertex texcoords + if(stateBits & ATTR_TEXCOORD) + { + memcpy(data + dataOfs, &verts[i].st, sizeof(verts[i].st)); + dataOfs += sizeof(verts[i].st); + } + + // feed vertex lightmap texcoords + if(stateBits & ATTR_LIGHTCOORD) + { + memcpy(data + dataOfs, &verts[i].lightmap, sizeof(verts[i].lightmap)); + dataOfs += sizeof(verts[i].lightmap); + } + + // feed vertex colors + if(stateBits & ATTR_COLOR) + { + memcpy(data + dataOfs, &verts[i].vertexColors, sizeof(verts[i].vertexColors)); + dataOfs += sizeof(verts[i].vertexColors); + } + } + } + else + { + // since these vertex attributes may be changed, put them in flat arrays + dataSize = sizeof(verts[0].xyz); + + if(stateBits & ATTR_NORMAL) + { + dataSize += sizeof(verts[0].normal); + } + + if(stateBits & ATTR_TEXCOORD) + { + dataSize += sizeof(verts[0].st); + } + + if(stateBits & ATTR_LIGHTCOORD) + { + dataSize += sizeof(verts[0].lightmap); + } + + if(stateBits & ATTR_COLOR) + { + dataSize += sizeof(verts[0].vertexColors); + } + + // create VBO + dataSize *= numVertexes; + data = ri.Hunk_AllocateTempMemory(dataSize); + dataOfs = 0; + + vbo->ofs_xyz = 0; + vbo->ofs_normal = 0; + vbo->ofs_st = 0; + vbo->ofs_lightmap = 0; + vbo->ofs_vertexcolor = 0; + + vbo->stride_xyz = sizeof(verts[0].xyz); + vbo->stride_normal = sizeof(verts[0].normal); + vbo->stride_vertexcolor = sizeof(verts[0].vertexColors); + vbo->stride_st = sizeof(verts[0].st); + vbo->stride_lightmap = sizeof(verts[0].lightmap); + + //ri.Printf(PRINT_ALL, "2CreateVBO: %d, %d %d %d %d %d, %d %d %d %d %d\n", dataSize, vbo->ofs_xyz, vbo->ofs_normal, vbo->ofs_st, vbo->ofs_lightmap, vbo->ofs_vertexcolor, + //vbo->stride_xyz, vbo->stride_normal, vbo->stride_st, vbo->stride_lightmap, vbo->stride_vertexcolor); + + // xyz + for (i = 0; i < numVertexes; i++) + { + memcpy(data + dataOfs, &verts[i].xyz, sizeof(verts[i].xyz)); + dataOfs += sizeof(verts[i].xyz); + } + + // normal + if(stateBits & ATTR_NORMAL) + { + vbo->ofs_normal = dataOfs; + for (i = 0; i < numVertexes; i++) + { + memcpy(data + dataOfs, &verts[i].normal, sizeof(verts[i].normal)); + dataOfs += sizeof(verts[i].normal); + } + } + + // vertex texcoords + if(stateBits & ATTR_TEXCOORD) + { + vbo->ofs_st = dataOfs; + for (i = 0; i < numVertexes; i++) + { + memcpy(data + dataOfs, &verts[i].st, sizeof(verts[i].st)); + dataOfs += sizeof(verts[i].st); + } + } + + // feed vertex lightmap texcoords + if(stateBits & ATTR_LIGHTCOORD) + { + vbo->ofs_lightmap = dataOfs; + for (i = 0; i < numVertexes; i++) + { + memcpy(data + dataOfs, &verts[i].lightmap, sizeof(verts[i].lightmap)); + dataOfs += sizeof(verts[i].lightmap); + } + } + + // feed vertex colors + if(stateBits & ATTR_COLOR) + { + vbo->ofs_vertexcolor = dataOfs; + for (i = 0; i < numVertexes; i++) + { + memcpy(data + dataOfs, &verts[i].vertexColors, sizeof(verts[i].vertexColors)); + dataOfs += sizeof(verts[i].vertexColors); + } + } + } + + + vbo->vertexesSize = dataSize; + + qglGenBuffersARB(1, &vbo->vertexesVBO); + + qglBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo->vertexesVBO); + qglBufferDataARB(GL_ARRAY_BUFFER_ARB, dataSize, data, glUsage); + + qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); + + glState.currentVBO = NULL; + + GL_CheckErrors(); + + ri.Hunk_FreeTempMemory(data); + + return vbo; +} + + +/* +============ +R_CreateIBO +============ +*/ +IBO_t *R_CreateIBO(const char *name, byte * indexes, int indexesSize, vboUsage_t usage) +{ + IBO_t *ibo; + int glUsage; + + switch (usage) + { + case VBO_USAGE_STATIC: + glUsage = GL_STATIC_DRAW_ARB; + break; + + case VBO_USAGE_DYNAMIC: + glUsage = GL_DYNAMIC_DRAW_ARB; + break; + + default: + Com_Error(ERR_FATAL, "bad vboUsage_t given: %i", usage); + return NULL; + } + + if(strlen(name) >= MAX_QPATH) + { + ri.Error(ERR_DROP, "R_CreateIBO: \"%s\" is too long\n", name); + } + + if ( tr.numIBOs == MAX_IBOS ) { + ri.Error( ERR_DROP, "R_CreateIBO: MAX_IBOS hit\n"); + } + + // make sure the render thread is stopped + R_SyncRenderThread(); + + ibo = tr.ibos[tr.numIBOs] = ri.Hunk_Alloc(sizeof(*ibo), h_low); + tr.numIBOs++; + + Q_strncpyz(ibo->name, name, sizeof(ibo->name)); + + ibo->indexesSize = indexesSize; + + qglGenBuffersARB(1, &ibo->indexesVBO); + + qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, ibo->indexesVBO); + qglBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, indexesSize, indexes, glUsage); + + qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); + + glState.currentIBO = NULL; + + GL_CheckErrors(); + + return ibo; +} + +/* +============ +R_CreateIBO2 +============ +*/ +IBO_t *R_CreateIBO2(const char *name, int numTriangles, srfTriangle_t * triangles, vboUsage_t usage) +{ + IBO_t *ibo; + int i, j; + + byte *indexes; + int indexesSize; + int indexesOfs; + + srfTriangle_t *tri; + glIndex_t index; + int glUsage; + + switch (usage) + { + case VBO_USAGE_STATIC: + glUsage = GL_STATIC_DRAW_ARB; + break; + + case VBO_USAGE_DYNAMIC: + glUsage = GL_DYNAMIC_DRAW_ARB; + break; + + default: + Com_Error(ERR_FATAL, "bad vboUsage_t given: %i", usage); + return NULL; + } + + if(!numTriangles) + return NULL; + + if(strlen(name) >= MAX_QPATH) + { + ri.Error(ERR_DROP, "R_CreateIBO2: \"%s\" is too long\n", name); + } + + if ( tr.numIBOs == MAX_IBOS ) { + ri.Error( ERR_DROP, "R_CreateIBO2: MAX_IBOS hit\n"); + } + + // make sure the render thread is stopped + R_SyncRenderThread(); + + ibo = tr.ibos[tr.numIBOs] = ri.Hunk_Alloc(sizeof(*ibo), h_low); + tr.numIBOs++; + + Q_strncpyz(ibo->name, name, sizeof(ibo->name)); + + indexesSize = numTriangles * 3 * sizeof(int); + indexes = ri.Hunk_AllocateTempMemory(indexesSize); + indexesOfs = 0; + + for(i = 0, tri = triangles; i < numTriangles; i++, tri++) + { + for(j = 0; j < 3; j++) + { + index = tri->indexes[j]; + memcpy(indexes + indexesOfs, &index, sizeof(glIndex_t)); + indexesOfs += sizeof(glIndex_t); + } + } + + ibo->indexesSize = indexesSize; + + qglGenBuffersARB(1, &ibo->indexesVBO); + + qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, ibo->indexesVBO); + qglBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, indexesSize, indexes, glUsage); + + qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); + + glState.currentIBO = NULL; + + GL_CheckErrors(); + + ri.Hunk_FreeTempMemory(indexes); + + return ibo; +} + +/* +============ +R_BindVBO +============ +*/ +void R_BindVBO(VBO_t * vbo) +{ + if(!vbo) + { + //R_BindNullVBO(); + ri.Error(ERR_DROP, "R_BindNullVBO: NULL vbo"); + return; + } + + if(r_logFile->integer) + { + // don't just call LogComment, or we will get a call to va() every frame! + GLimp_LogComment(va("--- R_BindVBO( %s ) ---\n", vbo->name)); + } + + if(glState.currentVBO != vbo) + { + glState.currentVBO = vbo; + glState.vertexAttribPointersSet = 0; + + glState.vertexAttribsInterpolation = 0; + glState.vertexAttribsOldFrame = 0; + glState.vertexAttribsNewFrame = 0; + + qglBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo->vertexesVBO); + + backEnd.pc.c_vboVertexBuffers++; + } +} + +/* +============ +R_BindNullVBO +============ +*/ +void R_BindNullVBO(void) +{ + GLimp_LogComment("--- R_BindNullVBO ---\n"); + + if(glState.currentVBO) + { + qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); + glState.currentVBO = NULL; + } + + GL_CheckErrors(); +} + +/* +============ +R_BindIBO +============ +*/ +void R_BindIBO(IBO_t * ibo) +{ + if(!ibo) + { + //R_BindNullIBO(); + ri.Error(ERR_DROP, "R_BindIBO: NULL ibo"); + return; + } + + if(r_logFile->integer) + { + // don't just call LogComment, or we will get a call to va() every frame! + GLimp_LogComment(va("--- R_BindIBO( %s ) ---\n", ibo->name)); + } + + if(glState.currentIBO != ibo) + { + qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, ibo->indexesVBO); + + glState.currentIBO = ibo; + + backEnd.pc.c_vboIndexBuffers++; + } +} + +/* +============ +R_BindNullIBO +============ +*/ +void R_BindNullIBO(void) +{ + GLimp_LogComment("--- R_BindNullIBO ---\n"); + + if(glState.currentIBO) + { + qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); + glState.currentIBO = NULL; + glState.vertexAttribPointersSet = 0; + } +} + +/* +============ +R_InitVBOs +============ +*/ +void R_InitVBOs(void) +{ + int dataSize; + byte *data; + + ri.Printf(PRINT_ALL, "------- R_InitVBOs -------\n"); + + tr.numVBOs = 0; + tr.numIBOs = 0; + + dataSize = sizeof(tess.xyz[0]); + dataSize += sizeof(tess.normal[0]); + dataSize += sizeof(tess.vertexColors[0]); + dataSize += sizeof(tess.texCoords[0][0]) * 2; + dataSize *= SHADER_MAX_VERTEXES; + + data = ri.Malloc(dataSize); + memset(data, 0, dataSize); + + tess.vbo = R_CreateVBO("tessVertexArray_VBO", data, dataSize, VBO_USAGE_DYNAMIC); + + ri.Free(data); + + tess.vbo->ofs_xyz = 0; + tess.vbo->ofs_normal = tess.vbo->ofs_xyz + sizeof(tess.xyz[0]) * SHADER_MAX_VERTEXES; + // these next two are actually interleaved + tess.vbo->ofs_st = tess.vbo->ofs_normal + sizeof(tess.normal[0]) * SHADER_MAX_VERTEXES; + tess.vbo->ofs_lightmap = tess.vbo->ofs_st + sizeof(tess.texCoords[0][0]); + + tess.vbo->ofs_vertexcolor = tess.vbo->ofs_st + sizeof(tess.texCoords[0][0]) * 2 * SHADER_MAX_VERTEXES; + + tess.vbo->stride_xyz = sizeof(tess.xyz[0]); + tess.vbo->stride_normal = sizeof(tess.normal[0]); + tess.vbo->stride_vertexcolor = sizeof(tess.vertexColors[0]); + tess.vbo->stride_st = sizeof(tess.texCoords[0][0]) * 2; + tess.vbo->stride_lightmap = sizeof(tess.texCoords[0][0]) * 2; + + dataSize = sizeof(tess.indexes[0]) * SHADER_MAX_INDEXES; + + data = ri.Malloc(dataSize); + memset(data, 0, dataSize); + + tess.ibo = R_CreateIBO("tessVertexArray_IBO", data, dataSize, VBO_USAGE_DYNAMIC); + + ri.Free(data); + + R_BindNullVBO(); + R_BindNullIBO(); + + GL_CheckErrors(); +} + +/* +============ +R_ShutdownVBOs +============ +*/ +void R_ShutdownVBOs(void) +{ + int i; + VBO_t *vbo; + IBO_t *ibo; + + ri.Printf(PRINT_ALL, "------- R_ShutdownVBOs -------\n"); + + R_BindNullVBO(); + R_BindNullIBO(); + + + for(i = 0; i < tr.numVBOs; i++) + { + vbo = tr.vbos[i]; + + if(vbo->vertexesVBO) + { + qglDeleteBuffersARB(1, &vbo->vertexesVBO); + } + + //ri.Free(vbo); + } + + for(i = 0; i < tr.numIBOs; i++) + { + ibo = tr.ibos[i]; + + if(ibo->indexesVBO) + { + qglDeleteBuffersARB(1, &ibo->indexesVBO); + } + + //ri.Free(ibo); + } + + tr.numVBOs = 0; + tr.numIBOs = 0; +} + +/* +============ +R_VBOList_f +============ +*/ +void R_VBOList_f(void) +{ + int i; + VBO_t *vbo; + IBO_t *ibo; + int vertexesSize = 0; + int indexesSize = 0; + + ri.Printf(PRINT_ALL, " size name\n"); + ri.Printf(PRINT_ALL, "----------------------------------------------------------\n"); + + for(i = 0; i < tr.numVBOs; i++) + { + vbo = tr.vbos[i]; + + ri.Printf(PRINT_ALL, "%d.%02d MB %s\n", vbo->vertexesSize / (1024 * 1024), + (vbo->vertexesSize % (1024 * 1024)) * 100 / (1024 * 1024), vbo->name); + + vertexesSize += vbo->vertexesSize; + } + + for(i = 0; i < tr.numIBOs; i++) + { + ibo = tr.ibos[i]; + + ri.Printf(PRINT_ALL, "%d.%02d MB %s\n", ibo->indexesSize / (1024 * 1024), + (ibo->indexesSize % (1024 * 1024)) * 100 / (1024 * 1024), ibo->name); + + indexesSize += ibo->indexesSize; + } + + ri.Printf(PRINT_ALL, " %i total VBOs\n", tr.numVBOs); + ri.Printf(PRINT_ALL, " %d.%02d MB total vertices memory\n", vertexesSize / (1024 * 1024), + (vertexesSize % (1024 * 1024)) * 100 / (1024 * 1024)); + + ri.Printf(PRINT_ALL, " %i total IBOs\n", tr.numIBOs); + ri.Printf(PRINT_ALL, " %d.%02d MB total triangle indices memory\n", indexesSize / (1024 * 1024), + (indexesSize % (1024 * 1024)) * 100 / (1024 * 1024)); +} + + +/* +============== +RB_UpdateVBOs + +Adapted from Tess_UpdateVBOs from xreal + +Tr3B: update the default VBO to replace the client side vertex arrays +============== +*/ +void RB_UpdateVBOs(unsigned int attribBits) +{ + GLimp_LogComment("--- RB_UpdateVBOs ---\n"); + + backEnd.pc.c_dynamicVboDraws++; + + // update the default VBO + if(tess.numVertexes > 0 && tess.numVertexes <= SHADER_MAX_VERTEXES) + { + R_BindVBO(tess.vbo); + + if(attribBits & ATTR_BITS) + { + if(attribBits & ATTR_POSITION) + { + qglBufferSubDataARB(GL_ARRAY_BUFFER_ARB, tess.vbo->ofs_xyz, tess.numVertexes * sizeof(tess.xyz[0]), tess.xyz); + } + + if(attribBits & ATTR_TEXCOORD || attribBits & ATTR_LIGHTCOORD) + { + // these are interleaved, so we update both if either need it + qglBufferSubDataARB(GL_ARRAY_BUFFER_ARB, tess.vbo->ofs_st, tess.numVertexes * sizeof(tess.texCoords[0][0]) * 2, tess.texCoords); + } + + if(attribBits & ATTR_NORMAL) + { + qglBufferSubDataARB(GL_ARRAY_BUFFER_ARB, tess.vbo->ofs_normal, tess.numVertexes * sizeof(tess.normal[0]), tess.normal); + } + + if(attribBits & ATTR_COLOR) + { + qglBufferSubDataARB(GL_ARRAY_BUFFER_ARB, tess.vbo->ofs_vertexcolor, tess.numVertexes * sizeof(tess.vertexColors[0]), tess.vertexColors); + } + } + else + { + qglBufferSubDataARB(GL_ARRAY_BUFFER_ARB, tess.vbo->ofs_xyz, tess.numVertexes * sizeof(tess.xyz[0]), tess.xyz); + qglBufferSubDataARB(GL_ARRAY_BUFFER_ARB, tess.vbo->ofs_st, tess.numVertexes * sizeof(tess.texCoords[0][0]) * 2, tess.texCoords); + qglBufferSubDataARB(GL_ARRAY_BUFFER_ARB, tess.vbo->ofs_normal, tess.numVertexes * sizeof(tess.normal[0]), tess.normal); + qglBufferSubDataARB(GL_ARRAY_BUFFER_ARB, tess.vbo->ofs_vertexcolor, tess.numVertexes * sizeof(tess.vertexColors[0]), tess.vertexColors); + } + + } + + // update the default IBO + if(tess.numIndexes > 0 && tess.numIndexes <= SHADER_MAX_INDEXES) + { + R_BindIBO(tess.ibo); + + qglBufferSubDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0, tess.numIndexes * sizeof(tess.indexes[0]), tess.indexes); + } +} diff --git a/reaction/code/renderer/tr_world.c b/reaction/code/renderer/tr_world.c index d05373ab..f6fb0436 100644 --- a/reaction/code/renderer/tr_world.c +++ b/reaction/code/renderer/tr_world.c @@ -23,230 +23,97 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -/* -================= -R_CullTriSurf - -Returns true if the grid is completely culled away. -Also sets the clipped hint bit in tess -================= -*/ -static qboolean R_CullTriSurf( srfTriangles_t *cv ) { - int boxCull; - - boxCull = R_CullLocalBox( cv->bounds ); - - if ( boxCull == CULL_OUT ) { - return qtrue; - } - return qfalse; -} - -/* -================= -R_CullGrid - -Returns true if the grid is completely culled away. -Also sets the clipped hint bit in tess -================= -*/ -static qboolean R_CullGrid( srfGridMesh_t *cv ) { - int boxCull; - int sphereCull; - - if ( r_nocurves->integer ) { - return qtrue; - } - - if ( tr.currentEntityNum != ENTITYNUM_WORLD ) { - sphereCull = R_CullLocalPointAndRadius( cv->localOrigin, cv->meshRadius ); - } else { - sphereCull = R_CullPointAndRadius( cv->localOrigin, cv->meshRadius ); - } - boxCull = CULL_OUT; - - // check for trivial reject - if ( sphereCull == CULL_OUT ) - { - tr.pc.c_sphere_cull_patch_out++; - return qtrue; - } - // check bounding box if necessary - else if ( sphereCull == CULL_CLIP ) - { - tr.pc.c_sphere_cull_patch_clip++; - - boxCull = R_CullLocalBox( cv->meshBounds ); - - if ( boxCull == CULL_OUT ) - { - tr.pc.c_box_cull_patch_out++; - return qtrue; - } - else if ( boxCull == CULL_IN ) - { - tr.pc.c_box_cull_patch_in++; - } - else - { - tr.pc.c_box_cull_patch_clip++; - } - } - else - { - tr.pc.c_sphere_cull_patch_in++; - } - - return qfalse; -} - - /* ================ R_CullSurface -Tries to back face cull surfaces before they are lighted or +Tries to cull surfaces before they are lighted or added to the sorting list. - -This will also allow mirrors on both sides of a model without recursion. ================ */ -static qboolean R_CullSurface( surfaceType_t *surface, shader_t *shader ) { - srfSurfaceFace_t *sface; - float d; - - if ( r_nocull->integer ) { +static qboolean R_CullSurface( msurface_t *surf ) { + if ( r_nocull->integer || surf->cullinfo.type == CULLINFO_NONE) { return qfalse; } - if ( *surface == SF_GRID ) { - return R_CullGrid( (srfGridMesh_t *)surface ); - } + if (surf->cullinfo.type & CULLINFO_PLANE) + { + // Only true for SF_FACE, so treat like its own function + float d; - if ( *surface == SF_TRIANGLES ) { - return R_CullTriSurf( (srfTriangles_t *)surface ); - } + if ( !r_facePlaneCull->integer ) { + return qfalse; + } + + if (surf->shader->cullType == CT_TWO_SIDED) + { + return qfalse; + } + + d = DotProduct (tr.or.viewOrigin, surf->cullinfo.plane.normal); + + // don't cull exactly on the plane, because there are levels of rounding + // through the BSP, ICD, and hardware that may cause pixel gaps if an + // epsilon isn't allowed here + if ( surf->shader->cullType == CT_FRONT_SIDED ) { + if ( d < surf->cullinfo.plane.dist - 8 ) { + return qtrue; + } + } else { + if ( d > surf->cullinfo.plane.dist + 8 ) { + return qtrue; + } + } - if ( *surface != SF_FACE ) { return qfalse; } - if ( shader->cullType == CT_TWO_SIDED ) { - return qfalse; - } + if (surf->cullinfo.type & CULLINFO_SPHERE) + { + int sphereCull; - // face culling - if ( !r_facePlaneCull->integer ) { - return qfalse; - } + if ( tr.currentEntityNum != ENTITYNUM_WORLD ) { + sphereCull = R_CullLocalPointAndRadius( surf->cullinfo.localOrigin, surf->cullinfo.radius ); + } else { + sphereCull = R_CullPointAndRadius( surf->cullinfo.localOrigin, surf->cullinfo.radius ); + } - sface = ( srfSurfaceFace_t * ) surface; - d = DotProduct (tr.or.viewOrigin, sface->plane.normal); - - // don't cull exactly on the plane, because there are levels of rounding - // through the BSP, ICD, and hardware that may cause pixel gaps if an - // epsilon isn't allowed here - if ( shader->cullType == CT_FRONT_SIDED ) { - if ( d < sface->plane.dist - 8 ) { + if ( sphereCull == CULL_OUT ) + { return qtrue; } - } else { - if ( d > sface->plane.dist + 8 ) { + + if ( sphereCull == CULL_IN ) + { + return qfalse; + } + } + + if (surf->cullinfo.type & CULLINFO_BOX) + { + int boxCull; + + if ( tr.currentEntityNum != ENTITYNUM_WORLD ) { + boxCull = R_CullLocalBox( surf->cullinfo.bounds ); + } else { + boxCull = R_CullBox( surf->cullinfo.bounds ); + } + + if ( boxCull == CULL_OUT ) + { return qtrue; } + + if ( boxCull == CULL_IN ) + { + return qfalse; + } } return qfalse; } -static int R_DlightFace( srfSurfaceFace_t *face, int dlightBits ) { - float d; - int i; - dlight_t *dl; - - for ( i = 0 ; i < tr.refdef.num_dlights ; i++ ) { - if ( ! ( dlightBits & ( 1 << i ) ) ) { - continue; - } - dl = &tr.refdef.dlights[i]; - d = DotProduct( dl->origin, face->plane.normal ) - face->plane.dist; - if ( d < -dl->radius || d > dl->radius ) { - // dlight doesn't reach the plane - dlightBits &= ~( 1 << i ); - } - } - - if ( !dlightBits ) { - tr.pc.c_dlightSurfacesCulled++; - } - - face->dlightBits[ tr.smpFrame ] = dlightBits; - return dlightBits; -} - -static int R_DlightGrid( srfGridMesh_t *grid, int dlightBits ) { - int i; - dlight_t *dl; - - for ( i = 0 ; i < tr.refdef.num_dlights ; i++ ) { - if ( ! ( dlightBits & ( 1 << i ) ) ) { - continue; - } - dl = &tr.refdef.dlights[i]; - if ( dl->origin[0] - dl->radius > grid->meshBounds[1][0] - || dl->origin[0] + dl->radius < grid->meshBounds[0][0] - || dl->origin[1] - dl->radius > grid->meshBounds[1][1] - || dl->origin[1] + dl->radius < grid->meshBounds[0][1] - || dl->origin[2] - dl->radius > grid->meshBounds[1][2] - || dl->origin[2] + dl->radius < grid->meshBounds[0][2] ) { - // dlight doesn't reach the bounds - dlightBits &= ~( 1 << i ); - } - } - - if ( !dlightBits ) { - tr.pc.c_dlightSurfacesCulled++; - } - - grid->dlightBits[ tr.smpFrame ] = dlightBits; - return dlightBits; -} - - -static int R_DlightTrisurf( srfTriangles_t *surf, int dlightBits ) { - // FIXME: more dlight culling to trisurfs... - surf->dlightBits[ tr.smpFrame ] = dlightBits; - return dlightBits; -#if 0 - int i; - dlight_t *dl; - - for ( i = 0 ; i < tr.refdef.num_dlights ; i++ ) { - if ( ! ( dlightBits & ( 1 << i ) ) ) { - continue; - } - dl = &tr.refdef.dlights[i]; - if ( dl->origin[0] - dl->radius > grid->meshBounds[1][0] - || dl->origin[0] + dl->radius < grid->meshBounds[0][0] - || dl->origin[1] - dl->radius > grid->meshBounds[1][1] - || dl->origin[1] + dl->radius < grid->meshBounds[0][1] - || dl->origin[2] - dl->radius > grid->meshBounds[1][2] - || dl->origin[2] + dl->radius < grid->meshBounds[0][2] ) { - // dlight doesn't reach the bounds - dlightBits &= ~( 1 << i ); - } - } - - if ( !dlightBits ) { - tr.pc.c_dlightSurfacesCulled++; - } - - grid->dlightBits[ tr.smpFrame ] = dlightBits; - return dlightBits; -#endif -} - /* ==================== R_DlightSurface @@ -257,12 +124,52 @@ more dlights if possible. ==================== */ static int R_DlightSurface( msurface_t *surf, int dlightBits ) { + float d; + int i; + dlight_t *dl; + + if ( surf->cullinfo.type & CULLINFO_PLANE ) + { + int i; + for ( i = 0 ; i < tr.refdef.num_dlights ; i++ ) { + if ( ! ( dlightBits & ( 1 << i ) ) ) { + continue; + } + dl = &tr.refdef.dlights[i]; + d = DotProduct( dl->origin, surf->cullinfo.plane.normal ) - surf->cullinfo.plane.dist; + if ( d < -dl->radius || d > dl->radius ) { + // dlight doesn't reach the plane + dlightBits &= ~( 1 << i ); + } + } + } + else if ( surf->cullinfo.type & CULLINFO_BOX ) + { + for ( i = 0 ; i < tr.refdef.num_dlights ; i++ ) { + if ( ! ( dlightBits & ( 1 << i ) ) ) { + continue; + } + dl = &tr.refdef.dlights[i]; + if ( dl->origin[0] - dl->radius > surf->cullinfo.bounds[1][0] + || dl->origin[0] + dl->radius < surf->cullinfo.bounds[0][0] + || dl->origin[1] - dl->radius > surf->cullinfo.bounds[1][1] + || dl->origin[1] + dl->radius < surf->cullinfo.bounds[0][1] + || dl->origin[2] - dl->radius > surf->cullinfo.bounds[1][2] + || dl->origin[2] + dl->radius < surf->cullinfo.bounds[0][2] ) { + // dlight doesn't reach the bounds + dlightBits &= ~( 1 << i ); + } + } + } + if ( *surf->data == SF_FACE ) { - dlightBits = R_DlightFace( (srfSurfaceFace_t *)surf->data, dlightBits ); + ((srfSurfaceFace_t *)surf->data)->dlightBits[ tr.smpFrame ] = dlightBits; } else if ( *surf->data == SF_GRID ) { - dlightBits = R_DlightGrid( (srfGridMesh_t *)surf->data, dlightBits ); + ((srfGridMesh_t *)surf->data)->dlightBits[ tr.smpFrame ] = dlightBits; } else if ( *surf->data == SF_TRIANGLES ) { - dlightBits = R_DlightTrisurf( (srfTriangles_t *)surf->data, dlightBits ); + ((srfTriangles_t *)surf->data)->dlightBits[ tr.smpFrame ] = dlightBits; + } else if ( *surf->data == SF_VBO_MESH ) { + ((srfVBOMesh_t *)surf->data)->dlightBits[ tr.smpFrame ] = dlightBits; } else { dlightBits = 0; } @@ -274,23 +181,16 @@ static int R_DlightSurface( msurface_t *surf, int dlightBits ) { return dlightBits; } - - /* ====================== R_AddWorldSurface ====================== */ static void R_AddWorldSurface( msurface_t *surf, int dlightBits ) { - if ( surf->viewCount == tr.viewCount ) { - return; // already in this view - } - - surf->viewCount = tr.viewCount; // FIXME: bmodel fog? // try to cull before dlighting or adding - if ( R_CullSurface( surf->data, surf->shader ) ) { + if ( R_CullSurface( surf ) ) { return; } @@ -334,7 +234,13 @@ void R_AddBrushModelSurfaces ( trRefEntity_t *ent ) { R_DlightBmodel( bmodel ); for ( i = 0 ; i < bmodel->numSurfaces ; i++ ) { - R_AddWorldSurface( bmodel->firstSurface + i, tr.currentEntity->needDlights ); + int surf = bmodel->firstSurface + i; + + if (tr.world->surfacesViewCount[surf] != tr.viewCount) + { + tr.world->surfacesViewCount[surf] = tr.viewCount; + R_AddWorldSurface( tr.world->surfaces + surf, tr.currentEntity->needDlights ); + } } } @@ -359,7 +265,7 @@ static void R_RecursiveWorldNode( mnode_t *node, int planeBits, int dlightBits ) int newDlights[2]; // if the node wasn't marked as potentially visible, exit - if (node->visframe != tr.visCount) { + if (node->visCounts[tr.visIndex] != tr.visCounts[tr.visIndex]) { return; } @@ -453,7 +359,7 @@ static void R_RecursiveWorldNode( mnode_t *node, int planeBits, int dlightBits ) { // leaf node, so add mark surfaces int c; - msurface_t *surf, **mark; + int surf, *view; tr.pc.c_leafs++; @@ -478,15 +384,27 @@ static void R_RecursiveWorldNode( mnode_t *node, int planeBits, int dlightBits ) tr.viewParms.visBounds[1][2] = node->maxs[2]; } - // add the individual surfaces - mark = node->firstmarksurface; + // add merged and unmerged surfaces + if (tr.world->viewSurfaces) + view = tr.world->viewSurfaces + node->firstmarksurface; + else + view = tr.world->marksurfaces + node->firstmarksurface; + c = node->nummarksurfaces; while (c--) { - // the surface may have already been added if it - // spans multiple leafs - surf = *mark; - R_AddWorldSurface( surf, dlightBits ); - mark++; + // just mark it as visible, so we don't jump out of the cache derefencing the surface + surf = *view; + if (surf < 0) + { + tr.world->mergedSurfacesViewCount[-surf - 1] = tr.viewCount; + tr.world->mergedSurfacesDlightBits[-surf - 1] = dlightBits; + } + else + { + tr.world->surfacesViewCount[surf] = tr.viewCount; + tr.world->surfacesDlightBits[surf] = dlightBits; + } + view++; } } @@ -583,12 +501,37 @@ static void R_MarkLeaves (void) { // if the cluster is the same and the area visibility matrix // hasn't changed, we don't need to mark everything again + for(i = 0; i < MAX_VISCOUNTS; i++) + { + if(tr.visClusters[i] == cluster) + { + //tr.visIndex = i; + break; + } + } + // if r_showcluster was just turned on, remark everything - if ( tr.viewCluster == cluster && !tr.refdef.areamaskModified - && !r_showcluster->modified ) { + if(i != MAX_VISCOUNTS && !tr.refdef.areamaskModified && !r_showcluster->modified)// && !r_dynamicBspOcclusionCulling->modified) + { + if(tr.visClusters[i] != tr.visClusters[tr.visIndex] && r_showcluster->integer) + { + ri.Printf(PRINT_ALL, "found cluster:%i area:%i index:%i\n", cluster, leaf->area, i); + } + tr.visIndex = i; return; } + // if the areamask was modified, invalidate all visclusters + // this caused doors to open into undrawn areas + if (tr.refdef.areamaskModified) + { + memset(tr.visClusters, -2, sizeof(tr.visClusters)); + } + + tr.visIndex = (tr.visIndex + 1) % MAX_VISCOUNTS; + tr.visCounts[tr.visIndex]++; + tr.visClusters[tr.visIndex] = cluster; + if ( r_showcluster->modified || r_showcluster->integer ) { r_showcluster->modified = qfalse; if ( r_showcluster->integer ) { @@ -596,19 +539,18 @@ static void R_MarkLeaves (void) { } } - tr.visCount++; - tr.viewCluster = cluster; - - if ( r_novis->integer || tr.viewCluster == -1 ) { + // set all nodes to visible if there is no vis + // this caused some levels to simply not render + if (r_novis->integer || !tr.world->vis || tr.visClusters[tr.visIndex] == -1) { for (i=0 ; inumnodes ; i++) { if (tr.world->nodes[i].contents != CONTENTS_SOLID) { - tr.world->nodes[i].visframe = tr.visCount; + tr.world->nodes[i].visCounts[tr.visIndex] = tr.visCounts[tr.visIndex]; } } return; } - vis = R_ClusterPVS (tr.viewCluster); + vis = R_ClusterPVS(tr.visClusters[tr.visIndex]); for (i=0,leaf=tr.world->nodes ; inumnodes ; i++, leaf++) { cluster = leaf->cluster; @@ -628,9 +570,9 @@ static void R_MarkLeaves (void) { parent = leaf; do { - if (parent->visframe == tr.visCount) + if(parent->visCounts[tr.visIndex] == tr.visCounts[tr.visIndex]) break; - parent->visframe = tr.visCount; + parent->visCounts[tr.visIndex] = tr.visCounts[tr.visIndex]; parent = parent->parent; } while (parent); } @@ -660,9 +602,30 @@ void R_AddWorldSurfaces (void) { // clear out the visible min/max ClearBounds( tr.viewParms.visBounds[0], tr.viewParms.visBounds[1] ); - // perform frustum culling and add all the potentially visible surfaces + // perform frustum culling and flag all the potentially visible surfaces if ( tr.refdef.num_dlights > 32 ) { tr.refdef.num_dlights = 32 ; } R_RecursiveWorldNode( tr.world->nodes, 15, ( 1 << tr.refdef.num_dlights ) - 1 ); + + // now add all the potentially visible surfaces + { + int i; + + for (i = 0; i < tr.world->numWorldSurfaces; i++) + { + if (tr.world->surfacesViewCount[i] != tr.viewCount) + continue; + + R_AddWorldSurface( tr.world->surfaces + i, tr.world->surfacesDlightBits[i] ); + } + + for (i = 0; i < tr.world->numMergedSurfaces; i++) + { + if (tr.world->mergedSurfacesViewCount[i] != tr.viewCount) + continue; + + R_AddWorldSurface( tr.world->mergedSurfaces + i, tr.world->mergedSurfacesDlightBits[i] ); + } + } } diff --git a/reaction/code/sdl/sdl_glimp.c b/reaction/code/sdl/sdl_glimp.c index c861b340..3e098b49 100644 --- a/reaction/code/sdl/sdl_glimp.c +++ b/reaction/code/sdl/sdl_glimp.c @@ -50,6 +50,13 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA typedef CGLContextObj QGLContext; #define GLimp_GetCurrentContext() CGLGetCurrentContext() #define GLimp_SetCurrentContext(ctx) CGLSetCurrentContext(ctx) +#elif defined(_WIN32) +typedef struct +{ + HDC hDC; // handle to device context + HGLRC hGLRC; // handle to GL rendering context +} QGLContext_t; +typedef QGLContext_t QGLContext; #else typedef void *QGLContext; #define GLimp_GetCurrentContext() (NULL) @@ -58,6 +65,40 @@ typedef void *QGLContext; static QGLContext opengl_context; +#ifdef _WIN32 +#include "SDL_syswm.h" +static QGLContext GLimp_GetCurrentContext(void) +{ + SDL_SysWMinfo info; + + QGLContext newcontext; + + SDL_VERSION(&info.version); + if(!SDL_GetWMInfo(&info)) + { + ri.Printf(PRINT_WARNING, "Failed to obtain HWND from SDL (InputRegistry)"); + newcontext.hDC = 0; + newcontext.hGLRC = 0; + return newcontext; + } + + newcontext.hDC = GetDC(info.window); + newcontext.hGLRC = info.hglrc; + + return newcontext; +} + +#ifdef SMP +static void GLimp_SetCurrentContext(qboolean enable) +{ + if(enable) + wglMakeCurrent(opengl_context.hDC, opengl_context.hGLRC); + else + wglMakeCurrent(opengl_context.hDC, NULL); +} +#endif +#endif + typedef enum { RSERR_OK, @@ -83,6 +124,92 @@ void (APIENTRYP qglMultiTexCoord2fARB) (GLenum target, GLfloat s, GLfloat t); void (APIENTRYP qglLockArraysEXT) (GLint first, GLsizei count); void (APIENTRYP qglUnlockArraysEXT) (void); +// GL_EXT_multi_draw_arrays +void (APIENTRY * qglMultiDrawArraysEXT) (GLenum mode, GLint *first, GLsizei *count, GLsizei primcount); +void (APIENTRY * qglMultiDrawElementsEXT) (GLenum mode, const GLsizei *count, GLenum type, const GLvoid **indices, GLsizei primcount); + +// GL_ARB_vertex_shader +void (APIENTRY * qglBindAttribLocationARB) (GLhandleARB programObj, GLuint index, const GLcharARB * name); +void (APIENTRY * qglGetActiveAttribARB) (GLhandleARB programObj, GLuint index, GLsizei maxLength, GLsizei * length, + GLint * size, GLenum * type, GLcharARB * name); +GLint(APIENTRY * qglGetAttribLocationARB) (GLhandleARB programObj, const GLcharARB * name); + +// GL_ARB_vertex_program +void (APIENTRY * qglVertexAttrib4fARB) (GLuint, GLfloat, GLfloat, GLfloat, GLfloat); +void (APIENTRY * qglVertexAttrib4fvARB) (GLuint, const GLfloat *); +void (APIENTRY * qglVertexAttribPointerARB) (GLuint index, GLint size, GLenum type, GLboolean normalized, + GLsizei stride, const GLvoid * pointer); +void (APIENTRY * qglEnableVertexAttribArrayARB) (GLuint index); +void (APIENTRY * qglDisableVertexAttribArrayARB) (GLuint index); + +// GL_ARB_vertex_buffer_object +void (APIENTRY * qglBindBufferARB) (GLenum target, GLuint buffer); +void (APIENTRY * qglDeleteBuffersARB) (GLsizei n, const GLuint * buffers); +void (APIENTRY * qglGenBuffersARB) (GLsizei n, GLuint * buffers); + +GLboolean(APIENTRY * qglIsBufferARB) (GLuint buffer); +void (APIENTRY * qglBufferDataARB) (GLenum target, GLsizeiptrARB size, const GLvoid * data, GLenum usage); +void (APIENTRY * qglBufferSubDataARB) (GLenum target, GLintptrARB offset, GLsizeiptrARB size, const GLvoid * data); +void (APIENTRY * qglGetBufferSubDataARB) (GLenum target, GLintptrARB offset, GLsizeiptrARB size, GLvoid * data); + +void (APIENTRY * qglGetBufferParameterivARB) (GLenum target, GLenum pname, GLint * params); +void (APIENTRY * qglGetBufferPointervARB) (GLenum target, GLenum pname, GLvoid * *params); + +// GL_ARB_shader_objects +void (APIENTRY * qglDeleteObjectARB) (GLhandleARB obj); + +GLhandleARB(APIENTRY * qglGetHandleARB) (GLenum pname); +void (APIENTRY * qglDetachObjectARB) (GLhandleARB containerObj, GLhandleARB attachedObj); + +GLhandleARB(APIENTRY * qglCreateShaderObjectARB) (GLenum shaderType); +void (APIENTRY * qglShaderSourceARB) (GLhandleARB shaderObj, GLsizei count, const GLcharARB * *string, + const GLint * length); +void (APIENTRY * qglCompileShaderARB) (GLhandleARB shaderObj); + +GLhandleARB(APIENTRY * qglCreateProgramObjectARB) (void); +void (APIENTRY * qglAttachObjectARB) (GLhandleARB containerObj, GLhandleARB obj); +void (APIENTRY * qglLinkProgramARB) (GLhandleARB programObj); +void (APIENTRY * qglUseProgramObjectARB) (GLhandleARB programObj); +void (APIENTRY * qglValidateProgramARB) (GLhandleARB programObj); +void (APIENTRY * qglUniform1fARB) (GLint location, GLfloat v0); +void (APIENTRY * qglUniform2fARB) (GLint location, GLfloat v0, GLfloat v1); +void (APIENTRY * qglUniform3fARB) (GLint location, GLfloat v0, GLfloat v1, GLfloat v2); +void (APIENTRY * qglUniform4fARB) (GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3); +void (APIENTRY * qglUniform1iARB) (GLint location, GLint v0); +void (APIENTRY * qglUniform2iARB) (GLint location, GLint v0, GLint v1); +void (APIENTRY * qglUniform3iARB) (GLint location, GLint v0, GLint v1, GLint v2); +void (APIENTRY * qglUniform4iARB) (GLint location, GLint v0, GLint v1, GLint v2, GLint v3); +void (APIENTRY * qglUniform2fvARB) (GLint location, GLsizei count, const GLfloat * value); +void (APIENTRY * qglUniform3fvARB) (GLint location, GLsizei count, const GLfloat * value); +void (APIENTRY * qglUniform4fvARB) (GLint location, GLsizei count, const GLfloat * value); +void (APIENTRY * qglUniform2ivARB) (GLint location, GLsizei count, const GLint * value); +void (APIENTRY * qglUniform3ivARB) (GLint location, GLsizei count, const GLint * value); +void (APIENTRY * qglUniform4ivARB) (GLint location, GLsizei count, const GLint * value); +void (APIENTRY * qglUniformMatrix2fvARB) (GLint location, GLsizei count, GLboolean transpose, const GLfloat * value); +void (APIENTRY * qglUniformMatrix3fvARB) (GLint location, GLsizei count, GLboolean transpose, const GLfloat * value); +void (APIENTRY * qglUniformMatrix4fvARB) (GLint location, GLsizei count, GLboolean transpose, const GLfloat * value); +void (APIENTRY * qglGetObjectParameterfvARB) (GLhandleARB obj, GLenum pname, GLfloat * params); +void (APIENTRY * qglGetObjectParameterivARB) (GLhandleARB obj, GLenum pname, GLint * params); +void (APIENTRY * qglGetInfoLogARB) (GLhandleARB obj, GLsizei maxLength, GLsizei * length, GLcharARB * infoLog); +void (APIENTRY * qglGetAttachedObjectsARB) (GLhandleARB containerObj, GLsizei maxCount, GLsizei * count, + GLhandleARB * obj); +GLint(APIENTRY * qglGetUniformLocationARB) (GLhandleARB programObj, const GLcharARB * name); +void (APIENTRY * qglGetActiveUniformARB) (GLhandleARB programObj, GLuint index, GLsizei maxIndex, GLsizei * length, + GLint * size, GLenum * type, GLcharARB * name); +void (APIENTRY * qglGetUniformfvARB) (GLhandleARB programObj, GLint location, GLfloat * params); +void (APIENTRY * qglGetUniformivARB) (GLhandleARB programObj, GLint location, GLint * params); +void (APIENTRY * qglGetShaderSourceARB) (GLhandleARB obj, GLsizei maxLength, GLsizei * length, GLcharARB * source); + +#if defined(WIN32) +// WGL_ARB_create_context +HGLRC(APIENTRY * qwglCreateContextAttribsARB) (HDC hdC, HGLRC hShareContext, const int *attribList); +#endif + +#if 0 //defined(__linux__) +// GLX_ARB_create_context +GLXContext (APIENTRY * qglXCreateContextAttribsARB) (Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list); +#endif + /* =============== GLimp_Shutdown @@ -96,6 +223,7 @@ void GLimp_Shutdown( void ) screen = NULL; Com_Memset( &glConfig, 0, sizeof( glConfig ) ); + Com_Memset( &glRefConfig, 0, sizeof( glRefConfig ) ); Com_Memset( &glState, 0, sizeof( glState ) ); } @@ -119,6 +247,7 @@ GLimp_LogComment */ void GLimp_LogComment( char *comment ) { + //ri.Printf(PRINT_ALL, comment); } /* @@ -434,6 +563,76 @@ static int GLimp_SetMode(int mode, qboolean fullscreen, qboolean noborder) break; } + // try to initialize an OpenGL 3.1 context +#if 0 //defined(WIN32) + qwglCreateContextAttribsARB = SDL_GL_GetProcAddress("wglCreateContextAttribsARB"); + if(qwglCreateContextAttribsARB) + { + int attribs[] = + { + WGL_CONTEXT_MAJOR_VERSION_ARB, + 3, + WGL_CONTEXT_MINOR_VERSION_ARB, + 1, + WGL_CONTEXT_FLAGS_ARB, + 0, //WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, + //WGL_CONTEXT_PROFILE_MASK_ARB, + //WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, + 0 + }; + + ri.Printf(PRINT_ALL, "Initializing OpenGL 3.1 context..."); + + opengl_context.hGLRC = qwglCreateContextAttribsARB(opengl_context.hDC, opengl_context.hGLRC, attribs); + if(wglMakeCurrent(opengl_context.hDC, opengl_context.hGLRC)) + { + ri.Printf(PRINT_ALL, " done\n"); + } + else + { + ri.Printf(PRINT_ALL, " failed\n"); + } + } +#elif 0 //defined(__linux__) + + // TODO + + /* +// GLX_ARB_create_context +#ifndef GLX_ARB_create_context +#define GLX_CONTEXT_DEBUG_BIT_ARB 0x00000001 +#define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002 +#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091 +#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092 +#define GLX_CONTEXT_FLAGS_ARB 0x2094 + +extern GLXContext (APIENTRY * qglXCreateContextAttribsARB) (Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list); +*/ + + qglXCreateContextAttribsARB = SDL_GL_GetProcAddress("glXCreateContextAttribsARB"); + if(qglXCreateContextAttribsARB) + { + int attribs[3]; + + ri.Printf(PRINT_ALL, "Initializing OpenGL 3.0 context..."); + + attribs[0] = WGL_CONTEXT_MAJOR_VERSION_ARB; + attribs[1] = 3; + attribs[2] = 0; //terminate first pair + + opengl_context->hGLRC = qglXCreateContextAttribsARB(opengl_context->, attribs); + if(wglMakeCurrent(opengl_context->hDC, opengl_context->hGLRC)) + { + ri.Printf(PRINT_ALL, " done\n"); + glConfig.driverType = GLDRV_OPENGL3; + } + else + { + ri.Printf(PRINT_ALL, " failed\n"); + } + } +#endif + GLimp_DetectAvailableModes(); if (!vidscreen) @@ -659,19 +858,19 @@ static void GLimp_InitExtensions( void ) ri.Printf( PRINT_ALL, "...GL_EXT_compiled_vertex_array not found\n" ); } - textureFilterAnisotropic = qfalse; + glRefConfig.textureFilterAnisotropic = qfalse; if ( GLimp_HaveExtension( "GL_EXT_texture_filter_anisotropic" ) ) { if ( r_ext_texture_filter_anisotropic->integer ) { - qglGetIntegerv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, (GLint *)&maxAnisotropy ); - if ( maxAnisotropy <= 0 ) { + qglGetIntegerv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, (GLint *)&glRefConfig.maxAnisotropy ); + if ( glRefConfig.maxAnisotropy <= 0 ) { ri.Printf( PRINT_ALL, "...GL_EXT_texture_filter_anisotropic not properly supported!\n" ); - maxAnisotropy = 0; + glRefConfig.maxAnisotropy = 0; } else { - ri.Printf( PRINT_ALL, "...using GL_EXT_texture_filter_anisotropic (max: %i)\n", maxAnisotropy ); - textureFilterAnisotropic = qtrue; + ri.Printf( PRINT_ALL, "...using GL_EXT_texture_filter_anisotropic (max: %i)\n", glRefConfig.maxAnisotropy ); + glRefConfig.textureFilterAnisotropic = qtrue; } } else @@ -683,6 +882,205 @@ static void GLimp_InitExtensions( void ) { ri.Printf( PRINT_ALL, "...GL_EXT_texture_filter_anisotropic not found\n" ); } + + // GL_EXT_multi_draw_arrays + glRefConfig.multiDrawArrays = qfalse; + qglMultiDrawArraysEXT = NULL; + qglMultiDrawElementsEXT = NULL; + if( GLimp_HaveExtension( "GL_EXT_multi_draw_arrays")) + { + qglMultiDrawArraysEXT = (PFNGLMULTIDRAWARRAYSEXTPROC) SDL_GL_GetProcAddress("glMultiDrawArraysEXT"); + qglMultiDrawElementsEXT = (PFNGLMULTIDRAWELEMENTSEXTPROC) SDL_GL_GetProcAddress("glMultiDrawElementsEXT"); + ri.Printf(PRINT_ALL, "...using GL_EXT_multi_draw_arrays\n"); + glRefConfig.multiDrawArrays = qtrue; + } + else + { + ri.Printf(PRINT_ALL, "...GL_EXT_multi_draw_arrays not found\n"); + } + + // GL_ARB_vertex_program + glRefConfig.vertexProgram = qfalse; + qglVertexAttrib4fARB = NULL; + qglVertexAttrib4fvARB = NULL; + qglVertexAttribPointerARB = NULL; + qglEnableVertexAttribArrayARB = NULL; + qglDisableVertexAttribArrayARB = NULL; + if( GLimp_HaveExtension( "GL_ARB_vertex_program")) + { + qglVertexAttrib4fARB = (PFNGLVERTEXATTRIB4FARBPROC) SDL_GL_GetProcAddress("glVertexAttrib4fARB"); + qglVertexAttrib4fvARB = (PFNGLVERTEXATTRIB4FVARBPROC) SDL_GL_GetProcAddress("glVertexAttrib4fvARB"); + qglVertexAttribPointerARB = (PFNGLVERTEXATTRIBPOINTERARBPROC) SDL_GL_GetProcAddress("glVertexAttribPointerARB"); + qglEnableVertexAttribArrayARB = + (PFNGLENABLEVERTEXATTRIBARRAYARBPROC) SDL_GL_GetProcAddress("glEnableVertexAttribArrayARB"); + qglDisableVertexAttribArrayARB = + (PFNGLDISABLEVERTEXATTRIBARRAYARBPROC) SDL_GL_GetProcAddress("glDisableVertexAttribArrayARB"); + ri.Printf(PRINT_ALL, "...using GL_ARB_vertex_program\n"); + glRefConfig.vertexProgram = qtrue; + } + else + { + ri.Printf(PRINT_ALL, "...GL_ARB_vertex_program not found\n"); + } + + // GL_ARB_vertex_buffer_object + glRefConfig.vertexBufferObject = qfalse; + qglBindBufferARB = NULL; + qglDeleteBuffersARB = NULL; + qglGenBuffersARB = NULL; + qglIsBufferARB = NULL; + qglBufferDataARB = NULL; + qglBufferSubDataARB = NULL; + qglGetBufferSubDataARB = NULL; + qglGetBufferParameterivARB = NULL; + qglGetBufferPointervARB = NULL; + if( GLimp_HaveExtension( "GL_ARB_vertex_buffer_object")) + { + qglBindBufferARB = (PFNGLBINDBUFFERARBPROC) SDL_GL_GetProcAddress("glBindBufferARB"); + qglDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC) SDL_GL_GetProcAddress("glDeleteBuffersARB"); + qglGenBuffersARB = (PFNGLGENBUFFERSARBPROC) SDL_GL_GetProcAddress("glGenBuffersARB"); + qglIsBufferARB = (PFNGLISBUFFERARBPROC) SDL_GL_GetProcAddress("glIsBufferARB"); + qglBufferDataARB = (PFNGLBUFFERDATAARBPROC) SDL_GL_GetProcAddress("glBufferDataARB"); + qglBufferSubDataARB = (PFNGLBUFFERSUBDATAARBPROC) SDL_GL_GetProcAddress("glBufferSubDataARB"); + qglGetBufferSubDataARB = (PFNGLGETBUFFERSUBDATAARBPROC) SDL_GL_GetProcAddress("glGetBufferSubDataARB"); + qglGetBufferParameterivARB = (PFNGLGETBUFFERPARAMETERIVARBPROC) SDL_GL_GetProcAddress("glGetBufferParameterivARB"); + qglGetBufferPointervARB = (PFNGLGETBUFFERPOINTERVARBPROC) SDL_GL_GetProcAddress("glGetBufferPointervARB"); + ri.Printf(PRINT_ALL, "...using GL_ARB_vertex_buffer_object\n"); + glRefConfig.vertexBufferObject = qtrue; + } + else + { + ri.Printf(PRINT_ALL, "...GL_ARB_vertex_buffer_object not found\n"); + } + + // GL_ARB_shader_objects + glRefConfig.shaderObjects = qfalse; + qglDeleteObjectARB = NULL; + qglGetHandleARB = NULL; + qglDetachObjectARB = NULL; + qglCreateShaderObjectARB = NULL; + qglShaderSourceARB = NULL; + qglCompileShaderARB = NULL; + qglCreateProgramObjectARB = NULL; + qglAttachObjectARB = NULL; + qglLinkProgramARB = NULL; + qglUseProgramObjectARB = NULL; + qglValidateProgramARB = NULL; + qglUniform1fARB = NULL; + qglUniform2fARB = NULL; + qglUniform3fARB = NULL; + qglUniform4fARB = NULL; + qglUniform1iARB = NULL; + qglUniform2iARB = NULL; + qglUniform3iARB = NULL; + qglUniform4iARB = NULL; + qglUniform2fvARB = NULL; + qglUniform3fvARB = NULL; + qglUniform4fvARB = NULL; + qglUniform2ivARB = NULL; + qglUniform3ivARB = NULL; + qglUniform4ivARB = NULL; + qglUniformMatrix2fvARB = NULL; + qglUniformMatrix3fvARB = NULL; + qglUniformMatrix4fvARB = NULL; + qglGetObjectParameterfvARB = NULL; + qglGetObjectParameterivARB = NULL; + qglGetInfoLogARB = NULL; + qglGetAttachedObjectsARB = NULL; + qglGetUniformLocationARB = NULL; + qglGetActiveUniformARB = NULL; + qglGetUniformfvARB = NULL; + qglGetUniformivARB = NULL; + qglGetShaderSourceARB = NULL; + if( GLimp_HaveExtension( "GL_ARB_shader_objects")) + { + qglDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC) SDL_GL_GetProcAddress("glDeleteObjectARB"); + qglGetHandleARB = (PFNGLGETHANDLEARBPROC) SDL_GL_GetProcAddress("glGetHandleARB"); + qglDetachObjectARB = (PFNGLDETACHOBJECTARBPROC) SDL_GL_GetProcAddress("glDetachObjectARB"); + qglCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC) SDL_GL_GetProcAddress("glCreateShaderObjectARB"); + qglShaderSourceARB = (PFNGLSHADERSOURCEARBPROC) SDL_GL_GetProcAddress("glShaderSourceARB"); + qglCompileShaderARB = (PFNGLCOMPILESHADERARBPROC) SDL_GL_GetProcAddress("glCompileShaderARB"); + qglCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glCreateProgramObjectARB"); + qglAttachObjectARB = (PFNGLATTACHOBJECTARBPROC) SDL_GL_GetProcAddress("glAttachObjectARB"); + qglLinkProgramARB = (PFNGLLINKPROGRAMARBPROC) SDL_GL_GetProcAddress("glLinkProgramARB"); + qglUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC) SDL_GL_GetProcAddress("glUseProgramObjectARB"); + qglValidateProgramARB = (PFNGLVALIDATEPROGRAMARBPROC) SDL_GL_GetProcAddress("glValidateProgramARB"); + qglUniform1fARB = (PFNGLUNIFORM1FARBPROC) SDL_GL_GetProcAddress("glUniform1fARB"); + qglUniform2fARB = (PFNGLUNIFORM2FARBPROC) SDL_GL_GetProcAddress("glUniform2fARB"); + qglUniform3fARB = (PFNGLUNIFORM3FARBPROC) SDL_GL_GetProcAddress("glUniform3fARB"); + qglUniform4fARB = (PFNGLUNIFORM4FARBPROC) SDL_GL_GetProcAddress("glUniform4fARB"); + qglUniform1iARB = (PFNGLUNIFORM1IARBPROC) SDL_GL_GetProcAddress("glUniform1iARB"); + qglUniform2iARB = (PFNGLUNIFORM2IARBPROC) SDL_GL_GetProcAddress("glUniform2iARB"); + qglUniform3iARB = (PFNGLUNIFORM3IARBPROC) SDL_GL_GetProcAddress("glUniform3iARB"); + qglUniform4iARB = (PFNGLUNIFORM4IARBPROC) SDL_GL_GetProcAddress("glUniform4iARB"); + qglUniform2fvARB = (PFNGLUNIFORM2FVARBPROC) SDL_GL_GetProcAddress("glUniform2fvARB"); + qglUniform3fvARB = (PFNGLUNIFORM3FVARBPROC) SDL_GL_GetProcAddress("glUniform3fvARB"); + qglUniform4fvARB = (PFNGLUNIFORM4FVARBPROC) SDL_GL_GetProcAddress("glUniform4fvARB"); + qglUniform2ivARB = (PFNGLUNIFORM2IVARBPROC) SDL_GL_GetProcAddress("glUniform2ivARB"); + qglUniform3ivARB = (PFNGLUNIFORM3IVARBPROC) SDL_GL_GetProcAddress("glUniform3ivARB"); + qglUniform4ivARB = (PFNGLUNIFORM4IVARBPROC) SDL_GL_GetProcAddress("glUniform4ivARB"); + qglUniformMatrix2fvARB = (PFNGLUNIFORMMATRIX2FVARBPROC) SDL_GL_GetProcAddress("glUniformMatrix2fvARB"); + qglUniformMatrix3fvARB = (PFNGLUNIFORMMATRIX3FVARBPROC) SDL_GL_GetProcAddress("glUniformMatrix3fvARB"); + qglUniformMatrix4fvARB = (PFNGLUNIFORMMATRIX4FVARBPROC) SDL_GL_GetProcAddress("glUniformMatrix4fvARB"); + qglGetObjectParameterfvARB = (PFNGLGETOBJECTPARAMETERFVARBPROC) SDL_GL_GetProcAddress("glGetObjectParameterfvARB"); + qglGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC) SDL_GL_GetProcAddress("glGetObjectParameterivARB"); + qglGetInfoLogARB = (PFNGLGETINFOLOGARBPROC) SDL_GL_GetProcAddress("glGetInfoLogARB"); + qglGetAttachedObjectsARB = (PFNGLGETATTACHEDOBJECTSARBPROC) SDL_GL_GetProcAddress("glGetAttachedObjectsARB"); + qglGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC) SDL_GL_GetProcAddress("glGetUniformLocationARB"); + qglGetActiveUniformARB = (PFNGLGETACTIVEUNIFORMARBPROC) SDL_GL_GetProcAddress("glGetActiveUniformARB"); + qglGetUniformfvARB = (PFNGLGETUNIFORMFVARBPROC) SDL_GL_GetProcAddress("glGetUniformfvARB"); + qglGetUniformivARB = (PFNGLGETUNIFORMIVARBPROC) SDL_GL_GetProcAddress("glGetUniformivARB"); + qglGetShaderSourceARB = (PFNGLGETSHADERSOURCEARBPROC) SDL_GL_GetProcAddress("glGetShaderSourceARB"); + ri.Printf(PRINT_ALL, "...using GL_ARB_shader_objects\n"); + glRefConfig.shaderObjects = qtrue; + } + else + { + ri.Printf(PRINT_ALL, "...GL_ARB_shader_objects not found\n"); + } + + // GL_ARB_vertex_shader + glRefConfig.vertexShader = qfalse; + qglBindAttribLocationARB = NULL; + qglGetActiveAttribARB = NULL; + qglGetAttribLocationARB = NULL; + if( GLimp_HaveExtension( "GL_ARB_vertex_shader")) + { + int reservedComponents; + + //qglGetIntegerv(GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB, &glConfig.maxVertexUniforms); + //qglGetIntegerv(GL_MAX_VARYING_FLOATS_ARB, &glConfig.maxVaryingFloats); + //qglGetIntegerv(GL_MAX_VERTEX_ATTRIBS_ARB, &glConfig.maxVertexAttribs); + + reservedComponents = 16 * 10; // approximation how many uniforms we have besides the bone matrices + +#if 0 + if(glConfig.driverType == GLDRV_MESA) + { + // HACK + // restrict to number of vertex uniforms to 512 because of: + // xreal.x86_64: nv50_program.c:4181: nv50_program_validate_data: Assertion `p->param_nr <= 512' failed + + glConfig.maxVertexUniforms = Q_bound(0, glConfig.maxVertexUniforms, 512); + } +#endif + + //glConfig.maxVertexSkinningBones = (int) Q_bound(0.0, (Q_max(glConfig.maxVertexUniforms - reservedComponents, 0) / 16), MAX_BONES); + //glConfig.vboVertexSkinningAvailable = r_vboVertexSkinning->integer && ((glConfig.maxVertexSkinningBones >= 12) ? qtrue : qfalse); + + qglBindAttribLocationARB = (PFNGLBINDATTRIBLOCATIONARBPROC) SDL_GL_GetProcAddress("glBindAttribLocationARB"); + qglGetActiveAttribARB = (PFNGLGETACTIVEATTRIBARBPROC) SDL_GL_GetProcAddress("glGetActiveAttribARB"); + qglGetAttribLocationARB = (PFNGLGETATTRIBLOCATIONARBPROC) SDL_GL_GetProcAddress("glGetAttribLocationARB"); + ri.Printf(PRINT_ALL, "...using GL_ARB_vertex_shader\n"); + glRefConfig.vertexShader = qtrue; + } + else + { + ri.Printf(PRINT_ALL, "...GL_ARB_vertex_shader not found\n"); + } + + glRefConfig.glsl = glRefConfig.vertexProgram && glRefConfig.shaderObjects && glRefConfig.vertexShader; + } #define R_MODE_FALLBACK 3 // 640 * 480 @@ -750,12 +1148,40 @@ success: glConfig.deviceSupportsGamma = SDL_SetGamma( 1.0f, 1.0f, 1.0f ) >= 0; // get our config strings - Q_strncpyz( glConfig.vendor_string, (char *) qglGetString (GL_VENDOR), sizeof( glConfig.vendor_string ) ); - Q_strncpyz( glConfig.renderer_string, (char *) qglGetString (GL_RENDERER), sizeof( glConfig.renderer_string ) ); - if (*glConfig.renderer_string && glConfig.renderer_string[strlen(glConfig.renderer_string) - 1] == '\n') - glConfig.renderer_string[strlen(glConfig.renderer_string) - 1] = 0; - Q_strncpyz( glConfig.version_string, (char *) qglGetString (GL_VERSION), sizeof( glConfig.version_string ) ); - Q_strncpyz( glConfig.extensions_string, (char *) qglGetString (GL_EXTENSIONS), sizeof( glConfig.extensions_string ) ); + { + char *string; + + string = (char *) qglGetString (GL_VENDOR); + if (string) + Q_strncpyz( glConfig.vendor_string, (char *) qglGetString (GL_VENDOR), sizeof( glConfig.vendor_string ) ); + else + glConfig.vendor_string[0] = '\0'; + + string = (char *) qglGetString (GL_RENDERER); + if (string) + Q_strncpyz( glConfig.renderer_string, (char *) qglGetString (GL_RENDERER), sizeof( glConfig.renderer_string ) ); + else + glConfig.renderer_string[0] = '\0'; + + if (*glConfig.renderer_string && glConfig.renderer_string[strlen(glConfig.renderer_string) - 1] == '\n') + glConfig.renderer_string[strlen(glConfig.renderer_string) - 1] = 0; + + string = (char *) qglGetString(GL_VERSION); + if (string) + Q_strncpyz( glConfig.version_string, (char *) qglGetString (GL_VERSION), sizeof( glConfig.version_string ) ); + else + glConfig.version_string[0] = '\0'; + + string = (char *) qglGetString (GL_EXTENSIONS); + if (string) + { + Q_strncpyz( glConfig.extensions_string, string, sizeof( glConfig.extensions_string ) ); + } + else + { + glConfig.extensions_string[0] = '\0'; + } + } // initialize extensions GLimp_InitExtensions( );