Add a skybox

Adding a skybox to the scene is pretty straight forward. You have to create an instance of the "SkyBox" class and assign suitable textures to it. Don't forget to center the skybox along its y-axis. Finally the skybox needs to be initialized. To actually apply the skybox to the scene its update() and draw() methods need to be called within the according methods of this class. Just take a look at the code example below.


01 package tutorial;
02 import javax.media.opengl.GL;
03 import javax.media.opengl.GLDrawable;
04 
05 import de.rico.engine.game.AbstractBaseGame;
06 import de.rico.engine.geometry.SkyBox;
07 
08 /**
09  * A class that shows how to add a skybox to the 3d environment
10  @author Frank Bruns
11  *
12  */
13 public class AddSkybox extends AbstractBaseGame
14 {  
15   private SkyBox skybox = null;
16   
17   /**
18    * Constructor for the new game.
19    @param title title for the window bar
20    @param maxFps maximum possible framerate
21    */
22   public AddSkybox(String title, int maxFps)
23   {
24     super(title, maxFps);
25   }
26 
27   /* (non-Javadoc)
28    * @see de.rico.engine.game.AbstractBaseGame#initResources(javax.media.opengl.GLDrawable, javax.media.opengl.GL, int, int)
29    */
30   @Override
31   public void initResources(GLDrawable gld, GL gl, int width, int height)
32   {
33     // create a skybox instance
34     this.skybox = new SkyBox(5f,5f,5f)// these are usually good values
35     
36     // center the skybox according to its dimensions
37     this.skybox.setPosY(-2.5f);
38     
39     // specifiy the textures for the different skybox sides
40     this.skybox.setTextureNorth("./images/skybox/desert/neg_z.jpg");
41     this.skybox.setTextureSouth("./images/skybox/desert/pos_z.jpg");
42     this.skybox.setTextureEast("./images/skybox/desert/neg_x.jpg");
43     this.skybox.setTextureWest("./images/skybox/desert/pos_x.jpg");
44     this.skybox.setTextureTop("./images/skybox/desert/pos_y.jpg");
45     this.skybox.setTextureBottom("./images/skybox/desert/neg_y.jpg");
46     
47     // a skybox needs to be initialized after the textures have been assigned
48     this.skybox.init();    
49   }
50 
51   /* (non-Javadoc)
52    * @see de.rico.engine.game.AbstractBaseGame#update(long)
53    */
54   @Override
55   public void update(long elapsedTime)
56   {
57     // update the skybox every frame
58     this.skybox.update(elapsedTime);
59   }
60 
61   /* (non-Javadoc)
62    * @see de.rico.engine.game.AbstractBaseGame#draw(javax.media.opengl.GL)
63    */
64   @Override
65   public void draw(GL gl)
66   {
67     // draw the skybox every frame
68     this.skybox.draw(gl);
69   }
70   
71   /**
72    * The usual main method. It's the entry point to the application.
73    @param args argument string
74    */
75   public static void main(String args[])
76   {
77     // create a new game
78     AddSkybox game = new AddSkybox("Test Game"100);
79     
80     // initialise the camera --> needs to be done!
81     game.initCamera(0f0f0f0f0f0.1f200f);
82     
83     // show framerate --> this is optional
84     game.setDisplayFPS(true);
85     
86     // finally start the game
87     game.start(game);
88   }
89 }
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. Of course the result depends on the specified textures.

Screenshot .