From a2acf0f1b4aa830929507c70483f0b1885bf4426 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 2 Mar 2008 15:56:43 +0000 Subject: fixed final maploader bugs --- src/render/model.cc | 61 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/src/render/model.cc b/src/render/model.cc index 5019c7e..81d9ee9 100644 --- a/src/render/model.cc +++ b/src/render/model.cc @@ -20,7 +20,7 @@ namespace render { const float MAX_BOUNDS = 8192; -const float delta = 10e-8; +const float delta = 10e-10; std::map Model::registry; @@ -47,7 +47,6 @@ Model::Model(std::string const & name) : con_warn << "Could not stream " << fn << "!\n"; return; } - con_debug << "Reading " << fn << "\n"; // --------- the actual reading using math::Vector3f; @@ -161,11 +160,18 @@ void Model::make_face(math::Plane3f *face, std::vector & planes // check if the face is x-axis oriented if ( (fabsf(face->normal().x) >= fabsf(face->normal().y) ) && (fabsf(face->normal().x) >= fabsf(face->normal().z)) ) { //cout << " x oriented" << std::endl; - vl.push_back(new math::Vector3f(0, -MAX_BOUNDS, -MAX_BOUNDS)); - vl.push_back(new math::Vector3f(0, -MAX_BOUNDS, MAX_BOUNDS)); - vl.push_back(new math::Vector3f(0, MAX_BOUNDS, MAX_BOUNDS)); - vl.push_back(new math::Vector3f(0, MAX_BOUNDS, -MAX_BOUNDS)); - + + if (face->normal().x >= 0) { + vl.push_back(new math::Vector3f(0, -MAX_BOUNDS, -MAX_BOUNDS)); + vl.push_back(new math::Vector3f(0, -MAX_BOUNDS, MAX_BOUNDS)); + vl.push_back(new math::Vector3f(0, MAX_BOUNDS, MAX_BOUNDS)); + vl.push_back(new math::Vector3f(0, MAX_BOUNDS, -MAX_BOUNDS)); + } else { + vl.push_back(new math::Vector3f(0, MAX_BOUNDS, -MAX_BOUNDS)); + vl.push_back(new math::Vector3f(0, MAX_BOUNDS, MAX_BOUNDS)); + vl.push_back(new math::Vector3f(0, -MAX_BOUNDS, MAX_BOUNDS)); + vl.push_back(new math::Vector3f(0, -MAX_BOUNDS, -MAX_BOUNDS)); + } // calculate the x coordinate of each face vertex for (std::vector::iterator it = vl.begin(); it != vl.end(); it++) { (*it)->x = ( -face->d() - @@ -178,10 +184,18 @@ void Model::make_face(math::Plane3f *face, std::vector & planes // check if the face is y-axis oriented else if ( (fabsf(face->normal().y) >= fabsf(face->normal().x) ) && (fabsf(face->normal().y) >= fabsf(face->normal().z)) ) { //cout << " y oriented" << std::endl; - vl.push_back(new Vector3f(-MAX_BOUNDS, 0, -MAX_BOUNDS)); - vl.push_back(new Vector3f(-MAX_BOUNDS, 0, MAX_BOUNDS)); - vl.push_back(new Vector3f(MAX_BOUNDS, 0, MAX_BOUNDS)); - vl.push_back(new Vector3f(MAX_BOUNDS, 0, -MAX_BOUNDS)); + + if (face->normal().y >= 0) { + vl.push_back(new Vector3f(-MAX_BOUNDS, 0, -MAX_BOUNDS)); + vl.push_back(new Vector3f(-MAX_BOUNDS, 0, MAX_BOUNDS)); + vl.push_back(new Vector3f(MAX_BOUNDS, 0, MAX_BOUNDS)); + vl.push_back(new Vector3f(MAX_BOUNDS, 0, -MAX_BOUNDS)); + } else { + vl.push_back(new Vector3f(MAX_BOUNDS, 0, -MAX_BOUNDS)); + vl.push_back(new Vector3f(MAX_BOUNDS, 0, MAX_BOUNDS)); + vl.push_back(new Vector3f(-MAX_BOUNDS, 0, MAX_BOUNDS)); + vl.push_back(new Vector3f(-MAX_BOUNDS, 0, -MAX_BOUNDS)); + } // calculate the x coordinate of each face vertex for (std::vector::iterator it = vl.begin(); it != vl.end(); it++) { @@ -195,10 +209,17 @@ void Model::make_face(math::Plane3f *face, std::vector & planes // face must be z-axis oriented else { //cout << " z oriented" << std::endl; - vl.push_back(new Vector3f(-MAX_BOUNDS, -MAX_BOUNDS, 0)); - vl.push_back(new Vector3f(-MAX_BOUNDS, MAX_BOUNDS, 0)); - vl.push_back(new Vector3f(MAX_BOUNDS, MAX_BOUNDS, 0)); - vl.push_back(new Vector3f(MAX_BOUNDS, -MAX_BOUNDS, 0)); + if (face->normal().z >= 0) { + vl.push_back(new Vector3f(-MAX_BOUNDS, -MAX_BOUNDS, 0)); + vl.push_back(new Vector3f(-MAX_BOUNDS, MAX_BOUNDS, 0)); + vl.push_back(new Vector3f(MAX_BOUNDS, MAX_BOUNDS, 0)); + vl.push_back(new Vector3f(MAX_BOUNDS, -MAX_BOUNDS, 0)); + } else { + vl.push_back(new Vector3f(MAX_BOUNDS, -MAX_BOUNDS, 0)); + vl.push_back(new Vector3f(MAX_BOUNDS, MAX_BOUNDS, 0)); + vl.push_back(new Vector3f(-MAX_BOUNDS, MAX_BOUNDS, 0)); + vl.push_back(new Vector3f(-MAX_BOUNDS, -MAX_BOUNDS, 0)); + } // calculate the x coordinate of each face vertex for (std::vector::iterator it = vl.begin(); it != vl.end(); it++) { @@ -221,7 +242,7 @@ void Model::make_face(math::Plane3f *face, std::vector & planes Vector3f pn = crossproduct(plane->point(1)-plane->point(0), plane->point(2)-plane->point(0)); Vector3f t = crossproduct(fn, pn); - if (t.x == 0 && t.y ==0 && t.z == 0) { + if ((t.x == 0) && (t.y == 0) && (t.z == 0) ) { continue; } @@ -252,7 +273,7 @@ void Model::make_face(math::Plane3f *face, std::vector & planes } //cout << " vertex " << i << " prev " << prev << " v " << v << " next " << next << std::endl; - if ((v.x*plane->normal().x + v.y*plane->normal().y + v.y*plane->normal().y + v.z*plane->normal().z +plane->d()) < -delta ) { + if ((v.x*plane->normal().x + v.y*plane->normal().y + v.z*plane->normal().z +plane->d()) < delta ) { // find current std::vector::iterator vit = vl.begin(); @@ -261,7 +282,7 @@ void Model::make_face(math::Plane3f *face, std::vector & planes } // check if prev - v intersects with plane - if ((prev.x*plane->normal().x + prev.y*plane->normal().y + prev.y*plane->normal().y + prev.z*plane->normal().z + plane->d()) > delta ) { + if ((prev.x*plane->normal().x + prev.y*plane->normal().y + prev.z*plane->normal().z + plane->d()) > -delta ) { // calculate intersection float t1 = -plane->normal().x * prev.x - plane->normal().y * prev.y - plane->normal().z * prev.z -plane->d(); @@ -285,7 +306,7 @@ void Model::make_face(math::Plane3f *face, std::vector & planes } // check if next - v intersects with plane - if ((next.x*plane->normal().x + next.y*plane->normal().y + next.y*plane->normal().y + next.z*plane->normal().z + plane->d()) > delta ) { + if ((next.x*plane->normal().x + next.y*plane->normal().y + next.z*plane->normal().z + plane->d()) > -delta ) { // calculate intersection // calculate intersection @@ -329,6 +350,8 @@ void Model::make_face(math::Plane3f *face, std::vector & planes //con_debug << "adding face\n"; add_face(mf); + } else { + con_debug << "Unresolved face!\n"; } for (std::vector::iterator it = vl.begin(); it != vl.end(); it++) { -- cgit v1.2.3