From c434819cf7768231f60ad231b529cf21243c4ef9 Mon Sep 17 00:00:00 2001 From: havoc Date: Wed, 2 Mar 2005 11:03:46 +0000 Subject: [PATCH] improved/modified q1bsp surfmesh stuff for more general use (to make q3bsp able to use it) git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@5020 d7cf8633-e32d-0410-b094-e92efae38249 --- model_brush.c | 41 +++++++++++------------------------------ model_brush.h | 18 ------------------ model_shared.c | 39 ++++++++++++++++++++++++++++++++++++++- model_shared.h | 36 ++++++++++++++++++++++++++++++++---- 4 files changed, 81 insertions(+), 53 deletions(-) diff --git a/model_brush.c b/model_brush.c index 3eab23d0..1c6943da 100644 --- a/model_brush.c +++ b/model_brush.c @@ -1742,25 +1742,6 @@ static void Mod_Q1BSP_GenerateWarpMesh(msurface_t *surf) } #endif -static surfmesh_t *Mod_Q1BSP_AllocSurfMesh(int numverts, int numtriangles) -{ - surfmesh_t *mesh; - mesh = Mem_Alloc(loadmodel->mempool, sizeof(surfmesh_t) + numtriangles * sizeof(int[6]) + numverts * (3 + 2 + 2 + 2 + 3 + 3 + 3 + 1) * sizeof(float)); - mesh->num_vertices = numverts; - mesh->num_triangles = numtriangles; - mesh->data_vertex3f = (float *)(mesh + 1); - mesh->data_texcoordtexture2f = mesh->data_vertex3f + mesh->num_vertices * 3; - mesh->data_texcoordlightmap2f = mesh->data_texcoordtexture2f + mesh->num_vertices * 2; - mesh->data_texcoorddetail2f = mesh->data_texcoordlightmap2f + mesh->num_vertices * 2; - mesh->data_svector3f = (float *)(mesh->data_texcoorddetail2f + mesh->num_vertices * 2); - mesh->data_tvector3f = mesh->data_svector3f + mesh->num_vertices * 3; - mesh->data_normal3f = mesh->data_tvector3f + mesh->num_vertices * 3; - mesh->data_lightmapoffsets = (int *)(mesh->data_normal3f + mesh->num_vertices * 3); - mesh->data_element3i = mesh->data_lightmapoffsets + mesh->num_vertices; - mesh->data_neighbor3i = mesh->data_element3i + mesh->num_triangles * 3; - return mesh; -} - static void Mod_Q1BSP_GenerateSurfacePolygon(msurface_t *surf, int firstedge, int numedges) { int i, lindex, j; @@ -1895,23 +1876,23 @@ static void Mod_Q1BSP_LoadFaces(lump_t *l) } } - loadmodel->brushq1.entiremesh = Mod_Q1BSP_AllocSurfMesh(totalverts, totaltris); + loadmodel->entiremesh = Mod_AllocSurfMesh(loadmodel->mempool, totalverts, totaltris, 0, 0, true, true, false); for (surfnum = 0, surf = loadmodel->brushq1.surfaces, totalverts = 0, totaltris = 0, totalmeshes = 0;surfnum < count;surfnum++, totalverts += surf->poly_numverts, totaltris += surf->poly_numverts - 2, totalmeshes++, surf++) { mesh = &surf->mesh; mesh->num_vertices = surf->poly_numverts; mesh->num_triangles = surf->poly_numverts - 2; - mesh->data_vertex3f = loadmodel->brushq1.entiremesh->data_vertex3f + totalverts * 3; - mesh->data_texcoordtexture2f = loadmodel->brushq1.entiremesh->data_texcoordtexture2f + totalverts * 2; - mesh->data_texcoordlightmap2f = loadmodel->brushq1.entiremesh->data_texcoordlightmap2f + totalverts * 2; - mesh->data_texcoorddetail2f = loadmodel->brushq1.entiremesh->data_texcoorddetail2f + totalverts * 2; - mesh->data_svector3f = loadmodel->brushq1.entiremesh->data_svector3f + totalverts * 3; - mesh->data_tvector3f = loadmodel->brushq1.entiremesh->data_tvector3f + totalverts * 3; - mesh->data_normal3f = loadmodel->brushq1.entiremesh->data_normal3f + totalverts * 3; - mesh->data_lightmapoffsets = loadmodel->brushq1.entiremesh->data_lightmapoffsets + totalverts; - mesh->data_element3i = loadmodel->brushq1.entiremesh->data_element3i + totaltris * 3; - mesh->data_neighbor3i = loadmodel->brushq1.entiremesh->data_neighbor3i + totaltris * 3; + mesh->data_vertex3f = loadmodel->entiremesh->data_vertex3f + totalverts * 3; + mesh->data_texcoordtexture2f = loadmodel->entiremesh->data_texcoordtexture2f + totalverts * 2; + mesh->data_texcoordlightmap2f = loadmodel->entiremesh->data_texcoordlightmap2f + totalverts * 2; + mesh->data_texcoorddetail2f = loadmodel->entiremesh->data_texcoorddetail2f + totalverts * 2; + mesh->data_svector3f = loadmodel->entiremesh->data_svector3f + totalverts * 3; + mesh->data_tvector3f = loadmodel->entiremesh->data_tvector3f + totalverts * 3; + mesh->data_normal3f = loadmodel->entiremesh->data_normal3f + totalverts * 3; + mesh->data_lightmapoffsets = loadmodel->entiremesh->data_lightmapoffsets + totalverts; + mesh->data_element3i = loadmodel->entiremesh->data_element3i + totaltris * 3; + mesh->data_neighbor3i = loadmodel->entiremesh->data_neighbor3i + totaltris * 3; surf->lightmaptexturestride = 0; surf->lightmaptexture = NULL; diff --git a/model_brush.h b/model_brush.h index f04ab606..ddc4082a 100644 --- a/model_brush.h +++ b/model_brush.h @@ -124,24 +124,6 @@ typedef struct } mtexinfo_t; -// LordHavoc: replaces glpoly, triangle mesh -typedef struct surfmesh_s -{ - int num_vertices; // number of vertices in the mesh - int num_triangles; // number of triangles in the mesh - float *data_vertex3f; // float[verts*3] vertex locations - float *data_svector3f; // float[verts*3] direction of 'S' (right) texture axis for each vertex - float *data_tvector3f; // float[verts*3] direction of 'T' (down) texture axis for each vertex - float *data_normal3f; // float[verts*3] direction of 'R' (out) texture axis for each vertex - int *data_lightmapoffsets; // index into surface's lightmap samples for vertex lighting - float *data_texcoordtexture2f; // float[verts*2] texcoords for surface texture - float *data_texcoordlightmap2f; // float[verts*2] texcoords for lightmap texture - float *data_texcoorddetail2f; // float[verts*2] texcoords for detail texture - int *data_element3i; // int[tris*3] triangles of the mesh, 3 indices into vertex arrays for each - int *data_neighbor3i; // int[tris*3] neighboring triangle on each edge (-1 if none) -} -surfmesh_t; - typedef struct msurface_s { // bounding box for onscreen checks diff --git a/model_shared.c b/model_shared.c index 6c31882d..913d3bb5 100644 --- a/model_shared.c +++ b/model_shared.c @@ -210,7 +210,7 @@ static void mod_newmap(void) { ssize = (surf->extents[0] >> 4) + 1; tsize = (surf->extents[1] >> 4) + 1; - + if (ssize > 256 || tsize > 256) Host_Error("Bad surface extents"); @@ -785,6 +785,43 @@ void Mod_BuildTextureVectorsAndNormals(int numverts, int numtriangles, const flo VectorNormalize(v); } +surfmesh_t *Mod_AllocSurfMesh(mempool_t *mempool, int numvertices, int numtriangles, int numcollisionvertices, int numcollisiontriangles, qboolean detailtexcoords, qboolean lightmapoffsets, qboolean vertexcolors) +{ + surfmesh_t *mesh; + qbyte *data; + mesh = Mem_Alloc(mempool, sizeof(surfmesh_t) + numvertices * (3 + 3 + 3 + 3 + 1 + 2 + 2 + (detailtexcoords ? 2 : 0) + (vertexcolors ? 4 : 0)) * sizeof(float) + numtriangles * sizeof(int) * (3 + 3 + (lightmapoffsets ? 1 : 0)) + numcollisionvertices * sizeof(float[3]) + numcollisiontriangles * sizeof(int[3])); + mesh->num_vertices = numvertices; + mesh->num_triangles = numtriangles; + mesh->num_collisionvertices = numcollisionvertices; + mesh->num_collisiontriangles = numcollisiontriangles; + data = (qbyte *)(mesh + 1); + if (mesh->num_vertices) + { + mesh->data_vertex3f = (float *)data, data += sizeof(float[3]) * mesh->num_vertices; + mesh->data_svector3f = (float *)data, data += sizeof(float[3]) * mesh->num_vertices; + mesh->data_tvector3f = (float *)data, data += sizeof(float[3]) * mesh->num_vertices; + mesh->data_normal3f = (float *)data, data += sizeof(float[3]) * mesh->num_vertices; + mesh->data_texcoordtexture2f = (float *)data, data += sizeof(float[2]) * mesh->num_vertices; + mesh->data_texcoordlightmap2f = (float *)data, data += sizeof(float[2]) * mesh->num_vertices; + if (detailtexcoords) + mesh->data_texcoorddetail2f = (float *)data, data += sizeof(float[2]) * mesh->num_vertices; + if (vertexcolors) + mesh->data_lightmapcolor4f = (float *)data, data += sizeof(float[4]) * mesh->num_vertices; + if (lightmapoffsets) + mesh->data_lightmapoffsets = (int *)data, data += sizeof(int) * mesh->num_vertices; + } + if (mesh->num_triangles) + { + mesh->data_element3i = (int *)data, data += sizeof(int[3]) * mesh->num_vertices; + mesh->data_neighbor3i = (int *)data, data += sizeof(int[3]) * mesh->num_vertices; + } + if (mesh->num_collisionvertices) + mesh->data_collisionvertex3f = (float *)data, data += sizeof(float[3]) * mesh->num_collisionvertices; + if (mesh->num_collisiontriangles) + mesh->data_collisionelement3i = (int *)data, data += sizeof(int[3]) * mesh->num_collisiontriangles; + return mesh; +} + shadowmesh_t *Mod_ShadowMesh_Alloc(mempool_t *mempool, int maxverts, int maxtriangles, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, int light, int neighbors, int expandable) { shadowmesh_t *newmesh; diff --git a/model_shared.h b/model_shared.h index f38f2a03..ae1bb4f9 100644 --- a/model_shared.h +++ b/model_shared.h @@ -73,6 +73,30 @@ typedef struct overridetagnameset_s } overridetagnameset_t; +// LordHavoc: replaces glpoly, triangle mesh +typedef struct surfmesh_s +{ + int num_vertices; // number of vertices in the mesh + int num_triangles; // number of triangles in the mesh + float *data_vertex3f; // float[verts*3] vertex locations + float *data_svector3f; // float[verts*3] direction of 'S' (right) texture axis for each vertex + float *data_tvector3f; // float[verts*3] direction of 'T' (down) texture axis for each vertex + float *data_normal3f; // float[verts*3] direction of 'R' (out) texture axis for each vertex + int *data_lightmapoffsets; // index into surface's lightmap samples for vertex lighting + float *data_texcoordtexture2f; // float[verts*2] texcoords for surface texture + float *data_texcoordlightmap2f; // float[verts*2] texcoords for lightmap texture + float *data_texcoorddetail2f; // float[verts*2] texcoords for detail texture + float *data_lightmapcolor4f; + int *data_element3i; // int[tris*3] triangles of the mesh, 3 indices into vertex arrays for each + int *data_neighbor3i; // int[tris*3] neighboring triangle on each edge (-1 if none) + + int num_collisionvertices; + int num_collisiontriangles; + float *data_collisionvertex3f; + int *data_collisionelement3i; +} +surfmesh_t; + #define SHADOWMESHVERTEXHASH 1024 typedef struct shadowmeshvertexhash_s { @@ -165,7 +189,7 @@ typedef struct model_brush_s //pvschain = model->brush.data_pvsclusters + mycluster * model->brush.num_pvsclusterbytes; //if (pvschain[thatcluster >> 3] & (1 << (thatcluster & 7))) - // a mesh containing all shadow casting geometry for the whole model (including submodels), portions of this are referenced by each surface's num_firstshadowmeshtriangle + // a mesh containing all shadow casting geometry for the whole model (including submodels), portions of this are referenced by each surface's num_firstshadowmeshtriangle shadowmesh_t *shadowmesh; // common functions @@ -220,7 +244,6 @@ typedef struct model_brushq1_s int *surfacevisframes; int *surfacepvsframes; msurface_t *surfacepvsnext; - surfmesh_t *entiremesh; int numsurfedges; int *surfedges; @@ -445,11 +468,11 @@ typedef struct q3msurface_s int num_vertices; int num_triangles; float *data_vertex3f; - float *data_texcoordtexture2f; - float *data_texcoordlightmap2f; float *data_svector3f; float *data_tvector3f; float *data_normal3f; + float *data_texcoordtexture2f; + float *data_texcoordlightmap2f; float *data_color4f; int *data_element3i; int *data_neighbor3i; @@ -593,6 +616,9 @@ typedef struct model_s int nummodelsurfaces; // list of surface numbers in this (sub)model int *surfacelist; + // entire static model in one set of arrays + // (portions referenced by each surface) + surfmesh_t *entiremesh; // draw the model's sky polygons (only used by brush models) void(*DrawSky)(struct entity_render_s *ent); // draw the model using lightmap/dlight shading @@ -660,6 +686,8 @@ void Mod_ValidateElements(const int *elements, int numtriangles, int numverts, c void Mod_BuildNormals(int numverts, int numtriangles, const float *vertex3f, const int *elements, float *normal3f); void Mod_BuildTextureVectorsAndNormals(int numverts, int numtriangles, const float *vertex, const float *texcoord, const int *elements, float *svectors, float *tvectors, float *normals); +surfmesh_t *Mod_AllocSurfMesh(mempool_t *mempool, int numvertices, int numtriangles, int numcollisionvertices, int numcollisiontriangles, qboolean detailtexcoords, qboolean lightmapoffsets, qboolean vertexcolors); + shadowmesh_t *Mod_ShadowMesh_Alloc(mempool_t *mempool, int maxverts, int maxtriangles, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, int light, int neighbors, int expandable); shadowmesh_t *Mod_ShadowMesh_ReAlloc(mempool_t *mempool, shadowmesh_t *oldmesh, int light, int neighbors); int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, float *vertex14f); -- 2.39.2