From 1a321ed999334f0d9f5255249ebeeed0278871d6 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 10 Dec 2009 09:08:16 +0100 Subject: Benutzte BSPTrees zum rendern --- BSPTree.cpp | 17 ++---------- DisplayClass.cpp | 79 +++++++++++++++++--------------------------------------- DisplayClass.h | 22 ++++++++++++---- main.cpp | 13 ++++++---- 4 files changed, 50 insertions(+), 81 deletions(-) diff --git a/BSPTree.cpp b/BSPTree.cpp index 1dbb6a9..e7e6416 100644 --- a/BSPTree.cpp +++ b/BSPTree.cpp @@ -1,17 +1,13 @@ #include "BSPTree.h" -#include + BSPTree::BSPTree(const std::list &triangles) : frontTree(0), backTree(0) { if(triangles.empty()) return; - vmml::vec3f center = findCenter(triangles); - std::cout << "Center at " << center << std::endl; - - const Triangle *planeT = findNearestTriangle(triangles, center); + const Triangle *planeT = findNearestTriangle(triangles, findCenter(triangles)); plane = Plane(*planeT); - std::cout << "The plane normal is " << plane.getNormal() << std::endl; std::list front, back; @@ -30,11 +26,6 @@ BSPTree::BSPTree(const std::list &triangles) : frontTree(0), backTree( } } - std::cout << "All in all: " << triangles.size() << " triangles" << std::endl; - std::cout << "Coplanar: " << this->triangles.size() << " triangles" << std::endl; - std::cout << "Front: " << front.size() << " triangles" << std::endl; - std::cout << "Back: " << back.size() << " triangles" << std::endl; - if(!front.empty()) frontTree = new BSPTree(front); @@ -71,9 +62,5 @@ const Triangle* BSPTree::findNearestTriangle(const std::list &triangle } } - vmml::vec3f tCenter = current->getCenter(); - std::cout << "Nearest triangle center at " << tCenter << std::endl; - std::cout << "DistanceSq is " << distanceSq << std::endl; - return current; } diff --git a/DisplayClass.cpp b/DisplayClass.cpp index b82b18e..206556e 100644 --- a/DisplayClass.cpp +++ b/DisplayClass.cpp @@ -4,35 +4,23 @@ #include "BSPTree.h" -void DisplayClass::RenderVisitor::operator() (const Triangle &t) const { - t.render(); -} +DisplayClass::OpaqueRenderer DisplayClass::opaqueRenderer; +DisplayClass::TransparentRenderer DisplayClass::transparentRenderer; DisplayClass::DisplayClass() { - static const float pos[5] = {-3.0, -2.0, 0.0, 2.0, 3.0}; - - for(int i = 0; i < 5; ++i) { - for(int j = 0; j < 5; ++j) { - for(int k = 0; k < 5; ++k) { - cubes[i][j][k].setSize(0.5, 0.5, 0.5); - cubes[i][j][k].setPos(pos[k], pos[j], pos[i]); - } - } - } + cubeing[0] = Trapezocube(11.0, 10.0, 10.0, 0.5, 0.0, 0.0, 1.75, 0); + cubeing[1] = Trapezocube(10.0, 9.0, 9.0, 0.5, 0.0, 0.0, 2.25, 90); + cubeing[2] = Trapezocube(9.0, 8.0, 8.0, 0.5, 0.0, 0.0, 2.75, 180); + cubeing[3] = Trapezocube(8.0, 7.0, 7.0, 0.5, 0.0, 0.0, 3.25, 270); + cubeing[4] = Trapezocube(7.0, 6.0, 6.0, 0.5, 0.0, 0.0, 3.75, 0); + cubeing[5] = Trapezocube(6.0, 5.0, 5.0, 0.5, 0.0, 0.0, 4.25, 90); + cubeing[6] = Trapezocube(5.0, 4.0, 4.0, 0.5, 0.0, 0.0, 4.75, 180); + cubeing[7] = Trapezocube(4.0, 3.0, 3.0, 0.5, 0.0, 0.0, 5.25, 270); } void DisplayClass::renderScene(unsigned long delta) { - //Cubehole cubeing(3.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.5); - Trapezocube cubeing1(11.0, 10.0, 10.0, 0.5, 0.0, 0.0, 1.75, 0); - Trapezocube cubeing2(10.0, 9.0, 9.0, 0.5, 0.0, 0.0, 2.25, 90); - Trapezocube cubeing3(9.0, 8.0, 8.0, 0.5, 0.0, 0.0, 2.75, 180); - Trapezocube cubeing4(8.0, 7.0, 7.0, 0.5, 0.0, 0.0, 3.25, 270); - Trapezocube cubeing5(7.0, 6.0, 6.0, 0.5, 0.0, 0.0, 3.75, 0); - Trapezocube cubeing6(6.0, 5.0, 5.0, 0.5, 0.0, 0.0, 4.25, 90); - Trapezocube cubeing7(5.0, 4.0, 4.0, 0.5, 0.0, 0.0, 4.75, 180); - Trapezocube cubeing8(4.0, 3.0, 3.0, 0.5, 0.0, 0.0, 5.25, 270); static float angle = 0.0; angle += delta*0.025; if(angle >= 360) @@ -40,9 +28,6 @@ void DisplayClass::renderScene(unsigned long delta) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - //Matrix p(GL_PROJECTION_MATRIX); - //Matrix m; - std::list triangles; glLoadIdentity(); // Clean up matrix @@ -52,42 +37,24 @@ void DisplayClass::renderScene(unsigned long delta) glRotatef(angle*3, 0.0, 0.0, 1.0); glRotatef(-angle*5, 1.0, 1.0, 1.0); - //m.store(); // Save current transformation - /*for(int i = 0; i < 5; ++i) { - for(int j = 0; j < 5; ++j) { - for(int k = 0; k < 5; ++k) { - std::list ct = cubes[i][j][k].getTriangles(m); - triangles.splice(triangles.end(), ct); - } - } - }*/ - std::list ct1 = cubeing1.getTriangles(); - triangles.splice(triangles.end(), ct1); - std::list ct2 = cubeing2.getTriangles(); - triangles.splice(triangles.end(), ct2); - std::list ct3 = cubeing3.getTriangles(); - triangles.splice(triangles.end(), ct3); - std::list ct4 = cubeing4.getTriangles(); - triangles.splice(triangles.end(), ct4); - std::list ct5 = cubeing5.getTriangles(); - triangles.splice(triangles.end(), ct5); - std::list ct6 = cubeing6.getTriangles(); - triangles.splice(triangles.end(), ct6); - std::list ct7 = cubeing7.getTriangles(); - triangles.splice(triangles.end(), ct7); - std::list ct8 = cubeing8.getTriangles(); - triangles.splice(triangles.end(), ct8); - - //glLoadIdentity(); + for(int i = 0; i < 8; ++i) { + std::list t = cubeing[i].getTriangles(); + triangles.splice(triangles.end(), t); + } BSPTree tree(triangles); + vmml::mat4f transform, inverseTransform; + glGetFloatv(GL_MODELVIEW_MATRIX, transform.array); + + transform.inverse(inverseTransform); + inverseTransform.set_translation(vmml::vec3f()); + vmml::vec3f viewVector = inverseTransform*vmml::vec3f(0, 0, -1); + glBegin(GL_TRIANGLES); - /*for(std::list::reverse_iterator t = triangles.rbegin(); t != triangles.rend(); ++t) { - t->render(); - }*/ - tree.visit(RenderVisitor(), vmml::vec3f(0, 0, -1)); + tree.visit(opaqueRenderer, -viewVector); + tree.visit(transparentRenderer, viewVector); glEnd(); glFlush(); diff --git a/DisplayClass.h b/DisplayClass.h index d297ca4..0341469 100644 --- a/DisplayClass.h +++ b/DisplayClass.h @@ -1,8 +1,7 @@ #ifndef _DISPLAYCLASS_H_ #define _DISPLAYCLASS_H_ -#include "Cuboid.h" -#include "Cubehole.h" +#include "Trapezocube.h" class DisplayClass { @@ -12,11 +11,24 @@ class DisplayClass void renderScene(unsigned long delta); private: - struct RenderVisitor { - void operator() (const Triangle &t) const; + struct OpaqueRenderer { + void operator() (const Triangle &t) const { + if(t.getColor().a() == 1.0) + t.render(); + } }; - Cuboid cubes[5][5][5]; + struct TransparentRenderer { + void operator() (const Triangle &t) const { + if(t.getColor().a() != 1.0) + t.render(); + } + }; + + static OpaqueRenderer opaqueRenderer; + static TransparentRenderer transparentRenderer; + + Trapezocube cubeing[8]; }; #endif /*_DISPLAYCLASS_H_*/ diff --git a/main.cpp b/main.cpp index b92c2d6..58309f1 100644 --- a/main.cpp +++ b/main.cpp @@ -23,8 +23,8 @@ void initGL(bool multisample) { glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); - //glEnable(GL_BLEND); - //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); #ifndef _WIN32 if(multisample) @@ -40,8 +40,8 @@ void initGL(bool multisample) { glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor); glEnable(GL_LIGHT0); - glEnable(GL_CULL_FACE); - glFrontFace(GL_CCW); + //glEnable(GL_CULL_FACE); + //glFrontFace(GL_CCW); } void resize(int width, int height) @@ -382,11 +382,13 @@ int main() { glXSwapBuffers(disp, wnd); XSync(disp, 0); + long slept = 0; gettimeofday(&tv, NULL); delta = ((tv.tv_usec + 1000000 - ticks)%1000000)/1000; if(delta < MIN_FRAME_DELTA) { usleep((MIN_FRAME_DELTA-delta)*1000); + slept += (MIN_FRAME_DELTA-delta); gettimeofday(&tv, NULL); delta = ((tv.tv_usec + 1000000 - ticks)%1000000)/1000; @@ -397,9 +399,10 @@ int main() { frames++; tocks += delta*1000; if(tocks > 1000000) { - std::cerr << frames << std::endl; + std::cerr << frames << " fps; slept a total of " << slept << " ms" << std::endl; frames = 0; tocks -= 1000000; + slept = 0; } } -- cgit v1.2.3