Add (animated) meshes from 3d model files

The following code shows how to add 3d models from static 3DS model files and animated MilkShape3D model files.

001 package tutorial;
002 import javax.media.opengl.GL;
003 import javax.media.opengl.GLDrawable;
004 
005 import de.rico.engine.effect.light.ogllight.PositionalLight;
006 import de.rico.engine.enums.BoundingVolume;
007 import de.rico.engine.game.AbstractBaseGame;
008 import de.rico.engine.geometry.modelloader.Animation;
009 import de.rico.engine.geometry.modelloader.ms3d.ModelMS3DAscii;
010 import de.rico.engine.geometry.modelloader.threeds.Model3DS;
011 
012 /**
013  * A class that shows how to add 3d models from certain 3d model files.
014  @author Frank Bruns
015  *
016  */
017 public class Add3DModel extends AbstractBaseGame
018 {
019   private ModelMS3DAscii milkshape = null// animated model
020   private Model3DS threeDS = null// static model
021   
022   private PositionalLight light = null;
023   
024   /**
025    * Constructor for the new game.
026    @param title title for the window bar
027    @param maxFps maximum possible framerate
028    */
029   public Add3DModel(String title, int maxFps)
030   {
031     super(title, maxFps);
032   }
033 
034   /* (non-Javadoc)
035    * @see de.rico.engine.game.AbstractBaseGame#initResources(javax.media.opengl.GLDrawable, javax.media.opengl.GL, int, int)
036    */
037   @Override
038   public void initResources(GLDrawable gld, GL gl, int width, int height)
039   {
040     // Create the model object. Be aware that a lot more options are
041     // available than shown in this little tutorial
042     
043     // create a smooth shaded 3ds model object of a plant with a sphere as a bounding volume
044     // 3ds models are always static (no animations!) in this engine
045     this.threeDS = new Model3DS(true,BoundingVolume.SPHERE);
046     // load the model data from the specified file and scale it 
047     // by the specified factor
048     this.threeDS.load("./models/3ds/plants/plant01a.3ds"0.1f);
049     // assign a rotated and flipped texture to the model --> optional!
050     // texture get rotated and flipped by default. use an overloaded 
051     // method (-->see milkshape object) that allows you to explicitly disable rotation and flipping for
052     // a texture
053     this.threeDS.setTexUnit0("./models/3ds/plants/plant01a.jpg"true);
054     // set the position (default would be (0f,0f,0f)) 
055     this.threeDS.setPosition(-5f0f0f);
056     
057     
058     // create a MilkShape3D Ascii model of a man. Such models can
059     // be animated. MD2 models work the same way as MilkShape3D models.
060     this.milkshape = new ModelMS3DAscii(BoundingVolume.BOX);
061     this.milkshape.load("./models/ms3d/recke/recke.txt"0.1f);
062     // don't rotate and flip the texture. It depends on the model, if rotation and flipping is needed
063     this.milkshape.setTexUnit0("./models/ms3d/recke/recke.jpg", true, false, false);
064     // define a "walk" animation to the model. The animation ranges from frames 2 to 7.
065     // Each frame will be displayed for 100 miliseconds.
066     // Make sure that the range of frames is available in the model file.
067     this.milkshape.addAnimation(new Animation("walk",2,7,100));
068     // set the model animation to the "walk" animation
069     this.milkshape.setAnimation("walk");
070     this.milkshape.setPosition(0f0f0f);
071     
072     
073     // create a simple white light to illuminate the scene
074     this.light = new PositionalLight(0);
075     this.light.setPosition(0f50f100f);
076   }
077 
078   /* (non-Javadoc)
079    * @see de.rico.engine.game.AbstractBaseGame#update(long)
080    */
081   @Override
082   public void update(long elapsedTime)
083   {
084     this.threeDS.update(elapsedTime);
085     this.milkshape.update(elapsedTime);
086   }
087 
088   /* (non-Javadoc)
089    * @see de.rico.engine.game.AbstractBaseGame#draw(javax.media.opengl.GL)
090    */
091   @Override
092   public void draw(GL gl)
093   {
094     // bind the light for illumination of the scene
095     this.light.bind(gl);
096     
097     this.threeDS.draw(gl);
098     this.milkshape.draw(gl);
099     
100     // release the light again
101     this.light.release(gl);
102   }
103   
104   /**
105    * The usual main method. It's the entry point to the application.
106    @param args argument string
107    */
108   public static void main(String args[])
109   {
110     // create a new game
111     Add3DModel game = new Add3DModel("Test Game"100);
112     
113     // initialise the camera --> needs to be done!
114     game.initCamera(0f5f20f0f0f0.1f200f);
115     
116     // show framerate --> this is optional
117     game.setDisplayFPS(true);
118     
119     // finally start the game
120     game.start(game);
121   }
122 }
Java2html

After executing the code you should see a display resolution dialog. Choose a resolution suitable for you and press "OK". If everything is correct there should be a window showing up on the screen that looks like the one in the picture below.

Screenshot .