Problems in rendering an OBJ file

Hi guys,

I am currently trying to read an Obj file and then store the vertices in a big array and render the model using vertex array. Well I wouldnt be needing the face indices data … i guess. So I am using glDrawArrays() or glArrayElements() to render the vertices. I am pasting the code to read from the file the vertex data … as thats the most important thing…

int LoadObjectIntoArrays()
{
	//float VertexArray[10] ;
	unsigned int gVertexIndex = 0 ;
	unsigned int TempVertex = 0 ; 
	int nFaces = 0 ;

	FILE *fp ;
	fp = fopen("c:\\sp3.obj","r") ;
	
	if(fp == NULL)
		return -1 ;

	char str[50] , Temp[50] ;
	char *Token = NULL ;
	float Coord[3] ;
	

	//Loading the vertex array.
	while(fgets(str , 50 , fp) != NULL) 
	{
		if(str[0] == 'v' && str[1] == ' ')
		{
			Token = strtok(str , " ") ;
			for(int i = 0 ; i < 3 ; i++)
			{
				Token = strtok(NULL , " ") ;
				strcpy(Temp , Token) ;
				Temp[strcspn(Temp, " ")] = 0 ;
				Coord[i] = (float) atof(Temp) ;
				VertexArray[gVertexIndex++] = Coord[i] ;
//				printf("%d %f
" , gVertexIndex , VertexArray[TempVertex++]) ;
			}

			//Find the minimum and maximum in the x and y axis.
			if(MinX > Coord[0])
			{
				MinX = Coord[0] ;
			}

			if(MinY > Coord[1])
			{
				MinY = Coord[1] ;
			}

			if(MaxX < Coord[0])
			{
				MaxX = Coord[0] ;
			}

			if(MaxY < Coord[1])
			{
				MaxY = Coord[1] ;
			}

			if(MinZ > Coord[2])
			{
				MinZ = Coord[2] ;
			}

			if(MaxZ < Coord[2])
			{
				MaxZ = Coord[2] ;
			}

		}
		nVertices++ ;	
	}

	printf("The no. of Vertices : %d",nVertices) ;

	return 1 ;

}

Well as far as i see i dont see any errors and i debugged it and saw that the array was getting populated with data from the file.

Now heres my Initialisation function :

GLvoid Init()
{
	glShadeModel(GL_SMOOTH) ;
	glClearColor(0.0f , 0.0f , 0.0f , 1.0f) ;
	glClearDepth(1.0f) ;
	glDepthFunc(GL_LEQUAL) ;
	glEnable(GL_DEPTH_TEST) ;
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) ;
	
	LoadObjectIntoArrays() ;
	glEnableClientState(GL_VERTEX_ARRAY) ;
	glVertexPointer(3 , GL_FLOAT , 0 , VertexArray) ;
	
CameraObj.PositionCamera(0.0f , 0.0f , 7*(MaxZ - MinZ) , (MaxX - MinZ)/2 , (MaxY - MinY)/2 , (MaxZ - MinZ)/2 , 0.0f , 1.0f , 0.0) ;
}

and heres my rendering code:

GLvoid RenderScene()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;	
	glLoadIdentity() ;

	gluLookAt(CameraObj.mPosition.x , CameraObj.mPosition.y , CameraObj.mPosition.z ,
		CameraObj.mView.x , CameraObj.mView.y , CameraObj.mView.z ,
		CameraObj.mUp.x , CameraObj.mUp.y , CameraObj.mUp.z) ;
	

	//Draw the x , y , z Axis and the grid initially.
	DrawAxis() ;
	Draw3DGrid() ;

	//glPushMatrix() ;

	//glTranslatef(MoveX , 0.0f , MoveZ) ;


	

	//glDrawArrays(GL_POINTS , 0 , 2000) ;
	glPointSize(4.0f) ;
	glBegin(GL_POINTS) ;
	for(int i = 0 ; i < 50 ; i++)
	{
		glArrayElement(i) ;	
	}
	glEnd() ;


	glFlush() ;

	glutSwapBuffers() ;
}

I dont understand I am unable to get any output at all. Can any one see if i am doing it all correctly and if you want the whole code, i’ll be more than happy to paste it here.

Thanx

Yours

Siddharth

What do you mean, you “won’t be needing the face indices data”??? Without this, your model won’t have any faces!

All that the vertex data does is describe a set of 3D points in space. The face indices are crucial, as they tell you which vertexes you need to actually make up each face, and in which order.

It looks as though your code is only reading the ‘v’ lines of the object and nothing else. Unfortunately, it’s not as simple as this!! You need to parse the ‘f’ lines too…

I suggest you do some reading up on the OBJ file format before you go further:

http://www.robthebloke.org/source/obj.html

And while I’ve never used glArrayElements() myself, if you want to just display your vertices, you could use something like:

gl.glBegin(GL_POINTS);
gl.glVertex3f(vert_x[0],vert_y[0],vert_z[0]);
gl.glVertex3f(vert_x[1],vert_y[1],vert_z[1]);
…etc

gl.glEnd();

Where the parameters you pass to the glVertex3f command are the x, y, z coords for each of your vertices.

It won’t look very pretty, but at least it should work.

Hope this helps anyway. :wink:

Dan.

hey, thanx for your reply. Yes I understood my folly and actually got the obj file parser done. But theres one thing I dont get… in the obj file format. I dont understand the vertex normals in the reference group. For example, in the line f v/vt/vn , the third group vn is the vertex normal and its not the same as the normals given in the line beginning with vn. And these are certainly wrong. So wats the difference between the vn in the triplets of data and the vn in each single line. I used vn that is in each single line and the lighting worked well…

thanx

No probs.

I’m not sure what the difference is to be honest. I’ve never used these ‘vn’ lines myself - I always calculate the normals myself from the vertex data when I read the objects in.

My program needs to re-calculate the normals at various stages anyway, so it’s no problem for me to re-use this code in my object loader. :slight_smile:

Glad you’ve got it sorted for yourself anyway! :smiley:

Dan.