diff --git a/GPU/Software/Lighting.cpp b/GPU/Software/Lighting.cpp index e5f8b0fe2c..ff920c17fe 100644 --- a/GPU/Software/Lighting.cpp +++ b/GPU/Software/Lighting.cpp @@ -38,7 +38,25 @@ void Process(VertexData& vertex) float maa = (gstate.materialupdate&1) ? gstate.getMaterialAmbientA()/255.f : vertex.color0.a(); vertex.color0.a() = gstate.getAmbientA()/255.f * maa; - // Currently only implementing ambient lighting, so secondary color is always zero anyway + for (unsigned int light = 0; light < 4; ++light) { + if (!gstate.isLightChanEnabled(light)) + continue; + + Vec3 ldc = Vec3(gstate.getDiffuseColorR(light), gstate.getDiffuseColorG(light), gstate.getDiffuseColorB(light))/255.f; + Vec3 mdc = (gstate.materialupdate&2) + ? Vec3(gstate.getMaterialDiffuseR(), gstate.getMaterialDiffuseG(), gstate.getMaterialDiffuseB())/255.f + : vertex.color0.rgb(); + 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 factor = Dot(L,vertex.normal) / L.Length() / vertex.worldpos.Length(); + + vertex.color0.r() += ldc.r() * mdc.r() * factor; + vertex.color0.g() += ldc.g() * mdc.g() * factor; + vertex.color0.b() += ldc.b() * mdc.b() * factor; + } + + // Currently only implementing ambient+diffuse lighting, so secondary color is always zero anyway //if (!gstate.isUsingSecondaryColor()) { vertex.color1 = Vec3(0.f, 0.f, 0.f); diff --git a/GPU/Software/TransformUnit.cpp b/GPU/Software/TransformUnit.cpp index f8d7a1e863..113290c9fa 100644 --- a/GPU/Software/TransformUnit.cpp +++ b/GPU/Software/TransformUnit.cpp @@ -115,6 +115,12 @@ void TransformUnit::SubmitPrimitive(void* vertices, void* indices, u32 prim_type data[i].texturecoords = Vec2(uv[0], uv[1]); } + if (vreader.hasNormal()) { + float normal[3]; + vreader.ReadNrm(normal); + data[i].normal = Vec3(normal[0], normal[1], normal[2]); + } + if (vreader.hasColor0()) { float col[4]; vreader.ReadColor0(col); @@ -133,7 +139,8 @@ void TransformUnit::SubmitPrimitive(void* vertices, void* indices, u32 prim_type if (!gstate.isModeThrough()) { ModelCoords mcoords(pos[0], pos[1], pos[2]); - data[i].clippos = ClipCoords(ClipCoords(TransformUnit::ViewToClip(TransformUnit::WorldToView(TransformUnit::ModelToWorld(mcoords))))); + data[i].worldpos = WorldCoords(TransformUnit::ModelToWorld(mcoords)); + data[i].clippos = ClipCoords(ClipCoords(TransformUnit::ViewToClip(TransformUnit::WorldToView(data[i].worldpos)))); 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 95d9900ade..9e7c8163bb 100644 --- a/GPU/Software/TransformUnit.h +++ b/GPU/Software/TransformUnit.h @@ -43,6 +43,8 @@ struct VertexData { #define LINTERP(T, OUT, IN) (OUT) + ((IN - OUT) * T) + // World 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); clippos.z = LINTERP(t, a.clippos.z, b.clippos.z); @@ -54,6 +56,10 @@ struct VertexData texturecoords.x = LINTERP(t, a.texturecoords.x, b.texturecoords.x); texturecoords.y = LINTERP(t, a.texturecoords.y, b.texturecoords.y); + normal.x = LINTERP(t, a.normal.x, b.normal.x); + normal.y = LINTERP(t, a.normal.y, b.normal.y); + normal.z = LINTERP(t, a.normal.z, b.normal.z); + color0.x = LINTERP(t, a.color0.x, b.color0.x); color0.y = LINTERP(t, a.color0.y, b.color0.y); color0.z = LINTERP(t, a.color0.z, b.color0.z); @@ -64,9 +70,11 @@ struct VertexData color1.z = LINTERP(t, a.color1.z, b.color1.z); } + WorldCoords worldpos; // TODO: Storing this is dumb, should transform the light to clip space instead ClipCoords clippos; DrawingCoords drawpos; // TODO: Shouldn't store this ? Vec2 texturecoords; + Vec3 normal; Vec4 color0; // TODO: Should be an int vector! Vec3 color1; // TODO: Should be an int vector! };