]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_shared.c
VM_stringtokeynum: return a float, not an int
[xonotic/darkplaces.git] / model_shared.c
index f5a4b4eac831436af99de877fe61c064d1faad72..37bae9787e171895cf0cc7a79598e88f7b928280 100644 (file)
@@ -28,6 +28,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "polygon.h"
 
 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"};
+cvar_t r_mipnormalmaps = {CVAR_SAVE, "r_mipnormalmaps", "1", "mipmaps normalmaps (turning it off looks sharper but may have aliasing)"};
 cvar_t mod_generatelightmaps_unitspersample = {CVAR_SAVE, "mod_generatelightmaps_unitspersample", "8", "lightmap resolution"};
 cvar_t mod_generatelightmaps_borderpixels = {CVAR_SAVE, "mod_generatelightmaps_borderpixels", "2", "extra space around polygons to prevent sampling artifacts"};
 cvar_t mod_generatelightmaps_texturesize = {CVAR_SAVE, "mod_generatelightmaps_texturesize", "1024", "size of lightmap textures"};
@@ -160,6 +161,7 @@ void Mod_Init (void)
        Mod_SpriteInit();
 
        Cvar_RegisterVariable(&r_mipskins);
+       Cvar_RegisterVariable(&r_mipnormalmaps);
        Cvar_RegisterVariable(&mod_generatelightmaps_unitspersample);
        Cvar_RegisterVariable(&mod_generatelightmaps_borderpixels);
        Cvar_RegisterVariable(&mod_generatelightmaps_texturesize);
