Results 1 to 8 of 8

Thread: Render an .OBJ file to the screen

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2013
    Posts
    5

    Angry Render an .OBJ file to the screen

    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:
    Code :
    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:

    Code :
    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.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,108
    Quote Originally Posted by Dako300 View Post
    Code :
    	for(int i = 2;i <= coordcount-1;i++){
    Shouldn't that be i+=3 ?

  3. #3
    Junior Member Newbie
    Join Date
    Oct 2013
    Posts
    5
    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.

  4. #4
    Junior Member Newbie
    Join Date
    Oct 2013
    Posts
    5
    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.

  5. #5
    Junior Member Newbie
    Join Date
    Oct 2013
    Posts
    5
    Can anyone help?

  6. #6
    Junior Member Newbie
    Join Date
    Sep 2008
    Posts
    25
    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.

  7. #7
    Member Contributor
    Join Date
    Jun 2013
    Posts
    71
    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

  8. #8
    Junior Member Newbie
    Join Date
    Oct 2013
    Posts
    5
    I will try and rewrite it to use the faces and use structs instead of the variables.

Similar Threads

  1. render to screen and file
    By christus in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 05-18-2007, 11:29 PM
  2. render to file without window
    By SoG in forum OpenGL: Basic Coding
    Replies: 9
    Last Post: 06-22-2004, 01:38 AM
  3. Render to a file
    By jesusgumbau in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 09-10-2002, 05:28 AM
  4. render view to avi file
    By manu in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 04-02-2002, 05:44 AM
  5. render view into avi file
    By manu in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 03-30-2002, 10:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean