From 577cd77e2be089a1bf2284e33e9b07fc36a4320b Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 24 Dec 2009 12:14:51 +0100 Subject: Some optimizations --- shader/null.frag | 3 +++ shader/null.vert | 3 +++ src/Game.cpp | 22 +++++++++++++--------- src/Renderer.cpp | 28 ++++++++++++++++++++++++++++ src/Renderer.h | 13 +++++++------ src/ShadowVolume.cpp | 28 +--------------------------- src/ShadowVolume.h | 8 +++++--- 7 files changed, 60 insertions(+), 45 deletions(-) create mode 100644 shader/null.frag create mode 100644 shader/null.vert diff --git a/shader/null.frag b/shader/null.frag new file mode 100644 index 0000000..4238b22 --- /dev/null +++ b/shader/null.frag @@ -0,0 +1,3 @@ +void main() { + gl_FragColor = vec4(0, 0, 0, 1); +} diff --git a/shader/null.vert b/shader/null.vert new file mode 100644 index 0000000..fab2170 --- /dev/null +++ b/shader/null.vert @@ -0,0 +1,3 @@ +void main() { + gl_Position = ftransform(); +} 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::load("null.vert", "null.frag")); ambientShader = boost::shared_ptr(Shader::load("ambient.vert", "ambient.frag")); lightShader = boost::shared_ptr(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::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::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 ambientShader, lightShader; + boost::shared_ptr 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; -- cgit v1.2.3