Contents / Previous / Next


OBJ Format

Some info on different formats and C++ code can be downloaded form: http://www.wotsit.org/list.asp?fc=2 From JautOGL http://today.java.net/pub/a/today/2006/10/10/development-of-3d-multiplayer-racing-game.html The engine uses a parser of Wavefront format files in order to load 3D models. The Wavefront format specification defines two types of files. The .obj files describe the geometry and the .mtl files include information about the materials and lights, which are connected with the geometry of the .obj files. The specification defines many complex geometrical characteristics and shapes. Some of them include Bezier curves, B-splines, Taylor polynomial curves, and various surfaces. The parsers of JautOGL are focused on the basics in order to reduce programming complexity and mathematical calculations that OpenGL easily does through its functions. The 3D models of the cars have been produced using 3D Studio Max 8, which exports the models and creates the Wavefront format files. Furthermore, JautOGL loads textures and images and applies them in geometry. The classes include a range of methods that manage the textures and apply techniques like mipmapping.


The JautOGL game engine includes parsers that load the 3D models by reading the information of the Wavefront format files. There are two classes responsible for this game element: GLModel and MtlLoader. Each one loads the information from the .obj and .mtl files, respectively. The files are read line by line and the parser looks for an identifier at the beginning of each line. For example, the format v x y z loads a vertex at the (x,y,z) coordinates of the 3D environment, while vn x y z loads a vertex normal at the (x,y,z). The .mtl files contain information about the ambient, diffuse, and specular lighting. JautOGL loads each one in an ArrayList or in multidimensional arrays. The splitting and the identification of the these strings relies on the StringTokenizer class and the methods trim(), startsWith(), and charAt(). As soon as JautOGL has stored all the information in array lists, it must then be transferred to the OpenGL pipeline. The information is loaded by using display lists (glGenLists()), which are an efficient and flexible way to retrieve such data. An example of simplified code of the class GLModel follows:

Code in the init() function of the OpenglCore, to load textures and 3D models:

 public void init(GLDrawable drawable) {


   protected TextureManager texture_manager;
   protected GLModel model1;
   protected GLModel model2;

       texture_manager = TextureManager.getInstance( gl, glu );
       gl.glEnable( GL.GL_TEXTURE_2D );
       gl.glShadeModel( GL.GL_SMOOTH );

       //the paths of the 3D models
       String path1 = "models/formula.obj";
       String path2 = "models/formula.obj";

   //here the needed textures for the race-ground are loaded
       try{
               texture_manager.createManagedTexture
                       ("road", "textures/roads/road.jpg", 
                        GL.GL_TEXTURE_2D, GL.GL_RGB, GL.GL_RGB, GL.GL_LINEAR,
                        GL.GL_LINEAR, true, true );
                       // ...
                       //in the same way we load each texture is needed...
                       //...

               //a file input stream reads the data and stores them in 
               //a buffer reader for each 3D model
               FileInputStream r_path1 = new FileInputStream(path1);
               BufferedReader b_read1 =
                    new BufferedReader(new InputStreamReader(r_path1));
               model1 = new GLModel(b_read1, true, "models/formula.mtl", gl);
               r_path1.close();
               b_read1.close();

               FileInputStream r_path2 = new FileInputStream(path2);
               BufferedReader b_read2 =
                    new BufferedReader(new InputStreamReader(r_path2));
               model2 = new GLModel(b_read2, true, "models/formula.mtl", gl);
               r_path2.close();
               b_read2.close();
       }
       catch( Exception e ){
               System.out.println("LOADING ERROR");
       }

   }
Code in the display() function of the OpenglCore, to show textures and 3D models:
public void display(GLDrawable drawable) {

          //it draws a race-ground
           draw_floor(gl);

           gl.glDisable(GL.GL_COLOR_MATERIAL);

           //*********positioning the car models*********
           //***car 1***
           gl.glPushMatrix();
           gl.glTranslatef(car1_x, car1_y, car1_z);
           gl.glScalef(0.0005F, 0.0005F, 0.0005F); //TOO BIG
           gl.glRotatef(-90.0f, 0.0F, 1.0F, 0.0F);
           gl.glRotatef((float)Math.toDegrees(movement_angle),0.0f,1.0f,0.0f);

           //draws the model
           model1.opengldraw(gl);
           gl.glPopMatrix();

           //***CAR2***
           gl.glPushMatrix();
           gl.glTranslated(car2_x, car2_y, car2_z);
           gl.glScalef(0.0005F, 0.0005F, 0.0005F); //TOO BIG
           gl.glRotatef(-90.0f, 0.0F, 1.0F, 0.0F);
           gl.glRotatef((float)Math.toDegrees(movement_angle2),0.0f,1.0f,0.0f);

           //draws the model
           model2.opengldraw(gl);
           gl.glPopMatrix();
   }