Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional triangle vertex array normals generation #107

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/collision/TriangleVertexArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ using namespace reactphysics3d;
*/
TriangleVertexArray::TriangleVertexArray(uint nbVertices, const void* verticesStart, uint verticesStride,
uint nbTriangles, const void* indexesStart, uint indexesStride,
VertexDataType vertexDataType, IndexDataType indexDataType) {
VertexDataType vertexDataType, IndexDataType indexDataType,
bool generateNormals) {
mNbVertices = nbVertices;
mVerticesStart = static_cast<const uchar*>(verticesStart);
mVerticesStride = verticesStride;
Expand All @@ -60,12 +61,14 @@ TriangleVertexArray::TriangleVertexArray(uint nbVertices, const void* verticesSt
mIndicesStart = static_cast<const uchar*>(indexesStart);
mIndicesStride = indexesStride;
mVertexDataType = vertexDataType;
mVertexNormaldDataType = NormalDataType::NORMAL_FLOAT_TYPE;
mVertexNormalsDataType = NormalDataType::NORMAL_FLOAT_TYPE;
mIndexDataType = indexDataType;
mAreVerticesNormalsProvidedByUser = false;

// Compute the vertices normals because they are not provided by the user
computeVerticesNormals();
if (generateNormals) {
computeVerticesNormals();
}
}

// Constructor with vertices normals
Expand Down Expand Up @@ -100,7 +103,7 @@ TriangleVertexArray::TriangleVertexArray(uint nbVertices, const void* verticesSt
mIndicesStart = static_cast<const uchar*>(indexesStart);
mIndicesStride = indexesStride;
mVertexDataType = vertexDataType;
mVertexNormaldDataType = normalDataType;
mVertexNormalsDataType = normalDataType;
mIndexDataType = indexDataType;
mAreVerticesNormalsProvidedByUser = true;

Expand Down Expand Up @@ -264,6 +267,10 @@ void TriangleVertexArray::getTriangleVerticesNormals(uint triangleIndex, Vector3

assert(triangleIndex >= 0 && triangleIndex < mNbTriangles);

if (!mVerticesNormalsStart) {
return;
}

// Get the three vertex index of the three vertices of the triangle
uint verticesIndices[3];
getTriangleVerticesIndices(triangleIndex, verticesIndices);
Expand All @@ -275,13 +282,13 @@ void TriangleVertexArray::getTriangleVerticesNormals(uint triangleIndex, Vector3
const void* vertexNormalPointer = static_cast<const void*>(vertexNormalPointerChar);

// Get the normals from the array
if (mVertexNormaldDataType == TriangleVertexArray::NormalDataType::NORMAL_FLOAT_TYPE) {
if (mVertexNormalsDataType == TriangleVertexArray::NormalDataType::NORMAL_FLOAT_TYPE) {
const float* normal = static_cast<const float*>(vertexNormalPointer);
outTriangleVerticesNormals[k][0] = decimal(normal[0]);
outTriangleVerticesNormals[k][1] = decimal(normal[1]);
outTriangleVerticesNormals[k][2] = decimal(normal[2]);
}
else if (mVertexNormaldDataType == TriangleVertexArray::NormalDataType::NORMAL_DOUBLE_TYPE) {
else if (mVertexNormalsDataType == TriangleVertexArray::NormalDataType::NORMAL_DOUBLE_TYPE) {
const double* normal = static_cast<const double*>(vertexNormalPointer);
outTriangleVerticesNormals[k][0] = decimal(normal[0]);
outTriangleVerticesNormals[k][1] = decimal(normal[1]);
Expand Down Expand Up @@ -332,17 +339,21 @@ void TriangleVertexArray::getNormal(uint vertexIndex, Vector3* outNormal) {

assert(vertexIndex < mNbVertices);

if (!mVerticesNormalsStart) {
return;
}

const uchar* vertexNormalPointerChar = mVerticesNormalsStart + vertexIndex * mVerticesNormalsStride;
const void* vertexNormalPointer = static_cast<const void*>(vertexNormalPointerChar);

// Get the normals from the array
if (mVertexNormaldDataType == TriangleVertexArray::NormalDataType::NORMAL_FLOAT_TYPE) {
if (mVertexNormalsDataType == TriangleVertexArray::NormalDataType::NORMAL_FLOAT_TYPE) {
const float* normal = static_cast<const float*>(vertexNormalPointer);
(*outNormal)[0] = decimal(normal[0]);
(*outNormal)[1] = decimal(normal[1]);
(*outNormal)[2] = decimal(normal[2]);
}
else if (mVertexNormaldDataType == TriangleVertexArray::NormalDataType::NORMAL_DOUBLE_TYPE) {
else if (mVertexNormalsDataType == TriangleVertexArray::NormalDataType::NORMAL_DOUBLE_TYPE) {
const double* normal = static_cast<const double*>(vertexNormalPointer);
(*outNormal)[0] = decimal(normal[0]);
(*outNormal)[1] = decimal(normal[1]);
Expand Down
7 changes: 4 additions & 3 deletions src/collision/TriangleVertexArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class TriangleVertexArray {
VertexDataType mVertexDataType;

/// Data type of the vertex normals in the array
NormalDataType mVertexNormaldDataType;
NormalDataType mVertexNormalsDataType;

/// Data type of the indices in the array
IndexDataType mIndexDataType;
Expand All @@ -111,7 +111,8 @@ class TriangleVertexArray {
/// Constructor without vertices normals
TriangleVertexArray(uint nbVertices, const void* verticesStart, uint verticesStride,
uint nbTriangles, const void* indexesStart, uint indexesStride,
VertexDataType vertexDataType, IndexDataType indexDataType);
VertexDataType vertexDataType, IndexDataType indexDataType,
bool generateNormals = true);

/// Constructor with vertices normals
TriangleVertexArray(uint nbVertices, const void* verticesStart, uint verticesStride,
Expand Down Expand Up @@ -191,7 +192,7 @@ inline TriangleVertexArray::VertexDataType TriangleVertexArray::getVertexDataTyp
* @return The data type of the normals in the array
*/
inline TriangleVertexArray::NormalDataType TriangleVertexArray::getVertexNormalDataType() const {
return mVertexNormaldDataType;
return mVertexNormalsDataType;
}

// Return the index data type
Expand Down