Scaled vertices to always be integers.

This commit is contained in:
David Williams 2007-08-31 21:10:24 +00:00
parent 5f836e0c31
commit c1286a7847
5 changed files with 90 additions and 61 deletions

View File

@ -26,11 +26,18 @@ namespace Ogre
template <typename Type> class IntegralVector3 template <typename Type> class IntegralVector3
{ {
public: public:
IntegralVector3(Type xToSet, Type yToSet, Type zToSet) IntegralVector3()
:x(0)
,y(0)
,z(0)
{ {
x = xToSet; }
y = yToSet;
z = zToSet; IntegralVector3(Type xToSet, Type yToSet, Type zToSet)
:x(xToSet)
,y(yToSet)
,z(zToSet)
{
} }
bool operator==(const IntegralVector3<Type>& rhs) const throw() bool operator==(const IntegralVector3<Type>& rhs) const throw()
@ -48,13 +55,25 @@ namespace Ogre
return (z < rhs.z); return (z < rhs.z);
else else
return false; //They are equal return false; //They are equal
} }
Type x; Type x;
Type y; Type y;
Type z; Type z;
}; };
template <typename Type>
IntegralVector3<Type> operator-(const IntegralVector3<Type>& lhs, const IntegralVector3<Type>& rhs)
{
IntegralVector3<Type> result;
result.x = lhs.x - rhs.x;
result.y = lhs.y - rhs.y;
result.z = lhs.z - rhs.z;
return result;
}
typedef IntegralVector3<char> CharVector3; typedef IntegralVector3<char> CharVector3;
typedef IntegralVector3<short> ShortVector3; typedef IntegralVector3<short> ShortVector3;
typedef IntegralVector3<int> IntVector3; typedef IntegralVector3<int> IntVector3;

View File

