Refactored Ray

This commit is contained in:
Matthias Schiffer 2009-12-24 23:17:51 +01:00
parent c883f50f90
commit d1c909ca57
4 changed files with 32 additions and 26 deletions

View file

@ -87,6 +87,24 @@ class MathUtil {
float d; float d;
}; };
class Ray {
public:
Ray() : vertex(vmml::vec3f::ZERO), dir(vmml::vec3f::ZERO) {}
Ray(const vmml::vec3f &v, const vmml::vec3f &d) : vertex(v), dir(d) {}
const vmml::vec3f& getVertex() const {
return vertex;
}
const vmml::vec3f& getDirection() const {
return dir;
}
private:
vmml::vec3f vertex;
vmml::vec3f dir;
};
static vmml::mat4f perspective(float fovy, float aspect, float zNear); static vmml::mat4f perspective(float fovy, float aspect, float zNear);
private: private:

View file

@ -62,14 +62,14 @@ void Renderer::renderShadowVolume(const ShadowVolume &v) {
if(!v.isVisible()) if(!v.isVisible())
return; return;
glVertex3fv(v.getVertex(2).array); glVertex3fv(v.getRay(2).getVertex().array);
glVertex3fv(v.getVertex(1).array); glVertex3fv(v.getRay(1).getVertex().array);
glVertex3fv(v.getVertex(0).array); glVertex3fv(v.getRay(0).getVertex().array);
for(int i = 0; i < 3; ++i) { for(int i = 0; i < 3; ++i) {
const vmml::vec3f &p1 = v.getVertex(i), &p2 = v.getVertex((i+1)%3); const vmml::vec3f &p1 = v.getRay(i).getVertex(), &p2 = v.getRay((i+1)%3).getVertex();
vmml::vec4f dir1 = vmml::vec4f(v.getDirection(i), 0); vmml::vec4f dir1 = vmml::vec4f(v.getRay(i).getDirection(), 0);
vmml::vec4f dir2 = vmml::vec4f(v.getDirection((i+1)%3), 0); vmml::vec4f dir2 = vmml::vec4f(v.getRay((i+1)%3).getDirection(), 0);
glVertex3fv(p1.array); glVertex3fv(p1.array);
glVertex3fv(p2.array); glVertex3fv(p2.array);
@ -80,9 +80,9 @@ void Renderer::renderShadowVolume(const ShadowVolume &v) {
glVertex4fv(dir2.array); glVertex4fv(dir2.array);
} }
glVertex4fv(vmml::vec4f(v.getDirection(0), 0).array); glVertex4fv(vmml::vec4f(v.getRay(0).getDirection(), 0).array);
glVertex4fv(vmml::vec4f(v.getDirection(1), 0).array); glVertex4fv(vmml::vec4f(v.getRay(1).getDirection(), 0).array);
glVertex4fv(vmml::vec4f(v.getDirection(2), 0).array); glVertex4fv(vmml::vec4f(v.getRay(2).getDirection(), 0).array);
} }
void Renderer::useTexture(unsigned texture) { void Renderer::useTexture(unsigned texture) {

View file

@ -27,10 +27,6 @@ namespace Zoom {
ShadowVolume::ShadowVolume(const Triangle &t, const vmml::vec3f &lightPos) : visible(true) { ShadowVolume::ShadowVolume(const Triangle &t, const vmml::vec3f &lightPos) : visible(true) {
MathUtil::Plane trianglePlane(t); MathUtil::Plane trianglePlane(t);
for(int i = 0; i < 3; ++i) {
rays[i].p = t.getVertex(i);
}
if(!trianglePlane.isBehind(lightPos)) { if(!trianglePlane.isBehind(lightPos)) {
visible = false; visible = false;
@ -38,7 +34,7 @@ ShadowVolume::ShadowVolume(const Triangle &t, const vmml::vec3f &lightPos) : vis
} }
for(int i = 0; i < 3; ++i) { for(int i = 0; i < 3; ++i) {
rays[i].dir = rays[i].p - lightPos; rays[i] = MathUtil::Ray(t.getVertex(i), t.getVertex(i) - lightPos);
} }
} }

View file

@ -20,8 +20,7 @@
#ifndef ZOOM_SHADOWVOLUME_H_ #ifndef ZOOM_SHADOWVOLUME_H_
#define ZOOM_SHADOWVOLUME_H_ #define ZOOM_SHADOWVOLUME_H_
#include <vmmlib/vector.hpp> #include "MathUtil.h"
#include <vmmlib/matrix.hpp>
namespace Zoom { namespace Zoom {
@ -32,12 +31,8 @@ class ShadowVolume {
ShadowVolume(const Triangle &t, const vmml::vec3f &lightPos); ShadowVolume(const Triangle &t, const vmml::vec3f &lightPos);
virtual ~ShadowVolume() {} virtual ~ShadowVolume() {}
const vmml::vec3f& getVertex(int i) const { const MathUtil::Ray& getRay(int i) const {
return rays[i].p; return rays[i];
}
const vmml::vec3f& getDirection(int i) const {
return rays[i].dir;
} }
bool isVisible() const { bool isVisible() const {
@ -47,10 +42,7 @@ class ShadowVolume {
private: private:
bool visible; bool visible;
struct Ray { MathUtil::Ray rays[3];
vmml::vec3f p;
vmml::vec3f dir;
} rays[3];
}; };
} }