aboutsummaryrefslogtreecommitdiff
path: root/externals/g3dlite/G3D.lib/source/GLight.cpp
diff options
context:
space:
mode:
authorXanadu <none@none>2010-07-17 22:58:24 +0200
committerXanadu <none@none>2010-07-17 22:58:24 +0200
commitb5f8bfd66561e4a63fa8c28961b829a35ceb2fb0 (patch)
tree6691fda7c0985077aeb6ff3a93e829447dddd736 /externals/g3dlite/G3D.lib/source/GLight.cpp
parentec244dbe366e84a93c8fa1ef294af4a2e4e3b0b1 (diff)
parentdc510c9a143de1977daedea0aefb9589c01adde2 (diff)
Merge
--HG-- branch : trunk
Diffstat (limited to 'externals/g3dlite/G3D.lib/source/GLight.cpp')
-rw-r--r--externals/g3dlite/G3D.lib/source/GLight.cpp143
1 files changed, 143 insertions, 0 deletions
diff --git a/externals/g3dlite/G3D.lib/source/GLight.cpp b/externals/g3dlite/G3D.lib/source/GLight.cpp
new file mode 100644
index 00000000000..8acb066ef54
--- /dev/null
+++ b/externals/g3dlite/G3D.lib/source/GLight.cpp
@@ -0,0 +1,143 @@
+/**
+ @file GLight.cpp
+
+ @maintainer Morgan McGuire, matrix@graphics3d.com
+
+ @created 2003-11-12
+ @edited 2007-10-22
+*/
+
+#include "G3D/GLight.h"
+#include "G3D/Sphere.h"
+
+namespace G3D {
+
+GLight::GLight() {
+ position = Vector4(0, 0, 0, 0);
+ color = Color3::white();
+ spotDirection = Vector3(0, 0, -1);
+ spotCutoff = 180;
+ enabled = false;
+ attenuation[0] = 1.0;
+ attenuation[1] = 0.0;
+ attenuation[2] = 0.0;
+ specular = true;
+ diffuse = true;
+}
+
+
+GLight GLight::directional(const Vector3& toLight, const Color3& color, bool s, bool d) {
+ GLight L;
+ L.position = Vector4(toLight.direction(), 0);
+ L.color = color;
+ L.specular = s;
+ L.diffuse = d;
+ return L;
+}
+
+
+GLight GLight::point(const Vector3& pos, const Color3& color, float constAtt, float linAtt, float quadAtt, bool s, bool d) {
+ GLight L;
+ L.position = Vector4(pos, 1);
+ L.color = color;
+ L.attenuation[0] = constAtt;
+ L.attenuation[1] = linAtt;
+ L.attenuation[2] = quadAtt;
+ L.specular = s;
+ L.diffuse = d;
+ return L;
+}
+
+
+GLight GLight::spot(const Vector3& pos, const Vector3& pointDirection, float cutOffAngleDegrees, const Color3& color, float constAtt, float linAtt, float quadAtt, bool s, bool d) {
+ GLight L;
+ L.position = Vector4(pos, 1.0f);
+ L.spotDirection = pointDirection.direction();
+ debugAssert(cutOffAngleDegrees <= 90);
+ L.spotCutoff = cutOffAngleDegrees;
+ L.color = color;
+ L.attenuation[0] = constAtt;
+ L.attenuation[1] = linAtt;
+ L.attenuation[2] = quadAtt;
+ L.specular = s;
+ L.diffuse = d;
+ return L;
+}
+
+
+bool GLight::operator==(const GLight& other) const {
+ return (position == other.position) &&
+ (spotDirection == other.spotDirection) &&
+ (spotCutoff == other.spotCutoff) &&
+ (attenuation[0] == other.attenuation[0]) &&
+ (attenuation[1] == other.attenuation[1]) &&
+ (attenuation[2] == other.attenuation[2]) &&
+ (color == other.color) &&
+ (enabled == other.enabled) &&
+ (specular == other.specular) &&
+ (diffuse == other.diffuse);
+}
+
+bool GLight::operator!=(const GLight& other) const {
+ return !(*this == other);
+}
+
+
+Sphere GLight::effectSphere(float cutoff) const {
+ if (position.w == 0) {
+ // Directional light
+ return Sphere(Vector3::zero(), (float)inf());
+ } else {
+ // Avoid divide by zero
+ cutoff = max(cutoff, 0.0001f);
+ float maxIntensity = max(color.r, max(color.g, color.b));
+
+ float radius = (float)inf();
+
+ if (attenuation[2] != 0) {
+
+ // Solve I / attenuation.dot(1, r, r^2) < cutoff for r
+ //
+ // a[0] + a[1] r + a[2] r^2 > I/cutoff
+ //
+
+ float a = attenuation[2];
+ float b = attenuation[1];
+ float c = attenuation[0] - maxIntensity / cutoff;
+
+ float discrim = square(b) - 4 * a * c;
+
+ if (discrim >= 0) {
+ discrim = sqrt(discrim);
+
+ float r1 = (-b + discrim) / (2 * a);
+ float r2 = (-b - discrim) / (2 * a);
+
+ if (r1 < 0) {
+ if (r2 > 0) {
+ radius = r2;
+ }
+ } else if (r2 > 0) {
+ radius = min(r1, r2);
+ } else {
+ radius = r1;
+ }
+ }
+
+ } else if (attenuation[1] != 0) {
+
+ // Solve I / attenuation.dot(1, r) < cutoff for r
+ //
+ // r * a[1] + a[0] = I / cutoff
+ // r = (I / cutoff - a[0]) / a[1]
+
+ float radius = (maxIntensity / cutoff - attenuation[0]) / attenuation[1];
+ radius = max(radius, 0.0f);
+ }
+
+ return Sphere(position.xyz(), radius);
+
+ }
+}
+
+}