]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_shared.c
changed texture formats from RGBA to BGRA, yes this is a massive change,
[xonotic/darkplaces.git] / model_shared.c
index 3b381c58a903eaec14f8e87d74dd73a41ca92a99..176711052c67a0f9c13707e516308ebce9fa0964 100644 (file)
@@ -1030,7 +1030,7 @@ void Mod_ShadowMesh_Free(shadowmesh_t *mesh)
        }
 }
 
-void Mod_GetTerrainVertex3fTexCoord2fFromRGBA(const unsigned char *imagepixels, int imagewidth, int imageheight, int ix, int iy, float *vertex3f, float *texcoord2f, matrix4x4_t *pixelstepmatrix, matrix4x4_t *pixeltexturestepmatrix)
+void Mod_GetTerrainVertex3fTexCoord2fFromBGRA(const unsigned char *imagepixels, int imagewidth, int imageheight, int ix, int iy, float *vertex3f, float *texcoord2f, matrix4x4_t *pixelstepmatrix, matrix4x4_t *pixeltexturestepmatrix)
 {
        float v[3], tc[3];
        v[0] = ix;
@@ -1045,16 +1045,16 @@ void Mod_GetTerrainVertex3fTexCoord2fFromRGBA(const unsigned char *imagepixels,
        texcoord2f[1] = tc[1];
 }
 
-void Mod_GetTerrainVertexFromRGBA(const unsigned char *imagepixels, int imagewidth, int imageheight, int ix, int iy, float *vertex3f, float *svector3f, float *tvector3f, float *normal3f, float *texcoord2f, matrix4x4_t *pixelstepmatrix, matrix4x4_t *pixeltexturestepmatrix)
+void Mod_GetTerrainVertexFromBGRA(const unsigned char *imagepixels, int imagewidth, int imageheight, int ix, int iy, float *vertex3f, float *svector3f, float *tvector3f, float *normal3f, float *texcoord2f, matrix4x4_t *pixelstepmatrix, matrix4x4_t *pixeltexturestepmatrix)
 {
        float vup[3], vdown[3], vleft[3], vright[3];
        float tcup[3], tcdown[3], tcleft[3], tcright[3];
        float sv[3], tv[3], nl[3];
-       Mod_GetTerrainVertex3fTexCoord2fFromRGBA(imagepixels, imagewidth, imageheight, ix, iy, vertex3f, texcoord2f, pixelstepmatrix, pixeltexturestepmatrix);
-       Mod_GetTerrainVertex3fTexCoord2fFromRGBA(imagepixels, imagewidth, imageheight, ix, iy - 1, vup, tcup, pixelstepmatrix, pixeltexturestepmatrix);
-       Mod_GetTerrainVertex3fTexCoord2fFromRGBA(imagepixels, imagewidth, imageheight, ix, iy + 1, vdown, tcdown, pixelstepmatrix, pixeltexturestepmatrix);
-       Mod_GetTerrainVertex3fTexCoord2fFromRGBA(imagepixels, imagewidth, imageheight, ix - 1, iy, vleft, tcleft, pixelstepmatrix, pixeltexturestepmatrix);
-       Mod_GetTerrainVertex3fTexCoord2fFromRGBA(imagepixels, imagewidth, imageheight, ix + 1, iy, vright, tcright, pixelstepmatrix, pixeltexturestepmatrix);
+       Mod_GetTerrainVertex3fTexCoord2fFromBGRA(imagepixels, imagewidth, imageheight, ix, iy, vertex3f, texcoord2f, pixelstepmatrix, pixeltexturestepmatrix);
+       Mod_GetTerrainVertex3fTexCoord2fFromBGRA(imagepixels, imagewidth, imageheight, ix, iy - 1, vup, tcup, pixelstepmatrix, pixeltexturestepmatrix);
+       Mod_GetTerrainVertex3fTexCoord2fFromBGRA(imagepixels, imagewidth, imageheight, ix, iy + 1, vdown, tcdown, pixelstepmatrix, pixeltexturestepmatrix);
+       Mod_GetTerrainVertex3fTexCoord2fFromBGRA(imagepixels, imagewidth, imageheight, ix - 1, iy, vleft, tcleft, pixelstepmatrix, pixeltexturestepmatrix);
+       Mod_GetTerrainVertex3fTexCoord2fFromBGRA(imagepixels, imagewidth, imageheight, ix + 1, iy, vright, tcright, pixelstepmatrix, pixeltexturestepmatrix);
        Mod_BuildBumpVectors(vertex3f, vup, vright, texcoord2f, tcup, tcright, svector3f, tvector3f, normal3f);
        Mod_BuildBumpVectors(vertex3f, vright, vdown, texcoord2f, tcright, tcdown, sv, tv, nl);
        VectorAdd(svector3f, sv, svector3f);
@@ -1070,7 +1070,7 @@ void Mod_GetTerrainVertexFromRGBA(const unsigned char *imagepixels, int imagewid
        VectorAdd(normal3f, nl, normal3f);
 }
 
-void Mod_ConstructTerrainPatchFromRGBA(const unsigned char *imagepixels, int imagewidth, int imageheight, int x1, int y1, int width, int height, int *element3i, int *neighbor3i, float *vertex3f, float *svector3f, float *tvector3f, float *normal3f, float *texcoord2f, matrix4x4_t *pixelstepmatrix, matrix4x4_t *pixeltexturestepmatrix)
+void Mod_ConstructTerrainPatchFromBGRA(const unsigned char *imagepixels, int imagewidth, int imageheight, int x1, int y1, int width, int height, int *element3i, int *neighbor3i, float *vertex3f, float *svector3f, float *tvector3f, float *normal3f, float *texcoord2f, matrix4x4_t *pixelstepmatrix, matrix4x4_t *pixeltexturestepmatrix)
 {
        int x, y, ix, iy, *e;
        e = element3i;
@@ -1090,7 +1090,7 @@ void Mod_ConstructTerrainPatchFromRGBA(const unsigned char *imagepixels, int ima
        Mod_BuildTriangleNeighbors(neighbor3i, element3i, width*height*2);
        for (y = 0, iy = y1;y < height + 1;y++, iy++)
                for (x = 0, ix = x1;x < width + 1;x++, ix++, vertex3f += 3, texcoord2f += 2, svector3f += 3, tvector3f += 3, normal3f += 3)
-                       Mod_GetTerrainVertexFromRGBA(imagepixels, imagewidth, imageheight, ix, iy, vertex3f, texcoord2f, svector3f, tvector3f, normal3f, pixelstepmatrix, pixeltexturestepmatrix);
+                       Mod_GetTerrainVertexFromBGRA(imagepixels, imagewidth, imageheight, ix, iy, vertex3f, texcoord2f, svector3f, tvector3f, normal3f, pixelstepmatrix, pixeltexturestepmatrix);
 }
 
 q3wavefunc_t Mod_LoadQ3Shaders_EnumerateWaveFunc(const char *s)
@@ -1133,7 +1133,15 @@ void Mod_LoadQ3Shaders(void)
                                break;
                        }
                        shader = q3shaders_shaders + q3shaders_numshaders++;
+
                        memset(shader, 0, sizeof(*shader));
+                       shader->reflectmin = 0;
+                       shader->reflectmax = 1;
+                       shader->refractfactor = 1;
+                       Vector4Set(shader->refractcolor4f, 1, 1, 1, 1);
+                       shader->reflectfactor = 1;
+                       Vector4Set(shader->reflectcolor4f, 1, 1, 1, 1);
+
                        strlcpy(shader->name, com_token, sizeof(shader->name));
                        if (!COM_ParseToken_QuakeC(&text, false) || strcasecmp(com_token, "{"))
                        {
@@ -1146,25 +1154,28 @@ void Mod_LoadQ3Shaders(void)
                                        break;
                                if (!strcasecmp(com_token, "{"))
                                {
+                                       static q3shaderinfo_layer_t dummy;
                                        if (shader->numlayers < Q3SHADER_MAXLAYERS)
                                        {
                                                layer = shader->layers + shader->numlayers++;
-                                               layer->rgbgen.rgbgen = Q3RGBGEN_IDENTITY;
-                                               layer->alphagen.alphagen = Q3ALPHAGEN_IDENTITY;
-                                               layer->tcgen.tcgen = Q3TCGEN_TEXTURE;
-                                               layer->blendfunc[0] = GL_ONE;
-                                               layer->blendfunc[1] = GL_ZERO;
                                        }
                                        else
-                                               layer = NULL;
+                                       {
+                                               // parse and process it anyway, just don't store it (so a map $lightmap or such stuff still is found)
+                                               memset(&dummy, 0, sizeof(dummy));
+                                               layer = &dummy;
+                                       }
+                                       layer->rgbgen.rgbgen = Q3RGBGEN_IDENTITY;
+                                       layer->alphagen.alphagen = Q3ALPHAGEN_IDENTITY;
+                                       layer->tcgen.tcgen = Q3TCGEN_TEXTURE;
+                                       layer->blendfunc[0] = GL_ONE;
+                                       layer->blendfunc[1] = GL_ZERO;
                                        while (COM_ParseToken_QuakeC(&text, false))
                                        {
                                                if (!strcasecmp(com_token, "}"))
                                                        break;
                                                if (!strcasecmp(com_token, "\n"))
                                                        continue;
-                                               if (layer == NULL)
-                                                       continue;
                                                numparameters = 0;
                                                for (j = 0;strcasecmp(com_token, "\n") && strcasecmp(com_token, "}");j++)
                                                {
@@ -1352,7 +1363,7 @@ void Mod_LoadQ3Shaders(void)
                                                        else
                                                                Con_DPrintf("%s parsing warning: too many tcmods on one layer\n", search->filenames[fileindex]);
                                                }
-                                               // break out a level if it was }
+                                               // break out a level if it was a closing brace (not using the character here to not confuse vim)
                                                if (!strcasecmp(com_token, "}"))
                                                        break;
                                        }
@@ -1490,6 +1501,28 @@ void Mod_LoadQ3Shaders(void)
                                        shader->textureflags |= Q3TEXTUREFLAG_NOPICMIP;
                                else if (!strcasecmp(parameter[0], "polygonoffset"))
                                        shader->textureflags |= Q3TEXTUREFLAG_POLYGONOFFSET;
+                               else if (!strcasecmp(parameter[0], "dp_refract") && numparameters >= 5)
+                               {
+                                       shader->textureflags |= Q3TEXTUREFLAG_REFRACTION;
+                                       shader->refractfactor = atof(parameter[1]);
+                                       Vector4Set(shader->refractcolor4f, atof(parameter[2]), atof(parameter[3]), atof(parameter[4]), 1);
+                               }
+                               else if (!strcasecmp(parameter[0], "dp_reflect") && numparameters >= 6)
+                               {
+                                       shader->textureflags |= Q3TEXTUREFLAG_REFLECTION;
+                                       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)
+                               {
+                                       shader->textureflags |= Q3TEXTUREFLAG_WATERSHADER;
+                                       shader->reflectmin = atof(parameter[1]);
+                                       shader->reflectmax = atof(parameter[2]);
+                                       shader->refractfactor = atof(parameter[3]);
+                                       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);
+                               }
                                else if (!strcasecmp(parameter[0], "deformvertexes") && numparameters >= 2)
                                {
                                        int i, deformindex;
@@ -1550,6 +1583,9 @@ void Mod_LoadQ3Shaders(void)
                                        shader->primarylayer = shader->layers + 1;
                                }
                        }
+                       // fix up multiple reflection types
+                       if(shader->textureflags & Q3TEXTUREFLAG_WATERSHADER)
+                               shader->textureflags &= ~(Q3TEXTUREFLAG_REFRACTION | Q3TEXTUREFLAG_REFLECTION);
                }
                Mem_Free(f);
        }
@@ -1558,7 +1594,7 @@ void Mod_LoadQ3Shaders(void)
 q3shaderinfo_t *Mod_LookupQ3Shader(const char *name)
 {
        int i;
-       for (i = 0;i < Q3SHADER_MAXSHADERS;i++)
+       for (i = 0;i < q3shaders_numshaders;i++)
                if (!strcasecmp(q3shaders_shaders[i].name, name))
                        return q3shaders_shaders + i;
        return NULL;
@@ -1605,6 +1641,12 @@ qboolean Mod_LoadTextureFromQ3Shader(texture_t *texture, const char *name, qbool
                        texture->basematerialflags |= MATERIALFLAG_NOSHADOW | MATERIALFLAG_NOCULLFACE;
                if (shader->textureflags & Q3TEXTUREFLAG_POLYGONOFFSET)
                        texture->basepolygonoffset -= 2;
+               if (shader->textureflags & Q3TEXTUREFLAG_REFRACTION)
+                       texture->basematerialflags |= MATERIALFLAG_REFRACTION;
+               if (shader->textureflags & Q3TEXTUREFLAG_REFLECTION)
+                       texture->basematerialflags |= MATERIALFLAG_REFLECTION;
+               if (shader->textureflags & Q3TEXTUREFLAG_WATERSHADER)
+                       texture->basematerialflags |= MATERIALFLAG_WATERSHADER;
                texture->customblendfunc[0] = GL_ONE;
                texture->customblendfunc[1] = GL_ZERO;
                if (shader->numlayers > 0)
@@ -1667,7 +1709,7 @@ nothing                GL_ZERO GL_ONE
                                        texflags |= TEXF_CLAMP;
                                if (!(texture->skinframes[j] = R_SkinFrame_LoadExternal(shader->primarylayer->texturename[j], texflags, false)))
                                {
-                                       Con_DPrintf("%s: could not load texture \"%s\" (frame %i) for shader \"%s\"\n", loadmodel->name, shader->primarylayer->texturename[j], j, texture->name);
+                                       Con_Printf("^1%s:^7 could not load texture ^3\"%s\"^7 (frame %i) for shader ^2\"%s\"\n", loadmodel->name, shader->primarylayer->texturename[j], j, texture->name);
                                        texture->skinframes[j] = R_SkinFrame_LoadMissing();
                                }
                        }
@@ -1680,12 +1722,18 @@ nothing                GL_ZERO GL_ONE
                        {
                                if (!(texture->backgroundskinframes[j] = R_SkinFrame_LoadExternal(shader->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)) | (shader->backgroundlayer->clampmap ? TEXF_CLAMP : 0), false)))
                                {
-                                       Con_DPrintf("%s: could not load texture \"%s\" (frame %i) for shader \"%s\"\n", loadmodel->name, shader->backgroundlayer->texturename[j], j, texture->name);
+                                       Con_Printf("^1%s:^7 could not load texture ^3\"%s\"^7 (frame %i) for shader ^2\"%s\"\n", loadmodel->name, shader->backgroundlayer->texturename[j], j, texture->name);
                                        texture->backgroundskinframes[j] = R_SkinFrame_LoadMissing();
                                }
                        }
                }
                memcpy(texture->deforms, shader->deforms, sizeof(texture->deforms));
+               texture->reflectmin = shader->reflectmin;
+               texture->reflectmax = shader->reflectmax;
+               texture->refractfactor = shader->refractfactor;
+               Vector4Copy(shader->refractcolor4f, texture->refractcolor4f);
+               texture->reflectfactor = shader->reflectfactor;
+               Vector4Copy(shader->reflectcolor4f, texture->reflectcolor4f);
        }
        else if (!strcmp(texture->name, "noshader"))
        {
@@ -1707,7 +1755,8 @@ nothing                GL_ZERO GL_ONE
                        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)))
-                       Con_DPrintf("%s: could not load texture for missing shader \"%s\"\n", loadmodel->name, texture->name);
+                       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);
        }
        // init the animation variables
        texture->currentframe = texture;