From 1450157e09d8a55089fe19ef5f078a83478845dc Mon Sep 17 00:00:00 2001 From: Tony Wasserka Date: Wed, 24 Jul 2013 14:35:52 +0200 Subject: [PATCH] softgpu: Implement environment mapping. --- GPU/Software/Lighting.cpp | 14 ++++++++++++++ GPU/Software/Rasterizer.cpp | 9 +++++++++ 2 files changed, 23 insertions(+) diff --git a/GPU/Software/Lighting.cpp b/GPU/Software/Lighting.cpp index 0ebd7d57ff..ef5f1ee066 100644 --- a/GPU/Software/Lighting.cpp +++ b/GPU/Software/Lighting.cpp @@ -35,6 +35,20 @@ void Process(VertexData& vertex) Vec3 specular_color(0, 0, 0); for (unsigned int light = 0; light < 4; ++light) { + // Always calculate texture coords from lighting results if environment mapping is active + // TODO: specular lighting should affect this, too! + if (gstate.getUVGenMode() == GE_TEXMAP_ENVIRONMENT_MAP) { + Vec3 L = Vec3(getFloat24(gstate.lpos[3*light]&0xFFFFFF), getFloat24(gstate.lpos[3*light+1]&0xFFFFFF),getFloat24(gstate.lpos[3*light+2]&0xFFFFFF)); + float diffuse_factor = Dot(L,vertex.worldnormal) / L.Length() / vertex.worldnormal.Length(); + + if (gstate.getUVLS0() == light) + vertex.texturecoords.s() = (diffuse_factor + 1.f) / 2.f; + + if (gstate.getUVLS1() == light) + vertex.texturecoords.t() = (diffuse_factor + 1.f) / 2.f; + } + + // Skip other calculations if light chan is disabled if (!gstate.isLightChanEnabled(light)) continue; diff --git a/GPU/Software/Rasterizer.cpp b/GPU/Software/Rasterizer.cpp index 5be3b4356c..a86e21c153 100644 --- a/GPU/Software/Rasterizer.cpp +++ b/GPU/Software/Rasterizer.cpp @@ -472,6 +472,15 @@ void DrawTriangle(const VertexData& v0, const VertexData& v1, const VertexData& Vec3 stq = tgen * source + Vec3(gstate.tgenMatrix[9], gstate.tgenMatrix[10], gstate.tgenMatrix[11]); uv_map(0, stq.x/stq.z, stq.y/stq.z, u, v); + } else if (gstate.getUVGenMode() == GE_TEXMAP_ENVIRONMENT_MAP) { + // environment mapping - ST coordinates are calculated during Lighting + float q0 = 1.f / v0.clippos.w; + float q1 = 1.f / v1.clippos.w; + float q2 = 1.f / v2.clippos.w; + float q = q0 * w0 + q1 * w1 + q2 * w2; + float s = (v0.texturecoords.s() * q0 * w0 + v1.texturecoords.s() * q1 * w1 + v2.texturecoords.s() * q2 * w2) / q; + float t = (v0.texturecoords.t() * q0 * w0 + v1.texturecoords.t() * q1 * w1 + v2.texturecoords.t() * q2 * w2) / q; + uv_map(0, s, t, u, v); } else { ERROR_LOG(G3D, "Unsupported texture mapping mode %x!", gstate.getUVGenMode()); }