@@ -2236,7 +2238,7 @@ q3shaderinfo_t *Mod_LookupQ3Shader(const char *name)
 qboolean Mod_LoadTextureFromQ3Shader(texture_t *texture, const char *name, qboolean warnmissing, qboolean fallback, int defaulttexflags)
 {
        int j;
-       int texflagsmask;
+       int texflagsmask, texflagsor;
        qboolean success = true;
        q3shaderinfo_t *shader;
        if (!name)
@@ -2249,6 +2251,11 @@ qboolean Mod_LoadTextureFromQ3Shader(texture_t *texture, const char *name, qbool
                texflagsmask &= ~TEXF_PICMIP;
        if(!(defaulttexflags & TEXF_COMPRESS))
                texflagsmask &= ~TEXF_COMPRESS;
+       texflagsor = 0;
+       if(defaulttexflags & TEXF_ISWORLD)
+               texflagsor |= TEXF_ISWORLD;
+       if(defaulttexflags & TEXF_ISSPRITE)
+               texflagsor |= TEXF_ISSPRITE;
        // unless later loaded from the shader
        texture->offsetmapping = (mod_q3shader_default_offsetmapping.value) ? OFFSETMAPPING_DEFAULT : OFFSETMAPPING_OFF;
        texture->offsetscale = 1;
@@ -2265,7 +2272,7 @@ qboolean Mod_LoadTextureFromQ3Shader(texture_t *texture, const char *name, qbool
                texture->surfaceparms = shader->surfaceparms;
 
                // allow disabling of picmip or compression by defaulttexflags
-               texture->textureflags = shader->textureflags & texflagsmask;
+               texture->textureflags = (shader->textureflags & texflagsmask) | texflagsor;
 
                if (shader->surfaceparms & Q3SURFACEPARM_SKY)
                {
@@ -2353,7 +2360,7 @@ nothing                GL_ZERO GL_ONE
                                {
                                        texture->skinframes[j] = NULL;
                                }
-                               else if (!(texture->skinframes[j] = R_SkinFrame_LoadExternal(primarylayer->texturename[j], primarylayer->texflags & texflagsmask, false)))
+                               else if (!(texture->skinframes[j] = R_SkinFrame_LoadExternal(primarylayer->texturename[j], (primarylayer->texflags & texflagsmask) | texflagsor, 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();
@@ -2374,7 +2381,7 @@ nothing                GL_ZERO GL_ONE
                                {
                                        texture->skinframes[j] = NULL;
                                }
-                               else if (!(texture->backgroundskinframes[j] = R_SkinFrame_LoadExternal(backgroundlayer->texturename[j], backgroundlayer->texflags & texflagsmask, false)))
+                               else if (!(texture->backgroundskinframes[j] = R_SkinFrame_LoadExternal(backgroundlayer->texturename[j], (backgroundlayer->texflags & texflagsmask) | texflagsor, 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();
@@ -2829,7 +2836,7 @@ void Mod_BuildVBOs(void)
 
 static void Mod_Decompile_OBJ(dp_model_t *model, const char *filename, const char *mtlfilename, const char *originalfilename)
 {
-       int vertexindex, surfaceindex, triangleindex, textureindex, countvertices = 0, countsurfaces = 0, countfaces = 0, counttextures = 0;
+       int submodelindex, vertexindex, surfaceindex, triangleindex, textureindex, countvertices = 0, countsurfaces = 0, countfaces = 0, counttextures = 0;
        int a, b, c;
        const char *texname;
        const int *e;
@@ -2841,6 +2848,7 @@ static void Mod_Decompile_OBJ(dp_model_t *model, const char *filename, const cha
        const msurface_t *surface;
        const int maxtextures = 256;
        char *texturenames = (char *) Z_Malloc(maxtextures * MAX_QPATH);
+       dp_model_t *submodel;
 
        // construct the mtllib file
        l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "# mtllib for %s exported by darkplaces engine\n", originalfilename);
@@ -2876,12 +2884,13 @@ static void Mod_Decompile_OBJ(dp_model_t *model, const char *filename, const cha
 
        // write the mtllib file
        FS_WriteFile(mtlfilename, outbuffer, outbufferpos);
-       outbufferpos = 0;
 
        // construct the obj file
+       outbufferpos = 0;
        l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "# model exported from %s by darkplaces engine\n# %i vertices, %i faces, %i surfaces\nmtllib %s\n", originalfilename, countvertices, countfaces, countsurfaces, mtlfilename);
        if (l > 0)
                outbufferpos += l;
+
        for (vertexindex = 0, v = model->surfmesh.data_vertex3f, vn = model->surfmesh.data_normal3f, vt = model->surfmesh.data_texcoordtexture2f;vertexindex < model->surfmesh.num_vertices;vertexindex++, v += 3, vn += 3, vt += 2)
        {
                if (outbufferpos >= outbuffermax >> 1)
@@ -2892,31 +2901,40 @@ static void Mod_Decompile_OBJ(dp_model_t *model, const char *filename, const cha
                        memcpy(outbuffer, oldbuffer, outbufferpos);
                        Z_Free(oldbuffer);
                }
-               l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "v %f %f %f\nvn %f %f %f\nvt %f %f\n", v[0], v[2], -v[1], vn[0], vn[2], -vn[1], vt[0], 1-vt[1]);
+               l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "v %f %f %f\nvn %f %f %f\nvt %f %f\n", v[0], v[2], v[1], vn[0], vn[2], vn[1], vt[0], 1-vt[1]);
                if (l > 0)
                        outbufferpos += l;
        }
-       for (surfaceindex = 0, surface = model->data_surfaces;surfaceindex < model->num_surfaces;surfaceindex++, surface++)
+
+       for (submodelindex = 0;submodelindex < max(1, model->brush.numsubmodels);submodelindex++)
        {
-               l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "usemtl %s\n", (surface->texture && surface->texture->name[0]) ? surface->texture->name : "default");
+               l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "o %i\n", submodelindex);
                if (l > 0)
                        outbufferpos += l;
-               for (triangleindex = 0, e = model->surfmesh.data_element3i + surface->num_firsttriangle * 3;triangleindex < surface->num_triangles;triangleindex++, e += 3)
+               submodel = model->brush.numsubmodels ? model->brush.submodels[submodelindex] : model;
+               for (surfaceindex = 0;surfaceindex < submodel->nummodelsurfaces;surfaceindex++)
                {
-                       if (outbufferpos >= outbuffermax >> 1)
-                       {
-                               outbuffermax *= 2;
-                               oldbuffer = outbuffer;
-                               outbuffer = (char *) Z_Malloc(outbuffermax);
-                               memcpy(outbuffer, oldbuffer, outbufferpos);
-                               Z_Free(oldbuffer);
-                       }
-                       a = e[0]+1;
-                       b = e[2]+1;
-                       c = e[1]+1;
-                       l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "f %i/%i/%i %i/%i/%i %i/%i/%i\n", a,a,a,b,b,b,c,c,c);
+                       surface = model->data_surfaces + submodel->sortedmodelsurfaces[surfaceindex];
+                       l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "usemtl %s\n", (surface->texture && surface->texture->name[0]) ? surface->texture->name : "default");
                        if (l > 0)
                                outbufferpos += l;
+                       for (triangleindex = 0, e = model->surfmesh.data_element3i + surface->num_firsttriangle * 3;triangleindex < surface->num_triangles;triangleindex++, e += 3)
+                       {
+                               if (outbufferpos >= outbuffermax >> 1)
+                               {
+                                       outbuffermax *= 2;
+                                       oldbuffer = outbuffer;
+                                       outbuffer = (char *) Z_Malloc(outbuffermax);
+                                       memcpy(outbuffer, oldbuffer, outbufferpos);
+                                       Z_Free(oldbuffer);
+                               }
+                               a = e[0]+1;
+                               b = e[1]+1;
+                               c = e[2]+1;
+                               l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "f %i/%i/%i %i/%i/%i %i/%i/%i\n", a,a,a,b,b,b,c,c,c);
+                               if (l > 0)
+                                       outbufferpos += l;
+                       }
                }
        }