From a540d6b16743dad657fd2d6220efea51b56e1f1d Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 18 Dec 2009 15:06:04 +0100 Subject: ShadowVolume: Well, it's a beginning... --- levels/level.xml | 16 ++++++++++++++++ src/CMakeLists.txt | 1 + src/Game.cpp | 31 +++++++++++++++++++------------ src/ShadowVolume.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/ShadowVolume.h | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 132 insertions(+), 12 deletions(-) create mode 100644 src/ShadowVolume.cpp create mode 100644 src/ShadowVolume.h diff --git a/levels/level.xml b/levels/level.xml index 0cbc92f..e01914e 100644 --- a/levels/level.xml +++ b/levels/level.xml @@ -40,6 +40,22 @@ + + + + + + + + + + + + + + + + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6b56713..6ec57fc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,6 +7,7 @@ add_executable(zoom MathUtil.cpp MathUtil.h Renderer.cpp Renderer.h Shader.cpp Shader.h + ShadowVolume.cpp ShadowVolume.h Texture.cpp Texture.h Triangle.h zoom.cpp diff --git a/src/Game.cpp b/src/Game.cpp index 625d4d4..205125a 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -19,12 +19,13 @@ #include "Game.h" +#include "gl.h" #include "BSPTree.h" #include "Level.h" #include "Shader.h" +#include "ShadowVolume.h" #include "Triangle.h" -#include "gl.h" #include namespace Zoom { @@ -34,17 +35,16 @@ Game::Game(bool multisample) : playerPos(vmml::vec3f::ZERO), playerRotY(vmml::ma glClearColor(0.0, 0.0, 0.0, 1.0); glClearDepth(1.0); glEnable(GL_DEPTH_TEST); - //glDepthFunc(GL_LEQUAL); glEnable(GL_BLEND); - //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); #ifndef _WIN32 if(multisample) glEnable(GL_MULTISAMPLE_ARB); #endif - glEnable(GL_LIGHTING); + glLightModelfv(GL_LIGHT_MODEL_AMBIENT, vmml::vec4f(0.1, 0.1, 0.1, 1).array); + glLightfv(GL_LIGHT0, GL_AMBIENT, vmml::vec4f::ZERO.array); glLightfv(GL_LIGHT0, GL_DIFFUSE, vmml::vec4f::ONE.array); glLightfv(GL_LIGHT0, GL_SPECULAR, vmml::vec4f::ONE.array); @@ -142,17 +142,24 @@ void Game::render() { renderer.render(triangles); - /*glBegin(GL_LINES); + Shader::disable(); + glDepthFunc(GL_LEQUAL); + glBlendFunc(GL_ONE, GL_ZERO); - glColor3f(0, 0, 1); - glVertex4f(-1, -0.5, -2, 1); - glVertex4f(1, -0.5, -2, 1); + glBegin(GL_LINES); - glColor3f(1, 0, 0); - glVertex4f(0, -0.5, -2, 1); - glVertex4f(1, 0, 0, 0); + glColor3f(1, 1, 0); + + for(std::vector::iterator t = triangles.begin(); t != triangles.end(); ++t) { + ShadowVolume v(t->triangle, light); + + for(int i = 0; i < 3; ++i) { + glVertex3fv(v.getVertex(i).array); + glVertex3fv((v.getVertex(i)+v.getDirection(i)).array); + } + } - glEnd();*/ + glEnd(); } } diff --git a/src/ShadowVolume.cpp b/src/ShadowVolume.cpp new file mode 100644 index 0000000..b0c25ca --- /dev/null +++ b/src/ShadowVolume.cpp @@ -0,0 +1,45 @@ +/* + * ShadowVolume.cpp + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along + * with this program. If not, see . + */ + +#include "ShadowVolume.h" +#include "MathUtil.h" + +namespace Zoom { + +ShadowVolume::ShadowVolume(const Triangle &t, const vmml::vec3f &lightPos) { + MathUtil::Plane trianglePlane(t); + + for(int i = 0; i < 3; ++i) { + rays[i].p = t.getVertex(i); + } + + if(trianglePlane.isBehind(lightPos)) { + for(int i = 0; i < 3; ++i) { + rays[i].dir = vmml::vec3f::ZERO; + } + + return; + } + + for(int i = 0; i < 3; ++i) { + rays[i].dir = rays[i].p - lightPos; + } +} + +} diff --git a/src/ShadowVolume.h b/src/ShadowVolume.h new file mode 100644 index 0000000..e33e19c --- /dev/null +++ b/src/ShadowVolume.h @@ -0,0 +1,51 @@ +/* + * ShadowVolume.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along + * with this program. If not, see . + */ + +#ifndef ZOOM_SHADOWVOLUME_H_ +#define ZOOM_SHADOWVOLUME_H_ + +#include + +namespace Zoom { + +class Triangle; + +class ShadowVolume { + public: + ShadowVolume(const Triangle &t, const vmml::vec3f &lightPos); + virtual ~ShadowVolume() {} + + const vmml::vec3f& getVertex(int i) { + return rays[i].p; + } + + const vmml::vec3f& getDirection(int i) { + return rays[i].dir; + } + + private: + struct Ray { + vmml::vec3f p; + vmml::vec3f dir; + } rays[3]; +}; + +} + +#endif /* ZOOM_SHADOWVOLUME_H_ */ -- cgit v1.2.3