summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2009-12-24 12:14:51 +0100
committerMatthias Schiffer <matthias@gamezock.de>2009-12-24 12:14:51 +0100
commit577cd77e2be089a1bf2284e33e9b07fc36a4320b (patch)
tree2f15ea093a0d7936b1d07b5393729da0afc224f9 /src
parent0f6fb2617e5234480994f954125f5c45d6d29e0b (diff)
downloadzoom++-577cd77e2be089a1bf2284e33e9b07fc36a4320b.tar
zoom++-577cd77e2be089a1bf2284e33e9b07fc36a4320b.zip
Some optimizations
Diffstat (limited to 'src')
-rw-r--r--src/Game.cpp22
-rw-r--r--src/Renderer.cpp28
-rw-r--r--src/Renderer.h13
-rw-r--r--src/ShadowVolume.cpp28
-rw-r--r--src/ShadowVolume.h8
5 files changed, 54 insertions, 45 deletions
diff --git a/src/Game.cpp b/src/Game.cpp
index e629aca..604eb64 100644
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -94,7 +94,7 @@ void Game::turn(float x, float y) {
void Game::run(int delta) {
lightPos += delta*0.5;
- lightPos = std::fmod(lightPos, 24000);
+ lightPos = std::fmod(lightPos, 26000);
vmml::vec3f playerMove(vmml::vec3f::ZERO);
@@ -119,20 +119,24 @@ void Game::render() {
int i;
vmml::vec3f light(vmml::vec3f::ZERO);
- if(lightPos < 12000) i = lightPos;
- else i = 24000 - lightPos;
+ if(lightPos < 13000) i = lightPos;
+ else i = 26000 - lightPos;
- if(i < 4000) {
+ if(i < 500) {
light.x() = 0.0;
- light.z() = -i * 0.001;
+ light.z() = 0.0;
}
- else if(i < 8000) {
- light.x() = (i-4000) * 0.001;
+ else if(i < 4500) {
+ light.x() = 0.0;
+ light.z() = -(i-500) * 0.001;
+ }
+ else if(i < 8500) {
+ light.x() = (i-4500) * 0.001;
light.z() = -4.0;
}
- else if(i < 12000) {
+ else if(i < 12500) {
light.x() = 4.0;
- light.z() = -4.0 - (i-8000) * 0.001;
+ light.z() = -4.0 - (i-8500) * 0.001;
}
else {
light.x() = 4.0;
diff --git a/src/Renderer.cpp b/src/Renderer.cpp
index 8aaf9c5..b02c5ff 100644
--- a/src/Renderer.cpp
+++ b/src/Renderer.cpp
@@ -23,6 +23,7 @@
namespace Zoom {
Renderer::Renderer() : activeTexture(0), renderVisitor(this) {
+ nullShader = boost::shared_ptr<Shader>(Shader::load("null.vert", "null.frag"));
ambientShader = boost::shared_ptr<Shader>(Shader::load("ambient.vert", "ambient.frag"));
lightShader = boost::shared_ptr<Shader>(Shader::load("light.vert", "light.frag"));
}
@@ -57,6 +58,33 @@ void Renderer::renderTriangle(const Triangle &t) {
}
}
+void Renderer::renderShadowVolume(const ShadowVolume &v) {
+ if(!v.isVisible())
+ return;
+
+ glVertex3fv(v.getVertex(2).array);
+ glVertex3fv(v.getVertex(1).array);
+ glVertex3fv(v.getVertex(0).array);
+
+ for(int i = 0; i < 3; ++i) {
+ const vmml::vec3f &p1 = v.getVertex(i), &p2 = v.getVertex((i+1)%3);
+ const vmml::vec4f &dir1 = vmml::vec4f(v.getDirection(i), 0);
+ const vmml::vec4f &dir2 = vmml::vec4f(v.getDirection((i+1)%3), 0);
+
+ glVertex3fv(p1.array);
+ glVertex3fv(p2.array);
+ glVertex4fv(dir1.array);
+
+ glVertex4fv(dir1.array);
+ glVertex3fv(p2.array);
+ glVertex4fv(dir2.array);
+ }
+
+ glVertex4fv(vmml::vec4f(v.getDirection(0), 0).array);
+ glVertex4fv(vmml::vec4f(v.getDirection(1), 0).array);
+ glVertex4fv(vmml::vec4f(v.getDirection(2), 0).array);
+}
+
void Renderer::useTexture(unsigned texture) {
if(texture) {
glBindTexture(GL_TEXTURE_2D, texture);
diff --git a/src/Renderer.h b/src/Renderer.h
index 1b6f91b..8072158 100644
--- a/src/Renderer.h
+++ b/src/Renderer.h
@@ -65,31 +65,31 @@ class Renderer {
glEnd();
// Render shadow volumes
+ Shader::enable(nullShader);
glClear(GL_STENCIL_BUFFER_BIT);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
- glFrontFace(GL_CCW);
+ glCullFace(GL_FRONT);
glStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
glBegin(GL_TRIANGLES);
for(std::vector<ShadowVolume>::iterator v = shadowVolumes.begin(); v != shadowVolumes.end(); ++v) {
- v->render();
+ renderShadowVolume(*v);
}
glEnd();
- glFrontFace(GL_CW);
+ glCullFace(GL_BACK);
glStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
glBegin(GL_TRIANGLES);
for(std::vector<ShadowVolume>::iterator v = shadowVolumes.begin(); v != shadowVolumes.end(); ++v) {
- v->render();
+ renderShadowVolume(*v);
}
glEnd();
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
- glFrontFace(GL_CCW);
// Render with point light
Shader::enable(lightShader);
@@ -115,6 +115,7 @@ class Renderer {
private:
void renderTriangle(const Triangle &t);
+ static void renderShadowVolume(const ShadowVolume &v);
void useTexture(unsigned texture);
class RenderVisitor {
@@ -129,7 +130,7 @@ class Renderer {
Renderer *renderer;
};
- boost::shared_ptr<Shader> ambientShader, lightShader;
+ boost::shared_ptr<Shader> nullShader, ambientShader, lightShader;
unsigned activeTexture;
const RenderVisitor renderVisitor;
diff --git a/src/ShadowVolume.cpp b/src/ShadowVolume.cpp
index 67a44db..27aba6e 100644
--- a/src/ShadowVolume.cpp
+++ b/src/ShadowVolume.cpp
@@ -31,7 +31,7 @@ ShadowVolume::ShadowVolume(const Triangle &t, const vmml::vec3f &lightPos) : vis
rays[i].p = t.getVertex(i);
}
- if(trianglePlane.isInFront(lightPos)) {
+ if(!trianglePlane.isBehind(lightPos)) {
visible = false;
return;
@@ -42,30 +42,4 @@ ShadowVolume::ShadowVolume(const Triangle &t, const vmml::vec3f &lightPos) : vis
}
}
-void ShadowVolume::render() const {
- if(!visible)
- return;
-
- glVertex3fv(rays[0].p.array);
- glVertex3fv(rays[1].p.array);
- glVertex3fv(rays[2].p.array);
-
- for(int i = 0; i < 3; ++i) {
- const Ray &r1 = rays[i];
- const Ray &r2 = rays[(i+1)%3];
-
- glVertex3fv(r1.p.array);
- glVertex4fv(vmml::vec4f(r1.dir, 0).array);
- glVertex3fv(r2.p.array);
-
- glVertex3fv(r2.p.array);
- glVertex4fv(vmml::vec4f(r1.dir, 0).array);
- glVertex4fv(vmml::vec4f(r2.dir, 0).array);
- }
-
- glVertex4fv(vmml::vec4f(rays[2].dir, 0).array);
- glVertex4fv(vmml::vec4f(rays[1].dir, 0).array);
- glVertex4fv(vmml::vec4f(rays[0].dir, 0).array);
-}
-
}
diff --git a/src/ShadowVolume.h b/src/ShadowVolume.h
index 81a0a33..445e7ba 100644
--- a/src/ShadowVolume.h
+++ b/src/ShadowVolume.h
@@ -32,15 +32,17 @@ class ShadowVolume {
ShadowVolume(const Triangle &t, const vmml::vec3f &lightPos);
virtual ~ShadowVolume() {}
- const vmml::vec3f& getVertex(int i) {
+ const vmml::vec3f& getVertex(int i) const {
return rays[i].p;
}
- const vmml::vec3f& getDirection(int i) {
+ const vmml::vec3f& getDirection(int i) const {
return rays[i].dir;
}
- void render() const;
+ bool isVisible() const {
+ return visible;
+ }
private:
bool visible;