diff options
-rw-r--r-- | CMakeLists.txt | 15 | ||||
-rw-r--r-- | src/BSPTree.cpp (renamed from BSPTree.cpp) | 0 | ||||
-rw-r--r-- | src/BSPTree.h (renamed from BSPTree.h) | 0 | ||||
-rw-r--r-- | src/CMakeLists.txt | 13 | ||||
-rw-r--r-- | src/Game.cpp (renamed from Game.cpp) | 13 | ||||
-rw-r--r-- | src/Game.h (renamed from Game.h) | 2 | ||||
-rw-r--r-- | src/Level.cpp (renamed from Level.cpp) | 0 | ||||
-rw-r--r-- | src/Level.h (renamed from Level.h) | 0 | ||||
-rw-r--r-- | src/Renderer.cpp (renamed from Renderer.cpp) | 42 | ||||
-rw-r--r-- | src/Renderer.h (renamed from Renderer.h) | 28 | ||||
-rw-r--r-- | src/Shader.cpp (renamed from Shader.cpp) | 0 | ||||
-rw-r--r-- | src/Shader.h (renamed from Shader.h) | 0 | ||||
-rw-r--r-- | src/Texture.cpp (renamed from Texture.cpp) | 0 | ||||
-rw-r--r-- | src/Texture.h (renamed from Texture.h) | 0 | ||||
-rw-r--r-- | src/Triangle.h (renamed from Triangle.h) | 0 | ||||
-rw-r--r-- | src/config.h (renamed from config.h) | 0 | ||||
-rw-r--r-- | src/gl.h (renamed from gl.h) | 0 | ||||
-rw-r--r-- | src/zoom.cpp (renamed from zoom.cpp) | 0 |
18 files changed, 68 insertions, 45 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 305c938..7a57452 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,17 +11,4 @@ find_package(LibXml2 REQUIRED) include_directories(${Boost_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR} ${GLEW_INCLUDE_DIR} ${GLPNG_INCLUDE_DIR} ${LIBXML2_INCLUDE_DIR}) -add_executable(zoom - BSPTree.cpp BSPTree.h - Game.cpp Game.h - config.h - gl.h - Level.cpp Level.h - Renderer.cpp Renderer.h - Shader.cpp Shader.h - Texture.cpp Texture.h - Triangle.h - zoom.cpp -) - -target_link_libraries(zoom ${Boost_LIBRARIES} ${OPENGL_LIBRARIES} ${GLEW_LIBRARY} ${GLPNG_LIBRARY} ${LIBXML2_LIBRARIES}) +add_subdirectory(src) diff --git a/BSPTree.cpp b/src/BSPTree.cpp index a94c77a..a94c77a 100644 --- a/BSPTree.cpp +++ b/src/BSPTree.cpp diff --git a/BSPTree.h b/src/BSPTree.h index 1944ef1..1944ef1 100644 --- a/BSPTree.h +++ b/src/BSPTree.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..e1c9378 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,13 @@ +add_executable(zoom + BSPTree.cpp BSPTree.h + Game.cpp Game.h + config.h + gl.h + Level.cpp Level.h + Renderer.cpp Renderer.h + Shader.cpp Shader.h + Texture.cpp Texture.h + Triangle.h + zoom.cpp +) +target_link_libraries(zoom ${Boost_LIBRARIES} ${OPENGL_LIBRARIES} ${GLEW_LIBRARY} ${GLPNG_LIBRARY} ${LIBXML2_LIBRARIES}) @@ -23,6 +23,7 @@ #include "Shader.h" #include "Triangle.h" #include "gl.h" +#include <algorithm> namespace Zoom { @@ -62,6 +63,8 @@ Game::Game(bool multisample) : angle(0), lightPos(0) { Shader::loadProgram("default.vert", "default.frag"); loadLevel("level.xml"); + triangles.insert(triangles.end(), level->getRooms().front().walls.begin(), level->getRooms().front().walls.end()); + std::sort(triangles.begin(), triangles.end(), Renderer::TextureSorter()); } bool Game::loadLevel(const std::string &name) { @@ -104,17 +107,21 @@ void Game::render() { light[2] = -8.0; } - glLightfv(GL_LIGHT0, GL_POSITION, light); + /*std::vector<BSPTree::TriangleRecord> triangles(level->getRooms().front().walls.begin(), + level->getRooms().front().walls.end()); + std::sort(triangles.begin(), triangles.end(), Renderer::TextureSorter());*/ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); - //glRotatef(10, 1, 2, 1); + glRotatef(15, 1, 2, 1); /*glRotatef(5*angle, 0, -1, 2); glRotatef(7*angle, 2, -1, 0); glRotatef(11*angle, 2, -1, 2);*/ - renderer.render(level->getRooms().front().walls); + glLightfv(GL_LIGHT0, GL_POSITION, light); + //renderer.render(std::list<BSPTree::TriangleRecord>(triangles.begin(), triangles.end())); + renderer.render(triangles); glPopMatrix(); } @@ -40,7 +40,9 @@ class Game { private: Renderer renderer; + boost::shared_ptr<Level> level; + std::vector<BSPTree::TriangleRecord> triangles; float angle; int lightPos; diff --git a/Level.cpp b/src/Level.cpp index 2867ab9..2867ab9 100644 --- a/Level.cpp +++ b/src/Level.cpp diff --git a/Renderer.cpp b/src/Renderer.cpp index 558e415..ad4849b 100644 --- a/Renderer.cpp +++ b/src/Renderer.cpp @@ -18,8 +18,6 @@ */ #include "Renderer.h" -#include "BSPTree.h" -#include "gl.h" namespace Zoom { @@ -37,33 +35,12 @@ void Renderer::render(const BSPTree &tree) { } -void Renderer::render(const std::list<BSPTree::TriangleRecord> &triangles) { - glBegin(GL_TRIANGLES); - for(std::list<BSPTree::TriangleRecord>::const_iterator t = triangles.begin(); t != triangles.end(); ++t) { - renderTriangle(t->triangle); - } - glEnd(); -} - void Renderer::renderTriangle(const Triangle &t) { glColor4fv(t.getColor().array); - if(t.getTexture() != lastTexture) { + if(t.getTexture() != activeTexture) { glEnd(); - - if(t.getTexture()) { - glBindTexture(GL_TEXTURE_2D, t.getTexture()); - - if(!lastTexture) { - glEnable(GL_TEXTURE_2D); - } - } - else { - glDisable(GL_TEXTURE_2D); - } - - lastTexture = t.getTexture(); - + useTexture(t.getTexture()); glBegin(GL_TRIANGLES); } @@ -74,4 +51,19 @@ void Renderer::renderTriangle(const Triangle &t) { } } +void Renderer::useTexture(unsigned texture) { + if(texture) { + glBindTexture(GL_TEXTURE_2D, texture); + + if(activeTexture) { + glEnable(GL_TEXTURE_2D); + } + } + else { + glDisable(GL_TEXTURE_2D); + } + + activeTexture = texture; +} + } diff --git a/Renderer.h b/src/Renderer.h index d72b5bb..fb98e38 100644 --- a/Renderer.h +++ b/src/Renderer.h @@ -21,18 +21,40 @@ #define ZOOM_RENDERER_H_ #include "BSPTree.h" +#include "gl.h" namespace Zoom { class Renderer { public: - Renderer() : lastTexture(0), renderVisitor(this) {} + Renderer() : activeTexture(0), renderVisitor(this) {} void render(const BSPTree &tree); - void render(const std::list<BSPTree::TriangleRecord> &triangles); + + template <typename T> + void render(const T &triangles) { + typename T::const_iterator t = triangles.begin(); + if(t == triangles.end()) + return; + + useTexture(t->triangle.getTexture()); + + glBegin(GL_TRIANGLES); + for(; t != triangles.end(); ++t) { + renderTriangle(t->triangle); + } + glEnd(); + } + + struct TextureSorter { + bool operator() (const BSPTree::TriangleRecord &t1, const BSPTree::TriangleRecord &t2) { + return (t1.triangle.getTexture() < t2.triangle.getTexture()); + } + }; private: void renderTriangle(const Triangle &t); + void useTexture(unsigned texture); class RenderVisitor { public: @@ -46,7 +68,7 @@ class Renderer { Renderer *renderer; }; - unsigned lastTexture; + unsigned activeTexture; const RenderVisitor renderVisitor; }; diff --git a/Shader.cpp b/src/Shader.cpp index 420b6c6..420b6c6 100644 --- a/Shader.cpp +++ b/src/Shader.cpp diff --git a/Texture.cpp b/src/Texture.cpp index d5ec1f2..d5ec1f2 100644 --- a/Texture.cpp +++ b/src/Texture.cpp diff --git a/Texture.h b/src/Texture.h index fd078f3..fd078f3 100644 --- a/Texture.h +++ b/src/Texture.h diff --git a/Triangle.h b/src/Triangle.h index 2b34c3f..2b34c3f 100644 --- a/Triangle.h +++ b/src/Triangle.h |