PDA

View Full Version : Render an .OBJ file to the screen



Dako300
10-19-2013, 11:23 AM
A freind of mine and myself are trying to make a map for a game that I am working on. He made the map and the faces are triangulated to go into my .OBJ loader (it just reads off all of the verticies and stores them into an array in order). HOWEVER, when I try and render it to the screen, it seems like all of the faces are tilted. Here is the code for the rendering:

for(int i = 2;i <= coordcount-1;i++){
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(coord[i-2][0],coord[i-2][1],coord[i-2][2]);
glVertex3f(coord[i-1][0],coord[i-1][1],coord[i-1][2]);
glVertex3f(coord[i][0],coord[i][1],coord[i][2]);
}
glEnd( );

and this is the object loader:


ifstream objfile ("triangulatedrockyhills.obj"); //open the .obj file
while(!(objfile.eof())){
string line;
stringstream ss;
string trash;
getline(objfile,line);
ss.str(line);
if(line[0] == 'v' && line[1] == ' '){
ss >> trash >> coord[coordcount][0] >> coord[coordcount][1] >> coord[coordcount][2];
printf("V\tI:%d\tX:%f\tY:%f\tZ:%f\t\n",coordcount,coord[coordcount][0],coord[coordcount][1],coord[coordcount][2]);
coordcount++;
}else if(line[0] == 'v' && line[1] == 'n'){
ss >> trash >> normcoord[normcoordcount][0] >> normcoord[normcoordcount][1] >> normcoord[normcoordcount][2];
printf("VN\tI:%d\tX:%f\tY:%f\tZ:%f\t\n",normcoordcount,coord[normcoordcount][0],coord[normcoordcount][1],coord[normcoordcount][2]);
normcoordcount++;
}else if(line[0] == 'f' && line[1] == ' '){
ss >> trash >> face[facecount][0] >> face[facecount][1] >> face[facecount][2];
printf("F\tI:%d\tA:%s\tB:%s\tC:%s\t\n",facecount,(char *)face[facecount][0].c_str(),(char *)face[facecount][1].c_str(),(char *)face[facecount][2].c_str());
facecount++;
}
}

not sure exactly where the problem is (if it is in the loader or the rendering) but I decided to post it here since I have more experience in the coding than OpenGL. I hope you can help.

GClements
10-20-2013, 06:34 AM
for(int i = 2;i <= coordcount-1;i++){


Shouldn't that be i+=3 ?

Dako300
10-20-2013, 08:49 AM
I tried that and that does not work (I think an OBJ file makes 2 points and any others go off of the 2 previous ones to make a triangle). I also checked the OBJ loader and it works perfectly. Here is an image of the OBJ loader failing to render a file.https://dl.dropboxusercontent.com/u/72671460/2013-10-19-121928_1024x768_scrot.png

Dako300
10-20-2013, 10:43 PM
Here is the full program if that will help:https://dl.dropboxusercontent.com/u/72671460/main.cpp

This is the program rendering a much simpler shape, a triangulated cube.https://dl.dropboxusercontent.com/u/72671460/2013-10-20-233903_1024x768_scrot.png

Dako300
10-21-2013, 12:25 AM
Can anyone help?

Steven Katic
10-21-2013, 06:58 AM
for starters, the face data is used to index into the array of vertices, i.e you'll need to rewrite your render loop to use the face data to index into the array of vertices to form the triangles correctly.
e.g. suppose one face is defined as 6, 7, 1
then the sixth, seventh and first vertices you read in will form one triangle.

sandbucket
10-21-2013, 09:08 AM
not to sound critical but im surprised this loader even works for you. ive looked at some obj files and they do things like describing a shape through indexed triangles and then suddenly switching to describing it in a long line strip or triangle strip or whatever. if you want a goo obj loader i think you're gonna need alot more than what you've just written. ive been thinking about how to write one and i think the easiest way would be to store all your data in a structure and include variables to indicate when it switches between primitive types. then you can write a display function to display these structures. thats what i would do. To make a good one, is gonna take alot of work. sorry :(

Dako300
10-21-2013, 03:18 PM
I will try and rewrite it to use the faces and use structs instead of the variables.