From 6c1b4ba32b7263136cfd5238b01b3a54eb9b6bb3 Mon Sep 17 00:00:00 2001 From: Tony Wasserka Date: Sat, 29 Jun 2013 12:16:43 +0200 Subject: [PATCH] softgpu: Implement specular lighting. --- GPU/Software/Lighting.cpp | 57 +++++++++++++++++++++++++--------- GPU/Software/TransformUnit.cpp | 3 +- GPU/Software/TransformUnit.h | 3 +- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/GPU/Software/Lighting.cpp b/GPU/Software/Lighting.cpp index dfe4dbfa0e..3dd953f0ed 100644 --- a/GPU/Software/Lighting.cpp +++ b/GPU/Software/Lighting.cpp @@ -31,17 +31,15 @@ void Process(VertexData& vertex) Vec3 mac = (gstate.materialupdate&1) ? Vec3(gstate.getMaterialAmbientR(), gstate.getMaterialAmbientG(), gstate.getMaterialAmbientB()) : vertex.color0.rgb(); - vertex.color0.r() = mec.r() + mac.r() * gstate.getAmbientR()/255; - vertex.color0.g() = mec.g() + mac.g() * gstate.getAmbientG()/255; - vertex.color0.b() = mec.b() + mac.b() * gstate.getAmbientB()/255; - - int maa = (gstate.materialupdate&1) ? gstate.getMaterialAmbientA() : vertex.color0.a(); - vertex.color0.a() = gstate.getAmbientA() * maa / 255; + Vec3 final_color = mec + mac * Vec3(gstate.getAmbientR(), gstate.getAmbientG(), gstate.getAmbientB()) / 255; + Vec3 specular_color(0, 0, 0); for (unsigned int light = 0; light < 4; ++light) { if (!gstate.isLightChanEnabled(light)) continue; + // L = vector from vertex to light source + // TODO: Should transfer the light positions to world/view space for these calculations Vec3 L = Vec3(getFloat24(gstate.lpos[3*light]&0xFFFFFF), getFloat24(gstate.lpos[3*light+1]&0xFFFFFF),getFloat24(gstate.lpos[3*light+2]&0xFFFFFF)); L -= vertex.worldpos; float d = L.Length(); @@ -72,9 +70,9 @@ void Process(VertexData& vertex) // ambient lighting Vec3 lac = Vec3(gstate.getLightAmbientColorR(light), gstate.getLightAmbientColorG(light), gstate.getLightAmbientColorB(light)); - vertex.color0.r() += att * spot * lac.r() * mac.r() / 255; - vertex.color0.g() += att * spot * lac.g() * mac.g() / 255; - vertex.color0.b() += att * spot * lac.b() * mac.b() / 255; + final_color.r() += att * spot * lac.r() * mac.r() / 255; // TODO: Brackets + final_color.g() += att * spot * lac.g() * mac.g() / 255; + final_color.b() += att * spot * lac.b() * mac.b() / 255; // diffuse lighting Vec3 ldc = Vec3(gstate.getDiffuseColorR(light), gstate.getDiffuseColorG(light), gstate.getDiffuseColorB(light)); @@ -88,16 +86,47 @@ void Process(VertexData& vertex) diffuse_factor = pow(diffuse_factor, k); } - vertex.color0.r() += att * spot * ldc.r() * mdc.r() * diffuse_factor / 255; - vertex.color0.g() += att * spot * ldc.g() * mdc.g() * diffuse_factor / 255; - vertex.color0.b() += att * spot * ldc.b() * mdc.b() * diffuse_factor / 255; + // TODO: checking for non-negativity doesn't work? +// if (diffuse_factor > 0.f) { + final_color.r() += att * spot * ldc.r() * mdc.r() * diffuse_factor / 255; + final_color.g() += att * spot * ldc.g() * mdc.g() * diffuse_factor / 255; + final_color.b() += att * spot * ldc.b() * mdc.b() * diffuse_factor / 255; +// } + + if (gstate.isUsingSpecularLight(light)) { + Vec3 E(0.f, 0.f, 1.f); + Vec3 H = E / E.Length() + L / d; + Vec3 lsc = Vec3(gstate.getSpecularColorR(light), gstate.getSpecularColorG(light), gstate.getSpecularColorB(light)); + Vec3 msc = (gstate.materialupdate&4) + ? Vec3(gstate.getMaterialSpecularR(), gstate.getMaterialSpecularG(), gstate.getMaterialSpecularB()) + : vertex.color0.rgb(); + + float specular_factor = Dot(H,vertex.normal) / H.Length() / vertex.normal.Length(); + float k = getFloat24(gstate.materialspecularcoef&0xFFFFFF); + specular_factor = pow(specular_factor, k); + + specular_color.r() += att * spot * lsc.r() * msc.r() * specular_factor / 255; + specular_color.g() += att * spot * lsc.g() * msc.g() * specular_factor / 255; + specular_color.b() += att * spot * lsc.b() * msc.b() * specular_factor / 255; + } } - // Currently only implementing ambient+diffuse lighting, so secondary color is always zero anyway - //if (!gstate.isUsingSecondaryColor()) + vertex.color0.r() = final_color.r(); + vertex.color0.g() = final_color.g(); + vertex.color0.b() = final_color.b(); + + if (!gstate.isUsingSecondaryColor()) { + vertex.color1 = specular_color; + } else { + vertex.color0.r() += specular_color.r(); + vertex.color0.g() += specular_color.g(); + vertex.color0.b() += specular_color.b(); vertex.color1 = Vec3(0, 0, 0); } + + int maa = (gstate.materialupdate&1) ? gstate.getMaterialAmbientA() : vertex.color0.a(); + vertex.color0.a() = gstate.getAmbientA() * maa / 255; } } // namespace diff --git a/GPU/Software/TransformUnit.cpp b/GPU/Software/TransformUnit.cpp index 35925a6578..53c3e2524a 100644 --- a/GPU/Software/TransformUnit.cpp +++ b/GPU/Software/TransformUnit.cpp @@ -140,7 +140,8 @@ void TransformUnit::SubmitPrimitive(void* vertices, void* indices, u32 prim_type if (!gstate.isModeThrough()) { ModelCoords mcoords(pos[0], pos[1], pos[2]); data[i].worldpos = WorldCoords(TransformUnit::ModelToWorld(mcoords)); - data[i].clippos = ClipCoords(ClipCoords(TransformUnit::ViewToClip(TransformUnit::WorldToView(data[i].worldpos)))); + data[i].viewpos = TransformUnit::WorldToView(data[i].worldpos); + data[i].clippos = ClipCoords(TransformUnit::ViewToClip(data[i].viewpos)); data[i].drawpos = DrawingCoords(TransformUnit::ScreenToDrawing(TransformUnit::ClipToScreen(data[i].clippos))); Lighting::Process(data[i]); diff --git a/GPU/Software/TransformUnit.h b/GPU/Software/TransformUnit.h index 3d87617e8f..723437548e 100644 --- a/GPU/Software/TransformUnit.h +++ b/GPU/Software/TransformUnit.h @@ -44,7 +44,7 @@ struct VertexData #define LINTERP(T, OUT, IN) (OUT) + ((IN - OUT) * T) #define LINTERP_INT(T, OUT, IN) (OUT) + (((IN - OUT) * T) >> 8) - // World coords only needed for lighting, so we don't Lerp those + // World and view coords only needed for lighting, so we don't Lerp those clippos.x = LINTERP(t, a.clippos.x, b.clippos.x); clippos.y = LINTERP(t, a.clippos.y, b.clippos.y); @@ -76,6 +76,7 @@ struct VertexData } WorldCoords worldpos; // TODO: Storing this is dumb, should transform the light to clip space instead + ViewCoords viewpos; // TODO: Storing this is dumb, should transform the light to clip space instead ClipCoords clippos; DrawingCoords drawpos; // TODO: Shouldn't store this ? Vec2 texturecoords;