Perform rotations, scalings and translations on every kind of mesh

The following code shows you how to translate, rotate and scale meshes in the world.


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.ms3d.ModelMS3DAscii;
009 
010 /**
011  * A class that shows how to transform (translate, rotate, scale) 3d models.
012  @author Frank Bruns
013  *
014  */
015 public class Transform3DModel extends AbstractBaseGame
016 {
017   private ModelMS3DAscii model1 = null;
018   private ModelMS3DAscii model2 = null;
019   private ModelMS3DAscii model3 = null;
020   
021   private PositionalLight light = null;
022   
023   /**
024    * Constructor for the new game.
025    @param title title for the window bar
026    @param maxFps maximum possible framerate
027    */
028   public Transform3DModel(String title, int maxFps)
029   {
030     super(title, maxFps);
031   }
032 
033   /* (non-Javadoc)
034    * @see de.rico.engine.game.AbstractBaseGame#initResources(javax.media.opengl.GLDrawable, javax.media.opengl.GL, int, int)
035    */
036   @Override
037   public void initResources(GLDrawable gld, GL gl, int width, int height)
038   {  
039     // I load the same model three times and transform them in different ways
040     
041     // this model won't be transformed to be able to compare
042     // it with the others. The default positon of model is (0f,0f,0f)
043     this.model1 = new ModelMS3DAscii(BoundingVolume.BOX);
044     this.model1.load("./models/ms3d/recke/recke.txt"0.1f);
045     this.model1.setTexUnit0("./models/ms3d/recke/recke.jpg", true, false, false);    
046     
047     // This model will be translated along the negative x-axis,
048     // rotated along its z-axis by 35° and scaled along its y-axis.
049     // Remember that the model got scaled down to 0.1f in every direction initially.
050     this.model2 = new ModelMS3DAscii(BoundingVolume.BOX);
051     this.model2.load("./models/ms3d/recke/recke.txt"0.1f);
052     this.model2.setTexUnit0("./models/ms3d/recke/recke.jpg", true, false, false);    
053     this.model2.translateX(-7.5f)// translate
054     this.model2.setRotZ(35f)// rotate
055     this.model2.setScaleY(0.05f)// scale
056     
057     // This model will be translated along the positive x-axis,
058     // rotated along its y-axis by 25° and scaled along its y-axis.
059     // Remember that the model got scaled down to 0.1f in every direction initially.
060     this.model3 = new ModelMS3DAscii(BoundingVolume.BOX);
061     this.model3.load("./models/ms3d/recke/recke.txt"0.1f);
062     this.model3.setTexUnit0("./models/ms3d/recke/recke.jpg", true, false, false);
063     this.model3.translateX(7.5f)// translate
064     this.model3.setRotY(25f)// rotate
065     this.model3.setScaleY(0.125f)// scale
066     
067     // Instead of the relative translation values you could also
068     // specify an absolut position either for every coordinate (setPosition(x,y,z))
069     // or for each coordinate on its own (setPosX(),setPosY(),setPosZ())
070     
071     
072     // create a simple white light to illuminate the scene
073     this.light = new PositionalLight(0);
074     this.light.setPosition(0f50f100f);
075   }
076 
077   /* (non-Javadoc)
078    * @see de.rico.engine.game.AbstractBaseGame#update(long)
079    */
080   @Override
081   public void update(long elapsedTime)
082   {
083     // update the models
084     this.model1.update(elapsedTime);
085     this.model2.update(elapsedTime);
086     this.model3.update(elapsedTime);
087   }
088 
089   /* (non-Javadoc)
090    * @see de.rico.engine.game.AbstractBaseGame#draw(javax.media.opengl.GL)
091    */
092   @Override
093   public void draw(GL gl)
094   {
095     // bind the light for illumination of the scene
096     this.light.bind(gl);
097     
098     // draw the models
099     this.model1.draw(gl);
100     this.model2.draw(gl);
101     this.model3.draw(gl);
102     
103     // release the light again
104     this.light.release(gl);
105   }
106   
107   /**
108    * The usual main method. It's the entry point to the application.
109    @param args argument string
110    */
111   public static void main(String args[])
112   {
113     // create a new game
114     Transform3DModel game = new Transform3DModel("Test Game"100);
115     
116     // initialise the camera --> needs to be done!
117     game.initCamera(0f7.5f20f0f0f0.1f200f);
118     
119     // show framerate --> this is optional
120     game.setDisplayFPS(true);
121     
122     // finally start the game
123     game.start(game);
124   }
125 }
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 .