Assign an environment map (sphere map or cube map) to a mesh

The following code shows you how to add an environment map to a mesh. The engine supports sphere maps and cube maps. Environment maps are treated like textures on a mesh.


001 package tutorial;
002 import javax.media.opengl.GL;
003 import javax.media.opengl.GLDrawable;
004 
005 import com.sun.opengl.util.texture.Texture;
006 
007 import de.rico.engine.effect.environmentmapping.CubeMap;
008 import de.rico.engine.effect.environmentmapping.SphereMap;
009 import de.rico.engine.effect.light.ogllight.PositionalLight;
010 import de.rico.engine.enums.BoundingVolume;
011 import de.rico.engine.game.AbstractBaseGame;
012 import de.rico.engine.geometry.modelloader.ms3d.ModelMS3DAscii;
013 import de.rico.engine.util.Tools;
014 
015 /**
016  * A class that demonstrates how to add an environment map to a mesh.
017  @author Frank Bruns
018  *
019  */
020 public class AddEnvironmentMap extends AbstractBaseGame
021 {
022   private ModelMS3DAscii model1 = null;
023   private ModelMS3DAscii model2 = null;
024   
025   private PositionalLight light = null;
026   
027   /**
028    * Constructor for the new game.
029    @param title title for the window bar
030    @param maxFps maximum possible framerate
031    */
032   public AddEnvironmenMap(String title, int maxFps)
033   {
034     super(title, maxFps);
035   }
036 
037   /* (non-Javadoc)
038    * @see de.rico.engine.game.AbstractBaseGame#initResources(javax.media.opengl.GLDrawable, javax.media.opengl.GL, int, int)
039    */
040   @Override
041   public void initResources(GLDrawable gld, GL gl, int width, int height)
042   {  
043     // This model will use a sphere map (model on the left)
044     this.model1 = new ModelMS3DAscii(BoundingVolume.BOX);
045     this.model1.load("./models/ms3d/recke/recke.txt"0.1f);
046     this.model1.setEnvironmentMap(new SphereMap("./images/misc/spheremap.jpg"));
047     this.model1.translateX(-3f);
048     
049     
050     // this model will use a cube map (model on the right)
051     this.model2 = new ModelMS3DAscii(BoundingVolume.BOX);
052     this.model2.load("./models/ms3d/recke/recke.txt"0.1f);
053     // Create a cubemap texture --> such textures consist of 6 textures
054     // that ususally reflect the skybox 
055     Texture cubemapTexture = Tools.createCubeMapTexture(
056         "./images/skybox/desert/pos_x.jpg"
057         "./images/skybox/desert/neg_x.jpg"
058         "./images/skybox/desert/pos_y.jpg"
059         "./images/skybox/desert/neg_y.jpg"
060         "./images/skybox/desert/pos_z.jpg",
061         "./images/skybox/desert/neg_z.jpg");
062     // assign the cubemap texture wrapped in a CubeMap object
063     // to the mesh
064     this.model2.setEnvironmentMap(new CubeMap(cubemapTexture));
065     this.model2.translateX(3f);
066     
067     
068     // create a simple white light to illuminate the scene
069     this.light = new PositionalLight(0);
070     this.light.setPosition(0f50f100f);
071   }
072 
073   /* (non-Javadoc)
074    * @see de.rico.engine.game.AbstractBaseGame#update(long)
075    */
076   @Override
077   public void update(long elapsedTime)
078   {
079     // update the models
080     this.model1.update(elapsedTime);
081     this.model2.update(elapsedTime);
082   }
083 
084   /* (non-Javadoc)
085    * @see de.rico.engine.game.AbstractBaseGame#draw(javax.media.opengl.GL)
086    */
087   @Override
088   public void draw(GL gl)
089   {
090     // bind the light for illumination of the scene
091     this.light.bind(gl);
092     
093     // draw the models
094     this.model1.draw(gl);
095     this.model2.draw(gl);
096     
097     // release the light again
098     this.light.release(gl);
099   }
100   
101   /**
102    * The usual main method. It's the entry point to the application.
103    @param args argument string
104    */
105   public static void main(String args[])
106   {
107     // create a new game
108     AddEnvironmentMap game = new AddEnvironmentMap("Test Game"100);
109     
110     // initialise the camera --> needs to be done!
111     game.initCamera(0f7.5f20f0f0f0.1f200f);
112     
113     // show framerate --> this is optional
114     game.setDisplayFPS(true);
115     
116     // finally start the game
117     game.start(game);
118   }
119 }
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 .