X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=model_shared.c;h=fb93d04f07db1cdf5e0d6fdf93b9da0bf2a4371b;hb=1548f11f0b7ace29a0f755222c429a9acdc0ccef;hp=4f046300aab2bffedb9222bf16621418ec2d94b0;hpb=8055d6b002a425841cac4cd6ea97b4a2b0691404;p=xonotic%2Fdarkplaces.git diff --git a/model_shared.c b/model_shared.c index 4f046300..fb93d04f 100644 --- a/model_shared.c +++ b/model_shared.c @@ -28,7 +28,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. cvar_t r_mipskins = {CVAR_SAVE, "r_mipskins", "0", "mipmaps model skins so they render faster in the distance and do not display noise artifacts, can cause discoloration of skins if they contain undesirable border colors"}; -model_t *loadmodel; +dp_model_t *loadmodel; static mempool_t *mod_mempool; static memexpandablearray_t models; @@ -52,13 +52,13 @@ static void mod_start(void) { int i; int nummodels = Mem_ExpandableArray_IndexRange(&models); - model_t *mod; + dp_model_t *mod; // parse the Q3 shader files Mod_LoadQ3Shaders(); for (i = 0;i < nummodels;i++) - if ((mod = Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*') + if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*') if (mod->used) Mod_LoadModel(mod, true, false, mod->isworldmodel); } @@ -67,10 +67,10 @@ static void mod_shutdown(void) { int i; int nummodels = Mem_ExpandableArray_IndexRange(&models); - model_t *mod; + dp_model_t *mod; for (i = 0;i < nummodels;i++) - if ((mod = Mem_ExpandableArray_RecordAtIndex(&models, i)) && (mod->loaded || mod->mempool)) + if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && (mod->loaded || mod->mempool)) Mod_UnloadModel(mod); Mem_FreePool (&q3shaders_mem); @@ -81,12 +81,12 @@ static void mod_newmap(void) msurface_t *surface; int i, j, k, surfacenum, ssize, tsize; int nummodels = Mem_ExpandableArray_IndexRange(&models); - model_t *mod; + dp_model_t *mod; R_SkinFrame_PrepareForPurge(); for (i = 0;i < nummodels;i++) { - if ((mod = Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->mempool && mod->data_textures) + if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->mempool && mod->data_textures) { for (j = 0;j < mod->num_textures;j++) { @@ -104,7 +104,7 @@ static void mod_newmap(void) for (i = 0;i < nummodels;i++) { - if ((mod = Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->mempool && mod->data_surfaces) + if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->mempool && mod->data_surfaces) { for (surfacenum = 0, surface = mod->data_surfaces;surfacenum < mod->num_surfaces;surfacenum++, surface++) { @@ -113,7 +113,7 @@ static void mod_newmap(void) ssize = (surface->lightmapinfo->extents[0] >> 4) + 1; tsize = (surface->lightmapinfo->extents[1] >> 4) + 1; memset(surface->lightmapinfo->stainsamples, 255, ssize * tsize * 3); - surface->cached_dlight = true; + mod->brushq1.lightmapupdateflags[surfacenum] = true; } } } @@ -131,7 +131,7 @@ static void Mod_BuildVBOs(void); void Mod_Init (void) { mod_mempool = Mem_AllocPool("modelinfo", 0, NULL); - Mem_ExpandableArray_NewArray(&models, mod_mempool, sizeof(model_t), 16); + Mem_ExpandableArray_NewArray(&models, mod_mempool, sizeof(dp_model_t), 16); Mod_BrushInit(); Mod_AliasInit(); @@ -147,23 +147,29 @@ void Mod_RenderInit(void) R_RegisterModule("Models", mod_start, mod_shutdown, mod_newmap); } -void Mod_UnloadModel (model_t *mod) +void Mod_UnloadModel (dp_model_t *mod) { char name[MAX_QPATH]; qboolean isworldmodel; qboolean used; + + if (developer_loading.integer) + Con_Printf("unloading model %s\n", mod->name); + strlcpy(name, mod->name, sizeof(name)); isworldmodel = mod->isworldmodel; used = mod->used; - if (mod->surfmesh.ebo) - R_Mesh_DestroyBufferObject(mod->surfmesh.ebo); + if (mod->surfmesh.ebo3i) + R_Mesh_DestroyBufferObject(mod->surfmesh.ebo3i); + if (mod->surfmesh.ebo3s) + R_Mesh_DestroyBufferObject(mod->surfmesh.ebo3s); if (mod->surfmesh.vbo) R_Mesh_DestroyBufferObject(mod->surfmesh.vbo); // free textures/memory attached to the model R_FreeTexturePool(&mod->texturepool); Mem_FreePool(&mod->mempool); // clear the struct to make it available - memset(mod, 0, sizeof(model_t)); + memset(mod, 0, sizeof(dp_model_t)); // restore the fields we want to preserve strlcpy(mod->name, name, sizeof(mod->name)); mod->isworldmodel = isworldmodel; @@ -171,6 +177,11 @@ void Mod_UnloadModel (model_t *mod) mod->loaded = false; } +void R_Model_Null_Draw(entity_render_t *ent) +{ + return; +} + /* ================== Mod_LoadModel @@ -178,7 +189,7 @@ Mod_LoadModel Loads a model ================== */ -model_t *Mod_LoadModel(model_t *mod, qboolean crash, qboolean checkdisk, qboolean isworldmodel) +dp_model_t *Mod_LoadModel(dp_model_t *mod, qboolean crash, qboolean checkdisk, qboolean isworldmodel) { int num; unsigned int crc; @@ -189,6 +200,44 @@ model_t *Mod_LoadModel(model_t *mod, qboolean crash, qboolean checkdisk, qboolea if (mod->name[0] == '*') // submodel return mod; + + if (!strcmp(mod->name, "null")) + { + if (mod->isworldmodel != isworldmodel) + mod->loaded = false; + + if(mod->loaded) + return mod; + + if (mod->loaded || mod->mempool) + Mod_UnloadModel(mod); + + if (developer_loading.integer) + Con_Printf("loading model %s\n", mod->name); + + mod->isworldmodel = isworldmodel; + mod->used = true; + mod->crc = -1; + mod->loaded = false; + + VectorClear(mod->normalmins); + VectorClear(mod->normalmaxs); + VectorClear(mod->yawmins); + VectorClear(mod->yawmaxs); + VectorClear(mod->rotatedmins); + VectorClear(mod->rotatedmaxs); + + mod->modeldatatypestring = "null"; + mod->type = mod_null; + mod->Draw = R_Model_Null_Draw; + mod->numframes = 2; + mod->numskins = 1; + + // no fatal errors occurred, so this model is ready to use. + mod->loaded = true; + + return mod; + } crc = 0; buf = NULL; @@ -224,7 +273,9 @@ model_t *Mod_LoadModel(model_t *mod, qboolean crash, qboolean checkdisk, qboolea return mod; } - Con_DPrintf("loading model %s\n", mod->name); + if (developer_loading.integer) + Con_Printf("loading model %s\n", mod->name); + // LordHavoc: unload the existing model in this slot (if there is one) if (mod->loaded || mod->mempool) Mod_UnloadModel(mod); @@ -276,8 +327,7 @@ model_t *Mod_LoadModel(model_t *mod, qboolean crash, qboolean checkdisk, qboolea else if (!memcmp(buf, "ZYMOTICMODEL", 12)) Mod_ZYMOTICMODEL_Load(mod, buf, bufend); else if (!memcmp(buf, "DARKPLACESMODEL", 16)) Mod_DARKPLACESMODEL_Load(mod, buf, bufend); else if (!memcmp(buf, "ACTRHEAD", 8)) Mod_PSKMODEL_Load(mod, buf, bufend); - else if (strlen(mod->name) >= 4 && !strcmp(mod->name - 4, ".map")) Mod_MAP_Load(mod, buf, bufend); - else if (!memcmp(buf, "MCBSPpad", 8)) Mod_Q1BSP_Load(mod, buf, bufend); + else if (strlen(mod->name) >= 4 && !strcmp(mod->name + strlen(mod->name) - 4, ".map")) Mod_MAP_Load(mod, buf, bufend); else if (num == BSPVERSION || num == 30) Mod_Q1BSP_Load(mod, buf, bufend); else Con_Printf("Mod_LoadModel: model \"%s\" is of unknown/unsupported type\n", mod->name); Mem_Free(buf); @@ -299,9 +349,9 @@ void Mod_ClearUsed(void) { int i; int nummodels = Mem_ExpandableArray_IndexRange(&models); - model_t *mod; + dp_model_t *mod; for (i = 0;i < nummodels;i++) - if ((mod = Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0]) + if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0]) mod->used = false; } @@ -309,10 +359,10 @@ void Mod_PurgeUnused(void) { int i; int nummodels = Mem_ExpandableArray_IndexRange(&models); - model_t *mod; + dp_model_t *mod; for (i = 0;i < nummodels;i++) { - if ((mod = Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && !mod->used) + if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && !mod->used) { Mod_UnloadModel(mod); Mem_ExpandableArray_FreeRecord(&models, mod); @@ -321,14 +371,14 @@ void Mod_PurgeUnused(void) } // only used during loading! -void Mod_RemoveStaleWorldModels(model_t *skip) +void Mod_RemoveStaleWorldModels(dp_model_t *skip) { int i; int nummodels = Mem_ExpandableArray_IndexRange(&models); - model_t *mod; + dp_model_t *mod; for (i = 0;i < nummodels;i++) { - if ((mod = Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->isworldmodel && mod->loaded && skip != mod) + if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->isworldmodel && mod->loaded && skip != mod) { Mod_UnloadModel(mod); mod->isworldmodel = false; @@ -343,11 +393,11 @@ Mod_FindName ================== */ -model_t *Mod_FindName(const char *name) +dp_model_t *Mod_FindName(const char *name) { int i; int nummodels = Mem_ExpandableArray_IndexRange(&models); - model_t *mod; + dp_model_t *mod; if (!name[0]) Host_Error ("Mod_ForName: NULL name"); @@ -355,7 +405,7 @@ model_t *Mod_FindName(const char *name) // search the currently loaded models for (i = 0;i < nummodels;i++) { - if ((mod = Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && !strcmp(mod->name, name)) + if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && !strcmp(mod->name, name)) { mod->used = true; return mod; @@ -363,7 +413,7 @@ model_t *Mod_FindName(const char *name) } // no match found, create a new one - mod = Mem_ExpandableArray_AllocRecord(&models); + mod = (dp_model_t *) Mem_ExpandableArray_AllocRecord(&models); strlcpy(mod->name, name, sizeof(mod->name)); mod->loaded = false; mod->used = true; @@ -377,9 +427,9 @@ Mod_ForName Loads in a model for the given name ================== */ -model_t *Mod_ForName(const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel) +dp_model_t *Mod_ForName(const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel) { - model_t *model; + dp_model_t *model; model = Mod_FindName(name); if (model->name[0] != '*' && (!model->loaded || checkdisk)) Mod_LoadModel(model, crash, checkdisk, isworldmodel); @@ -397,9 +447,9 @@ void Mod_Reload(void) { int i; int nummodels = Mem_ExpandableArray_IndexRange(&models); - model_t *mod; + dp_model_t *mod; for (i = 0;i < nummodels;i++) - if ((mod = Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*' && mod->used) + if ((mod = (dp_model_t *) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*' && mod->used) Mod_LoadModel(mod, true, true, mod->isworldmodel); } @@ -417,11 +467,11 @@ static void Mod_Print(void) { int i; int nummodels = Mem_ExpandableArray_IndexRange(&models); - model_t *mod; + dp_model_t *mod; Con_Print("Loaded models:\n"); for (i = 0;i < nummodels;i++) - if ((mod = Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0]) + if ((mod = (dp_model_t *) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0]) Con_Printf("%4iK %s\n", mod->mempool ? (int)((mod->mempool->totalsize + 1023) / 1024) : 0, mod->name); } @@ -747,7 +797,7 @@ void Mod_BuildTextureVectorsFromNormals(int firstvertex, int numvertices, int nu void Mod_AllocSurfMesh(mempool_t *mempool, int numvertices, int numtriangles, qboolean lightmapoffsets, qboolean vertexcolors, qboolean neighbors) { unsigned char *data; - data = (unsigned char *)Mem_Alloc(mempool, numvertices * (3 + 3 + 3 + 3 + 2 + 2 + (vertexcolors ? 4 : 0)) * sizeof(float) + numvertices * (lightmapoffsets ? 1 : 0) * sizeof(int) + numtriangles * (3 + (neighbors ? 3 : 0)) * sizeof(int)); + data = (unsigned char *)Mem_Alloc(mempool, numvertices * (3 + 3 + 3 + 3 + 2 + 2 + (vertexcolors ? 4 : 0)) * sizeof(float) + numvertices * (lightmapoffsets ? 1 : 0) * sizeof(int) + numtriangles * (3 + (neighbors ? 3 : 0)) * sizeof(int) + (numvertices <= 65536 ? numtriangles * sizeof(unsigned short[3]) : 0)); loadmodel->surfmesh.num_vertices = numvertices; loadmodel->surfmesh.num_triangles = numtriangles; if (loadmodel->surfmesh.num_vertices) @@ -768,6 +818,8 @@ void Mod_AllocSurfMesh(mempool_t *mempool, int numvertices, int numtriangles, qb loadmodel->surfmesh.data_element3i = (int *)data, data += sizeof(int[3]) * loadmodel->surfmesh.num_triangles; if (neighbors) loadmodel->surfmesh.data_neighbor3i = (int *)data, data += sizeof(int[3]) * loadmodel->surfmesh.num_triangles; + if (loadmodel->surfmesh.num_vertices <= 65536) + loadmodel->surfmesh.data_element3s = (unsigned short *)data, data += sizeof(unsigned short[3]) * loadmodel->surfmesh.num_triangles; } } @@ -781,6 +833,8 @@ shadowmesh_t *Mod_ShadowMesh_Alloc(mempool_t *mempool, int maxverts, int maxtria if (light) size += maxverts * sizeof(float[11]); size += maxtriangles * sizeof(int[3]); + if (maxverts <= 65536) + size += maxtriangles * sizeof(unsigned short[3]); if (neighbors) size += maxtriangles * sizeof(int[3]); if (expandable) @@ -813,6 +867,8 @@ shadowmesh_t *Mod_ShadowMesh_Alloc(mempool_t *mempool, int maxverts, int maxtria newmesh->vertexhashtable = (shadowmeshvertexhash_t **)data;data += SHADOWMESHVERTEXHASH * sizeof(shadowmeshvertexhash_t *); newmesh->vertexhashentries = (shadowmeshvertexhash_t *)data;data += maxverts * sizeof(shadowmeshvertexhash_t); } + if (maxverts <= 65536) + newmesh->element3s = (unsigned short *)data;data += maxtriangles * sizeof(unsigned short[3]); return newmesh; } @@ -929,10 +985,16 @@ void Mod_ShadowMesh_AddMesh(mempool_t *mempool, shadowmesh_t *mesh, rtexture_t * } Mod_ShadowMesh_AddTriangle(mempool, mesh, map_diffuse, map_specular, map_normal, vbuf); } + + // the triangle calculation can take a while, so let's do a keepalive here + CL_KeepaliveMessage(false); } shadowmesh_t *Mod_ShadowMesh_Begin(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) { + // the preparation before shadow mesh initialization can take a while, so let's do a keepalive here + CL_KeepaliveMessage(false); + return Mod_ShadowMesh_Alloc(mempool, maxverts, maxtriangles, map_diffuse, map_specular, map_normal, light, neighbors, expandable); } @@ -943,7 +1005,12 @@ static void Mod_ShadowMesh_CreateVBOs(shadowmesh_t *mesh) // element buffer is easy because it's just one array if (mesh->numtriangles) - mesh->ebo = R_Mesh_CreateStaticBufferObject(GL_ELEMENT_ARRAY_BUFFER_ARB, mesh->element3i, mesh->numtriangles * sizeof(int[3]), "shadowmesh"); + { + if (mesh->element3s) + mesh->ebo3s = R_Mesh_CreateStaticBufferObject(GL_ELEMENT_ARRAY_BUFFER_ARB, mesh->element3s, mesh->numtriangles * sizeof(unsigned short[3]), "shadowmesh"); + else + mesh->ebo3i = R_Mesh_CreateStaticBufferObject(GL_ELEMENT_ARRAY_BUFFER_ARB, mesh->element3i, mesh->numtriangles * sizeof(unsigned int[3]), "shadowmesh"); + } // vertex buffer is several arrays and we put them in the same buffer // @@ -983,11 +1050,21 @@ shadowmesh_t *Mod_ShadowMesh_Finish(mempool_t *mempool, shadowmesh_t *firstmesh, newmesh = Mod_ShadowMesh_ReAlloc(mempool, mesh, light, neighbors); newmesh->next = firstmesh; firstmesh = newmesh; + if (newmesh->element3s) + { + int i; + for (i = 0;i < newmesh->numtriangles*3;i++) + newmesh->element3s[i] = newmesh->element3i[i]; + } if (createvbo) Mod_ShadowMesh_CreateVBOs(newmesh); } Mem_Free(mesh); } + + // this can take a while, so let's do a keepalive here + CL_KeepaliveMessage(false); + return firstmesh; } @@ -1045,8 +1122,10 @@ void Mod_ShadowMesh_Free(shadowmesh_t *mesh) shadowmesh_t *nextmesh; for (;mesh;mesh = nextmesh) { - if (mesh->ebo) - R_Mesh_DestroyBufferObject(mesh->ebo); + if (mesh->ebo3i) + R_Mesh_DestroyBufferObject(mesh->ebo3i); + if (mesh->ebo3s) + R_Mesh_DestroyBufferObject(mesh->ebo3s); if (mesh->vbo) R_Mesh_DestroyBufferObject(mesh->vbo); nextmesh = mesh->next; @@ -1137,7 +1216,7 @@ static void Q3Shaders_Clear() q3shaders_mem = Mem_AllocPool("q3shaders", 0, NULL); q3shader_data = (q3shader_data_t*)Mem_Alloc (q3shaders_mem, sizeof (q3shader_data_t)); - Mem_ExpandableArray_NewArray (&q3shader_data->hash_entries, + Mem_ExpandableArray_NewArray (&q3shader_data->hash_entries, q3shaders_mem, sizeof (q3shader_hash_entry_t), 256); Mem_ExpandableArray_NewArray (&q3shader_data->char_ptrs, q3shaders_mem, sizeof (char**), 256); @@ -1165,7 +1244,7 @@ static void Q3Shader_AddToHash (q3shaderinfo_t* shader) /* Add to chain */ q3shader_hash_entry_t* newEntry = (q3shader_hash_entry_t*) Mem_ExpandableArray_AllocRecord (&q3shader_data->hash_entries); - + while (lastEntry->chain != NULL) lastEntry = lastEntry->chain; lastEntry->chain = newEntry; newEntry->chain = NULL; @@ -1177,6 +1256,7 @@ static void Q3Shader_AddToHash (q3shaderinfo_t* shader) memcpy (&entry->shader, shader, sizeof (q3shaderinfo_t)); } +extern cvar_t r_picmipworld; void Mod_LoadQ3Shaders(void) { int j; @@ -1208,6 +1288,7 @@ void Mod_LoadQ3Shaders(void) Vector4Set(shader.refractcolor4f, 1, 1, 1, 1); shader.reflectfactor = 1; Vector4Set(shader.reflectcolor4f, 1, 1, 1, 1); + shader.r_water_wateralpha = 1; strlcpy(shader.name, com_token, sizeof(shader.name)); if (!COM_ParseToken_QuakeC(&text, false) || strcasecmp(com_token, "{")) @@ -1332,7 +1413,7 @@ void Mod_LoadQ3Shaders(void) int i; layer->numframes = min(numparameters - 2, TEXTURE_MAXFRAMES); layer->framerate = atof(parameter[1]); - layer->texturename = Mem_Alloc (q3shaders_mem, sizeof (char*) * layer->numframes); + layer->texturename = (char **) Mem_Alloc (q3shaders_mem, sizeof (char*) * layer->numframes); for (i = 0;i < layer->numframes;i++) layer->texturename[i] = Mem_strdup (q3shaders_mem, parameter[i + 2]); } @@ -1374,7 +1455,7 @@ void Mod_LoadQ3Shaders(void) else if (!strcasecmp(parameter[1], "vertex")) layer->alphagen.alphagen = Q3ALPHAGEN_VERTEX; else if (!strcasecmp(parameter[1], "wave")) { - layer->alphagen.alphagen = Q3RGBGEN_WAVE; + layer->alphagen.alphagen = Q3ALPHAGEN_WAVE; layer->alphagen.wavefunc = Mod_LoadQ3Shaders_EnumerateWaveFunc(parameter[2]); for (i = 0;i < numparameters - 3 && i < Q3WAVEPARMS;i++) layer->alphagen.waveparms[i] = atof(parameter[i+3]); @@ -1419,6 +1500,7 @@ void Mod_LoadQ3Shaders(void) else if (!strcasecmp(parameter[1], "rotate")) layer->tcmods[tcmodindex].tcmod = Q3TCMOD_ROTATE; else if (!strcasecmp(parameter[1], "scale")) layer->tcmods[tcmodindex].tcmod = Q3TCMOD_SCALE; else if (!strcasecmp(parameter[1], "scroll")) layer->tcmods[tcmodindex].tcmod = Q3TCMOD_SCROLL; + else if (!strcasecmp(parameter[1], "page")) layer->tcmods[tcmodindex].tcmod = Q3TCMOD_PAGE; else if (!strcasecmp(parameter[1], "stretch")) { layer->tcmods[tcmodindex].tcmod = Q3TCMOD_STRETCH; @@ -1452,6 +1534,13 @@ void Mod_LoadQ3Shaders(void) shader.textureblendalpha = true; } } + layer->texflags = TEXF_ALPHA | TEXF_PRECACHE; + if (!(shader.surfaceparms & Q3SURFACEPARM_NOMIPMAPS)) + layer->texflags |= TEXF_MIPMAP; + if (!(shader.textureflags & Q3TEXTUREFLAG_NOPICMIP)) + layer->texflags |= TEXF_PICMIP | TEXF_COMPRESS; + if (layer->clampmap) + layer->texflags |= TEXF_CLAMP; continue; } numparameters = 0; @@ -1547,6 +1636,10 @@ void Mod_LoadQ3Shaders(void) else Con_DPrintf("%s parsing warning: unknown surfaceparm \"%s\"\n", search->filenames[fileindex], parameter[1]); } + else if (!strcasecmp(parameter[0], "dpshadow")) + shader.dpshadow = true; + else if (!strcasecmp(parameter[0], "dpnoshadow")) + shader.dpnoshadow = true; else if (!strcasecmp(parameter[0], "sky") && numparameters >= 2) { // some q3 skies don't have the sky parm set @@ -1583,7 +1676,7 @@ void Mod_LoadQ3Shaders(void) shader.reflectfactor = atof(parameter[1]); Vector4Set(shader.reflectcolor4f, atof(parameter[2]), atof(parameter[3]), atof(parameter[4]), atof(parameter[5])); } - else if (!strcasecmp(parameter[0], "dp_water") && numparameters >= 11) + else if (!strcasecmp(parameter[0], "dp_water") && numparameters >= 12) { shader.textureflags |= Q3TEXTUREFLAG_WATERSHADER; shader.reflectmin = atof(parameter[1]); @@ -1592,6 +1685,7 @@ void Mod_LoadQ3Shaders(void) shader.reflectfactor = atof(parameter[4]); Vector4Set(shader.refractcolor4f, atof(parameter[5]), atof(parameter[6]), atof(parameter[7]), 1); Vector4Set(shader.reflectcolor4f, atof(parameter[8]), atof(parameter[9]), atof(parameter[10]), 1); + shader.r_water_wateralpha = atof(parameter[11]); } else if (!strcasecmp(parameter[0], "deformvertexes") && numparameters >= 2) { @@ -1633,26 +1727,29 @@ void Mod_LoadQ3Shaders(void) } } } - // identify if this is a blended terrain shader or similar + // pick the primary layer to render with if (shader.numlayers) { shader.backgroundlayer = -1; shader.primarylayer = 0; - if ((shader.layers[0].blendfunc[0] == GL_ONE && shader.layers[0].blendfunc[1] == GL_ZERO && !shader.layers[0].alphatest) - && ((shader.layers[1].blendfunc[0] == GL_SRC_ALPHA && shader.layers[1].blendfunc[1] == GL_ONE_MINUS_SRC_ALPHA && !shader.layers[0].alphatest) - || (shader.layers[1].blendfunc[0] == GL_ONE && shader.layers[1].blendfunc[1] == GL_ZERO && shader.layers[1].alphatest))) - { - // terrain blending or other effects - shader.backgroundlayer = 0; - shader.primarylayer = 1; - } - // now see if the lightmap came first, and if so choose the second texture instead + // if lightmap comes first this is definitely an ordinary texture + // if the first two layers have the correct blendfuncs and use vertex alpha, it is a blended terrain shader if ((shader.layers[shader.primarylayer].texturename != NULL) && !strcasecmp(shader.layers[shader.primarylayer].texturename[0], "$lightmap")) { shader.backgroundlayer = -1; shader.primarylayer = 1; } + else if (shader.numlayers >= 2 + && shader.layers[1].alphagen.alphagen == Q3ALPHAGEN_VERTEX + && (shader.layers[0].blendfunc[0] == GL_ONE && shader.layers[0].blendfunc[1] == GL_ZERO && !shader.layers[0].alphatest) + && ((shader.layers[1].blendfunc[0] == GL_SRC_ALPHA && shader.layers[1].blendfunc[1] == GL_ONE_MINUS_SRC_ALPHA) + || (shader.layers[1].blendfunc[0] == GL_ONE && shader.layers[1].blendfunc[1] == GL_ZERO && shader.layers[1].alphatest))) + { + // terrain blending or other effects + shader.backgroundlayer = 0; + shader.primarylayer = 1; + } } // fix up multiple reflection types if(shader.textureflags & Q3TEXTUREFLAG_WATERSHADER) @@ -1677,25 +1774,31 @@ q3shaderinfo_t *Mod_LookupQ3Shader(const char *name) return NULL; } -extern cvar_t r_picmipworld; -qboolean Mod_LoadTextureFromQ3Shader(texture_t *texture, const char *name, qboolean q1bsp, qboolean q3bsp, qboolean md3) +qboolean Mod_LoadTextureFromQ3Shader(texture_t *texture, const char *name, qboolean warnmissing, qboolean fallback, int defaulttexflags) { int j; - int texflags; qboolean success = true; q3shaderinfo_t *shader; + if (!name) + name = ""; strlcpy(texture->name, name, sizeof(texture->name)); shader = name[0] ? Mod_LookupQ3Shader(name) : NULL; if (shader) { - if (developer.integer >= 100) - Con_DPrintf("%s: loaded shader for %s\n", loadmodel->name, name); + if (developer_loading.integer) + Con_Printf("%s: loaded shader for %s\n", loadmodel->name, name); texture->surfaceparms = shader->surfaceparms; texture->textureflags = shader->textureflags; - texture->basematerialflags = 0; + + // allow disabling of picmip or compression by defaulttexflags + if(!(defaulttexflags & TEXF_PICMIP)) + texture->textureflags &= ~TEXF_PICMIP; + if(!(defaulttexflags & TEXF_COMPRESS)) + texture->textureflags &= ~TEXF_COMPRESS; + if (shader->surfaceparms & Q3SURFACEPARM_SKY) { - texture->basematerialflags |= MATERIALFLAG_SKY | MATERIALFLAG_NOSHADOW; + texture->basematerialflags = MATERIALFLAG_SKY | MATERIALFLAG_NOSHADOW; if (shader->skyboxname[0]) { // quake3 seems to append a _ to the skybox name, so this must do so as well @@ -1703,21 +1806,16 @@ qboolean Mod_LoadTextureFromQ3Shader(texture_t *texture, const char *name, qbool } } else if ((texture->surfaceflags & Q3SURFACEFLAG_NODRAW) || shader->numlayers == 0) - texture->basematerialflags |= MATERIALFLAG_NODRAW | MATERIALFLAG_NOSHADOW; - else if (shader->surfaceparms & Q3SURFACEPARM_LAVA) - texture->basematerialflags |= MATERIALFLAG_WATER | MATERIALFLAG_LIGHTBOTHSIDES | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOSHADOW; - else if (shader->surfaceparms & Q3SURFACEPARM_SLIME) - texture->basematerialflags |= MATERIALFLAG_WATER | MATERIALFLAG_LIGHTBOTHSIDES | MATERIALFLAG_WATERALPHA | MATERIALFLAG_NOSHADOW; - else if (shader->surfaceparms & Q3SURFACEPARM_WATER) - texture->basematerialflags |= MATERIALFLAG_WATER | MATERIALFLAG_LIGHTBOTHSIDES | MATERIALFLAG_WATERALPHA | MATERIALFLAG_NOSHADOW; + texture->basematerialflags = MATERIALFLAG_NODRAW | MATERIALFLAG_NOSHADOW; else - texture->basematerialflags |= MATERIALFLAG_WALL; + texture->basematerialflags = MATERIALFLAG_WALL; + if (shader->layers[0].alphatest) texture->basematerialflags |= MATERIALFLAG_ALPHATEST | MATERIALFLAG_NOSHADOW; if (shader->textureflags & Q3TEXTUREFLAG_TWOSIDED) texture->basematerialflags |= MATERIALFLAG_NOSHADOW | MATERIALFLAG_NOCULLFACE; if (shader->textureflags & Q3TEXTUREFLAG_POLYGONOFFSET) - texture->basepolygonoffset -= 2; + texture->biaspolygonoffset -= 2; if (shader->textureflags & Q3TEXTUREFLAG_REFRACTION) texture->basematerialflags |= MATERIALFLAG_REFRACTION; if (shader->textureflags & Q3TEXTUREFLAG_REFLECTION) @@ -1778,14 +1876,11 @@ nothing GL_ZERO GL_ONE texture->skinframerate = primarylayer->framerate; for (j = 0;j < primarylayer->numframes;j++) { - texflags = TEXF_ALPHA | TEXF_PRECACHE; - if (!(shader->surfaceparms & Q3SURFACEPARM_NOMIPMAPS)) - texflags |= TEXF_MIPMAP; - if (!(shader->textureflags & Q3TEXTUREFLAG_NOPICMIP) && ((!q1bsp && !q3bsp) || r_picmipworld.integer)) - texflags |= TEXF_PICMIP | TEXF_COMPRESS; - if (primarylayer->clampmap) - texflags |= TEXF_CLAMP; - if (!(texture->skinframes[j] = R_SkinFrame_LoadExternal(primarylayer->texturename[j], texflags, false))) + if(cls.state == ca_dedicated) + { + texture->skinframes[j] = NULL; + } + else if (!(texture->skinframes[j] = R_SkinFrame_LoadExternal(primarylayer->texturename[j], primarylayer->texflags, false))) { Con_Printf("^1%s:^7 could not load texture ^3\"%s\"^7 (frame %i) for shader ^2\"%s\"\n", loadmodel->name, primarylayer->texturename[j], j, texture->name); texture->skinframes[j] = R_SkinFrame_LoadMissing(); @@ -1799,13 +1894,21 @@ nothing GL_ZERO GL_ONE texture->backgroundskinframerate = backgroundlayer->framerate; for (j = 0;j < backgroundlayer->numframes;j++) { - if (!(texture->backgroundskinframes[j] = R_SkinFrame_LoadExternal(backgroundlayer->texturename[j], ((shader->surfaceparms & Q3SURFACEPARM_NOMIPMAPS) ? 0 : TEXF_MIPMAP) | TEXF_ALPHA | TEXF_PRECACHE | ((!r_picmipworld.integer || (shader->textureflags & Q3TEXTUREFLAG_NOPICMIP)) ? 0 : (TEXF_PICMIP | TEXF_COMPRESS)) | (backgroundlayer->clampmap ? TEXF_CLAMP : 0), false))) + if(cls.state == ca_dedicated) { - Con_Printf("^1%s:^7 could not load texture ^3\"%s\"^7 (frame %i) for shader ^2\"%s\"\n", loadmodel->name, backgroundlayer->texturename[j], j, texture->name); + texture->skinframes[j] = NULL; + } + else if (!(texture->backgroundskinframes[j] = R_SkinFrame_LoadExternal(backgroundlayer->texturename[j], backgroundlayer->texflags, false))) + { + Con_Printf("^1%s:^7 could not load texture ^3\"%s\"^7 (background frame %i) for shader ^2\"%s\"\n", loadmodel->name, backgroundlayer->texturename[j], j, texture->name); texture->backgroundskinframes[j] = R_SkinFrame_LoadMissing(); } } } + if (shader->dpshadow) + texture->basematerialflags &= ~MATERIALFLAG_NOSHADOW; + if (shader->dpnoshadow) + texture->basematerialflags |= MATERIALFLAG_NOSHADOW; memcpy(texture->deforms, shader->deforms, sizeof(texture->deforms)); texture->reflectmin = shader->reflectmin; texture->reflectmax = shader->reflectmax; @@ -1813,18 +1916,25 @@ nothing GL_ZERO GL_ONE Vector4Copy(shader->refractcolor4f, texture->refractcolor4f); texture->reflectfactor = shader->reflectfactor; Vector4Copy(shader->reflectcolor4f, texture->reflectcolor4f); + texture->r_water_wateralpha = shader->r_water_wateralpha; + } + else if (!strcmp(texture->name, "noshader") || !texture->name[0]) + { + if (developer.integer >= 100) + Con_Printf("^1%s:^7 using fallback noshader material for ^3\"%s\"\n", loadmodel->name, name); + texture->surfaceparms = 0; } - else if (!strcmp(texture->name, "noshader")) + else if (!strcmp(texture->name, "common/nodraw") || !strcmp(texture->name, "textures/common/nodraw")) { if (developer.integer >= 100) - Con_DPrintf("%s: using default handler for %s\n", loadmodel->name, name); + Con_Printf("^1%s:^7 using fallback nodraw material for ^3\"%s\"\n", loadmodel->name, name); texture->surfaceparms = 0; + texture->basematerialflags = MATERIALFLAG_NODRAW | MATERIALFLAG_NOSHADOW; } else { - success = false; - if (developer.integer >= 100 || loadmodel->type == mod_brushq3) - Con_DPrintf("%s: No shader found for texture \"%s\"\n", loadmodel->name, texture->name); + if (developer.integer >= 100) + Con_Printf("^1%s:^7 No shader found for texture ^3\"%s\"\n", loadmodel->name, texture->name); texture->surfaceparms = 0; if (texture->surfaceflags & Q3SURFACEFLAG_NODRAW) texture->basematerialflags |= MATERIALFLAG_NODRAW | MATERIALFLAG_NOSHADOW; @@ -1833,9 +1943,28 @@ nothing GL_ZERO GL_ONE else texture->basematerialflags |= MATERIALFLAG_WALL; texture->numskinframes = 1; - if (!(texture->skinframes[0] = R_SkinFrame_LoadExternal(texture->name, TEXF_MIPMAP | TEXF_ALPHA | TEXF_PRECACHE | (r_picmipworld.integer ? TEXF_PICMIP : 0) | TEXF_COMPRESS, false))) - if(developer.integer || q3bsp) // only the Q3BSP path provides no alternative (like loading image directly, or internal texture) - Con_Printf("^1%s:^7 could not load texture for missing shader ^3\"%s\"\n", loadmodel->name, texture->name); + if(cls.state == ca_dedicated) + { + texture->skinframes[0] = NULL; + } + else + { + if (fallback) + { + qboolean has_alpha; + if ((texture->skinframes[0] = R_SkinFrame_LoadExternal_CheckAlpha(texture->name, defaulttexflags, false, &has_alpha))) + { + if(has_alpha && (defaulttexflags & TEXF_ALPHA)) + texture->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW; + } + else + success = false; + } + else + success = false; + if (!success && warnmissing) + Con_Printf("^1%s:^7 could not load texture ^3\"%s\"\n", loadmodel->name, texture->name); + } } // init the animation variables texture->currentframe = texture; @@ -1850,14 +1979,12 @@ nothing GL_ZERO GL_ONE skinfile_t *Mod_LoadSkinFiles(void) { - int i, words, numtags, line, tagsetsused = false, wordsoverflow; + int i, words, line, wordsoverflow; char *text; const char *data; skinfile_t *skinfile = NULL, *first = NULL; skinfileitem_t *skinfileitem; char word[10][MAX_QPATH]; - overridetagnameset_t tagsets[MAX_SKINS]; - overridetagname_t tags[256]; /* sample file: @@ -1874,12 +2001,9 @@ tag_head, tag_weapon, tag_torso, */ - memset(tagsets, 0, sizeof(tagsets)); memset(word, 0, sizeof(word)); for (i = 0;i < MAX_SKINS && (data = text = (char *)FS_LoadFile(va("%s_%i.skin", loadmodel->name, i), tempmempool, true, NULL));i++) { - numtags = 0; - // If it's the first file we parse if (skinfile == NULL) { @@ -1920,7 +2044,8 @@ tag_torso, { if (words == 3) { - Con_DPrintf("Mod_LoadSkinFiles: parsed mesh \"%s\" shader replacement \"%s\"\n", word[1], word[2]); + if (developer_loading.integer) + Con_Printf("Mod_LoadSkinFiles: parsed mesh \"%s\" shader replacement \"%s\"\n", word[1], word[2]); skinfileitem = (skinfileitem_t *)Mem_Alloc(loadmodel->mempool, sizeof(skinfileitem_t)); skinfileitem->next = skinfile->items; skinfile->items = skinfileitem; @@ -1930,18 +2055,16 @@ tag_torso, else Con_Printf("Mod_LoadSkinFiles: parsing error in file \"%s_%i.skin\" on line #%i: wrong number of parameters to command \"%s\", see documentation in DP_GFX_SKINFILES extension in dpextensions.qc\n", loadmodel->name, i, line, word[0]); } - else if (words == 2 && !strcmp(word[1], ",")) + else if (words >= 2 && !strncmp(word[0], "tag_", 4)) { // tag name, like "tag_weapon," - Con_DPrintf("Mod_LoadSkinFiles: parsed tag #%i \"%s\"\n", numtags, word[0]); - memset(tags + numtags, 0, sizeof(tags[numtags])); - strlcpy (tags[numtags].name, word[0], sizeof (tags[numtags].name)); - numtags++; + // not used for anything (not even in Quake3) } - else if (words == 3 && !strcmp(word[1], ",")) + else if (words >= 2 && !strcmp(word[1], ",")) { // mesh shader name, like "U_RArm,models/players/Legoman/BikerA1.tga" - Con_DPrintf("Mod_LoadSkinFiles: parsed mesh \"%s\" shader replacement \"%s\"\n", word[0], word[2]); + if (developer_loading.integer) + Con_Printf("Mod_LoadSkinFiles: parsed mesh \"%s\" shader replacement \"%s\"\n", word[0], word[2]); skinfileitem = (skinfileitem_t *)Mem_Alloc(loadmodel->mempool, sizeof(skinfileitem_t)); skinfileitem->next = skinfile->items; skinfile->items = skinfileitem; @@ -1952,21 +2075,6 @@ tag_torso, Con_Printf("Mod_LoadSkinFiles: parsing error in file \"%s_%i.skin\" on line #%i: does not look like tag or mesh specification, or replace command, see documentation in DP_GFX_SKINFILES extension in dpextensions.qc\n", loadmodel->name, i, line); } Mem_Free(text); - - if (numtags) - { - overridetagnameset_t *t; - t = tagsets + i; - t->num_overridetagnames = numtags; - t->data_overridetagnames = (overridetagname_t *)Mem_Alloc(loadmodel->mempool, t->num_overridetagnames * sizeof(overridetagname_t)); - memcpy(t->data_overridetagnames, tags, t->num_overridetagnames * sizeof(overridetagname_t)); - tagsetsused = true; - } - } - if (tagsetsused) - { - loadmodel->data_overridetagnamesforskin = (overridetagnameset_t *)Mem_Alloc(loadmodel->mempool, i * sizeof(overridetagnameset_t)); - memcpy(loadmodel->data_overridetagnamesforskin, tagsets, i * sizeof(overridetagnameset_t)); } if (i) loadmodel->numskins = i; @@ -2007,23 +2115,20 @@ void Mod_SnapVertices(int numcomponents, int numvertices, float *vertices, float int Mod_RemoveDegenerateTriangles(int numtriangles, const int *inelement3i, int *outelement3i, const float *vertex3f) { int i, outtriangles; - float d, edgedir[3], temp[3]; + float edgedir1[3], edgedir2[3], temp[3]; // a degenerate triangle is one with no width (thickness, surface area) // these are characterized by having all 3 points colinear (along a line) // or having two points identical + // the simplest check is to calculate the triangle's area for (i = 0, outtriangles = 0;i < numtriangles;i++, inelement3i += 3) { // calculate first edge - VectorSubtract(vertex3f + inelement3i[1] * 3, vertex3f + inelement3i[0] * 3, edgedir); - if (VectorLength2(edgedir) < 0.0001f) - continue; // degenerate first edge (no length) - VectorNormalize(edgedir); - // check if third point is on the edge (colinear) - d = -DotProduct(vertex3f + inelement3i[2] * 3, edgedir); - VectorMA(vertex3f + inelement3i[2] * 3, d, edgedir, temp); - if (VectorLength2(temp) < 0.0001f) - continue; // third point colinear with first edge - // valid triangle (no colinear points, no duplicate points) + VectorSubtract(vertex3f + inelement3i[1] * 3, vertex3f + inelement3i[0] * 3, edgedir1); + VectorSubtract(vertex3f + inelement3i[2] * 3, vertex3f + inelement3i[0] * 3, edgedir2); + CrossProduct(edgedir1, edgedir2, temp); + if (VectorLength2(temp) < 0.001f) + continue; // degenerate triangle (no area) + // valid triangle (has area) VectorCopy(inelement3i, outelement3i); outelement3i += 3; outtriangles++; @@ -2060,7 +2165,17 @@ static void Mod_BuildVBOs(void) // element buffer is easy because it's just one array if (loadmodel->surfmesh.num_triangles) - loadmodel->surfmesh.ebo = R_Mesh_CreateStaticBufferObject(GL_ELEMENT_ARRAY_BUFFER_ARB, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles * sizeof(int[3]), loadmodel->name); + { + if (loadmodel->surfmesh.data_element3s) + { + int i; + for (i = 0;i < loadmodel->surfmesh.num_triangles*3;i++) + loadmodel->surfmesh.data_element3s[i] = loadmodel->surfmesh.data_element3i[i]; + loadmodel->surfmesh.ebo3s = R_Mesh_CreateStaticBufferObject(GL_ELEMENT_ARRAY_BUFFER_ARB, loadmodel->surfmesh.data_element3s, loadmodel->surfmesh.num_triangles * sizeof(unsigned short[3]), loadmodel->name); + } + else + loadmodel->surfmesh.ebo3i = R_Mesh_CreateStaticBufferObject(GL_ELEMENT_ARRAY_BUFFER_ARB, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles * sizeof(unsigned int[3]), loadmodel->name); + } // vertex buffer is several arrays and we put them in the same buffer //