From 2bc1fc2b880ef8bfdb23bf359f461c12c1db9dc2 Mon Sep 17 00:00:00 2001 From: David Williams Date: Wed, 16 Apr 2008 19:56:07 +0000 Subject: [PATCH] PolyVox now returns vertex positions as floats rather than ints. Avoids converting and scaling in the engine. --- include/SurfaceVertex.h | 8 ++-- source/PolyVoxSceneManager.cpp | 77 ++++++++++++++++++---------------- source/SurfaceVertex.cpp | 6 +-- 3 files changed, 48 insertions(+), 43 deletions(-) diff --git a/include/SurfaceVertex.h b/include/SurfaceVertex.h index 4e19d9c0..a704ca73 100644 --- a/include/SurfaceVertex.h +++ b/include/SurfaceVertex.h @@ -30,8 +30,8 @@ namespace PolyVox { public: SurfaceVertex(); - SurfaceVertex(Vector3DUint32 positionToSet, float materialToSet, float alphaToSet); - SurfaceVertex(Vector3DUint32 positionToSet, Vector3DFloat normalToSet); + SurfaceVertex(Vector3DFloat positionToSet, float materialToSet, float alphaToSet); + SurfaceVertex(Vector3DFloat positionToSet, Vector3DFloat normalToSet); friend bool operator==(const SurfaceVertex& lhs, const SurfaceVertex& rhs); friend bool operator < (const SurfaceVertex& lhs, const SurfaceVertex& rhs); @@ -40,7 +40,7 @@ namespace PolyVox const SurfaceEdgeIterator& getEdge(void) const; float getMaterial(void) const; const Vector3DFloat& getNormal(void) const; - const Vector3DUint32& getPosition(void) const; + const Vector3DFloat& getPosition(void) const; void setAlpha(float alphaToSet); void setEdge(const SurfaceEdgeIterator& edgeToSet); @@ -50,7 +50,7 @@ namespace PolyVox std::string tostring(void) const; private: - Vector3DUint32 position; + Vector3DFloat position; Vector3DFloat normal; float material; float alpha; diff --git a/source/PolyVoxSceneManager.cpp b/source/PolyVoxSceneManager.cpp index 04a31e4a..69d4df4d 100644 --- a/source/PolyVoxSceneManager.cpp +++ b/source/PolyVoxSceneManager.cpp @@ -244,7 +244,7 @@ namespace PolyVox const uint16_t lastZ = (std::min)(firstZ + POLYVOX_REGION_SIDE_LENGTH-1,static_cast(POLYVOX_VOLUME_SIDE_LENGTH-2)); //Offset from lower block corner - const Vector3DUint32 offset(firstX*2,firstY*2,firstZ*2); + const Vector3DFloat offset(firstX,firstY,firstZ); Vector3DUint32 vertlist[12]; uint8_t vertMaterials[12]; @@ -380,9 +380,14 @@ namespace PolyVox for (int i=0;triTable[iCubeIndex][i]!=-1;i+=3) { //The three vertices forming a triangle - const Vector3DUint32 vertex0 = vertlist[triTable[iCubeIndex][i ]] - offset; - const Vector3DUint32 vertex1 = vertlist[triTable[iCubeIndex][i+1]] - offset; - const Vector3DUint32 vertex2 = vertlist[triTable[iCubeIndex][i+2]] - offset; + const Vector3DUint32 vertex0 = vertlist[triTable[iCubeIndex][i ]]; + const Vector3DUint32 vertex1 = vertlist[triTable[iCubeIndex][i+1]]; + const Vector3DUint32 vertex2 = vertlist[triTable[iCubeIndex][i+2]]; + + //Cast to floats and divide by two. + const Vector3DFloat vertex0AsFloat = (static_cast(vertex0) / 2.0f) - offset; + const Vector3DFloat vertex1AsFloat = (static_cast(vertex1) / 2.0f) - offset; + const Vector3DFloat vertex2AsFloat = (static_cast(vertex2) / 2.0f) - offset; const uint8_t material0 = vertMaterials[triTable[iCubeIndex][i ]]; const uint8_t material1 = vertMaterials[triTable[iCubeIndex][i+1]]; @@ -392,79 +397,79 @@ namespace PolyVox //If all the materials are the same, we just need one triangle for that material with all the alphas set high. if((material0 == material1) && (material1 == material2)) { - SurfaceVertex surfaceVertex0Alpha1(vertex0,material0 + 0.1,1.0); - SurfaceVertex surfaceVertex1Alpha1(vertex1,material1 + 0.1,1.0); - SurfaceVertex surfaceVertex2Alpha1(vertex2,material2 + 0.1,1.0); + SurfaceVertex surfaceVertex0Alpha1(vertex0AsFloat,material0 + 0.1,1.0); + SurfaceVertex surfaceVertex1Alpha1(vertex1AsFloat,material1 + 0.1,1.0); + SurfaceVertex surfaceVertex2Alpha1(vertex2AsFloat,material2 + 0.1,1.0); singleMaterialPatch->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha1); } else if(material0 == material1) { { - SurfaceVertex surfaceVertex0Alpha1(vertex0,material0 + 0.1,1.0); - SurfaceVertex surfaceVertex1Alpha1(vertex1,material0 + 0.1,1.0); - SurfaceVertex surfaceVertex2Alpha1(vertex2,material0 + 0.1,0.0); + SurfaceVertex surfaceVertex0Alpha1(vertex0AsFloat,material0 + 0.1,1.0); + SurfaceVertex surfaceVertex1Alpha1(vertex1AsFloat,material0 + 0.1,1.0); + SurfaceVertex surfaceVertex2Alpha1(vertex2AsFloat,material0 + 0.1,0.0); multiMaterialPatch->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha1); } { - SurfaceVertex surfaceVertex0Alpha1(vertex0,material2 + 0.1,0.0); - SurfaceVertex surfaceVertex1Alpha1(vertex1,material2 + 0.1,0.0); - SurfaceVertex surfaceVertex2Alpha1(vertex2,material2 + 0.1,1.0); + SurfaceVertex surfaceVertex0Alpha1(vertex0AsFloat,material2 + 0.1,0.0); + SurfaceVertex surfaceVertex1Alpha1(vertex1AsFloat,material2 + 0.1,0.0); + SurfaceVertex surfaceVertex2Alpha1(vertex2AsFloat,material2 + 0.1,1.0); multiMaterialPatch->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha1); } } else if(material0 == material2) { { - SurfaceVertex surfaceVertex0Alpha1(vertex0,material0 + 0.1,1.0); - SurfaceVertex surfaceVertex1Alpha1(vertex1,material0 + 0.1,0.0); - SurfaceVertex surfaceVertex2Alpha1(vertex2,material0 + 0.1,1.0); + SurfaceVertex surfaceVertex0Alpha1(vertex0AsFloat,material0 + 0.1,1.0); + SurfaceVertex surfaceVertex1Alpha1(vertex1AsFloat,material0 + 0.1,0.0); + SurfaceVertex surfaceVertex2Alpha1(vertex2AsFloat,material0 + 0.1,1.0); multiMaterialPatch->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha1); } { - SurfaceVertex surfaceVertex0Alpha1(vertex0,material1 + 0.1,0.0); - SurfaceVertex surfaceVertex1Alpha1(vertex1,material1 + 0.1,1.0); - SurfaceVertex surfaceVertex2Alpha1(vertex2,material1 + 0.1,0.0); + SurfaceVertex surfaceVertex0Alpha1(vertex0AsFloat,material1 + 0.1,0.0); + SurfaceVertex surfaceVertex1Alpha1(vertex1AsFloat,material1 + 0.1,1.0); + SurfaceVertex surfaceVertex2Alpha1(vertex2AsFloat,material1 + 0.1,0.0); multiMaterialPatch->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha1); } } else if(material1 == material2) { { - SurfaceVertex surfaceVertex0Alpha1(vertex0,material1 + 0.1,0.0); - SurfaceVertex surfaceVertex1Alpha1(vertex1,material1 + 0.1,1.0); - SurfaceVertex surfaceVertex2Alpha1(vertex2,material1 + 0.1,1.0); + SurfaceVertex surfaceVertex0Alpha1(vertex0AsFloat,material1 + 0.1,0.0); + SurfaceVertex surfaceVertex1Alpha1(vertex1AsFloat,material1 + 0.1,1.0); + SurfaceVertex surfaceVertex2Alpha1(vertex2AsFloat,material1 + 0.1,1.0); multiMaterialPatch->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha1); } { - SurfaceVertex surfaceVertex0Alpha1(vertex0,material0 + 0.1,1.0); - SurfaceVertex surfaceVertex1Alpha1(vertex1,material0 + 0.1,0.0); - SurfaceVertex surfaceVertex2Alpha1(vertex2,material0 + 0.1,0.0); + SurfaceVertex surfaceVertex0Alpha1(vertex0AsFloat,material0 + 0.1,1.0); + SurfaceVertex surfaceVertex1Alpha1(vertex1AsFloat,material0 + 0.1,0.0); + SurfaceVertex surfaceVertex2Alpha1(vertex2AsFloat,material0 + 0.1,0.0); multiMaterialPatch->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha1); } } else { { - SurfaceVertex surfaceVertex0Alpha1(vertex0,material0 + 0.1,1.0); - SurfaceVertex surfaceVertex1Alpha1(vertex1,material0 + 0.1,0.0); - SurfaceVertex surfaceVertex2Alpha1(vertex2,material0 + 0.1,0.0); + SurfaceVertex surfaceVertex0Alpha1(vertex0AsFloat,material0 + 0.1,1.0); + SurfaceVertex surfaceVertex1Alpha1(vertex1AsFloat,material0 + 0.1,0.0); + SurfaceVertex surfaceVertex2Alpha1(vertex2AsFloat,material0 + 0.1,0.0); multiMaterialPatch->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha1); } { - SurfaceVertex surfaceVertex0Alpha1(vertex0,material1 + 0.1,0.0); - SurfaceVertex surfaceVertex1Alpha1(vertex1,material1 + 0.1,1.0); - SurfaceVertex surfaceVertex2Alpha1(vertex2,material1 + 0.1,0.0); + SurfaceVertex surfaceVertex0Alpha1(vertex0AsFloat,material1 + 0.1,0.0); + SurfaceVertex surfaceVertex1Alpha1(vertex1AsFloat,material1 + 0.1,1.0); + SurfaceVertex surfaceVertex2Alpha1(vertex2AsFloat,material1 + 0.1,0.0); multiMaterialPatch->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha1); } { - SurfaceVertex surfaceVertex0Alpha1(vertex0,material2 + 0.1,0.0); - SurfaceVertex surfaceVertex1Alpha1(vertex1,material2 + 0.1,0.0); - SurfaceVertex surfaceVertex2Alpha1(vertex2,material2 + 0.1,1.0); + SurfaceVertex surfaceVertex0Alpha1(vertex0AsFloat,material2 + 0.1,0.0); + SurfaceVertex surfaceVertex1Alpha1(vertex1AsFloat,material2 + 0.1,0.0); + SurfaceVertex surfaceVertex2Alpha1(vertex2AsFloat,material2 + 0.1,1.0); multiMaterialPatch->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha1); } } @@ -510,7 +515,7 @@ namespace PolyVox std::vector::iterator iterSurfaceVertex = singleMaterialPatch->m_vecVertices.begin(); while(iterSurfaceVertex != singleMaterialPatch->m_vecVertices.end()) { - Vector3DFloat tempNormal = computeNormal(static_cast(iterSurfaceVertex->getPosition() + offset)/2.0f, CENTRAL_DIFFERENCE); + Vector3DFloat tempNormal = computeNormal(static_cast(iterSurfaceVertex->getPosition() + offset), CENTRAL_DIFFERENCE); const_cast(*iterSurfaceVertex).setNormal(tempNormal); ++iterSurfaceVertex; } @@ -518,7 +523,7 @@ namespace PolyVox iterSurfaceVertex = multiMaterialPatch->m_vecVertices.begin(); while(iterSurfaceVertex != multiMaterialPatch->m_vecVertices.end()) { - Vector3DFloat tempNormal = computeNormal(static_cast(iterSurfaceVertex->getPosition() + offset)/2.0f, CENTRAL_DIFFERENCE); + Vector3DFloat tempNormal = computeNormal(static_cast(iterSurfaceVertex->getPosition() + offset), CENTRAL_DIFFERENCE); const_cast(*iterSurfaceVertex).setNormal(tempNormal); ++iterSurfaceVertex; } diff --git a/source/SurfaceVertex.cpp b/source/SurfaceVertex.cpp index 23f1ce8c..54a771f0 100644 --- a/source/SurfaceVertex.cpp +++ b/source/SurfaceVertex.cpp @@ -11,7 +11,7 @@ namespace PolyVox { } - SurfaceVertex::SurfaceVertex(Vector3DUint32 positionToSet, float materialToSet, float alphaToSet) + SurfaceVertex::SurfaceVertex(Vector3DFloat positionToSet, float materialToSet, float alphaToSet) :material(materialToSet) ,alpha(alphaToSet) ,position(positionToSet) @@ -20,7 +20,7 @@ namespace PolyVox } - SurfaceVertex::SurfaceVertex(Vector3DUint32 positionToSet, Vector3DFloat normalToSet) + SurfaceVertex::SurfaceVertex(Vector3DFloat positionToSet, Vector3DFloat normalToSet) :position(positionToSet) ,normal(normalToSet) { @@ -47,7 +47,7 @@ namespace PolyVox return normal; } - const Vector3DUint32& SurfaceVertex::getPosition(void) const + const Vector3DFloat& SurfaceVertex::getPosition(void) const { return position; }