Index: code/qcommon/q_platform.h =================================================================== --- code/qcommon/q_platform.h (revision 1802) +++ code/qcommon/q_platform.h (working copy) @@ -98,7 +98,7 @@ #define DLL_EXT ".dll" -#elif __WIN32__ +#elif defined(__WIN32__) || defined(_WIN32) #undef QDECL #define QDECL __cdecl Index: code/qcommon/qfiles.h =================================================================== --- code/qcommon/qfiles.h (revision 1802) +++ code/qcommon/qfiles.h (working copy) @@ -37,6 +37,7 @@ // 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 Index: code/renderer/qgl.h =================================================================== --- code/renderer/qgl.h (revision 1802) +++ code/renderer/qgl.h (working copy) @@ -39,6 +39,16 @@ extern void (APIENTRYP qglLockArraysEXT) (GLint first, GLsizei count); extern void (APIENTRYP qglUnlockArraysEXT) (void); +// 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); //=========================================================================== Index: code/renderer/tr_bsp.c =================================================================== --- code/renderer/tr_bsp.c (revision 1802) +++ code/renderer/tr_bsp.c (working copy) @@ -127,6 +127,41 @@ /* =============== +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 =============== @@ -306,63 +341,81 @@ 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; - lightmapNum = LittleLong( ds->lightmapNum ); + surf->lightmapNum = 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, surf->lightmapNum ); 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->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] ); + + 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 ); @@ -379,19 +432,18 @@ 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; - lightmapNum = LittleLong( ds->lightmapNum ); + surf->lightmapNum = 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, surf->lightmapNum ); if ( r_singleShader->integer && !surf->shader->isSky ) { surf->shader = tr.defaultShader; } @@ -406,18 +458,26 @@ 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 ); + + R_ColorShiftLightingBytes( verts[i].color, points[i].vertexColors ); } // pre-tesseleate @@ -443,10 +503,15 @@ =============== */ 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; + static surfaceType_t skipData = SF_SKIP; + // get lightmap + surf->lightmapNum = -1; // FIXME LittleLong(ds->lightmapNum); + // get fog volume surf->fogIndex = LittleLong( ds->fogNum ) + 1; @@ -456,45 +521,57 @@ 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->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] ); + 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]); } - 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] ); + + 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, tri->verts[i].color ); + 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"); + } } - } -} + }} /* =============== @@ -505,6 +582,9 @@ srfFlare_t *flare; int i; + // get lightmap + surf->lightmapNum = -1; + // get fog volume surf->fogIndex = LittleLong( ds->fogNum ) + 1; @@ -1222,8 +1302,917 @@ } } + /* +================= +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 < bb->shader) + return -1; + + else if(aa->shader > bb->shader) + return 1; + + // by lightmap + if(aa->lightmapNum < bb->lightmapNum) + return -1; + + else if(aa->lightmapNum > bb->lightmapNum) + return 1; + + return 0; +} + + +static void CopyVert(const srfVert_t * in, srfVert_t * out) +{ + 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 +} + + + +/* +================= +R_CreateClusters +================= +*/ +static void R_CreateClusters() +{ + int i, j; + int numClusters; + mnode_t *node, *parent; + bspCluster_t *cluster; + const byte *vis; + int c; + msurface_t *surface, **mark; + int surfaceNum; + vec3_t mins, maxs; + int currentNumMarkSurfaces; + + ri.Printf(PRINT_ALL, "...creating BSP clusters\n"); + + if(s_worldData.vis) + { + // go through the leaves and count clusters + numClusters = 0; + for(i = 0, node = s_worldData.nodes; i < s_worldData.numnodes; i++, node++) + { + if(node->cluster >= numClusters) + { + numClusters = node->cluster; + } + } + numClusters++; + + s_worldData.numClusters = numClusters; + s_worldData.clusters = ri.Hunk_Alloc((numClusters + 1) * sizeof(*s_worldData.clusters), h_low); // + supercluster + + // reset surfaces' viewCount + for(i = 0, surface = s_worldData.surfaces; i < s_worldData.numsurfaces; i++, surface++) + { + surface->viewCount = -1; + } + + for(j = 0, node = s_worldData.nodes; j < s_worldData.numnodes; j++, node++) + { + node->visCounts[0] = -1; + } + + for(i = 0; i < numClusters; i++) + { + cluster = &s_worldData.clusters[i]; + + // mark leaves in cluster + vis = s_worldData.vis + i * s_worldData.clusterBytes; + + for(j = 0, node = s_worldData.nodes; j < s_worldData.numnodes; j++, node++) + { + if(node->cluster < 0 || node->cluster >= numClusters) + { + continue; + } + + // check general pvs + if(!(vis[node->cluster >> 3] & (1 << (node->cluster & 7)))) + { + continue; + } + + parent = node; + do + { + if(parent->visCounts[0] == i) + break; + parent->visCounts[0] = i; + parent = parent->parent; + } while(parent); + } + + + // add cluster surfaces + cluster->numMarkSurfaces = 0; + currentNumMarkSurfaces = 0; + + ClearBounds(mins, maxs); + for(j = 0, node = s_worldData.nodes; j < s_worldData.numnodes; j++, node++) + { + if(node->contents == CONTENTS_NODE) + continue; + + if(node->visCounts[0] != i) + continue; + + //BoundsAdd(mins, maxs, node->mins, node->maxs); + AddPointToBounds(node->mins, mins, maxs); + AddPointToBounds(node->maxs, mins, maxs); + + mark = node->firstmarksurface; + c = node->nummarksurfaces; + while(c--) + { + // the surface may have already been added if it + // spans multiple leafs + surface = *mark; + + surfaceNum = surface - s_worldData.surfaces; + + if((surface->viewCount != i) && (surfaceNum < s_worldData.numWorldSurfaces)) + { + surface->viewCount = i; + + currentNumMarkSurfaces++; + } + + mark++; + } + } + + cluster->markSurfaces = ri.Hunk_Alloc(sizeof(*cluster->markSurfaces) * currentNumMarkSurfaces, h_low); + + // Actually link the surfaces to the cluster now + for(j = 0, surface = s_worldData.surfaces; j < s_worldData.numWorldSurfaces; j++, surface++) + { + surfaceNum = surface - s_worldData.surfaces; + + if(surface->viewCount == i) + { + if (cluster->numMarkSurfaces == currentNumMarkSurfaces) + { + ri.Error(ERR_DROP, "R_CreateClusters: Incorrect allocation for MarkSurfaces!\n", cluster->numMarkSurfaces); + return; + } + cluster->markSurfaces[cluster->numMarkSurfaces] = surface; + cluster->numMarkSurfaces++; + } + } + + cluster->origin[0] = (mins[0] + maxs[0]) / 2; + cluster->origin[1] = (mins[1] + maxs[1]) / 2; + cluster->origin[2] = (mins[2] + maxs[2]) / 2; + + //ri.Printf(PRINT_ALL, "cluster %i origin at (%i %i %i)\n", i, (int)cluster->origin[0], (int)cluster->origin[1], (int)cluster->origin[2]); + + //ri.Printf(PRINT_ALL, "cluster %i contains %i bsp surfaces\n", i, cluster->numMarkSurfaces); + } + } + else + { + numClusters = 0; + + s_worldData.numClusters = numClusters; + s_worldData.clusters = ri.Hunk_Alloc((numClusters + 1) * sizeof(*s_worldData.clusters), h_low); // + supercluster + } + + // create a super cluster that will be always used when no view cluster can be found + cluster = &s_worldData.clusters[numClusters]; + + if (cluster->numMarkSurfaces) + { + ri.Free(cluster->markSurfaces); + } + cluster->numMarkSurfaces = 0; + currentNumMarkSurfaces = s_worldData.numWorldSurfaces; + cluster->markSurfaces = ri.Hunk_Alloc(sizeof(*cluster->markSurfaces) * currentNumMarkSurfaces, h_low); + + for(i = 0, surface = s_worldData.surfaces; i < s_worldData.numWorldSurfaces; i++, surface++) + { + if (cluster->numMarkSurfaces == currentNumMarkSurfaces) + { + ri.Error(ERR_DROP, "R_CreateClusters(): cluster->numMarkSurfaces == currentNumMarkSurfaces !? \n"); + } + cluster->markSurfaces[cluster->numMarkSurfaces] = surface; + cluster->numMarkSurfaces++; + } + + for(i = 0; i < MAX_VISCOUNTS; i++) + { + s_worldData.numClusterVBOSurfaces[i] = 0; + } + + //ri.Printf(PRINT_ALL, "noVis cluster contains %i bsp surfaces\n", cluster->numMarkSurfaces); + + ri.Printf(PRINT_ALL, "%i world clusters created\n", numClusters + 1); + + + // reset surfaces' viewCount + for(i = 0, surface = s_worldData.surfaces; i < s_worldData.numsurfaces; i++, surface++) + { + surface->viewCount = -1; + } + + for(j = 0, node = s_worldData.nodes; j < s_worldData.numnodes; j++, node++) + { + node->visCounts[0] = -1; + } +} + + +/* =============== +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; + +// trRefLight_t *light; + + int startTime, endTime; + + startTime = ri.Milliseconds(); + + numVerts = 0; + numTriangles = 0; + for(k = 0, surface = &s_worldData.surfaces[0]; k < 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; + } + 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; + } + 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; + } + } + + 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); + + // set up triangle indices + numVerts = 0; + numTriangles = 0; + for(k = 0, surface = &s_worldData.surfaces[0]; k < s_worldData.numWorldSurfaces; k++, surface++) + { + if(*surface->data == SF_FACE) + { + srfSurfaceFace_t *srf = (srfSurfaceFace_t *) surface->data; + + srf->firstTriangle = numTriangles; + + 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->firstTriangle = numTriangles; + + 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->firstTriangle = numTriangles; + + 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 = &s_worldData.surfaces[0]; k < s_worldData.numWorldSurfaces; k++, surface++) + { + 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_TANGENT | ATTR_BINORMAL | + ATTR_NORMAL | ATTR_COLOR | ATTR_PAINTCOLOR | ATTR_LIGHTDIRECTION, VBO_USAGE_STATIC); +#endif + + 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 = &s_worldData.surfaces[0]; k < s_worldData.numWorldSurfaces; k++, surface++) + { + if(*surface->data == SF_FACE) + { + srfSurfaceFace_t *srf = (srfSurfaceFace_t *) surface->data; + + if( /*r_arb_vertex_buffer_object->integer && */ srf->numVerts && srf->numTriangles) + { + srf->vbo = s_worldData.vbo; + srf->ibo = R_CreateIBO2(va("staticBspModel0_planarSurface_IBO %i", k), srf->numTriangles, triangles + srf->firstTriangle, VBO_USAGE_STATIC); + } + } + else if(*surface->data == SF_GRID) + { + srfGridMesh_t *srf = (srfGridMesh_t *) surface->data; + + if( /*r_arb_vertex_buffer_object->integer && */ srf->numVerts && srf->numTriangles) + { + srf->vbo = s_worldData.vbo; + srf->ibo = R_CreateIBO2(va("staticBspModel0_curveSurface_IBO %i", k), srf->numTriangles, triangles + srf->firstTriangle, VBO_USAGE_STATIC); + } + } + else if(*surface->data == SF_TRIANGLES) + { + srfTriangles_t *srf = (srfTriangles_t *) surface->data; + + if( /*r_arb_vertex_buffer_object->integer && */ srf->numVerts && srf->numTriangles) + { + srf->vbo = s_worldData.vbo; + srf->ibo = R_CreateIBO2(va("staticBspModel0_triangleSurface_IBO %i", k), srf->numTriangles, triangles + srf->firstTriangle, VBO_USAGE_STATIC); + } + } + } + + + 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.Hunk_FreeTempMemory(triangles); + //ri.Hunk_FreeTempMemory(optimizedVerts); + //ri.Hunk_FreeTempMemory(verts); +} + +/* +=============== +R_CreateSubModelVBOs +=============== +*/ + +static void R_CreateSubModelVBOs() +{ + int i, j, k, l, m; + + int numVerts; + srfVert_t *verts; + + srfVert_t *optimizedVerts; + + int numTriangles; + srfTriangle_t *triangles; + + shader_t *shader, *oldShader; + int lightmapNum, oldLightmapNum; + + int numSurfaces; + msurface_t *surface, *surface2; + msurface_t **surfacesSorted; + + bmodel_t *model; + + srfVBOMesh_t *vboSurf; + + for(m = 1, model = s_worldData.bmodels; m < s_worldData.numBModels; m++, model++) + { + // count number of static area surfaces + numSurfaces = 0; + for(k = 0; k < model->numSurfaces; k++) + { + surface = model->firstSurface + k; + shader = surface->shader; + + if(shader->isSky) + continue; + + if(shader->isPortal) + continue; + + if(ShaderRequiresCPUDeforms(shader)) + continue; + + numSurfaces++; + } + + if(!numSurfaces) + continue; + + // build interaction caches list + surfacesSorted = ri.Malloc(numSurfaces * sizeof(surfacesSorted[0])); + + numSurfaces = 0; + for(k = 0; k < model->numSurfaces; k++) + { + surface = model->firstSurface + k; + shader = surface->shader; + + if(shader->isSky) + continue; + + if(shader->isPortal) + continue; + + if(ShaderRequiresCPUDeforms(shader)) + continue; + + surfacesSorted[numSurfaces] = surface; + numSurfaces++; + } + + model->numVBOSurfaces = 0; + + // sort surfaces by shader + qsort(surfacesSorted, numSurfaces, sizeof(surfacesSorted), BSPSurfaceCompare); + + // create a VBO for each shader + shader = oldShader = NULL; + lightmapNum = oldLightmapNum = -1; + + for(k = 0; k < numSurfaces; k++) + { + surface = surfacesSorted[k]; + shader = surface->shader; + lightmapNum = surface->lightmapNum; + + if(shader != oldShader) // || (r_precomputedLighting->integer ? lightmapNum != oldLightmapNum : 0)) + { + oldShader = shader; + oldLightmapNum = lightmapNum; + + // count vertices and indices + numVerts = 0; + numTriangles = 0; + + for(l = k; l < numSurfaces; l++) + { + surface2 = surfacesSorted[l]; + + if(surface2->shader != shader) + continue; + + if(*surface2->data == SF_FACE) + { + srfSurfaceFace_t *face = (srfSurfaceFace_t *) surface2->data; + + if(face->numVerts) + numVerts += face->numVerts; + + if(face->numTriangles) + numTriangles += face->numTriangles; + } + else if(*surface2->data == SF_GRID) + { + srfGridMesh_t *grid = (srfGridMesh_t *) surface2->data; + + if(grid->numVerts) + numVerts += grid->numVerts; + + if(grid->numTriangles) + numTriangles += grid->numTriangles; + } + else if(*surface2->data == SF_TRIANGLES) + { + srfTriangles_t *tri = (srfTriangles_t *) surface2->data; + + if(tri->numVerts) + numVerts += tri->numVerts; + + if(tri->numTriangles) + numTriangles += tri->numTriangles; + } + } + + if(!numVerts || !numTriangles) + continue; + + ri.Printf(PRINT_DEVELOPER, "...calculating entity mesh VBOs ( %s, %i verts %i tris )\n", shader->name, numVerts, + numTriangles); + + // create surface + vboSurf = ri.Hunk_Alloc(sizeof(*vboSurf), h_low); + + if (model->numVBOSurfaces == MAX_MODEL_VBO_SURFACES) + { + ri.Error(ERR_DROP, "Hit MAX_MODEL_VBO_SURFACES!\n"); + } + + model->vboSurfaces[model->numVBOSurfaces] = vboSurf; + model->numVBOSurfaces++; + + vboSurf->surfaceType = SF_VBO_MESH; + vboSurf->numIndexes = numTriangles * 3; + vboSurf->numVerts = numVerts; + + vboSurf->shader = shader; + vboSurf->lightmapNum = lightmapNum; + + // create arrays + verts = ri.Malloc(numVerts * sizeof(srfVert_t)); + optimizedVerts = ri.Malloc(numVerts * sizeof(srfVert_t)); + numVerts = 0; + + triangles = ri.Malloc(numTriangles * sizeof(srfTriangle_t)); + numTriangles = 0; + + ClearBounds(vboSurf->bounds[0], vboSurf->bounds[1]); + + // build triangle indices + for(l = k; l < numSurfaces; l++) + { + surface2 = surfacesSorted[l]; + + if(surface2->shader != shader) + continue; + + // set up triangle indices + if(*surface2->data == SF_FACE) + { + srfSurfaceFace_t *srf = (srfSurfaceFace_t *) surface2->data; + + 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(*surface2->data == SF_GRID) + { + srfGridMesh_t *srf = (srfGridMesh_t *) surface2->data; + + 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(*surface2->data == SF_TRIANGLES) + { + srfTriangles_t *srf = (srfTriangles_t *) surface2->data; + + 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(l = k; l < numSurfaces; l++) + { + surface2 = surfacesSorted[l]; + + if(surface2->shader != shader) + continue; + + if(*surface2->data == SF_FACE) + { + srfSurfaceFace_t *cv = (srfSurfaceFace_t *) surface2->data; + + if(cv->numVerts) + { + for(i = 0; i < cv->numVerts; i++) + { + CopyVert(&cv->verts[i], &verts[numVerts + i]); + + AddPointToBounds(cv->verts[i].xyz, vboSurf->bounds[0], vboSurf->bounds[1]); + } + + numVerts += cv->numVerts; + } + } + else if(*surface2->data == SF_GRID) + { + srfGridMesh_t *cv = (srfGridMesh_t *) surface2->data; + + if(cv->numVerts) + { + for(i = 0; i < cv->numVerts; i++) + { + CopyVert(&cv->verts[i], &verts[numVerts + i]); + + AddPointToBounds(cv->verts[i].xyz, vboSurf->bounds[0], vboSurf->bounds[1]); + } + + numVerts += cv->numVerts; + } + } + else if(*surface2->data == SF_TRIANGLES) + { + srfTriangles_t *cv = (srfTriangles_t *) surface2->data; + + if(cv->numVerts) + { + for(i = 0; i < cv->numVerts; i++) + { + CopyVert(&cv->verts[i], &verts[numVerts + i]); + + AddPointToBounds(cv->verts[i].xyz, vboSurf->bounds[0], vboSurf->bounds[1]); + } + + numVerts += cv->numVerts; + } + } + } + +#if 0 + numVerts = OptimizeVertices(numVerts, verts, numTriangles, triangles, optimizedVerts, CompareWorldVert); + if(c_redundantVertexes) + { + ri.Printf(PRINT_DEVELOPER, + "...removed %i redundant vertices from staticEntityMesh %i ( %s, %i verts %i tris )\n", + c_redundantVertexes, model->numVBOSurfaces, shader->name, numVerts, numTriangles); + } + + vboSurf->vbo = + R_CreateVBO2(va("staticBspModel%i_VBO %i", m, model->numVBOSurfaces), numVerts, optimizedVerts, + ATTR_POSITION | ATTR_TEXCOORD | ATTR_LIGHTCOORD | ATTR_TANGENT | ATTR_BINORMAL | ATTR_NORMAL + | ATTR_COLOR | GLCS_LIGHTCOLOR | ATTR_LIGHTDIRECTION, VBO_USAGE_STATIC); +#else + vboSurf->vbo = + R_CreateVBO2(va("staticBspModel%i_VBO %i", m, model->numVBOSurfaces), numVerts, verts, + ATTR_POSITION | ATTR_TEXCOORD | ATTR_LIGHTCOORD | ATTR_TANGENT | ATTR_BINORMAL | ATTR_NORMAL + | ATTR_COLOR | ATTR_PAINTCOLOR | ATTR_LIGHTDIRECTION, VBO_USAGE_STATIC); +#endif + + vboSurf->ibo = + R_CreateIBO2(va("staticBspModel%i_IBO %i", m, model->numVBOSurfaces), numTriangles, triangles, + VBO_USAGE_STATIC); + + ri.Free(triangles); + ri.Free(optimizedVerts); + ri.Free(verts); + } + } + + ri.Free(surfacesSorted); + + ri.Printf(PRINT_ALL, "%i VBO surfaces created for BSP submodel %i\n", model->numVBOSurfaces, m); + } +} + +/* +=============== R_LoadSurfaces =============== */ @@ -1313,6 +2302,7 @@ 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 ; ifirstSurface = s_worldData.surfaces + LittleLong( in->firstSurface ); out->numSurfaces = LittleLong( in->numSurfaces ); + + if(i == 0) + { + // Tr3B: add this for limiting VBO surface creation + s_worldData.numWorldSurfaces = out->numSurfaces; + } } } @@ -1861,11 +2857,20 @@ R_LoadEntities( &header->lumps[LUMP_ENTITIES] ); R_LoadLightGrid( &header->lumps[LUMP_LIGHTGRID] ); + // create static VBOS from the world + R_CreateWorldVBO(); + R_CreateClusters(); + R_CreateSubModelVBOs(); + 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 + R_BindNullVBO(); + R_BindNullIBO(); + ri.FS_FreeFile( buffer.v ); } Index: code/renderer/tr_curve.c =================================================================== --- code/renderer/tr_curve.c (revision 1802) +++ code/renderer/tr_curve.c (working copy) @@ -33,7 +33,7 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 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 @@ 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 @@ ================= */ 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 @@ 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 @@ 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; @@ -358,16 +427,18 @@ ================= */ 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 +582,13 @@ } #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 +599,12 @@ 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 +633,10 @@ } // 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 +645,7 @@ // 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 +659,12 @@ 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 +693,10 @@ } // 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 +705,7 @@ // 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; Index: code/renderer/tr_init.c =================================================================== --- code/renderer/tr_init.c (revision 1802) +++ code/renderer/tr_init.c (working copy) @@ -92,6 +92,10 @@ cvar_t *r_ext_texture_filter_anisotropic; cvar_t *r_ext_max_anisotropy; +cvar_t *r_arb_vertex_buffer_object; + +cvar_t *r_mergeClusterSurfaces; + cvar_t *r_ignoreGLErrors; cvar_t *r_logFile; @@ -219,7 +223,7 @@ GL_CheckErrors ================== */ -void GL_CheckErrors( void ) { +void GL_CheckErrs( char *file, int line ) { int err; char s[64]; @@ -254,7 +258,7 @@ break; } - ri.Error( ERR_FATAL, "GL_CheckErrors: %s", s ); + ri.Error( ERR_FATAL, "GL_CheckErrors: %s in %s at line %d", s , file, line); } @@ -888,6 +892,7 @@ 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); r_ext_texture_filter_anisotropic = ri.Cvar_Get( "r_ext_texture_filter_anisotropic", "0", CVAR_ARCHIVE | CVAR_LATCH ); @@ -962,6 +967,7 @@ r_directedScale = ri.Cvar_Get( "r_directedScale", "1", CVAR_CHEAT ); r_anaglyphMode = ri.Cvar_Get("r_anaglyphMode", "0", CVAR_ARCHIVE); + r_mergeClusterSurfaces = ri.Cvar_Get("r_mergeClusterSurfaces", "1", CVAR_ARCHIVE); // // temporary variables that can change at any time Index: code/renderer/tr_local.h =================================================================== --- code/renderer/tr_local.h (revision 1802) +++ code/renderer/tr_local.h (working copy) @@ -40,6 +40,7 @@ #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 +57,11 @@ // can't be increased without changing bit packing for drawsurfs +#define MAX_VISCOUNTS 5 +#define MAX_VBOS 65536 +#define MAX_IBOS 65536 +#define MAX_CLUSTER_VBO_SURFACES 32768 +#define MAX_MODEL_VBO_SURFACES 32768 typedef struct dlight_s { vec3_t origin; @@ -108,6 +114,37 @@ 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 stride; + uint32_t ofs_xyz; + uint32_t ofs_normal; + uint32_t ofs_st; + uint32_t ofs_lightmap; + uint32_t ofs_vertexcolor; + + 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 { @@ -379,6 +416,7 @@ fogParms_t fogParms; float portalRange; // distance to fog out at + qboolean isPortal; int multitextureEnv; // 0, GL_MODULATE, GL_ADD (FIXME: put in stage) @@ -418,6 +456,46 @@ struct shader_s *next; } shader_t; +static ID_INLINE qboolean ShaderRequiresCPUDeforms(const shader_t * shader) +{ + if(shader->numDeforms) + { +#if 1 + const deformStage_t *ds = &shader->deforms[0]; + + switch (ds->deformation) + { + case DEFORM_WAVE: + case DEFORM_BULGE: + return qfalse; + + default: + return qtrue; + } +#else + int i; + for (i = 0; i < shader->numDeforms; i++) + { + const deformStage_t *ds = &shader->deforms[i]; + + switch (ds->deformation) + { + case DEFORM_WAVE: + case DEFORM_BULGE: + break; + + default: + return qtrue; + } + } +#endif + } + + + + 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 @@ -426,7 +504,50 @@ 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, +}; +enum +{ + ATTR_POSITION = 0x001, + ATTR_TEXCOORD = 0x002, + ATTR_LIGHTCOORD = 0x004, + ATTR_TANGENT = 0x008, + ATTR_BINORMAL = 0x010, + ATTR_NORMAL = 0x020, + ATTR_COLOR = 0x040, + ATTR_PAINTCOLOR = 0x080, + ATTR_LIGHTDIRECTION = 0x100, + ATTR_BONE_INDEXES = 0x200, + ATTR_BONE_WEIGHTS = 0x400, + + 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 +}; + + // trRefdef_t holds everything that comes in refdef_t, // as well as the locally generated scene information typedef struct { @@ -519,6 +640,7 @@ ============================================================================== */ +typedef byte color4ub_t[4]; // any changes in surfaceType must be mirrored in rb_surfaceTable[] typedef enum { @@ -536,6 +658,7 @@ SF_FLARE, SF_ENTITY, // beams, rails, lightning, etc that can be determined by entity SF_DISPLAY_LIST, + SF_VBO_MESH, SF_NUM_SURFACE_TYPES, SF_MAX = 0x7fffffff // ensures that sizeof( surfaceType_t ) == sizeof( int ) @@ -574,9 +697,35 @@ 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]; @@ -597,49 +746,99 @@ 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 firstTriangle; + + // static render data + VBO_t *vbo; // points to bsp model VBO + IBO_t *ibo; } srfGridMesh_t; +typedef struct +{ + surfaceType_t surfaceType; -#define VERTEXSIZE 8 -typedef struct { - surfaceType_t surfaceType; - cplane_t plane; - // 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 firstTriangle; + + // 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 firstTriangle; + + // 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 lightmapNum; // FIXME get rid of this by merging all lightmaps at level load + + // culling information + vec3_t bounds[2]; + + // backEnd stats + int numIndexes; + int numVerts; + + // static render data + VBO_t *vbo; + IBO_t *ibo; +} srfVBOMesh_t; + + extern void (*rb_surfaceTable[SF_NUM_SURFACE_TYPES])(void *); /* @@ -663,6 +862,7 @@ int viewCount; // if == tr.viewCount, already added struct shader_s *shader; int fogIndex; + int16_t lightmapNum; // -1 = no lightmap surfaceType_t *data; // any of srf*_t } msurface_t; @@ -673,7 +873,7 @@ 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; @@ -689,10 +889,21 @@ int nummarksurfaces; } mnode_t; +typedef struct +{ + int numMarkSurfaces; + msurface_t **markSurfaces; + + vec3_t origin; // used for cubemaps +} bspCluster_t; + typedef struct { vec3_t bounds[2]; // for culling msurface_t *firstSurface; int numSurfaces; + + uint32_t numVBOSurfaces; + srfVBOMesh_t *vboSurfaces[MAX_MODEL_VBO_SURFACES]; } bmodel_t; typedef struct { @@ -704,6 +915,7 @@ int numShaders; dshader_t *shaders; + int numBModels; bmodel_t *bmodels; int numplanes; @@ -713,6 +925,19 @@ 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; + + int numTriangles; + srfTriangle_t *triangles; + + int numWorldSurfaces; + int numsurfaces; msurface_t *surfaces; @@ -730,11 +955,15 @@ int numClusters; + bspCluster_t *clusters; int clusterBytes; const byte *vis; // may be passed in by CM_LoadMap to save space byte *novis; // clusterBytes of 0xff + int numClusterVBOSurfaces[MAX_VISCOUNTS]; + srfVBOMesh_t clusterVBOSurfaces[MAX_VISCOUNTS][MAX_CLUSTER_VBO_SURFACES]; // updated every time when changing the view cluster + char *entityString; char *entityParsePoint; } world_t; @@ -839,6 +1068,10 @@ int texEnv[2]; int faceCulling; unsigned long glStateBits; + uint32_t vertexAttribsState; + uint32_t vertexAttribPointersSet; + VBO_t *currentVBO; + IBO_t *currentIBO; } glstate_t; @@ -846,6 +1079,11 @@ int c_surfaces, c_shaders, c_vertexes, c_indexes, c_totalIndexes; float c_overDraw; + int c_vboVertexBuffers; + int c_vboIndexBuffers; + int c_vboVertexes; + int c_vboIndexes; + int c_dlightVertexes; int c_dlightIndexes; @@ -885,7 +1123,10 @@ 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) @@ -952,6 +1193,12 @@ 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) @@ -1056,6 +1303,8 @@ 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_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 +1347,8 @@ extern cvar_t *r_stereoEnabled; extern cvar_t *r_anaglyphMode; +extern cvar_t *r_mergeClusterSurfaces; + extern cvar_t *r_greyscale; extern cvar_t *r_ignoreGLErrors; @@ -1140,6 +1391,8 @@ 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 @@ -1160,8 +1413,11 @@ 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_VertexAttribsState(uint32_t stateBits); +void GL_VertexAttribPointers(uint32_t attribBits); void GL_TexEnv( int env ); void GL_Cull( int cullType ); @@ -1292,7 +1548,6 @@ ==================================================================== */ -typedef byte color4ub_t[4]; typedef struct stageVars { @@ -1310,6 +1565,9 @@ color4ub_t vertexColors[SHADER_MAX_VERTEXES] ALIGN(16); int vertexDlightBits[SHADER_MAX_VERTEXES] ALIGN(16); + VBO_t *vbo; + IBO_t *ibo; + stageVars_t svars ALIGN(16); color4ub_t constantColor255[SHADER_MAX_VERTEXES] ALIGN(16); @@ -1425,7 +1683,7 @@ #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 ); @@ -1445,6 +1703,31 @@ /* ============================================================ +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); + + + +/* +============================================================ + SCENE GENERATION ============================================================ @@ -1527,6 +1810,13 @@ 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_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 ); Index: code/renderer/tr_main.c =================================================================== --- code/renderer/tr_main.c (revision 1802) +++ code/renderer/tr_main.c (working copy) @@ -45,6 +45,97 @@ /* ================= +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 Returns CULL_IN, CULL_CLIP, or CULL_OUT @@ -622,7 +713,7 @@ 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 +727,9 @@ 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]; Index: code/renderer/tr_marks.c =================================================================== --- code/renderer/tr_marks.c (revision 1802) +++ code/renderer/tr_marks.c (working copy) @@ -266,11 +266,11 @@ 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 +407,12 @@ 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 +430,12 @@ 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 Index: code/renderer/tr_model.c =================================================================== --- code/renderer/tr_model.c (revision 1802) +++ code/renderer/tr_model.c (working copy) @@ -915,7 +915,9 @@ 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(); Index: code/renderer/tr_shade.c =================================================================== --- code/renderer/tr_shade.c (revision 1802) +++ code/renderer/tr_shade.c (working copy) @@ -335,6 +335,69 @@ } +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, BUFFER_OFFSET(glState.currentVBO->ofs_st) ); + qglEnableClientState ( GL_TEXTURE_COORD_ARRAY ); + break; + + case TCGEN_LIGHTMAP: + qglTexCoordPointer( 2, GL_FLOAT, glState.currentVBO->stride, BUFFER_OFFSET(glState.currentVBO->ofs_lightmap) ); + qglEnableClientState ( GL_TEXTURE_COORD_ARRAY ); + break; + + case TCGEN_ENVIRONMENT_MAPPED: + 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; + + default: + // nothing else is supported, bail out + break; + } +} /* =================== DrawMultitextured @@ -358,6 +421,39 @@ qglPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); } + if (glState.currentVBO) + { + GL_SelectTexture( 0 ); + GenerateGLTexCoords( pStage, 0 ); + //qglTexCoordPointer(2, GL_FLOAT, glState.currentVBO->stride, BUFFER_OFFSET(glState.currentVBO->ofs_st)); + R_BindAnimatedImage( &pStage->bundle[0] ); + qglEnableClientState ( GL_TEXTURE_COORD_ARRAY ); + + GL_SelectTexture( 1 ); + qglEnable( GL_TEXTURE_2D ); + GenerateGLTexCoords( pStage, 1 ); + //qglTexCoordPointer(2, GL_FLOAT, glState.currentVBO->stride, BUFFER_OFFSET(glState.currentVBO->ofs_lightmap)); + qglEnableClientState ( GL_TEXTURE_COORD_ARRAY ); + + if ( r_lightmap->integer ) { + GL_TexEnv( GL_REPLACE ); + } else { + GL_TexEnv( tess.shader->multitextureEnv ); + } + + R_BindAnimatedImage( &pStage->bundle[1] ); + + qglDrawElements(GL_TRIANGLES, input->numIndexes, GL_INDEX_TYPE, BUFFER_OFFSET(0)); + + qglDisable( GL_TEXTURE_2D ); + + GL_SelectTexture( 0 ); + + GL_CheckErrors(); + + return; + } + // // base // @@ -742,6 +838,13 @@ fog_t *fog; int i; + if(glState.currentVBO) + { + R_BindNullVBO(); + R_BindNullIBO(); + return; + } + qglEnableClientState( GL_COLOR_ARRAY ); qglColorPointer( 4, GL_UNSIGNED_BYTE, 0, tess.svars.colors ); @@ -767,6 +870,59 @@ R_DrawElements( tess.numIndexes, tess.indexes ); } +// Some matrix helper functions +// FIXME: do these already exist in ioq3 and I don't know about them? + +static void Matrix16Zero( float out[16] ) +{ + 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; +} + +static void Matrix16Identity( float out[16] ) +{ + 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; +} + +static void Matrix16Copy( float in[16], float out[16] ) +{ + 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]; +} + +static void Matrix16Multiply( float in1[16], float in2[16], float out[16] ) +{ + 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]; +} + +extern float EvalWaveForm( const waveForm_t *wf ); +extern float EvalWaveFormClamped( const waveForm_t *wf ); + /* =============== ComputeColors @@ -975,6 +1131,333 @@ /* =============== +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 =============== */ @@ -1074,6 +1557,81 @@ } } + +static void ComputeTexMatrix( shaderStage_t *pStage, int bundleNum, float *outmatrix) +{ + int tm; + float matrix[16], currentmatrix[16]; + textureBundle_t *bundle = &pStage->bundle[bundleNum]; + + 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: + //FIXME: implement this +#if 0 + RB_CalcTurbulentTexCoords( &pStage->bundle[b].texMods[tm].wave, + ( float * ) tess.svars.texcoords[b] ); +#endif + break; + + case TMOD_ENTITY_TRANSLATE: + RB_CalcScrollTexMatrix( backEnd.currentEntity->e.shaderTexCoord, + matrix ); + Matrix16Multiply(currentmatrix, matrix, outmatrix); + Matrix16Copy(outmatrix, currentmatrix); + break; + + case TMOD_SCROLL: + RB_CalcScrollTexMatrix( bundle->texMods[tm].scroll, + matrix ); + Matrix16Multiply(currentmatrix, matrix, outmatrix); + Matrix16Copy(outmatrix, currentmatrix); + break; + + case TMOD_SCALE: + RB_CalcScaleTexMatrix( bundle->texMods[tm].scale, + matrix ); + Matrix16Multiply(currentmatrix, matrix, outmatrix); + Matrix16Copy(outmatrix, currentmatrix); + break; + + case TMOD_STRETCH: + RB_CalcStretchTexMatrix( &bundle->texMods[tm].wave, + matrix ); + Matrix16Multiply(currentmatrix, matrix, outmatrix); + Matrix16Copy(outmatrix, currentmatrix); + break; + + case TMOD_TRANSFORM: + RB_CalcTransformTexMatrix( &bundle->texMods[tm], + matrix ); + Matrix16Multiply(currentmatrix, matrix, outmatrix); + Matrix16Copy(outmatrix, currentmatrix); + break; + + case TMOD_ROTATE: + RB_CalcRotateTexMatrix( bundle->texMods[tm].rotateSpeed, + matrix ); + Matrix16Multiply(currentmatrix, matrix, 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; + } + } +} + /* ** RB_IterateStagesGeneric */ @@ -1081,20 +1639,62 @@ { int stage; + if(glState.currentVBO) + { + qglEnableClientState( GL_VERTEX_ARRAY ); + qglVertexPointer(3, GL_FLOAT, glState.currentVBO->stride, BUFFER_OFFSET(glState.currentVBO->ofs_xyz)); + qglEnableClientState( GL_NORMAL_ARRAY ); + qglNormalPointer(GL_FLOAT, glState.currentVBO->stride, BUFFER_OFFSET(glState.currentVBO->ofs_normal)); + } + for ( stage = 0; stage < MAX_SHADER_STAGES; stage++ ) { shaderStage_t *pStage = tess.xstages[stage]; + qboolean disableVertexColors = qfalse; if ( !pStage ) { break; } - ComputeColors( pStage ); - ComputeTexCoords( pStage ); + if ( glState.currentVBO ) + { + float matrix[16]; - if ( !setArraysOnce ) + ComputeColorMatrix( pStage, matrix, &disableVertexColors); + + if (!disableVertexColors) + { + qglMatrixMode(GL_COLOR); + qglLoadMatrixf(matrix); + } + else + { + qglColor4fv(&matrix[12]); + } + qglMatrixMode(GL_MODELVIEW); + + } + else { + ComputeColors( pStage ); + ComputeTexCoords( pStage ); + } + + if ( glState.currentVBO ) + { + if (!disableVertexColors) + { + qglEnableClientState( GL_COLOR_ARRAY ); + qglColorPointer(4, GL_UNSIGNED_BYTE, glState.currentVBO->stride, BUFFER_OFFSET(glState.currentVBO->ofs_vertexcolor)); + } + else + { + qglDisableClientState( GL_COLOR_ARRAY ); + } + } + else if ( !setArraysOnce ) + { qglEnableClientState( GL_COLOR_ARRAY ); qglColorPointer( 4, GL_UNSIGNED_BYTE, 0, input->svars.colors ); } @@ -1108,8 +1708,19 @@ } else { - if ( !setArraysOnce ) + if ( glState.currentVBO ) { + float matrix[16]; + + GenerateGLTexCoords( pStage, 0 ); + + ComputeTexMatrix( pStage, 0, matrix ); + qglMatrixMode(GL_TEXTURE); + qglLoadMatrixf(matrix); + qglMatrixMode(GL_MODELVIEW); + } + else if ( !setArraysOnce ) + { qglTexCoordPointer( 2, GL_FLOAT, 0, input->svars.texcoords[0] ); } @@ -1128,7 +1739,30 @@ // // draw // - R_DrawElements( input->numIndexes, input->indexes ); + + if (glState.currentVBO) + { + qglDrawElements(GL_TRIANGLES, input->numIndexes, GL_INDEX_TYPE, BUFFER_OFFSET(0)); + } + else + { + R_DrawElements( input->numIndexes, input->indexes ); + } + + if (glState.currentVBO) + { + // reset state for vertex arrays + qglEnableClientState ( GL_TEXTURE_COORD_ARRAY ); + qglEnableClientState ( GL_COLOR_ARRAY ); + qglDisable(GL_TEXTURE_GEN_S); + qglDisable(GL_TEXTURE_GEN_T); + qglDisable(GL_TEXTURE_GEN_Q); + 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 ) ) @@ -1145,8 +1779,14 @@ void RB_StageIteratorGeneric( void ) { shaderCommands_t *input; + qboolean vboactive = qfalse; input = &tess; + + if (!input->numVertexes || !input->numIndexes) + { + return; + } RB_DeformTessGeometry(); @@ -1160,6 +1800,11 @@ GLimp_LogComment( va("--- RB_StageIteratorGeneric( %s ) ---\n", tess.shader->name) ); } + if (glState.currentVBO) + { + vboactive = qtrue; + } + // // set face culling appropriately // @@ -1178,37 +1823,43 @@ // to avoid compiling those arrays since they will change // during multipass rendering // - if ( tess.numPasses > 1 || input->shader->multitextureEnv ) + if (!vboactive) { - setArraysOnce = qfalse; - qglDisableClientState (GL_COLOR_ARRAY); - qglDisableClientState (GL_TEXTURE_COORD_ARRAY); - } - else - { - setArraysOnce = qtrue; + if ( tess.numPasses > 1 || input->shader->multitextureEnv ) + { + setArraysOnce = qfalse; + qglDisableClientState (GL_COLOR_ARRAY); + qglDisableClientState (GL_TEXTURE_COORD_ARRAY); + } + else + { + setArraysOnce = qtrue; - qglEnableClientState( GL_COLOR_ARRAY); - qglColorPointer( 4, GL_UNSIGNED_BYTE, 0, tess.svars.colors ); + qglEnableClientState( GL_COLOR_ARRAY); + qglColorPointer( 4, GL_UNSIGNED_BYTE, 0, tess.svars.colors ); - qglEnableClientState( GL_TEXTURE_COORD_ARRAY); - qglTexCoordPointer( 2, GL_FLOAT, 0, tess.svars.texcoords[0] ); + qglEnableClientState( GL_TEXTURE_COORD_ARRAY); + qglTexCoordPointer( 2, GL_FLOAT, 0, tess.svars.texcoords[0] ); + } } // // lock XYZ // - qglVertexPointer (3, GL_FLOAT, 16, input->xyz); // padded for SIMD - if (qglLockArraysEXT) + if (!vboactive) { - qglLockArraysEXT(0, input->numVertexes); - GLimp_LogComment( "glLockArraysEXT\n" ); + qglVertexPointer (3, GL_FLOAT, 16, input->xyz); // padded for SIMD + if (qglLockArraysEXT) + { + qglLockArraysEXT(0, input->numVertexes); + GLimp_LogComment( "glLockArraysEXT\n" ); + } } // // enable color and texcoord arrays after the lock if necessary // - if ( !setArraysOnce ) + if ( !setArraysOnce && !vboactive ) { qglEnableClientState( GL_TEXTURE_COORD_ARRAY ); qglEnableClientState( GL_COLOR_ARRAY ); @@ -1237,7 +1888,7 @@ // // unlock arrays // - if (qglUnlockArraysEXT) + if (qglUnlockArraysEXT && !vboactive) { qglUnlockArraysEXT(); GLimp_LogComment( "glUnlockArraysEXT\n" ); @@ -1261,6 +1912,13 @@ shaderCommands_t *input; shader_t *shader; + if(glState.currentVBO) + { + R_BindNullVBO(); + R_BindNullIBO(); + return; + } + input = &tess; shader = input->shader; @@ -1337,6 +1995,13 @@ void RB_StageIteratorLightmappedMultitexture( void ) { shaderCommands_t *input; + if(glState.currentVBO) + { + R_BindNullVBO(); + R_BindNullIBO(); + return; + } + input = &tess; // @@ -1444,7 +2109,7 @@ input = &tess; - if (input->numIndexes == 0) { + if (input->numIndexes == 0 || input->numVertexes == 0) { return; } @@ -1478,6 +2143,12 @@ // tess.currentStageIteratorFunc(); + if(glState.currentVBO) + { + R_BindNullVBO(); + R_BindNullIBO(); + } + // // draw debugging stuff // @@ -1489,7 +2160,9 @@ } // clear shader so we can tell we don't have any unclosed surfaces tess.numIndexes = 0; + tess.numVertexes = 0; GLimp_LogComment( "----------\n" ); } + Index: code/renderer/tr_shade_calc.c =================================================================== --- code/renderer/tr_shade_calc.c (revision 1802) +++ code/renderer/tr_shade_calc.c (working copy) @@ -57,7 +57,7 @@ ** ** 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 @@ 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 @@ 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 ); +} + /* ==================================================================== @@ -948,6 +966,14 @@ } } +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 +998,26 @@ } } +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] = 0.0f; matrix[12] = adjustedScrollS; + matrix[ 1] = 0.0f; matrix[ 5] = 1.0f; matrix[ 9] = 0.0f; matrix[13] = adjustedScrollT; + 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 +1035,14 @@ } } +void RB_CalcTransformTexMatrix( const texModInfo_t *tmi, float *matrix ) +{ + matrix[ 0] = tmi->matrix[0][0]; matrix[ 4] = tmi->matrix[1][0]; matrix[ 8] = 0.0f; matrix[12] = tmi->translate[0]; + matrix[ 1] = tmi->matrix[0][1]; matrix[ 5] = tmi->matrix[1][1]; matrix[ 9] = 0.0f; matrix[13] = tmi->translate[1]; + 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,11 +1071,36 @@ 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 ); +} + + + + + + #if id386 && !defined(__GNUC__) long myftol( float f ) { Index: code/renderer/tr_shader.c =================================================================== --- code/renderer/tr_shader.c (revision 1802) +++ code/renderer/tr_shader.c (working copy) @@ -1552,6 +1552,7 @@ else if ( !Q_stricmp(token, "portal") ) { shader.sort = SS_PORTAL; + shader.isPortal = qtrue; continue; } // skyparms Index: code/renderer/tr_surface.c =================================================================== --- code/renderer/tr_surface.c (revision 1802) +++ code/renderer/tr_surface.c (working copy) @@ -217,40 +217,33 @@ 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; + srfVert_t *dv; + float *xyz, *normal, *texCoords; byte *color; - int dlightBits; qboolean needsNormal; - dlightBits = srf->dlightBits[backEnd.smpFrame]; tess.dlightBits |= dlightBits; - RB_CHECKOVERFLOW( srf->numVerts, srf->numIndexes ); + RB_CHECKOVERFLOW( numVerts, numTriangles * 3 ); - 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 ]; + for ( i = 0 ; i < numTriangles ; i += 1 ) { + tess.indexes[ tess.numIndexes + i * 3 + 0 ] = tess.numVertexes + triangles[ i ].indexes[0]; + tess.indexes[ tess.numIndexes + i * 3 + 1 ] = tess.numVertexes + triangles[ i ].indexes[1]; + tess.indexes[ tess.numIndexes + i * 3 + 2 ] = tess.numVertexes + triangles[ i ].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; - 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,18 +260,42 @@ 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++ ) { + for ( i = 0 ; i < numVerts ; i++ ) { tess.vertexDlightBits[ tess.numVertexes + i] = dlightBits; } - tess.numVertexes += srf->numVerts; + tess.numVertexes += numVerts; } +/* +============= +RB_SurfaceTriangles +============= +*/ +static void RB_SurfaceTriangles( srfTriangles_t *srf ) { + if( r_arb_vertex_buffer_object->integer && srf->vbo && srf->ibo && !ShaderRequiresCPUDeforms(tess.shader) + && !tess.shader->isSky && !tess.shader->isPortal) + { + RB_EndSurface(); + RB_BeginSurface(tess.shader, tess.fogNum); + R_BindVBO(srf->vbo); + R_BindIBO(srf->ibo); + tess.numIndexes += srf->numTriangles * 3; + tess.numVertexes += srf->numVerts; + + RB_EndSurface(); + return; + } + RB_SurfaceHelper(srf->numVerts, srf->verts, srf->numTriangles, srf->triangles, srf->dlightBits[backEnd.smpFrame]); +} + + + /* ============== RB_SurfaceBeam @@ -903,56 +920,23 @@ 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; +static void RB_SurfaceFace( srfSurfaceFace_t *srf ) { + if( r_arb_vertex_buffer_object->integer && srf->vbo && srf->ibo && !ShaderRequiresCPUDeforms(tess.shader) + && !tess.shader->isSky && !tess.shader->isPortal) + { + RB_EndSurface(); + RB_BeginSurface(tess.shader, tess.fogNum); - RB_CHECKOVERFLOW( surf->numPoints, surf->numIndices ); + R_BindVBO(srf->vbo); + R_BindIBO(srf->ibo); - dlightBits = surf->dlightBits[backEnd.smpFrame]; - tess.dlightBits |= dlightBits; + tess.numIndexes += srf->numTriangles * 3; + tess.numVertexes += srf->numVerts; - 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; + RB_EndSurface(); + 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]); } @@ -993,7 +977,26 @@ Just copy the grid of points and triangulate ============= */ -static void RB_SurfaceGrid( srfGridMesh_t *cv ) { +static void RB_SurfaceGrid( srfGridMesh_t *srf ) { +#if 1 + if( r_arb_vertex_buffer_object->integer && srf->vbo && srf->ibo && !ShaderRequiresCPUDeforms(tess.shader) + && !tess.shader->isSky && !tess.shader->isPortal) + { + RB_EndSurface(); + RB_BeginSurface(tess.shader, tess.fogNum); + + R_BindVBO(srf->vbo); + R_BindIBO(srf->ibo); + + tess.numIndexes += srf->numTriangles * 3; + tess.numVertexes += srf->numVerts; + + RB_EndSurface(); + return; + } + RB_SurfaceHelper(srf->numVerts, srf->verts, srf->numTriangles, srf->triangles, srf->dlightBits[backEnd.smpFrame]); +#else + // FIXME: We should be using a lot of this logic instead of dumping the whole thing int i, j; float *xyz; float *texCoords; @@ -1142,6 +1145,7 @@ used += rows - 1; } +#endif } @@ -1220,6 +1224,24 @@ RB_AddFlare(surf, tess.fogNum, surf->origin, surf->color, surf->normal); } +static void RB_SurfaceVBOMesh(srfVBOMesh_t * srf) +{ + if(!srf->vbo || !srf->ibo) + return; + + RB_EndSurface(); + RB_BeginSurface(tess.shader, tess.fogNum); + + R_BindVBO(srf->vbo); + R_BindIBO(srf->ibo); + + tess.numIndexes += srf->numIndexes; + tess.numVertexes += srf->numVerts; + + RB_EndSurface(); +} + + static void RB_SurfaceDisplayList( srfDisplayList_t *surf ) { // all apropriate state must be set in RB_BeginSurface // this isn't implemented yet... @@ -1244,5 +1266,6 @@ #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, }; Index: code/renderer/tr_world.c =================================================================== --- code/renderer/tr_world.c (revision 1802) +++ code/renderer/tr_world.c (working copy) @@ -131,6 +131,17 @@ return qfalse; } + // now it must be a SF_FACE + sface = (srfSurfaceFace_t *) surface; + + if(shader->isPortal) + { + if(R_CullLocalBox(sface->bounds) == CULL_OUT) + { + return qtrue; + } + } + if ( shader->cullType == CT_TWO_SIDED ) { return qfalse; } @@ -281,12 +292,28 @@ R_AddWorldSurface ====================== */ -static void R_AddWorldSurface( msurface_t *surf, int dlightBits ) { +static void R_AddWorldSurface( msurface_t *surf, int dlightBits, qboolean skipClusterCheck ) { + shader_t *shader; + if ( surf->viewCount == tr.viewCount ) { return; // already in this view } surf->viewCount = tr.viewCount; + + shader = surf->shader; + + + if( r_arb_vertex_buffer_object->integer && r_mergeClusterSurfaces->integer && + /*!r_dynamicBspOcclusionCulling->integer && */ + ((/*r_mergeClusterFaces->integer && */ *surf->data == SF_FACE) || + (/* r_mergeClusterCurves->integer && */ *surf->data == SF_GRID) || + (/* r_mergeClusterTriangles->integer && */ *surf->data == SF_TRIANGLES)) && + !shader->isSky && !shader->isPortal && !ShaderRequiresCPUDeforms(shader) && + !skipClusterCheck) + return; + + // FIXME: bmodel fog? // try to cull before dlighting or adding @@ -334,7 +361,7 @@ R_DlightBmodel( bmodel ); for ( i = 0 ; i < bmodel->numSurfaces ; i++ ) { - R_AddWorldSurface( bmodel->firstSurface + i, tr.currentEntity->needDlights ); + R_AddWorldSurface( bmodel->firstSurface + i, tr.currentEntity->needDlights, qtrue ); } } @@ -359,7 +386,7 @@ 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; } @@ -485,7 +512,7 @@ // the surface may have already been added if it // spans multiple leafs surf = *mark; - R_AddWorldSurface( surf, dlightBits ); + R_AddWorldSurface( surf, dlightBits, qfalse); mark++; } } @@ -557,7 +584,391 @@ } /* +================= +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 < bb->shader) + return -1; + + else if(aa->shader > bb->shader) + return 1; + + // by lightmap + if(aa->lightmapNum < bb->lightmapNum) + return -1; + + else if(aa->lightmapNum > bb->lightmapNum) + return 1; + + return 0; +} + +/* =============== +R_UpdateClusterSurfaces() +=============== +*/ +static void R_UpdateClusterSurfaces() +{ + int i, k, l; + + int numVerts; + int numTriangles; + +// static glIndex_t indexes[MAX_MAP_DRAW_INDEXES]; +// static byte indexes[MAX_MAP_DRAW_INDEXES * sizeof(glIndex_t)]; + glIndex_t *indexes; + int indexesSize; + + shader_t *shader, *oldShader; + int lightmapNum, oldLightmapNum; + + int numSurfaces; + int currentNumClusterVBOSurfaces; + msurface_t *surface, *surface2; + msurface_t **surfacesSorted; + + bspCluster_t *cluster; + + srfVBOMesh_t *vboSurf; + IBO_t *ibo; + + vec3_t bounds[2]; + + //int startTime, currentTime; + //int triangleTime = 0, allocTime = 0; + //int totaltris = 0; + + if(tr.visClusters[tr.visIndex] < 0 || tr.visClusters[tr.visIndex] >= tr.world->numClusters) + { + // Tr3B: this is not a bug, the super cluster is the last one in the array + cluster = &tr.world->clusters[tr.world->numClusters]; + } + else + { + cluster = &tr.world->clusters[tr.visClusters[tr.visIndex]]; + } + + //startTime = ri.Milliseconds(); + + currentNumClusterVBOSurfaces = 0; + + // count number of static cluster surfaces + numSurfaces = 0; + for(k = 0; k < cluster->numMarkSurfaces; k++) + { + surface = cluster->markSurfaces[k]; + shader = surface->shader; + + if(shader->isSky) + continue; + + if(shader->isPortal) + continue; + + if(ShaderRequiresCPUDeforms(shader)) + continue; + + numSurfaces++; + } + + if(!numSurfaces) + return; + + // build interaction caches list + // previously used Hunk_AllocateTempMemory, but that memory isn't freed until all files are closed + surfacesSorted = ri.Malloc(numSurfaces * sizeof(surfacesSorted[0])); + + numSurfaces = 0; + for(k = 0; k < cluster->numMarkSurfaces; k++) + { + surface = cluster->markSurfaces[k]; + shader = surface->shader; + + if(shader->isSky) + continue; + + if(shader->isPortal) + continue; + + if(ShaderRequiresCPUDeforms(shader)) + continue; + + surfacesSorted[numSurfaces] = surface; + numSurfaces++; + } + + // sort surfaces by shader + qsort(surfacesSorted, numSurfaces, sizeof(surfacesSorted), BSPSurfaceCompare); + + //currentTime = ri.Milliseconds(); + //ri.Printf(PRINT_ALL, "built & sorted cache: %d\n", currentTime - startTime); + + shader = oldShader = NULL; + lightmapNum = oldLightmapNum = -1; + + for(k = 0; k < numSurfaces; k++) + { + surface = surfacesSorted[k]; + shader = surface->shader; + lightmapNum = surface->lightmapNum; + + if(shader != oldShader || lightmapNum != oldLightmapNum) + { + //currentTime = ri.Milliseconds(); + oldShader = shader; + oldLightmapNum = lightmapNum; + + // count vertices and indices + numVerts = 0; + numTriangles = 0; + + for(l = k; l < numSurfaces; l++) + { + surface2 = surfacesSorted[l]; + + if(surface2->shader != shader || surface2->lightmapNum != lightmapNum) + break; // was continue, but continue made no sense + + if(*surface2->data == SF_FACE) + { + srfSurfaceFace_t *face = (srfSurfaceFace_t *) surface2->data; + + //if(!r_mergeClusterFaces->integer) + //continue; + + if(face->numVerts) + numVerts += face->numVerts; + + if(face->numTriangles) + numTriangles += face->numTriangles; + } + else if(*surface2->data == SF_GRID) + { + srfGridMesh_t *grid = (srfGridMesh_t *) surface2->data; + + //if(!r_mergeClusterCurves->integer) + //continue; + + if(grid->numVerts) + numVerts += grid->numVerts; + + if(grid->numTriangles) + numTriangles += grid->numTriangles; + } + else if(*surface2->data == SF_TRIANGLES) + { + srfTriangles_t *tri = (srfTriangles_t *) surface2->data; + + //if(!r_mergeClusterTriangles->integer) + //continue; + + if(tri->numVerts) + numVerts += tri->numVerts; + + if(tri->numTriangles) + numTriangles += tri->numTriangles; + } + } + + //totaltris += numTriangles; + + if(!numVerts || !numTriangles) + continue; + + ClearBounds(bounds[0], bounds[1]); + + // build triangle indices + indexesSize = numTriangles * 3 * sizeof(glIndex_t); + // previously used Hunk_AllocateTempMemory, but that memory isn't freed until all files are closed + indexes = ri.Malloc(indexesSize); + + numTriangles = 0; + for(l = k; l < numSurfaces; l++) + { + surface2 = surfacesSorted[l]; + + if(surface2->shader != shader || surface2->lightmapNum != lightmapNum) + break; // was continue, but continue made no sense + + // set up triangle indices + if(*surface2->data == SF_FACE) + { + srfSurfaceFace_t *srf = (srfSurfaceFace_t *) surface2->data; + + //if(!r_mergeClusterFaces->integer) + //continue; + + if(srf->numTriangles) + { + srfTriangle_t *tri; + + for(i = 0, tri = tr.world->triangles + srf->firstTriangle; i < srf->numTriangles; i++, tri++) + { + indexes[numTriangles * 3 + i * 3 + 0] = tri->indexes[0]; + indexes[numTriangles * 3 + i * 3 + 1] = tri->indexes[1]; + indexes[numTriangles * 3 + i * 3 + 2] = tri->indexes[2]; + } + + numTriangles += srf->numTriangles; + //BoundsAdd(bounds[0], bounds[1], srf->bounds[0], srf->bounds[1]); + AddPointToBounds(srf->bounds[0], bounds[0], bounds[1]); + AddPointToBounds(srf->bounds[1], bounds[0], bounds[1]); + } + } + else if(*surface2->data == SF_GRID) + { + srfGridMesh_t *srf = (srfGridMesh_t *) surface2->data; + + //if(!r_mergeClusterCurves->integer) + //continue; + + if(srf->numTriangles) + { + srfTriangle_t *tri; + + for(i = 0, tri = tr.world->triangles + srf->firstTriangle; i < srf->numTriangles; i++, tri++) + { + indexes[numTriangles * 3 + i * 3 + 0] = tri->indexes[0]; + indexes[numTriangles * 3 + i * 3 + 1] = tri->indexes[1]; + indexes[numTriangles * 3 + i * 3 + 2] = tri->indexes[2]; + } + + numTriangles += srf->numTriangles; + //BoundsAdd(bounds[0], bounds[1], srf->meshBounds[0], srf->meshBounds[1]); + AddPointToBounds(srf->meshBounds[0], bounds[0], bounds[1]); + AddPointToBounds(srf->meshBounds[1], bounds[0], bounds[1]); + } + } + else if(*surface2->data == SF_TRIANGLES) + { + srfTriangles_t *srf = (srfTriangles_t *) surface2->data; + + //if(!r_mergeClusterTriangles->integer) + //continue; + + if(srf->numTriangles) + { + srfTriangle_t *tri; + + for(i = 0, tri = tr.world->triangles + srf->firstTriangle; i < srf->numTriangles; i++, tri++) + { + indexes[numTriangles * 3 + i * 3 + 0] = tri->indexes[0]; + indexes[numTriangles * 3 + i * 3 + 1] = tri->indexes[1]; + indexes[numTriangles * 3 + i * 3 + 2] = tri->indexes[2]; + } + + numTriangles += srf->numTriangles; + //BoundsAdd(bounds[0], bounds[1], srf->bounds[0], srf->bounds[1]); + AddPointToBounds(srf->bounds[0], bounds[0], bounds[1]); + AddPointToBounds(srf->bounds[1], bounds[0], bounds[1]); + } + } + } + + //triangleTime += ri.Milliseconds() - currentTime; + //currentTime = ri.Milliseconds(); + + if(currentNumClusterVBOSurfaces < tr.world->numClusterVBOSurfaces[tr.visIndex]) + { + vboSurf = &tr.world->clusterVBOSurfaces[tr.visIndex][currentNumClusterVBOSurfaces]; + + ibo = vboSurf->ibo; + + /* + if(ibo->indexesVBO) + { + qglDeleteBuffersARB(1, &ibo->indexesVBO); + ibo->indexesVBO = 0; + } + */ + + //Com_Dealloc(ibo); + //Com_Dealloc(vboSurf); + } + else + { + if (tr.world->numClusterVBOSurfaces[tr.visIndex] == MAX_CLUSTER_VBO_SURFACES) + { + ri.Error( ERR_DROP, "R_UpdateClusterSurfaces: MAX_CLUSTER_VBO_SURFACES hit\n"); + } + vboSurf = &tr.world->clusterVBOSurfaces[tr.visIndex][tr.world->numClusterVBOSurfaces[tr.visIndex]]; + tr.world->numClusterVBOSurfaces[tr.visIndex]++; + + vboSurf->surfaceType = SF_VBO_MESH; + + vboSurf->vbo = tr.world->vbo; + vboSurf->ibo = ibo = ri.Hunk_Alloc(sizeof(*ibo), h_low); + + qglGenBuffersARB(1, &ibo->indexesVBO); + } + + //ri.Printf(PRINT_ALL, "creating VBO cluster surface for shader '%s'\n", shader->name); + + // update surface properties + vboSurf->numIndexes = numTriangles * 3; + vboSurf->numVerts = numVerts; + + vboSurf->shader = shader; + vboSurf->lightmapNum = lightmapNum; + + VectorCopy(bounds[0], vboSurf->bounds[0]); + VectorCopy(bounds[1], vboSurf->bounds[1]); + + // make sure the render thread is stopped + R_SyncRenderThread(); + + // update IBO + Q_strncpyz(ibo->name, + va("staticWorldMesh_IBO_visIndex%i_surface%i", 0, tr.world->numClusterVBOSurfaces[tr.visIndex] - 1), + sizeof(ibo->name)); + ibo->indexesSize = indexesSize; + + R_BindIBO(ibo); + + qglBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, indexesSize, indexes, GL_DYNAMIC_DRAW_ARB); + + R_BindNullIBO(); + + //GL_CheckErrors(); + + // previously used Hunk_FreeTempMemory, but that memory isn't freed until all files are closed + ri.Free(indexes); + + currentNumClusterVBOSurfaces++; + //allocTime += ri.Milliseconds() - currentTime; + } + } + + //currentTime = ri.Milliseconds(); + //ri.Printf(PRINT_ALL, "tri time: %d, total tris: %d, total time: %d\n", triangleTime, totaltris, currentTime - startTime); + + // previously used Hunk_FreeTempMemory, but that memory isn't freed until all files are closed + ri.Free(surfacesSorted); + + if(r_showcluster->modified || r_showcluster->integer) + { + r_showcluster->modified = qfalse; + if(r_showcluster->integer) + { + ri.Printf(PRINT_ALL, " surfaces:%i\n", tr.world->numClusterVBOSurfaces[tr.visIndex]); + } + } + +} + +/* +=============== R_MarkLeaves Mark the leaves and nodes that are in the PVS for the current @@ -583,12 +994,30 @@ // 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; } + 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 +1025,21 @@ } } - tr.visCount++; - tr.viewCluster = cluster; + if (r_mergeClusterSurfaces->integer && r_arb_vertex_buffer_object->integer) + { + R_UpdateClusterSurfaces(); + } - if ( r_novis->integer || tr.viewCluster == -1 ) { + if (r_novis->integer || 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 +1059,9 @@ 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); } @@ -664,5 +1095,40 @@ if ( tr.refdef.num_dlights > 32 ) { tr.refdef.num_dlights = 32 ; } + R_RecursiveWorldNode( tr.world->nodes, 15, ( 1 << tr.refdef.num_dlights ) - 1 ); + + //if(r_mergeClusterSurfaces->integer && !r_dynamicBspOcclusionCulling->integer) + if (r_mergeClusterSurfaces->integer && r_arb_vertex_buffer_object->integer) + { + int j, i; + srfVBOMesh_t *srf; + cplane_t *frust; + int r; + + for(j = 0; j < tr.world->numClusterVBOSurfaces[tr.visIndex]; j++) + { + srf = &tr.world->clusterVBOSurfaces[tr.visIndex][j]; + + for(i = 0; i < 4 /* FRUSTUM_PLANES */; i++) + { + frust = &tr.viewParms.frustum[i]; + + r = BoxOnPlaneSide(srf->bounds[0], srf->bounds[1], frust); + + if(r == 2) + { + // completely outside frustum + continue; + } + } + + // try to cull before dlighting or adding + if ( R_CullSurface( (void *)srf, srf->shader ) ) { + continue; + } + + R_AddDrawSurf( (void *)srf, srf->shader, 0 /*fogIndex*/ , 0 /*dlightBits*/ ); + } + } } Index: code/sdl/sdl_glimp.c =================================================================== --- code/sdl/sdl_glimp.c (revision 1802) +++ code/sdl/sdl_glimp.c (working copy) @@ -83,6 +83,19 @@ void (APIENTRYP qglLockArraysEXT) (GLint first, GLsizei count); void (APIENTRYP qglUnlockArraysEXT) (void); +// 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); + /* =============== GLimp_Shutdown @@ -106,6 +119,7 @@ */ void GLimp_LogComment( char *comment ) { + //ri.Printf(PRINT_ALL, comment); } /* @@ -654,6 +668,34 @@ { ri.Printf( PRINT_ALL, "...GL_EXT_texture_filter_anisotropic not found\n" ); } + + // GL_ARB_vertex_buffer_object + qglBindBufferARB = NULL; + qglDeleteBuffersARB = NULL; + qglGenBuffersARB = NULL; + qglIsBufferARB = NULL; + qglBufferDataARB = NULL; + qglBufferSubDataARB = NULL; + qglGetBufferSubDataARB = NULL; + qglGetBufferParameterivARB = NULL; + qglGetBufferPointervARB = NULL; + if(Q_stristr(glConfig.extensions_string, "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"); + } + else + { + ri.Error(ERR_FATAL, "...GL_ARB_vertex_buffer_object not found\n"); + } } #define R_MODE_FALLBACK 3 // 640 * 480 Index: misc/msvc/quake3.vcproj =================================================================== --- misc/msvc/quake3.vcproj (revision 1802) +++ misc/msvc/quake3.vcproj (working copy) @@ -3017,6 +3017,10 @@ > + +