Renamed members to differentiate between encoded and decoded values.

This commit is contained in:
David Williams 2014-06-02 08:45:25 +02:00
parent 4fa2400ef2
commit d7d1d99ca9
5 changed files with 23 additions and 26 deletions

View File

@ -92,14 +92,14 @@ OpenGLMeshData buildOpenGLMeshData(const PolyVox::Mesh< PolyVox::MarchingCubesVe
// Every surface extractor outputs valid positions for the vertices, so tell OpenGL how these are laid out
glEnableVertexAttribArray(0); // Attrib '0' is the vertex positions
glVertexAttribIPointer(0, 3, GL_UNSIGNED_SHORT, sizeof(MarchingCubesVertex< uint8_t >), (GLvoid*)(offsetof(MarchingCubesVertex< uint8_t >, position))); //take the first 3 floats from every sizeof(decltype(vecVertices)::value_type)
glVertexAttribIPointer(0, 3, GL_UNSIGNED_SHORT, sizeof(MarchingCubesVertex< uint8_t >), (GLvoid*)(offsetof(MarchingCubesVertex< uint8_t >, encodedPosition))); //take the first 3 floats from every sizeof(decltype(vecVertices)::value_type)
// Some surface extractors also generate normals, so tell OpenGL how these are laid out. If a surface extractor
// does not generate normals then nonsense values are written into the buffer here and sghould be ignored by the
// shader. This is mostly just to simplify this example code - in a real application you will know whether your
// chosen surface extractor generates normals and can skip uploading them if not.
glEnableVertexAttribArray(1); // Attrib '1' is the vertex normals.
glVertexAttribIPointer(1, 1, GL_UNSIGNED_SHORT, sizeof(MarchingCubesVertex< uint8_t >), (GLvoid*)(offsetof(MarchingCubesVertex< uint8_t >, normal)));
glVertexAttribIPointer(1, 1, GL_UNSIGNED_SHORT, sizeof(MarchingCubesVertex< uint8_t >), (GLvoid*)(offsetof(MarchingCubesVertex< uint8_t >, encodedNormal)));
// Finally a surface extractor will probably output additional data. This is highly application dependant. For this example code
// we're just uploading it as a set of bytes which we can read individually, but real code will want to do something specialised here.

View File

@ -47,19 +47,16 @@ namespace PolyVox
// Each component of the position is stored as a single unsigned byte.
// The true position is found by offseting each component by 0.5f.
Vector3DUint8 position;
// Currently unused and just serves as passing
uint8_t unused;
Vector3DUint8 encodedPosition;
// User data
DataType data;
};
/// Decodes a position from a CubicVertex
inline Vector3DFloat decode(const Vector3DUint8& position)
inline Vector3DFloat decode(const Vector3DUint8& encodedPosition)
{
Vector3DFloat result(position.getX(), position.getY(), position.getZ());
Vector3DFloat result(encodedPosition.getX(), encodedPosition.getY(), encodedPosition.getZ());
result -= 0.5f; // Apply the required offset
return result;
}
@ -69,7 +66,7 @@ namespace PolyVox
Vertex<DataType> decode(const CubicVertex<DataType>& cubicVertex)
{
Vertex<DataType> result;
result.position = decode(cubicVertex.position);
result.position = decode(cubicVertex.encodedPosition);
result.normal.setElements(0.0f, 0.0f, 0.0f); // Currently not calculated
result.data = cubicVertex.data; // Data is not encoded
return result;

View File

@ -221,7 +221,7 @@ namespace PolyVox
{
//No vertices matched and we've now hit an empty space. Fill it by creating a vertex. The 0.5f offset is because vertices set between voxels in order to build cubes around them.
CubicVertex<typename VolumeType::VoxelType> cubicVertex;
cubicVertex.position.setElements(static_cast<uint8_t>(uX), static_cast<uint8_t>(uY), static_cast<uint8_t>(uZ));
cubicVertex.encodedPosition.setElements(static_cast<uint8_t>(uX), static_cast<uint8_t>(uY), static_cast<uint8_t>(uZ));
cubicVertex.data = uMaterialIn;
rEntry.iIndex = m_meshCurrent->addVertex(cubicVertex);
rEntry.uMaterial = uMaterialIn;

View File

@ -45,32 +45,32 @@ namespace PolyVox
typedef _DataType DataType;
// Each component of the position is stored using 8.8 fixed-point encoding.
Vector3DUint16 position;
Vector3DUint16 encodedPosition;
// Each component of the normal is encoded using 5 bits of this variable.
// The 16 bits are -xxxxxyyyyyzzzzz (note the left-most bit is currently
// unused). Some extra shifting and scaling is required to make it signed.
uint16_t normal;
uint16_t encodedNormal;
// User data
DataType data;
};
/// Decodes a position from a MarchingCubesVertex
inline Vector3DFloat decode(const Vector3DUint16& position)
inline Vector3DFloat decode(const Vector3DUint16& encodedPosition)
{
Vector3DFloat result(position.getX(), position.getY(), position.getZ());
Vector3DFloat result(encodedPosition.getX(), encodedPosition.getY(), encodedPosition.getZ());
result *= (1.0f / 256.0f); // Division is compile-time constant
return result;
}
/// Decodes a normal from a MarchingCubesVertex
inline Vector3DFloat decode(const uint16_t normal)
inline Vector3DFloat decode(const uint16_t encodedNormal)
{
// Get normal components in the range 0 to 31
uint16_t x = (normal >> 10) & 0x1F;
uint16_t y = (normal >> 5) & 0x1F;
uint16_t z = (normal) & 0x1F;
uint16_t x = (encodedNormal >> 10) & 0x1F;
uint16_t y = (encodedNormal >> 5) & 0x1F;
uint16_t z = (encodedNormal) & 0x1F;
// Build the resulting vector
Vector3DFloat result(x, y, z);
@ -89,8 +89,8 @@ namespace PolyVox
Vertex<DataType> decode(const MarchingCubesVertex<DataType>& marchingCubesVertex)
{
Vertex<DataType> result;
result.position = decode(marchingCubesVertex.position);
result.normal = decode(marchingCubesVertex.normal);
result.position = decode(marchingCubesVertex.encodedPosition);
result.normal = decode(marchingCubesVertex.encodedNormal);
result.data = marchingCubesVertex.data; // Data is not encoded
return result;
}

View File

@ -470,8 +470,8 @@ namespace PolyVox
const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v100, fInterp);
MarchingCubesVertex<typename VolumeType::VoxelType> surfaceVertex;
surfaceVertex.position = v3dScaledPosition;
surfaceVertex.normal = encodedNormal;
surfaceVertex.encodedPosition = v3dScaledPosition;
surfaceVertex.encodedNormal = encodedNormal;
surfaceVertex.data = uMaterial;
const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex);
@ -513,8 +513,8 @@ namespace PolyVox
const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v010, fInterp);
MarchingCubesVertex<typename VolumeType::VoxelType> surfaceVertex;
surfaceVertex.position = v3dScaledPosition;
surfaceVertex.normal = encodedNormal;
surfaceVertex.encodedPosition = v3dScaledPosition;
surfaceVertex.encodedNormal = encodedNormal;
surfaceVertex.data = uMaterial;
uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex);
@ -555,8 +555,8 @@ namespace PolyVox
const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v001, fInterp);
MarchingCubesVertex<typename VolumeType::VoxelType> surfaceVertex;
surfaceVertex.position = v3dScaledPosition;
surfaceVertex.normal = encodedNormal;
surfaceVertex.encodedPosition = v3dScaledPosition;
surfaceVertex.encodedNormal = encodedNormal;
surfaceVertex.data = uMaterial;
const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex);