Simplify lighting clamping in softgpu.

This commit is contained in:
Unknown W. Brackets 2013-10-05 13:05:32 -07:00
parent 013e3f3be2
commit eae6e87620
2 changed files with 29 additions and 17 deletions

View File

@ -19,6 +19,17 @@
#include <cmath>
// Helper for Vec classes to clamp values.
template<typename T>
inline static T VecClamp(const T &v, const T &low, const T &high)
{
if (v > high)
return high;
if (v < low)
return low;
return v;
}
template<typename T>
class Vec2
{
@ -100,6 +111,11 @@ public:
return x*x + y*y;
}
Vec2 Clamp(const T &l, const T &h) const
{
return Vec2(VecClamp(x, l, h), VecClamp(y, l, h));
}
// Only implemented for T=float
float Length() const;
void SetLength(const float l);
@ -232,6 +248,11 @@ public:
return x*x + y*y + z*z;
}
Vec3 Clamp(const T &l, const T &h) const
{
return Vec3(VecClamp(x, l, h), VecClamp(y, l, h), VecClamp(z, l, h));
}
// Only implemented for T=float
float Length() const;
void SetLength(const float l);
@ -393,6 +414,11 @@ public:
return x*x + y*y + z*z + w*w;
}
Vec4 Clamp(const T &l, const T &h) const
{
return Vec4(VecClamp(x, l, h), VecClamp(y, l, h), VecClamp(z, l, h), VecClamp(w, l, h));
}
// Only implemented for T=float
float Length() const;
void SetLength(const float l);

View File

@ -135,9 +135,8 @@ void Process(VertexData& vertex)
vertex.color0.g() = final_color.g();
vertex.color0.b() = final_color.b();
if (gstate.isUsingSecondaryColor())
{
vertex.color1 = specular_color;
if (gstate.isUsingSecondaryColor()) {
vertex.color1 = specular_color.Clamp(0, 255);
} else {
vertex.color0.r() += specular_color.r();
vertex.color0.g() += specular_color.g();
@ -148,20 +147,7 @@ void Process(VertexData& vertex)
int maa = (gstate.materialupdate&1) ? vertex.color0.a() : gstate.getMaterialAmbientA();
vertex.color0.a() = gstate.getAmbientA() * maa / 255;
if (vertex.color0.r() > 255) vertex.color0.r() = 255;
if (vertex.color0.g() > 255) vertex.color0.g() = 255;
if (vertex.color0.b() > 255) vertex.color0.b() = 255;
if (vertex.color0.a() > 255) vertex.color0.a() = 255;
if (vertex.color1.r() > 255) vertex.color1.r() = 255;
if (vertex.color1.g() > 255) vertex.color1.g() = 255;
if (vertex.color1.b() > 255) vertex.color1.b() = 255;
if (vertex.color0.r() < 0) vertex.color0.r() = 0;
if (vertex.color0.g() < 0) vertex.color0.g() = 0;
if (vertex.color0.b() < 0) vertex.color0.b() = 0;
if (vertex.color0.a() < 0) vertex.color0.a() = 0;
if (vertex.color1.r() < 0) vertex.color1.r() = 0;
if (vertex.color1.g() < 0) vertex.color1.g() = 0;
if (vertex.color1.b() < 0) vertex.color1.b() = 0;
vertex.color0 = vertex.color0.Clamp(0, 255);
}
} // namespace