Sign in to follow this  
Stranger

OBJ loader problems.

Recommended Posts

Stranger    160
Ok I have the code for loading an obj model into OGL. However when I try to load a model exported from 3dsmax the model is completely wrong. Please have a look at the code and tell me what I am doing wrong.
#include <windows.h>
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>


float angle = 0;

// this structure defines a point in 3-space
struct Point3d {
	double x, y, z;
};


// this structure defines a triangular facs with identifiable by its 
// corners as i, j and k   
struct face {

   int i, j, k;

};


// define an upper value for the number of triangles and points
struct face Faces[10000];
struct Point3d  Points[10000];
int numPoints=0;
int numFaces=0;


void init ( void )
{

	glClearColor(0.0, 0.0, 0.0, 0.0); // paint the window background with black
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glShadeModel(GL_FLAT);
	glMatrixMode(GL_PROJECTION); // set the view volume shape
	glLoadIdentity();
	glOrtho(-30.0, 30.0, -30.0, 30.0, -30.0, 30.0);
	//glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);
  
}


void RotateModel ()
{

 glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glRotatef(30, 1.0, 0.0, 0.0);
  glRotatef(angle, 0.0, 1.0, 0.0);

   angle=angle+1;
  if ( angle>=360 )
    angle=angle-360;


}

void mouse(int button, int state, int x, int y)
{
  if (button == GLUT_LEFT_BUTTON) {
    if (state == GLUT_DOWN) {
    
	RotateModel ();
	
    glutPostRedisplay();

	
    }
    if (state == GLUT_UP) {
      
    }
  }


  
}


void  drawModel()
{
  

	int i;


	glColor3f(1.0, 0.0, 0.0); 
   for (i = 1; i<= numFaces; i++) {

    
  glBegin(GL_LINE_LOOP);
    glVertex3f(Points[Faces[i].i].x, Points[Faces[i].i].y, Points[Faces[i].i].z);
    glVertex3f(Points[Faces[i].j].x, Points[Faces[i].j].y, Points[Faces[i].j].z);
	glVertex3f(Points[Faces[i].k].x, Points[Faces[i].k].y, Points[Faces[i].k].z);
  glEnd();
   }
  




}

void display ( void )
{
  
	
  glClearColor(0.0, 0.0, 0.0, 0.0); // paint the window background with black
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 

  drawModel();
  glFlush();
  glutSwapBuffers();
  

}


void readModel ()
{


	FILE *fin;

	char c;
	int i;

	fin = fopen("house.obj", "r");

// read through the file once to count the exact number of points and faces
  while (!feof(fin))  {     
    c=fgetc(fin);   // reads a character from a file 
 
	// if we come across a "v" then its a vertex i.e. a point
	if(c == 'v')  { numPoints++; } 

	// if we come across a "f" then its a face i.e. a tringle definition
	if(c == 'f')  { numFaces++; }
   }

   fclose(fin);


 
// open to read the actual points and faces now
   fin = fopen("house.obj", "r");

   // now we know the number of points we can read them off from the file
   // and save them in the memory
   for (i = 1; i<= numPoints; i++) {

  fscanf(fin, "%c %lf %lf %lf\n", &c, &Points[i].x,  &Points[i].y, &Points[i].z);

   }

 // read the faces now ... 
   for (i = 1; i<= numFaces; i++) {
  fscanf(fin, "%c %d %d %d\n", &c, &Faces[i].i,  &Faces[i].j, &Faces[i].k);
   }
  
   fclose(fin);



}

int main ( int argc, char** argv )
{

   readModel ();

   glutInit(&argc,argv);
   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
   glutInitWindowSize(500, 500);
   glutCreateWindow("Cube");
   init();
   glutMouseFunc(mouse);
   glutDisplayFunc(display);
   glutMainLoop();
   return 0;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this