#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].x, Points[Faces.i].y, Points[Faces.i].z);
glVertex3f(Points[Faces.j].x, Points[Faces.j].y, Points[Faces.j].z);
glVertex3f(Points[Faces.k].x, Points[Faces.k].y, Points[Faces.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.x, &Points.y, &Points.z);
}
// read the faces now ...
for (i = 1; i<= numFaces; i++) {
fscanf(fin, "%c %d %d %d\n", &c, &Faces.i, &Faces.j, &Faces.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;
}
OBJ loader problems.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement