From ca0805bd2f65d055ce54059a739251fb0801325d Mon Sep 17 00:00:00 2001 From: David Williams Date: Fri, 31 Aug 2007 22:19:47 +0000 Subject: [PATCH] Scaled vertices to always be integers. --- include/Constants.h | 25 ++++++++++++++++ source/SurfacePatch.cpp | 54 ++++++++++++++++----------------- source/SurfaceVertex.cpp | 65 ++++++++++++++++++++++++++++++---------- 3 files changed, 101 insertions(+), 43 deletions(-) create mode 100644 include/Constants.h diff --git a/include/Constants.h b/include/Constants.h new file mode 100644 index 00000000..de0b5ad2 --- /dev/null +++ b/include/Constants.h @@ -0,0 +1,25 @@ +#ifndef __Constants_H__ +#define __Constants_H__ + +namespace Ogre +{ + //FIXME - i think we can define mod using a bitmask which flattens the upper bits. Should define that here. + const unsigned int OGRE_BLOCK_SIDE_LENGTH_POWER = 5; + const unsigned int OGRE_BLOCK_SIDE_LENGTH = (0x0001 << OGRE_BLOCK_SIDE_LENGTH_POWER); + const unsigned int OGRE_NO_OF_VOXELS_IN_BLOCK = (OGRE_BLOCK_SIDE_LENGTH * OGRE_BLOCK_SIDE_LENGTH * OGRE_BLOCK_SIDE_LENGTH); + + const unsigned int OGRE_VOLUME_SIDE_LENGTH_POWER = 8; + const unsigned int OGRE_VOLUME_SIDE_LENGTH = (0x0001 << OGRE_VOLUME_SIDE_LENGTH_POWER); + const unsigned int OGRE_VOLUME_SIDE_LENGTH_IN_BLOCKS = (OGRE_VOLUME_SIDE_LENGTH >> OGRE_BLOCK_SIDE_LENGTH_POWER); + const unsigned int OGRE_NO_OF_BLOCKS_IN_VOLUME = (OGRE_VOLUME_SIDE_LENGTH_IN_BLOCKS * OGRE_VOLUME_SIDE_LENGTH_IN_BLOCKS * OGRE_VOLUME_SIDE_LENGTH_IN_BLOCKS); + const unsigned int OGRE_NO_OF_VOXELS_IN_VOLUME = (OGRE_VOLUME_SIDE_LENGTH * OGRE_VOLUME_SIDE_LENGTH * OGRE_VOLUME_SIDE_LENGTH); + + const unsigned int OGRE_REGION_SIDE_LENGTH_POWER = 4; + const unsigned int OGRE_REGION_SIDE_LENGTH = (0x0001 << OGRE_REGION_SIDE_LENGTH_POWER); + const unsigned int OGRE_VOLUME_SIDE_LENGTH_IN_REGIONS = (OGRE_VOLUME_SIDE_LENGTH >> OGRE_REGION_SIDE_LENGTH_POWER); + const unsigned int OGRE_NO_OF_REGIONS_IN_VOLUME = (OGRE_VOLUME_SIDE_LENGTH_IN_REGIONS * OGRE_VOLUME_SIDE_LENGTH_IN_REGIONS * OGRE_VOLUME_SIDE_LENGTH_IN_REGIONS); + + const unsigned long OGRE_MAX_VOXELS_TO_BURN_PER_FRAME = 1000; +} + +#endif diff --git a/source/SurfacePatch.cpp b/source/SurfacePatch.cpp index a7dc6eab..d09d7b99 100644 --- a/source/SurfacePatch.cpp +++ b/source/SurfacePatch.cpp @@ -44,6 +44,10 @@ namespace Ogre void SurfacePatch::addTriangle(const SurfaceVertex& v0,const SurfaceVertex& v1,const SurfaceVertex& v2) { + m_uTrianglesAdded++; + + m_uVerticesAdded += 3; + /*m_vecVertexData.push_back(v0); m_vecIndexData.push_back(m_vecVertexData.size()-1); m_vecVertexData.push_back(v1); @@ -51,13 +55,7 @@ namespace Ogre m_vecVertexData.push_back(v2); m_vecIndexData.push_back(m_vecVertexData.size()-1);*/ - m_uTrianglesAdded++; - - m_uVerticesAdded += 3; - - SurfaceTriangle triangle; - - long int index; + /*long int index; //If a vertex has not yet been added, it's index is -1 index = vertexIndices[(v0.position.x*(OGRE_REGION_SIDE_LENGTH*2+1)*(OGRE_REGION_SIDE_LENGTH*2+1)) + (v0.position.y*(OGRE_REGION_SIDE_LENGTH*2+1)) + (v0.position.z)]; if((index == -1)) @@ -101,20 +99,19 @@ namespace Ogre { //Just reuse the existing vertex m_vecIndexData.push_back(index); - } + }*/ + SurfaceTriangle triangle; + triangle.v0 = m_setVertices.insert(v0).first; + triangle.v1 = m_setVertices.insert(v1).first; + triangle.v2 = m_setVertices.insert(v2).first; - - /*m_setVertices.insert(v0); - m_setVertices.insert(v1); - m_setVertices.insert(v2); - - triangle.v0 = std::find(m_setVertices.begin(), m_setVertices.end(), v0); + /*triangle.v0 = std::find(m_setVertices.begin(), m_setVertices.end(), v0); triangle.v1 = std::find(m_setVertices.begin(), m_setVertices.end(), v1); - triangle.v2 = std::find(m_setVertices.begin(), m_setVertices.end(), v2);*/ + triangle.v2 = std::find(m_setVertices.begin(), m_setVertices.end(), v2); */ - //m_listTriangles.push_back(triangle); + m_listTriangles.push_back(triangle); /*triangle.v0->listTrianglesUsingThisVertex.push_back(m_listTriangles.end()); triangle.v1->listTrianglesUsingThisVertex.push_back(m_listTriangles.end()); @@ -123,21 +120,22 @@ namespace Ogre void SurfacePatch::getVertexAndIndexData(std::vector& vertexData, std::vector& indexData) { - /*vertexData.clear(); - indexData.clear();*/ + vertexData.clear(); + indexData.clear(); - vertexData = m_vecVertexData; - indexData = m_vecIndexData; + /*vertexData = m_vecVertexData; + indexData = m_vecIndexData;*/ - return; -#ifdef BLAH + //return; vertexData.resize(m_setVertices.size()); std::copy(m_setVertices.begin(), m_setVertices.end(), vertexData.begin()); - sort(vertexData.begin(),vertexData.end()); + //sort(vertexData.begin(),vertexData.end()); //reverse(vertexData.begin(),vertexData.end()); + //LogManager::getSingleton().logMessage("No of triangles = " + StringConverter::toString(m_listTriangles.size())); + for(std::list::iterator iterTriangles = m_listTriangles.begin(); iterTriangles != m_listTriangles.end(); ++iterTriangles) { /*vertexData.push_back(*(iterTriangles->v0)); @@ -145,7 +143,7 @@ namespace Ogre vertexData.push_back(*(iterTriangles->v1)); indexData.push_back(vertexData.size()-1); vertexData.push_back(*(iterTriangles->v2)); - indexData.push_back(vertexData.size()-1);*/ + indexData.push_back(vertexData.size()-1); */ /*std::set::iterator iterVertex = m_setVertices.find((*(iterTriangles->v0))); indexData.push_back(iterVertex - m_setVertices.begin()); @@ -163,21 +161,21 @@ namespace Ogre } }*/ - std::vector::iterator iterVertex = find(vertexData.begin(), vertexData.end(),(*(iterTriangles->v0))); + std::vector::iterator iterVertex = lower_bound(vertexData.begin(), vertexData.end(),(*(iterTriangles->v0))); if(iterVertex == vertexData.end()) { LogManager::getSingleton().logMessage("Vertex 0 Not Found"); } indexData.push_back(iterVertex - vertexData.begin()); - iterVertex = find(vertexData.begin(), vertexData.end(),(*(iterTriangles->v1))); + iterVertex = lower_bound(vertexData.begin(), vertexData.end(),(*(iterTriangles->v1))); if(iterVertex == vertexData.end()) { LogManager::getSingleton().logMessage("Vertex 1 Not Found"); } indexData.push_back(iterVertex - vertexData.begin()); - iterVertex = find(vertexData.begin(), vertexData.end(),(*(iterTriangles->v2))); + iterVertex = lower_bound(vertexData.begin(), vertexData.end(),(*(iterTriangles->v2))); if(iterVertex == vertexData.end()) { LogManager::getSingleton().logMessage("Vertex 2 Not Found"); @@ -202,10 +200,10 @@ namespace Ogre } }*/ } + //LogManager::getSingleton().logMessage("Out of Loop"); /*vertexData = m_vecVertexData; indexData = m_vecIndexData;*/ -#endif } } diff --git a/source/SurfaceVertex.cpp b/source/SurfaceVertex.cpp index 43b370d2..8d9835b8 100644 --- a/source/SurfaceVertex.cpp +++ b/source/SurfaceVertex.cpp @@ -1,3 +1,4 @@ +#include "Constants.h" #include "SurfaceVertex.h" namespace Ogre @@ -22,32 +23,45 @@ namespace Ogre //We dont't check the normal here as it may not have been set. But if two vertices have the same position they should have the same normal too. /*return ( - (abs(position.x - rhs.position.x) <= 0.01) && - (abs(position.y - rhs.position.y) <= 0.01) && - (abs(position.z - rhs.position.z) <= 0.01) && - (abs(alpha - rhs.alpha) <= 0.01) + (position.x == rhs.position.x) && + (position.y == rhs.position.y) && + (position.z == rhs.position.z) //&& + //(abs(alpha - rhs.alpha) <= 0.01) );*/ - return - ( - (position.x == rhs.position.x) && - (position.x == rhs.position.y) && - (position.x == rhs.position.z) && - (abs(alpha - rhs.alpha) <= 0.01) - ); + /*ulong value = 0; + value |= position.x; + value << 10; + value |= position.y; + value << 10; + value |= position.z; + + ulong rhsValue = 0; + rhsValue |= rhs.position.x; + rhsValue << 10; + rhsValue |= rhs.position.y; + rhsValue << 10; + rhsValue |= rhs.position.z; + + return value == rhsValue;*/ + + unsigned long offset = (position.x*(OGRE_REGION_SIDE_LENGTH*2+1)*(OGRE_REGION_SIDE_LENGTH*2+1)) + (position.y*(OGRE_REGION_SIDE_LENGTH*2+1)) + (position.z); + unsigned long rhsOffset = (rhs.position.x*(OGRE_REGION_SIDE_LENGTH*2+1)*(OGRE_REGION_SIDE_LENGTH*2+1)) + (rhs.position.y*(OGRE_REGION_SIDE_LENGTH*2+1)) + (rhs.position.z); + + return offset == rhsOffset; } bool SurfaceVertex::operator < (const SurfaceVertex& rhs) const { - if((*this) == rhs) + /*if((*this) == rhs) { return false; } if(alpha < rhs.alpha) { return true; - } - if(position.z < rhs.position.z) + }*/ + /*if(position.z < rhs.position.z) { return true; } @@ -60,6 +74,27 @@ namespace Ogre return true; } - return false; + return false;*/ + + /*ulong value = 0; + value |= position.x; + value << 10; + value |= position.y; + value << 10; + value |= position.z; + + ulong rhsValue = 0; + rhsValue |= rhs.position.x; + rhsValue << 10; + rhsValue |= rhs.position.y; + rhsValue << 10; + rhsValue |= rhs.position.z; + + return value < rhsValue;*/ + + unsigned long offset = (position.x*(OGRE_REGION_SIDE_LENGTH*2+1)*(OGRE_REGION_SIDE_LENGTH*2+1)) + (position.y*(OGRE_REGION_SIDE_LENGTH*2+1)) + (position.z); + unsigned long rhsOffset = (rhs.position.x*(OGRE_REGION_SIDE_LENGTH*2+1)*(OGRE_REGION_SIDE_LENGTH*2+1)) + (rhs.position.y*(OGRE_REGION_SIDE_LENGTH*2+1)) + (rhs.position.z); + + return offset < rhsOffset; } }