Hello!
I have written a small .obj file loader, to be able to draw the model with JOGL. Im able to parse the .obj file and retrieve all the vertices and faces, storing them in two ArrayLists (vertices and faces). These ArrayLists contains two object classes, Vertex and Face:
Vertex class:
This class contains three public float variables: x, y and z.
Face class:
This class contains an int array of the index numbers of the vertices for a polygon.
The .obj file im trying to load is a cube. I got this code from (http://www.eg-models.de/formats/Format_Obj.html).
#Cube.obj
Vertices…
v 1 1 1
v 1 1 -1
v 1 -1 1
v 1 -1 -1
v -1 1 1
v -1 1 -1
v -1 -1 1
v -1 -1 -1
Faces…
f 1 3 4 2
f 5 7 8 6
f 1 5 6 2
f 3 7 8 4
f 1 5 7 3
f 2 6 8 4
Now, the parsing goes fine, and all the values in the ArrayLists are correct. Although, when trying to display the cube, and rotate it, the cube looks weird.
(Ive created an applet, which can be viewed here: http://www.geocities.com/java_12345x/ ).
Im using some lighting code which i got form a tutorial, had to convert it from C code to Java, but this code seems to work.
But Im not sure if Ive understood the .obj format correctly. Each line in an .obj file starting with a ‘v’ is a vertex. My understanding is, that the vertices are numbered in the sequence they appear in the file.
The lines starting with ‘f’ are faces, containing a reference to vertices, indexed by the sequence they appear in the file.
Example .obj file
v 1 1 1
v 0 1 1
v 0 1 0
f 3 1 2
If you look at the applet above, there is clearly something wrong. The loader can display all the .obj files I have been able to download from the net, but they dont look too good. Seems like some of the faces are drawn the “wrong way”, They are either completely black or transparent.
So my question is, have I completely misunderstood the .obj file format, or am I drawing them wrong? Im no 3D expert and most of the openGL code is not written by me, but Ive got fragments of it from different tutorials on the net. So I dont claim to have written any of this OpenGL code.
The code I use to display is below:
#################################
public void display(GLDrawable glDrawable)
{
GL myGL = glDrawable.getGL();
myGL.glMatrixMode(GL.GL_PROJECTION);
myGL.glLoadIdentity();
int width = canvas.getWidth();
int height = canvas.getHeight();
float aspect = height / (float) width;
myGL.glFrustum(-10,10,-10*aspect,10*aspect,20,2000);
myGL.glTranslatef(0, 0, -25);
myGL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
/**
* Light...
*/
float light_position[]={ 10.0f, 10.0f, -10.0f, 1.0f };
float light_color[]={ 1.0f, 1.0f, 1.0f, 1.0f };
float ambient_color[]={ 0.2f, 0.2f, 0.2f, 1.0f };
float mat_specular[]={ 1.0f, 1.0f, 1.0f, 1.0f };
myGL.glClearColor( 0.0f, 0.0f, 1.0f, 0.0f );
myGL.glShadeModel(GL.GL_SMOOTH); // use gourand shading
myGL.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, mat_specular );
myGL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position );
myGL.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, ambient_color );
myGL.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, light_color );
myGL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, light_color );
myGL.glLightModeli(GL.GL_LIGHT_MODEL_TWO_SIDE, 1 ); // two-sided lighting
myGL.glEnable(GL.GL_LIGHTING); // so lighting models are used
myGL.glEnable(GL.GL_LIGHT0); // we'll use LIGHT0
/**
* Light end..
*/
//myGL.glColor3f(1.0f,0.5f,0.0f);
//myGL.glRotatef(-90, 1, 0, 0);
//myGL.glRotatef(45, 0, 0, 1);
myGL.glRotatef(angle, 0, 1, 0);
myGL.glScalef( 3, 3, 3 );
/**
* Draw the .obj file..
*/
for( Face face : faces )
{
int[] vertexNumbers = face.getVertexNumbers();
myGL.glBegin( GL.GL_POLYGON );
for( int vertexNumber : vertexNumbers )
{
Vertex vertex = vertices.get( vertexNumber -1 );
myGL.glVertex3f( vertex.x, vertex.y, vertex.z );
}
myGL.glEnd();
}
}