OBJ.cpp   OBJ.cpp 
skipping to change at line 316 skipping to change at line 316
} }
qint64 secondPassTime = timer.restart(); qint64 secondPassTime = timer.restart();
//Find bounding extrema //Find bounding extrema
findBounds(); findBounds();
qint64 boundTime = timer.restart(); qint64 boundTime = timer.restart();
//Create vertex normals if specified or required //Create vertex normals if specified or required
if(rebuildNormals) if(rebuildNormals || !hasNormals())
{ {
generateNormals(); generateNormals();
} }
else
{
if(!hasNormals())
generateNormals();
}
//Create tangents //Create tangents
generateTangents(); generateTangents();
qint64 normalTime = timer.elapsed(); qint64 normalTime = timer.elapsed();
//Loaded //Loaded
qDebug() << "[OBJ] Loaded OBJ successfully: " << qtFile->fileName(); qDebug() << "[OBJ] Loaded OBJ successfully: " << qtFile->fileName();
qDebug() << "[OBJ] Triangles#: " << m_numberOfTriangles; qDebug() << "[OBJ] Triangles#: " << m_numberOfTriangles;
qDebug() << "[OBJ] Vertices#: " << m_numberOfVertexCoords<<" unique / "<< m_vertexArray.size()<<" total"; qDebug() << "[OBJ] Vertices#: " << m_numberOfVertexCoords<<" unique / "<< m_vertexArray.size()<<" total";
skipping to change at line 557 skipping to change at line 552
void OBJ::generateNormals() void OBJ::generateNormals()
{ {
const unsigned int *pTriangle = 0; const unsigned int *pTriangle = 0;
Vertex *pVertex0 = 0; Vertex *pVertex0 = 0;
Vertex *pVertex1 = 0; Vertex *pVertex1 = 0;
Vertex *pVertex2 = 0; Vertex *pVertex2 = 0;
float edge1[3] = {0.0f, 0.0f, 0.0f}; float edge1[3] = {0.0f, 0.0f, 0.0f};
float edge2[3] = {0.0f, 0.0f, 0.0f}; float edge2[3] = {0.0f, 0.0f, 0.0f};
float normal[3] = {0.0f, 0.0f, 0.0f}; float normal[3] = {0.0f, 0.0f, 0.0f};
float length = 0.0f; float invlength = 0.0f;
int totalVertices = getNumberOfVertices(); int totalVertices = getNumberOfVertices();
int totalTriangles = getNumberOfTriangles(); int totalTriangles = getNumberOfTriangles();
// Initialize all the vertex normals. // Initialize all the vertex normals.
for (int i=0; i<totalVertices; ++i) for (int i=0; i<totalVertices; ++i)
{ {
pVertex0 = &m_vertexArray[i]; pVertex0 = &m_vertexArray[i];
pVertex0->normal[0] = 0.0f; pVertex0->normal[0] = 0.0f;
pVertex0->normal[1] = 0.0f; pVertex0->normal[1] = 0.0f;
pVertex0->normal[2] = 0.0f; pVertex0->normal[2] = 0.0f;
skipping to change at line 612 skipping to change at line 607
pVertex2->normal[0] += normal[0]; pVertex2->normal[0] += normal[0];
pVertex2->normal[1] += normal[1]; pVertex2->normal[1] += normal[1];
pVertex2->normal[2] += normal[2]; pVertex2->normal[2] += normal[2];
} }
// Normalize the vertex normals. // Normalize the vertex normals.
for (int i=0; i<totalVertices; ++i) for (int i=0; i<totalVertices; ++i)
{ {
pVertex0 = &m_vertexArray[i]; pVertex0 = &m_vertexArray[i];
length = 1.0f / sqrtf(pVertex0->normal[0]*pVertex0->normal[0 invlength = 1.0f / sqrt(pVertex0->normal[0]*pVertex0->normal
] + [0] +
pVertex0->normal[1]*pVertex0->normal[1 pVertex0->normal[1]*pVertex0->normal
] + [1] +
pVertex0->normal[2]*pVertex0->normal[2 pVertex0->normal[2]*pVertex0->normal
]); [2]);
pVertex0->normal[0] *= length; pVertex0->normal[0] *= invlength;
pVertex0->normal[1] *= length; pVertex0->normal[1] *= invlength;
pVertex0->normal[2] *= length; pVertex0->normal[2] *= invlength;
} }
m_hasNormals = true; m_hasNormals = true;
} }
void OBJ::generateTangents() void OBJ::generateTangents()
{ {
const unsigned int *pTriangle = 0; const unsigned int *pTriangle = 0;
Vertex *pVertex0 = 0; Vertex *pVertex0 = 0;
Vertex *pVertex1 = 0; Vertex *pVertex1 = 0;
Vertex *pVertex2 = 0; Vertex *pVertex2 = 0;
float edge1[3] = {0.0f, 0.0f, 0.0f}; float edge1[3] = {0.0f, 0.0f, 0.0f};
float edge2[3] = {0.0f, 0.0f, 0.0f}; float edge2[3] = {0.0f, 0.0f, 0.0f};
float texEdge1[2] = {0.0f, 0.0f}; float texEdge1[2] = {0.0f, 0.0f};
float texEdge2[2] = {0.0f, 0.0f}; float texEdge2[2] = {0.0f, 0.0f};
float tangent[3] = {0.0f, 0.0f, 0.0f}; float tangent[3] = {0.0f, 0.0f, 0.0f};
float bitangent[3] = {0.0f, 0.0f, 0.0f}; float bitangent[3] = {0.0f, 0.0f, 0.0f};
float det = 0.0f; float det = 0.0f;
float nDotT = 0.0f; float nDotT = 0.0f;
float bDotB = 0.0f; float bDotB = 0.0f;
float length = 0.0f; float invlength = 0.0f;
int totalVertices = getNumberOfVertices(); int totalVertices = getNumberOfVertices();
int totalTriangles = getNumberOfTriangles(); int totalTriangles = getNumberOfTriangles();
// Initialize all the vertex tangents and bitangents. // Initialize all the vertex tangents and bitangents.
for (int i=0; i<totalVertices; ++i) for (int i=0; i<totalVertices; ++i)
{ {
pVertex0 = &m_vertexArray[i]; pVertex0 = &m_vertexArray[i];
pVertex0->tangent[0] = 0.0f; pVertex0->tangent[0] = 0.0f;
pVertex0->tangent[1] = 0.0f; pVertex0->tangent[1] = 0.0f;
skipping to change at line 749 skipping to change at line 744
nDotT = pVertex0->normal[0]*pVertex0->tangent[0] + nDotT = pVertex0->normal[0]*pVertex0->tangent[0] +
pVertex0->normal[1]*pVertex0->tangent[1] + pVertex0->normal[1]*pVertex0->tangent[1] +
pVertex0->normal[2]*pVertex0->tangent[2]; pVertex0->normal[2]*pVertex0->tangent[2];
pVertex0->tangent[0] -= pVertex0->normal[0]*nDotT; pVertex0->tangent[0] -= pVertex0->normal[0]*nDotT;
pVertex0->tangent[1] -= pVertex0->normal[1]*nDotT; pVertex0->tangent[1] -= pVertex0->normal[1]*nDotT;
pVertex0->tangent[2] -= pVertex0->normal[2]*nDotT; pVertex0->tangent[2] -= pVertex0->normal[2]*nDotT;
// Normalize the tangent. // Normalize the tangent.
length = 1.0f / sqrtf(pVertex0->tangent[0]*pVertex0->tangent [0] + invlength = 1.0f / sqrtf(pVertex0->tangent[0]*pVertex0->tang ent[0] +
pVertex0->tangent[1]*pVertex0->tangent [1] + pVertex0->tangent[1]*pVertex0->tangent [1] +
pVertex0->tangent[2]*pVertex0->tangent [2]); pVertex0->tangent[2]*pVertex0->tangent [2]);
pVertex0->tangent[0] *= length; pVertex0->tangent[0] *= invlength;
pVertex0->tangent[1] *= length; pVertex0->tangent[1] *= invlength;
pVertex0->tangent[2] *= length; pVertex0->tangent[2] *= invlength;
// Calculate the handedness of the local tangent space. // Calculate the handedness of the local tangent space.
// The bitangent vector is the cross product between the tri angle face // The bitangent vector is the cross product between the tri angle face
// normal vector and the calculated tangent vector. The resu lting // normal vector and the calculated tangent vector. The resu lting
// bitangent vector should be the same as the bitangent vect or // bitangent vector should be the same as the bitangent vect or
// calculated from the set of linear equations above. If the y point in // calculated from the set of linear equations above. If the y point in
// different directions then we need to invert the cross pro duct // different directions then we need to invert the cross pro duct
// calculated bitangent vector. We store this scalar multipl ier in the // calculated bitangent vector. We store this scalar multipl ier in the
// tangent vector's 'w' component so that the correct bitang ent vector // tangent vector's 'w' component so that the correct bitang ent vector
// can be generated in the normal mapping shader's vertex sh ader. // can be generated in the normal mapping shader's vertex sh ader.
skipping to change at line 930 skipping to change at line 925
AttributeVector attributeArray(m_numberOfTriangles); AttributeVector attributeArray(m_numberOfTriangles);
PosVector vertexCoords(m_numberOfVertexCoords); PosVector vertexCoords(m_numberOfVertexCoords);
VF2Vector textureCoords(m_numberOfTextureCoords); VF2Vector textureCoords(m_numberOfTextureCoords);
VF3Vector normals(m_numberOfNormals); VF3Vector normals(m_numberOfNormals);
VertCacheT vertexCache; VertCacheT vertexCache;
#define ADD_ATTRIB addFaceAttrib(attributeArray,numTriangles,activeM aterial,activeObject); #define ADD_ATTRIB addFaceAttrib(attributeArray,numTriangles,activeM aterial,activeObject);
float fTmp[3] = {0.0f}; float fTmp[3] = {0.0f};
double dTmp[3] = {0.0}; double dTmp[3] = {0.0};
Vec3f tmpNrm;
while (fscanf(pFile, "%s", buffer) != EOF) while (fscanf(pFile, "%s", buffer) != EOF)
{ {
switch (buffer[0]) switch (buffer[0])
{ {
case 'f': //! v, v//vn, v/vt, or v/vt/vn. case 'f': //! v, v//vn, v/vt, or v/vt/vn.
v[0] = v[1] = v[2] = 0; v[0] = v[1] = v[2] = 0;
vt[0] = vt[1] = vt[2] = 0; vt[0] = vt[1] = vt[2] = 0;
vn[0] = vn[1] = vn[2] = 0; vn[0] = vn[1] = vn[2] = 0;
skipping to change at line 1114 skipping to change at line 1110
break; break;
case 'v': //! v, vn, or vt. case 'v': //! v, vn, or vt.
switch (buffer[1]) switch (buffer[1])
{ {
case '\0': //! v case '\0': //! v
fscanf(pFile, "%lf %lf %lf", &dTmp[0], &dTmp[1], &dTmp[2]); fscanf(pFile, "%lf %lf %lf", &dTmp[0], &dTmp[1], &dTmp[2]);
switch(order) switch(order)
{ {
case XYZ:
vertexCoords
[numVertices] = VPos(dTmp[0],dTmp[1],dTmp[2]);
break;
case XZY: case XZY:
vertexCoords [numVertices] = VPos(dTmp[0],-dTmp[2],dTmp[1]); vertexCoords [numVertices] = VPos(dTmp[0],-dTmp[2],dTmp[1]);
break; break;
case YXZ:
default: //! XYZ vertexCoords
[numVertices] = VPos(dTmp[1],dTmp[0],dTmp[2]);
break;
case YZX:
vertexCoords
[numVertices] = VPos(dTmp[1],dTmp[2],dTmp[0]);
break;
case ZXY:
vertexCoords
[numVertices] = VPos(dTmp[2],dTmp[0],dTmp[1]);
break;
case ZYX:
vertexCoords
[numVertices] = VPos(dTmp[2],dTmp[1],dTmp[0]);
break;
default:
Q_ASSERT(0);
qDebug() <<
"OBJ::importSecondPass() vertex order not implemented. assuming XYZ.";
vertexCoords [numVertices] = VPos(dTmp[0],dTmp[1],dTmp[2]); vertexCoords [numVertices] = VPos(dTmp[0],dTmp[1],dTmp[2]);
break; break;
} }
++numVertices; ++numVertices;
break; break;
case 'n': //! vn case 'n': //! vn
fscanf(pFile, "%f %f %f", &f Tmp[0], &fTmp[1], &fTmp[2]); fscanf(pFile, "%f %f %f", &f Tmp[0], &fTmp[1], &fTmp[2]);
switch(order) switch(order)
{ {
// Only the first tw
o are known in practice.
case XYZ:
tmpNrm.set(f
Tmp[0],fTmp[1],fTmp[2]);
break;
case XZY: case XZY:
normals[numN ormals] = Vec3f(fTmp[0], -fTmp[2], fTmp[1]); tmpNrm.set(f Tmp[0], -fTmp[2], fTmp[1]);
break; break;
default: // all othe
default: //! XYZ rs: complain, but process as XYZ
normals[numN qDebug() <<
ormals] = Vec3f(fTmp[0],fTmp[1],fTmp[2]); "OBJ::importSecondPass() vertex order for normals not implemented. assuming
XYZ.";
tmpNrm.set(f
Tmp[0],fTmp[1],fTmp[2]);
break; break;
} }
tmpNrm.normalize();
normals[numNormals] = tmpNrm
;
++numNormals; ++numNormals;
break; break;
case 't': //! vt case 't': //! vt
fscanf(pFile, "%f %f", &fTmp [0], &fTmp[1]); fscanf(pFile, "%f %f", &fTmp [0], &fTmp[1]);
textureCoords[numTexCoords] = Vec2f(fTmp[0],fTmp[1]); textureCoords[numTexCoords] = Vec2f(fTmp[0],fTmp[1]);
++numTexCoords; ++numTexCoords;
break; break;
default: default:
 End of changes. 14 change blocks. 
29 lines changed or deleted 59 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/