Fix the shading in scud. When lighting is disabled the fixed shading intensities appear to be offset values added to the colour value.

This commit is contained in:
Ian Curtis 2016-11-11 17:50:27 +00:00
parent 0a7c0d0864
commit 483ba2f398
3 changed files with 27 additions and 23 deletions

View file

@ -16,7 +16,7 @@ struct Vertex
float pos[3];
float normal[3];
float texcoords[2];
UINT8 color[4]; //rgba
float color[4]; //rgba
};
struct Poly // our polys are always 3 triangles, unlike the real h/w

View file

@ -242,7 +242,7 @@ void CNew3D::RenderFrame(void)
glVertexPointer (3, GL_FLOAT, sizeof(Vertex), 0);
glNormalPointer (GL_FLOAT, sizeof(Vertex), (void*)offsetof(Vertex, normal));
glTexCoordPointer (2, GL_FLOAT, sizeof(Vertex), (void*)offsetof(Vertex, texcoords));
glColorPointer (4, GL_UNSIGNED_BYTE, sizeof(Vertex), (void*)offsetof(Vertex, color));
glColorPointer (4, GL_FLOAT, sizeof(Vertex), (void*)offsetof(Vertex, color));
m_r3dShader.SetShader(true);
@ -844,9 +844,9 @@ void CNew3D::CopyVertexData(const R3DPoly& r3dPoly, std::vector<Poly>& polyArray
//multiply face attributes with vertex attributes if required
for (int i = 0; i < 4; i++) {
p.p1.color[i] = (UINT8)(p.p1.color[i] * r3dPoly.faceColour[i]);
p.p2.color[i] = (UINT8)(p.p2.color[i] * r3dPoly.faceColour[i]);
p.p3.color[i] = (UINT8)(p.p3.color[i] * r3dPoly.faceColour[i]);
p.p1.color[i] = p.p1.color[i] * r3dPoly.faceColour[i];
p.p2.color[i] = p.p2.color[i] * r3dPoly.faceColour[i];
p.p3.color[i] = p.p3.color[i] * r3dPoly.faceColour[i];
}
polyArray.emplace_back(p);
@ -866,9 +866,9 @@ void CNew3D::CopyVertexData(const R3DPoly& r3dPoly, std::vector<Poly>& polyArray
//multiply face attributes with vertex attributes if required
for (int i = 0; i < 4; i++) {
p.p1.color[i] = (UINT8)(p.p1.color[i] * r3dPoly.faceColour[i]);
p.p2.color[i] = (UINT8)(p.p2.color[i] * r3dPoly.faceColour[i]);
p.p3.color[i] = (UINT8)(p.p3.color[i] * r3dPoly.faceColour[i]);
p.p1.color[i] = p.p1.color[i] * r3dPoly.faceColour[i];
p.p2.color[i] = p.p2.color[i] * r3dPoly.faceColour[i];
p.p3.color[i] = p.p3.color[i] * r3dPoly.faceColour[i];
}
polyArray.emplace_back(p);
@ -1083,18 +1083,19 @@ void CNew3D::CacheModel(Model *m, const UINT32 *data)
p.v[j].normal[2] = (INT8)(iz & 0xFF) / 128.f;
}
if (ph.FixedShading() && ph.LightEnabled()) {
UINT8 shade = (UINT8)((ix + 128) & 0xFF);
p.v[j].color[0] = shade; // hardware doesn't really have per vertex colours, only per poly
if (ph.FixedShading() && ph.TexEnabled() && !ph.SmoothShading()) { // fixed shading seems to be disabled if actual normals are set
float offset = !ph.LightEnabled() ? 1.f : 0.f; // if lighting is disabled colour seems to be an offset
float shade = (((ix + 128) & 0xFF) / 255.f) + offset;
p.v[j].color[0] = shade; // hardware doesn't really have per vertex colours, only per poly
p.v[j].color[1] = shade;
p.v[j].color[2] = shade;
p.v[j].color[3] = 255;
p.v[j].color[3] = 1;
}
else {
p.v[j].color[0] = 255;
p.v[j].color[1] = 255;
p.v[j].color[2] = 255;
p.v[j].color[3] = 255;
p.v[j].color[0] = 1;
p.v[j].color[1] = 1;
p.v[j].color[2] = 1;
p.v[j].color[3] = 1;
}
float texU, texV = 0;

View file

@ -15,6 +15,7 @@ static const char *vertexShaderBasic =
"varying float fsSpecularTerm;\n" // specular light term (additive)
"varying vec3 fsViewVertex;\n"
"varying vec3 fsViewNormal;\n" // per vertex normal vector
"varying vec4 fsColor;\n"
"void main(void)\n"
"{\n"
@ -23,7 +24,7 @@ static const char *vertexShaderBasic =
"float z = length(fsViewVertex);\n"
"fsFogFactor = fogIntensity * clamp(fogStart + z * fogDensity, 0.0, 1.0);\n"
"gl_FrontColor = gl_Color;\n"
"fsColor = gl_Color;\n"
"gl_TexCoord[0] = gl_MultiTexCoord0;\n"
"gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
"}\n";
@ -51,6 +52,7 @@ static const char *fragmentShaderBasic =
"varying float fsSpecularTerm;\n" // specular light term (additive)
"varying vec3 fsViewVertex;\n"
"varying vec3 fsViewNormal;\n" // per vertex normal vector
"varying vec4 fsColor;\n"
"void main()\n"
"{\n"
@ -82,7 +84,7 @@ static const char *fragmentShaderBasic =
"}\n"
"}\n"
"colData = gl_Color;\n"
"colData = fsColor;\n"
"finalData = tex1Data * colData;\n"
"if (finalData.a < (1.0/16.0)) {\n" // basically chuck out any totally transparent pixels value = 1/16 the smallest transparency level h/w supports
@ -120,17 +122,18 @@ static const char *fragmentShaderBasic =
"lightIntensity.rgb += (1.0 - insideSpot)*spotColor;\n"
"}\n"
"finalData.rgb *= lightIntensity;\n"
"if (sunFactor > 0.0 && specularCoefficient > 0.0) {\n"
"vec3 v = normalize(-fsViewVertex);\n"
"vec3 h = normalize(sunVector + v);\n" // halfway vector
"float nDotL = max(dot(fsViewNormal,sunVector),0.0);\n"
"float NdotHV = max(dot(fsViewNormal,h),0.0);\n"
"finalData.rgb += vec3(specularCoefficient * pow(nDotL,shininess));\n"
"finalData.rgb += vec3(specularCoefficient * pow(NdotHV,shininess));\n"
//"vec3 v = normalize(-fsViewVertex);\n"
//"vec3 h = normalize(sunVector + v);\n" // halfway vector
//"float NdotHV = max(dot(fsViewNormal,h),0.0);\n"
//"finalData.rgb += vec3(specularCoefficient * pow(NdotHV,shininess));\n"
"}\n"
"}\n"