Displaying and manipulating -

Hi all - I’m posting because I’m in need of a lot of help here :slight_smile: I need to create a program that imports a .obj file and displays it, then I should be able to rotate it, move it, squish it, etc etc. The teacher will NOT help me because he says it is all in the books - I’m sure it is, but I cannot piece all the information together to work for me. I’ve been through them and online from gametutorials to this site and I still cannot figure out how to make this work. If you’ll notice, the program (substitute any .obj file in the main) won’t display the .obj file, but will display the cube when that part is commented out. I assume I can just manipulate the camera using LookAt or gluPerspective to handle all the other manips, right? Speaking of which, I cannot figure out which way to make the camera aim so that I hit the object - it could be anywhere, where do I find it? Any help would be greatly, greatly appreciated - I can also be reached at thekingironfist@hotmail.com

Thanks a lot

CODE EDITED OUT FOR SPACE - new code below :slight_smile:

Calculate min/max values for all three directions, then it is easy to create a bounding box for the loaded object, then you can aim your camera at the center of the bounding box, careful selection of distance and fov value will make sure all of the object is visible.


first of all, you’re attempting to draw the entire 3D object as 1 polygon glBegin(GL_POLYGON).

I don’t think the .obj file is defined that way.
try changing that to glBegin(GL_TRIANGLES).

furthermore, you need to clear the depth buffer bits in addition to the color bits in your display function otherwise you’re going to get some wacky output. ensure depth testing is enabled in your init call by using glEnable(GL_DEPTH_TEST).

it appears as though you’re not using lighting…you’ll want this later because the way it is now, you’ll see your 3D object but it’ll appear as a silhouette with no contrast between the various faces.

anyway…get the object displaying then we can talk more about lighting and material colors.

good luck.

Ok, well I changed it to GL_TRIANGLES, added GL_DEPTH_TEST, and changed my storage style to vectors. I’ve pasted the new code below if you care to compile it - I really have no idea why the screen stays black - let me just describe how I made the vectors in case its confusing. I access the face_vect[i].vert1 to get the number of which vector in the object file I need, then I grab that one with vert_vect[…] and get the proper x/y/z coords by appending them on the end like vert_vect[face_vect[i].WHICHVERTEX].x and so on. I do that three times to draw a triangle using that face. Okay, thanks for any help. I really appreciate it :slight_smile:


Ok - here’s just the init, display, reshape and main - I can display the object mostly now, but how can I make it appear 3D or manipulate it? I’m using glut for this.

 void init ()
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
	glClearDepth(1.0f);									// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do


void display ()
	glPushMatrix ();

	glTranslatef(0,0, nearfar);
		for(int i = 0; i < face_vect.size(); i++)
			glColor3f(1.0, 1.0, 1.0);
			glVertex3f(vert_vect[face_vect[i].vert1].x, vert_vect[face_vect[i].vert1].y, vert_vect[face_vect[i].vert1].z);
			glColor3f(1.0, 1.0, 1.0);
			glVertex3f(vert_vect[face_vect[i].vert2].x, vert_vect[face_vect[i].vert2].y, vert_vect[face_vect[i].vert2].z);
			glColor3f(1.0, 1.0, 1.0);
			glVertex3f(vert_vect[face_vect[i].vert3].x, vert_vect[face_vect[i].vert3].y, vert_vect[face_vect[i].vert3].z);

	glutSwapBuffers ();


void reshape (int newWidth, int newHeight)
	glViewport(0, 0, (GLsizei)newWidth, (GLsizei)newHeight);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	gluPerspective (60.0, (float)newWidth/(float)newHeight, 0.0, 3000.0);
	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity ();

	glutSwapBuffers ();

	glutInit (&argc, argv);
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize (500, 500);
	glutInitWindowPosition (0, 0);
	glutCreateWindow ("OpenGL Program 1");

	init ();
	glutDisplayFunc (display);
	glutReshapeFunc (reshape);
	glutKeyboardFunc (keyboard);
	glutMouseFunc (mouse);
	glutMainLoop (); 

I can display the object mostly now, but how can I make it appear 3D or manipulate it?
So I assume you’ve got the object displaying at least at this point.

as a side note: you don’t need to set the color before each vertex call. set the color once just before your for loop if all triangles are to have the same color anyway. This eliminates redundant function calls as well as unnecessary OpenGL validations.

so…to make it look 3D you need to enable lighting.

put this code in your init function:

//set up first light source as a point light.
//you need to play around with the light position
//I'm not sure how your world space is set up.
//You might want to render a gluSphere centered
//at your light position to serve as a visual aid.
GLfloat pos[4] = {?,?,?,1.0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 0.0f);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 180.0f);

//enable materials

//lighting needs a surface normal for each face
//take the easy way out (this normally does not produce the best results) - tell OpenGL to generate normals for us.


AND in your display function, right where you should’ve had the call to glColor* (just outside the for loop) place a call to set the material color like so:

GLfloat shininess = ?; //find a value that you like.
glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, shininess);

That’s a quick overview of what needs to be done, hope i didn’t forget anything…let me know if it displays properly.