From bc797d4c8b5f8a7ac925ca8478dc5960907a81dd Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 17 Dec 2009 02:14:32 +0100 Subject: Moved Plane to MathUtil; some other tweaks --- src/BSPTree.cpp | 27 ++++++++++------------- src/BSPTree.h | 66 +++++--------------------------------------------------- src/Game.cpp | 17 ++++++++++++++- src/Game.h | 5 ++--- src/MathUtil.cpp | 13 +++++++++-- src/MathUtil.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/zoom.cpp | 9 +++----- 7 files changed, 109 insertions(+), 89 deletions(-) diff --git a/src/BSPTree.cpp b/src/BSPTree.cpp index a94c77a..91efb6d 100644 --- a/src/BSPTree.cpp +++ b/src/BSPTree.cpp @@ -21,20 +21,15 @@ namespace Zoom { -vmml::vec3f BSPTree::Plane::intersection(const vmml::vec3f &p, const vmml::vec3f &dir) const { - float r = (d - p.dot(normal))/dir.dot(normal); - return p + r*dir; -} - -void BSPTree::Plane::partition(const TriangleRecord &t, std::list *front, std::list *back) const { +void BSPTree::partition(const TriangleRecord &t, std::list *front, std::list *back) const { for(int i = 0; i < 3; ++i) { - if(contains(t.triangle.getVertex(i))) { + if(plane.contains(t.triangle.getVertex(i))) { const vmml::vec3f *v[3] = {&t.triangle.getVertex(i), &t.triangle.getVertex((i+1)%3), &t.triangle.getVertex((i+2)%3)}; - vmml::vec3f is = intersection(*v[1], *v[2]-*v[1]); + vmml::vec3f is = plane.intersection(*v[1], *v[2]-*v[1]); - if(isInFront(*v[1])) { + if(plane.isInFront(*v[1])) { front->push_back(TriangleRecord(Triangle(*v[0], *v[1], is, t.triangle.getColor()), t.data)); back->push_back(TriangleRecord(Triangle(*v[0], is, *v[2], t.triangle.getColor()), t.data)); } @@ -50,12 +45,12 @@ void BSPTree::Plane::partition(const TriangleRecord &t, std::listpush_back(TriangleRecord(Triangle(*v[0], is1, is2, t.triangle.getColor()), t.data)); back->push_back(TriangleRecord(Triangle(is1, *v[1], is2, t.triangle.getColor()), t.data)); back->push_back(TriangleRecord(Triangle(*v[1], *v[2], is2, t.triangle.getColor()), t.data)); @@ -78,7 +73,7 @@ BSPTree::BSPTree(const std::list &triangles) : frontTree(0), bac if(!planeT) return; - plane = Plane(*planeT); + plane = MathUtil::Plane(*planeT); std::list front, back; @@ -99,7 +94,7 @@ BSPTree::BSPTree(const std::list &triangles) : frontTree(0), bac continue; } - plane.partition(*t, &front, &back); + partition(*t, &front, &back); } if(!front.empty()) diff --git a/src/BSPTree.h b/src/BSPTree.h index 1944ef1..e5015d0 100644 --- a/src/BSPTree.h +++ b/src/BSPTree.h @@ -21,6 +21,8 @@ #define ZOOM_BSPTREE_H_ #include "Triangle.h" +#include "MathUtil.h" + #include #include @@ -50,66 +52,6 @@ class BSPTree { boost::shared_ptr data; }; - private: - class Plane { - public: - Plane() : d(0) {} - Plane(const vmml::vec3f &n, float d0) : normal(n), d(d0) {} - Plane(const Triangle &t) : normal(t.computeNormal()), d(t.getVertex(0).dot(normal)) {} - - bool contains(const vmml::vec3f &v) const { - return (fabsf(normal.dot(v) - d) < 1E-6); - } - - bool isBehind(const vmml::vec3f &v) const { - return (normal.dot(v) - d) < 0; - } - - bool isInFront(const vmml::vec3f &v) const { - return (normal.dot(v) - d) > 0; - } - - - bool contains(const Triangle &t) const { - for(int i = 0; i < 3; ++i) { - if(!contains(t.getVertex(i))) - return false; - } - - return true; - } - - bool isBehind(const Triangle &t) const { - for(int i = 0; i < 3; ++i) { - if(!isBehind(t.getVertex(i)) && !contains(t.getVertex(i))) - return false; - } - - return true; - } - - bool isInFront(const Triangle &t) const { - for(int i = 0; i < 3; ++i) { - if(!isInFront(t.getVertex(i)) && !contains(t.getVertex(i))) - return false; - } - - return true; - } - - - const vmml::vec3f& getNormal() const { - return normal; - } - - vmml::vec3f intersection(const vmml::vec3f &p, const vmml::vec3f &dir) const; - void partition(const TriangleRecord &t, std::list *front, std::list *back) const; - - private: - vmml::vec3f normal; - float d; - }; - public: BSPTree(const std::list &triangles); @@ -138,7 +80,7 @@ class BSPTree { } private: - Plane plane; + MathUtil::Plane plane; std::list triangles; BSPTree *frontTree, *backTree; @@ -168,6 +110,8 @@ class BSPTree { } } + void partition(const TriangleRecord &t, std::list *front, std::list *back) const; + static vmml::vec3f findCenter(const std::list &triangles); static const Triangle* findNearestTriangle(const std::list &triangles, const vmml::vec3f &v); }; diff --git a/src/Game.cpp b/src/Game.cpp index 487019c..8f5af26 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -30,7 +30,7 @@ namespace Zoom { Game::Game(bool multisample) : playerPos(vmml::vec3f::ZERO), playerRot(vmml::mat4f::IDENTITY), - input(NONE), lightPos(0) { + input(0), lightPos(0) { glClearColor(0.0, 0.0, 0.0, 1.0); glClearDepth(1.0); glEnable(GL_DEPTH_TEST); @@ -80,6 +80,8 @@ void Game::run(int delta) { lightPos += delta; lightPos %= 24000; + playerRot.rotate_y(delta*M_PI/180/40); + if(input & FORWARD) { playerPos -= playerRot*vmml::vec3f::UNIT_Z*0.01*delta; } @@ -127,7 +129,20 @@ void Game::render() { glLoadMatrixf(inverse.array); glLightfv(GL_LIGHT0, GL_POSITION, light); + renderer.render(triangles); + + /*glBegin(GL_LINES); + + glColor3f(0, 0, 1); + glVertex4f(-1, -0.5, -2, 1); + glVertex4f(1, -0.5, -2, 1); + + glColor3f(1, 0, 0); + glVertex4f(0, -0.5, -2, 1); + glVertex4f(1, 0, 0, 0); + + glEnd();*/ } } diff --git a/src/Game.h b/src/Game.h index a81c7f5..d935b7c 100644 --- a/src/Game.h +++ b/src/Game.h @@ -32,7 +32,6 @@ class Triangle; class Game { public: enum Input { - NONE = 0, FORWARD = (1 << 0), BACKWARD = (1 << 1), LEFT = (1 << 2), @@ -44,7 +43,7 @@ class Game { bool loadLevel(const std::string &name); void setInput(unsigned input) { - this->input = static_cast(input); + this->input = input; } void run(int delta); @@ -59,7 +58,7 @@ class Game { vmml::vec3f playerPos; vmml::mat4f playerRot; - Input input; + unsigned input; int lightPos; }; diff --git a/src/MathUtil.cpp b/src/MathUtil.cpp index 3905450..8ea3599 100644 --- a/src/MathUtil.cpp +++ b/src/MathUtil.cpp @@ -22,14 +22,23 @@ namespace Zoom { +const float MathUtil::EPSILON = 1E-6; + +vmml::vec3f MathUtil::Plane::intersection(const vmml::vec3f &p, const vmml::vec3f &dir) const { + float r = (d - p.dot(normal))/dir.dot(normal); + + return p + r*dir; +} + + vmml::mat4f MathUtil::perspective(float fovy, float aspect, float zNear) { float f = 1/std::tan(fovy*M_PI/360); vmml::mat4f ret(vmml::mat4f::ZERO); ret[0][0] = f/aspect; ret[1][1] = f; - ret[2][2] = -1; - ret[2][3] = -2*zNear; + ret[2][2] = EPSILON-1; + ret[2][3] = (EPSILON-2)*zNear; ret[3][2] = -1; return ret; diff --git a/src/MathUtil.h b/src/MathUtil.h index 2376c2d..9e6efc7 100644 --- a/src/MathUtil.h +++ b/src/MathUtil.h @@ -20,12 +20,73 @@ #ifndef ZOOM_MATHUTIL_H_ #define ZOOM_MATHUTIL_H_ +#include "Triangle.h" + #include namespace Zoom { class MathUtil { public: + static const float EPSILON; + + class Plane { + public: + Plane(const vmml::vec3f &n = vmml::vec3f::ZERO, float d0 = 0) : normal(n), d(d0) {} + Plane(const Triangle &t) : normal(t.computeNormal()), d(t.getVertex(0).dot(normal)) {} + + bool contains(const vmml::vec3f &v) const { + return (fabsf(normal.dot(v) - d) < EPSILON); + } + + bool isBehind(const vmml::vec3f &v) const { + return (normal.dot(v) - d) < 0; + } + + bool isInFront(const vmml::vec3f &v) const { + return (normal.dot(v) - d) > 0; + } + + + bool contains(const Triangle &t) const { + for(int i = 0; i < 3; ++i) { + if(!contains(t.getVertex(i))) + return false; + } + + return true; + } + + bool isBehind(const Triangle &t) const { + for(int i = 0; i < 3; ++i) { + if(!isBehind(t.getVertex(i)) && !contains(t.getVertex(i))) + return false; + } + + return true; + } + + bool isInFront(const Triangle &t) const { + for(int i = 0; i < 3; ++i) { + if(!isInFront(t.getVertex(i)) && !contains(t.getVertex(i))) + return false; + } + + return true; + } + + + const vmml::vec3f& getNormal() const { + return normal; + } + + vmml::vec3f intersection(const vmml::vec3f &p, const vmml::vec3f &dir) const; + + private: + vmml::vec3f normal; + float d; + }; + static vmml::mat4f perspective(float fovy, float aspect, float zNear); private: diff --git a/src/zoom.cpp b/src/zoom.cpp index b222286..e5b0978 100644 --- a/src/zoom.cpp +++ b/src/zoom.cpp @@ -32,8 +32,7 @@ #include #endif -void resize(int width, int height) -{ +void resize(int width, int height) { if(height == 0) { height = 1; } @@ -180,13 +179,11 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine bool running = true; MSG msg; - while(running) - { + while(running) { unsigned long delta = 0; unsigned long ticks = GetTickCount(); - while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) - { + while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if(msg.message == WM_QUIT) { running = false; -- cgit v1.2.3