I wrote a .obj loader for openGl. The geometry loads fine, but the normals are always mixed up. I tried to export models in 2 different programs and nothing works. As far as I know, this is how you set the normal mode for GL_TRIANGLES
glNormal3fv(norm1); glVertex3fv(vert1); glNormal3fv(norm2); glVertex3fv(vert2); glNormal3fv(norm3); glVertex3fv(vert3);
(Normals refer to GLfloats in the rest of the code.)
EDITOR: Here is a picture of an isohedron with broken normals 
This is the complete code and obj loader file:
void loadOBJFromFile(NSString *path,float movex,float movey) { NSString *contentns = [[NSString alloc]initWithContentsOfFile:path encoding:NSASCIIStringEncoding error:NULL]; NSArray *pieces = [contentns componentsSeparatedByString:@"#"]; //creating the arrays to read the vertecies and normals from. NSArray *normals = [[pieces objectAtIndex:3]componentsSeparatedByString:@"\n"]; NSArray *vertecies = [[pieces objectAtIndex:2]componentsSeparatedByString:@"\n"]; //The +1 is to make sure we ignore the texture/ material definition that vomes before the faces. int normalCount = [[normals objectAtIndex:0]intValue]+2; int faceCount = [[normals objectAtIndex:normalCount]intValue]; //glTranslatef(movex, 0, movey); glBegin(GL_TRIANGLES); { for (int i = 0; i < faceCount;i++) { //aquires all the numbers in thye current face. NSArray *currentFace = [[normals objectAtIndex:normalCount+i+1]componentsSeparatedByString:@" "]; NSArray *v1 = [[currentFace objectAtIndex:1]componentsSeparatedByString:@"//"]; NSArray *v2 = [[currentFace objectAtIndex:2]componentsSeparatedByString:@"//"]; NSArray *v3 = [[currentFace objectAtIndex:3]componentsSeparatedByString:@"//"]; //crewatres the arrays to contain the vertecies NSArray *vertex1 = [[vertecies objectAtIndex:[[v1 objectAtIndex:1]intValue]]componentsSeparatedByString:@" "]; NSArray *vertex2 = [[vertecies objectAtIndex:[[v2 objectAtIndex:1]intValue]]componentsSeparatedByString:@" "]; NSArray *vertex3 = [[vertecies objectAtIndex:[[v3 objectAtIndex:1]intValue]]componentsSeparatedByString:@" "]; //creates all the arrays for the normals NSArray *normal1 = [[normals objectAtIndex:[[v1 objectAtIndex:1]intValue]]componentsSeparatedByString:@" "]; NSArray *normal2 = [[normals objectAtIndex:[[v2 objectAtIndex:1]intValue]]componentsSeparatedByString:@" "]; NSArray *normal3 = [[normals objectAtIndex:[[v3 objectAtIndex:1]intValue]]componentsSeparatedByString:@" "]; //creates the vertecies coordinates GLfloat vert1[] = {[[vertex1 objectAtIndex:1]floatValue],[[vertex1 objectAtIndex:2]floatValue],[[vertex1 objectAtIndex:3]floatValue]}; GLfloat vert2[] = {[[vertex2 objectAtIndex:1]floatValue],[[vertex2 objectAtIndex:2]floatValue],[[vertex2 objectAtIndex:3]floatValue]}; GLfloat vert3[] = {[[vertex3 objectAtIndex:1]floatValue],[[vertex3 objectAtIndex:2]floatValue],[[vertex3 objectAtIndex:3]floatValue]}; //creates the normals coordinates GLfloat norm1[] = {[[normal1 objectAtIndex:1]floatValue],[[normal1 objectAtIndex:2]floatValue],[[normal1 objectAtIndex:3]floatValue]}; GLfloat norm2[] = {[[normal2 objectAtIndex:1]floatValue],[[normal2 objectAtIndex:2]floatValue],[[normal2 objectAtIndex:3]floatValue]}; GLfloat norm3[] = {[[normal3 objectAtIndex:1]floatValue],[[normal3 objectAtIndex:2]floatValue],[[normal3 objectAtIndex:3]floatValue]}; glNormal3fv(norm1); glVertex3fv(vert1); glNormal3fv(norm2); glVertex3fv(vert2); glNormal3fv(norm3); glVertex3fv(vert3); }} glEnd();
}
obj file:
#Wavefront OBJ file created by Hexagon 2 mtllib object.mtl g Form0 usemtl def_surf_mat
source share