myism
04-23-2007, 05:25 AM
I am writing a cpp/cg application that involves reading obj files exported from maya. In order to draw the imported model via glDrawElement (i.e. allows only one indices array) I rearranged the normal and uv arrays as follows:
void Obj::rearrangeData()
{
vector<float> tmpUV;
vector<float> tmpN;
int size = faces.size();
int normalSize = normal.size();
int uvSize = uv.size();
int psize = pos.size();
tmpUV.assign(uv.begin(), uv.end());
tmpN.assign(normal.begin(), normal.end());
normal.clear();
normal.resize(psize);
uv.clear();
uv.resize(psize*2/3);
for (int i = 0; i < size; i++) {
for (int j = 0; j < 3; j++) {
int iP = ((ObjFace)faces[i]).v[j].indexP - 1;
int iN = ((ObjFace)faces[i]).v[j].indexN - 1;
int iUV = ((ObjFace)faces[i]).v[j].indexUV - 1;
indices.push_back(iP);
uv[iP*2] = tmpUV[iUV*2];
uv[iP*2+1] = tmpUV[iUV*2+1];
normal[iP*3] = tmpN[iN*3];
normal[iP*3+1] = tmpN[iN*3+1];
normal[iP*3+2] = tmpN[iN*3+2];
}
}
//checkNormal(tmpN);
}
However, when I tried to draw the model in my own application, the normals are way off.
Screenshot with lighting and texture on:
http://mengyang.org/Documents/lightinghead_bad.jpg
Screenshot with only texture (it implies the UVs are correct):
http://mengyang.org/Documents/Karen_Texture.jpg
Screenshot for the diffuse lighting map only:
http://mengyang.org/Documents/lightingmap_bad.jpg
Here's the thing that really confuses me:
In order to test the rearranged data, I write the exact model data that I used to draw the geometry back to another obj file, import the file into Maya, and the model, normals and UV's just look correct.
I also tested the DrawGeometry function in my application on other simple geometry such as a sphere, it works fine, i.e. the cg and opengl part should be OK. I suspect the problem is due to the rearrangement of Normals or something like that.
void Obj::rearrangeData()
{
vector<float> tmpUV;
vector<float> tmpN;
int size = faces.size();
int normalSize = normal.size();
int uvSize = uv.size();
int psize = pos.size();
tmpUV.assign(uv.begin(), uv.end());
tmpN.assign(normal.begin(), normal.end());
normal.clear();
normal.resize(psize);
uv.clear();
uv.resize(psize*2/3);
for (int i = 0; i < size; i++) {
for (int j = 0; j < 3; j++) {
int iP = ((ObjFace)faces[i]).v[j].indexP - 1;
int iN = ((ObjFace)faces[i]).v[j].indexN - 1;
int iUV = ((ObjFace)faces[i]).v[j].indexUV - 1;
indices.push_back(iP);
uv[iP*2] = tmpUV[iUV*2];
uv[iP*2+1] = tmpUV[iUV*2+1];
normal[iP*3] = tmpN[iN*3];
normal[iP*3+1] = tmpN[iN*3+1];
normal[iP*3+2] = tmpN[iN*3+2];
}
}
//checkNormal(tmpN);
}
However, when I tried to draw the model in my own application, the normals are way off.
Screenshot with lighting and texture on:
http://mengyang.org/Documents/lightinghead_bad.jpg
Screenshot with only texture (it implies the UVs are correct):
http://mengyang.org/Documents/Karen_Texture.jpg
Screenshot for the diffuse lighting map only:
http://mengyang.org/Documents/lightingmap_bad.jpg
Here's the thing that really confuses me:
In order to test the rearranged data, I write the exact model data that I used to draw the geometry back to another obj file, import the file into Maya, and the model, normals and UV's just look correct.
I also tested the DrawGeometry function in my application on other simple geometry such as a sphere, it works fine, i.e. the cg and opengl part should be OK. I suspect the problem is due to the rearrangement of Normals or something like that.