@ -23,6 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "OgrePrerequisites.h" #include "OgrePrerequisites.h"
#include "OgreVector3.h" #include "OgreVector3.h"
#include "IntegralVector3.h"
namespace Ogre namespace Ogre
{ {
@ -31,7 +32,7 @@ namespace Ogre
class SurfaceVertex class SurfaceVertex
{ {
public: public:
Vector3 position; UIntVector3 position;
Vector3 normal; Vector3 normal;
float alpha; float alpha;
@ -39,9 +40,9 @@ namespace Ogre
SurfaceVertex(); SurfaceVertex();
SurfaceVertex(Vector3 positionToSet); SurfaceVertex(UIntVector3 positionToSet);
SurfaceVertex(Vector3 positionToSet, Vector3 normalToSet); SurfaceVertex(UIntVector3 positionToSet, Vector3 normalToSet);
bool operator==(const SurfaceVertex& rhs) const; bool operator==(const SurfaceVertex& rhs) const;

View File

@ -593,9 +593,9 @@ namespace Ogre
const uint lastZ = (std::min)(firstZ + OGRE_REGION_SIDE_LENGTH-1,static_cast<uint>(OGRE_VOLUME_SIDE_LENGTH-2)); const uint lastZ = (std::min)(firstZ + OGRE_REGION_SIDE_LENGTH-1,static_cast<uint>(OGRE_VOLUME_SIDE_LENGTH-2));
//Offset from lower block corner //Offset from lower block corner
const Vector3 offset(firstX,firstY,firstZ); const UIntVector3 offset(firstX*2,firstY*2,firstZ*2);
Vector3 vertlist[12]; UIntVector3 vertlist[12];
uchar vertMaterials[12]; uchar vertMaterials[12];
VolumeIterator volIter(*volumeData); VolumeIterator volIter(*volumeData);
volIter.setValidRegion(firstX,firstY,firstZ,lastX,lastY,lastZ); volIter.setValidRegion(firstX,firstY,firstZ,lastX,lastY,lastZ);
@ -645,95 +645,95 @@ namespace Ogre
/* Find the vertices where the surface intersects the cube */ /* Find the vertices where the surface intersects the cube */
if (edgeTable[iCubeIndex] & 1) if (edgeTable[iCubeIndex] & 1)
{ {
vertlist[0].x = (static_cast<double>(x) + 0.5); vertlist[0].x = 2*x + 1;
vertlist[0].y = (static_cast<double>(y)); vertlist[0].y = 2*y;
vertlist[0].z = (static_cast<double>(z)); vertlist[0].z = 2*z;
vertMaterials[0] = (std::max)(v000,v100); //FIXME - faster way? vertMaterials[0] = (std::max)(v000,v100); //FIXME - faster way?
} }
if (edgeTable[iCubeIndex] & 2) if (edgeTable[iCubeIndex] & 2)
{ {
vertlist[1].x = (static_cast<double>(x + 1)); vertlist[1].x = 2*x + 2;
vertlist[1].y = (static_cast<double>(y) + 0.5); vertlist[1].y = 2*y + 1;
vertlist[1].z = (static_cast<double>(z)); vertlist[1].z = 2*z;
vertMaterials[1] = (std::max)(v100,v110); vertMaterials[1] = (std::max)(v100,v110);
} }
if (edgeTable[iCubeIndex] & 4) if (edgeTable[iCubeIndex] & 4)
{ {
vertlist[2].x = (static_cast<double>(x) + 0.5); vertlist[2].x = 2*x + 1;
vertlist[2].y = (static_cast<double>(y + 1)); vertlist[2].y = 2*y + 2;
vertlist[2].z = (static_cast<double>(z)); vertlist[2].z = 2*z;
vertMaterials[2] = (std::max)(v010,v110); vertMaterials[2] = (std::max)(v010,v110);
} }
if (edgeTable[iCubeIndex] & 8) if (edgeTable[iCubeIndex] & 8)
{ {
vertlist[3].x = (static_cast<double>(x)); vertlist[3].x = 2*x;
vertlist[3].y = (static_cast<double>(y) + 0.5); vertlist[3].y = 2*y + 1;
vertlist[3].z = (static_cast<double>(z)); vertlist[3].z = 2*z;
vertMaterials[3] = (std::max)(v000,v010); vertMaterials[3] = (std::max)(v000,v010);
} }
if (edgeTable[iCubeIndex] & 16) if (edgeTable[iCubeIndex] & 16)
{ {
vertlist[4].x = (static_cast<double>(x) + 0.5); vertlist[4].x = 2*x + 1;
vertlist[4].y = (static_cast<double>(y)); vertlist[4].y = 2*y;
vertlist[4].z = (static_cast<double>(z + 1)); vertlist[4].z = 2*z + 2;
vertMaterials[4] = (std::max)(v001,v101); vertMaterials[4] = (std::max)(v001,v101);
} }
if (edgeTable[iCubeIndex] & 32) if (edgeTable[iCubeIndex] & 32)
{ {
vertlist[5].x = (static_cast<double>(x + 1)); vertlist[5].x = 2*x + 2;
vertlist[5].y = (static_cast<double>(y) + 0.5); vertlist[5].y = 2*y + 1;
vertlist[5].z = (static_cast<double>(z + 1)); vertlist[5].z = 2*z + 2;
vertMaterials[5] = (std::max)(v101,v111); vertMaterials[5] = (std::max)(v101,v111);
} }
if (edgeTable[iCubeIndex] & 64) if (edgeTable[iCubeIndex] & 64)
{ {
vertlist[6].x = (static_cast<double>(x) + 0.5); vertlist[6].x = 2*x + 1;
vertlist[6].y = (static_cast<double>(y + 1)); vertlist[6].y = 2*y + 2;
vertlist[6].z = (static_cast<double>(z + 1)); vertlist[6].z = 2*z + 2;
vertMaterials[6] = (std::max)(v011,v111); vertMaterials[6] = (std::max)(v011,v111);
} }
if (edgeTable[iCubeIndex] & 128) if (edgeTable[iCubeIndex] & 128)
{ {
vertlist[7].x = (static_cast<double>(x)); vertlist[7].x = 2*x;
vertlist[7].y = (static_cast<double>(y) + 0.5); vertlist[7].y = 2*y + 1;
vertlist[7].z = (static_cast<double>(z + 1)); vertlist[7].z = 2*z + 2;
vertMaterials[7] = (std::max)(v001,v011); vertMaterials[7] = (std::max)(v001,v011);
} }
if (edgeTable[iCubeIndex] & 256) if (edgeTable[iCubeIndex] & 256)
{ {
vertlist[8].x = (static_cast<double>(x)); vertlist[8].x = 2*x;
vertlist[8].y = (static_cast<double>(y)); vertlist[8].y = 2*y;
vertlist[8].z = (static_cast<double>(z) + 0.5); vertlist[8].z = 2*z + 1;
vertMaterials[8] = (std::max)(v000,v001); vertMaterials[8] = (std::max)(v000,v001);
} }
if (edgeTable[iCubeIndex] & 512) if (edgeTable[iCubeIndex] & 512)
{ {
vertlist[9].x = (static_cast<double>(x + 1)); vertlist[9].x = 2*x + 2;
vertlist[9].y = (static_cast<double>(y)); vertlist[9].y = 2*y;
vertlist[9].z = (static_cast<double>(z) + 0.5); vertlist[9].z = 2*z + 1;
vertMaterials[9] = (std::max)(v100,v101); vertMaterials[9] = (std::max)(v100,v101);
} }
if (edgeTable[iCubeIndex] & 1024) if (edgeTable[iCubeIndex] & 1024)
{ {
vertlist[10].x = (static_cast<double>(x + 1)); vertlist[10].x = 2*x + 2;
vertlist[10].y = (static_cast<double>(y + 1)); vertlist[10].y = 2*y + 2;
vertlist[10].z = (static_cast<double>(z) + 0.5); vertlist[10].z = 2*z + 1;
vertMaterials[10] = (std::max)(v110,v111); vertMaterials[10] = (std::max)(v110,v111);
} }
if (edgeTable[iCubeIndex] & 2048) if (edgeTable[iCubeIndex] & 2048)
{ {
vertlist[11].x = (static_cast<double>(x)); vertlist[11].x = 2*x;
vertlist[11].y = (static_cast<double>(y + 1)); vertlist[11].y = 2*y + 2;
vertlist[11].z = (static_cast<double>(z) + 0.5); vertlist[11].z = 2*z + 1;
vertMaterials[11] = (std::max)(v010,v011); vertMaterials[11] = (std::max)(v010,v011);
} }
for (int i=0;triTable[iCubeIndex][i]!=-1;i+=3) for (int i=0;triTable[iCubeIndex][i]!=-1;i+=3)
{ {
//The three vertices forming a triangle //The three vertices forming a triangle
const Vector3 vertex0 = vertlist[triTable[iCubeIndex][i ]] - offset; const UIntVector3 vertex0 = vertlist[triTable[iCubeIndex][i ]] - offset;
const Vector3 vertex1 = vertlist[triTable[iCubeIndex][i+1]] - offset; const UIntVector3 vertex1 = vertlist[triTable[iCubeIndex][i+1]] - offset;
const Vector3 vertex2 = vertlist[triTable[iCubeIndex][i+2]] - offset; const UIntVector3 vertex2 = vertlist[triTable[iCubeIndex][i+2]] - offset;
const uchar material0 = vertMaterials[triTable[iCubeIndex][i ]]; const uchar material0 = vertMaterials[triTable[iCubeIndex][i ]];
const uchar material1 = vertMaterials[triTable[iCubeIndex][i+1]]; const uchar material1 = vertMaterials[triTable[iCubeIndex][i+1]];

View File

@ -64,8 +64,14 @@ namespace Ogre
// Drawing stuff // Drawing stuff
int size = verticesToSet.size(); int size = verticesToSet.size();
Vector3 vaabMin = verticesToSet[0].position; Vector3 vaabMin;
Vector3 vaabMax = verticesToSet[0].position; Vector3 vaabMax;
vaabMin.x = verticesToSet[0].position.x/2.0f;
vaabMin.y = verticesToSet[0].position.y/2.0f;
vaabMin.z = verticesToSet[0].position.z/2.0f;
vaabMax.x = verticesToSet[0].position.x/2.0f;
vaabMax.y = verticesToSet[0].position.y/2.0f;
vaabMax.z = verticesToSet[0].position.z/2.0f;
//LogManager::getSingleton().logMessage("Setting Vertex Data of size " + StringConverter::toString(size)); //LogManager::getSingleton().logMessage("Setting Vertex Data of size " + StringConverter::toString(size));
@ -73,9 +79,9 @@ namespace Ogre
for(int i = 0; i < size; i++) for(int i = 0; i < size; i++)
{ {
*prPos++ = verticesToSet[i].position.x; *prPos++ = verticesToSet[i].position.x/2.0f;
*prPos++ = verticesToSet[i].position.y; *prPos++ = verticesToSet[i].position.y/2.0f;
*prPos++ = verticesToSet[i].position.z; *prPos++ = verticesToSet[i].position.z/2.0f;
*prPos++ = verticesToSet[i].normal.x; *prPos++ = verticesToSet[i].normal.x;
*prPos++ = verticesToSet[i].normal.y; *prPos++ = verticesToSet[i].normal.y;
@ -100,6 +106,9 @@ namespace Ogre
vbuf->unlock(); vbuf->unlock();
vaabMin /= 2.0f;
vaabMax /= 2.0f;
mBox.setExtents(vaabMin, vaabMax); mBox.setExtents(vaabMin, vaabMax);
unsigned short* pIdx = static_cast<unsigned short*>(ibuf->lock(HardwareBuffer::HBL_DISCARD)); unsigned short* pIdx = static_cast<unsigned short*>(ibuf->lock(HardwareBuffer::HBL_DISCARD));

View File

@ -6,12 +6,12 @@ namespace Ogre
{ {
} }
SurfaceVertex::SurfaceVertex(Vector3 positionToSet) SurfaceVertex::SurfaceVertex(UIntVector3 positionToSet)
:position(positionToSet) :position(positionToSet)
{ {
} }
SurfaceVertex::SurfaceVertex(Vector3 positionToSet, Vector3 normalToSet) SurfaceVertex::SurfaceVertex(UIntVector3 positionToSet, Vector3 normalToSet)
:position(positionToSet) :position(positionToSet)
,normal(normalToSet) ,normal(normalToSet)
{ {
@ -20,21 +20,21 @@ namespace Ogre
bool SurfaceVertex::operator==(const SurfaceVertex& rhs) const bool SurfaceVertex::operator==(const SurfaceVertex& rhs) const
{ {
//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. //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 /*return
( (
(abs(position.x - rhs.position.x) <= 0.01) && (abs(position.x - rhs.position.x) <= 0.01) &&
(abs(position.y - rhs.position.y) <= 0.01) && (abs(position.y - rhs.position.y) <= 0.01) &&
(abs(position.z - rhs.position.z) <= 0.01) && (abs(position.z - rhs.position.z) <= 0.01) &&
(abs(alpha - rhs.alpha) <= 0.01) (abs(alpha - rhs.alpha) <= 0.01)
); );*/
/*return return
( (
(position.x == rhs.position.x) && (position.x == rhs.position.x) &&
(position.x == rhs.position.y) && (position.x == rhs.position.y) &&
(position.x == rhs.position.z) && (position.x == rhs.position.z) &&
(alpha == rhs.alpha) (abs(alpha - rhs.alpha) <= 0.01)
);*/ );
} }
bool SurfaceVertex::operator < (const SurfaceVertex& rhs) const bool SurfaceVertex::operator < (const SurfaceVertex& rhs) const