Commit 055481bf authored by Janis Born's avatar Janis Born

OBJ loader can now compute per-face normals

parent 2d97c252
......@@ -24,14 +24,14 @@ namespace OpenGL{
// generic load/save
///////////////////////////////////////////////////////////////////////////////////////////////////
//! generic load function that will use one of the loading functions below based on the file ending
//! Generic load function that will use one of the loading functions below based on the file ending
SharedGeometryData loadGeometryData(const std::string& _filename);
///////////////////////////////////////////////////////////////////////////////////////////////////
// library specific load
///////////////////////////////////////////////////////////////////////////////////////////////////
//! loads from a Wavefront OBJ file
//! Loads from a Wavefront OBJ file. If _computeNormals, face normals are computed from geometry
SharedGeometryData loadGeometryDataFromOBJ(const std::string& _filename, bool _computeNormals = false);
///////////////////////////////////////////////////////////////////////////////////////////////////
......
......@@ -264,6 +264,16 @@ SharedGeometryData loadGeometryDataFromOBJ(const std::string& _filename, bool _c
}
}
if(_computeNormals && primitiveType != GL_TRIANGLES)
{
warning() << "computing OBJ normals is only supported for models with faces" << std::endl;
_computeNormals = false;
}
else if(_computeNormals)
{
hasNormals = true;
}
// all data are read from the file. construct an ArrayBuffer from the data
data = SharedGeometryData(new GeometryData());
......@@ -290,10 +300,24 @@ SharedGeometryData loadGeometryDataFromOBJ(const std::string& _filename, bool _c
if(hasNormals)
{
const glm::vec3& normal = normalData[indices[i].normal];
abData[pos++] = normal.x;
abData[pos++] = normal.y;
abData[pos++] = normal.z;
if(_computeNormals)
{
size_t triangleIndex = i / 3;
glm::vec3 v0 = (glm::vec3)positionData[indices[triangleIndex+0].position];
glm::vec3 v1 = (glm::vec3)positionData[indices[triangleIndex+1].position];
glm::vec3 v2 = (glm::vec3)positionData[indices[triangleIndex+2].position];
glm::vec3 normal = glm::normalize(glm::cross(v1 - v0, v2 - v0));
abData[pos++] = normal.x;
abData[pos++] = normal.y;
abData[pos++] = normal.z;
}
else
{
const glm::vec3& normal = normalData[indices[i].normal];
abData[pos++] = normal.x;
abData[pos++] = normal.y;
abData[pos++] = normal.z;
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment