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