Add a ground plane

Adding a simple ground plane to the scene is very easy. You have to create an instance of the "GroundPlane" class and assign a texture to it, if you like it. Be aware that the "size" parameter in the ground plane constructor needs to be a power of two. The texture repeat factor determines how often a potential texture is to be repeated along both axises (x and y) of the ground plane. To actually apply the ground plane 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.GroundPlane;
07 
08 /**
09  * A class that shows how to add a simple ground plane  to the 3d environmenst.
10  @author Frank Bruns
11  *
12  */
13 public class AddGroundPlane extends AbstractBaseGame
14 {  
15   private GroundPlane ground = 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 AddGroundPlane(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 ground plane object --> size =  64, texture repeat factor = 10f
34     this.ground = new GroundPlane(64,10f);
35     
36     // assign texture to texture unit 0
37     this.ground.setTexUnit0("./images/misc/blech.jpg"true);  
38   }
39 
40   /* (non-Javadoc)
41    * @see de.rico.engine.game.AbstractBaseGame#update(long)
42    */
43   @Override
44   public void update(long elapsedTime)
45   {
46     // update the ground plane every frame
47     this.ground.update(elapsedTime);
48   }
49 
50   /* (non-Javadoc)
51    * @see de.rico.engine.game.AbstractBaseGame#draw(javax.media.opengl.GL)
52    */
53   @Override
54   public void draw(GL gl)
55   {
56     // draw the ground plane every frame
57     this.ground.draw(gl);
58   }
59   
60   /**
61    * The usual main method. It's the entry point to the application.
62    @param args argument string
63    */
64   public static void main(String args[])
65   {
66     // create a new game
67     AddGroundPlane game = new AddGroundPlane("Test Game"100);
68     
69     // initialise the camera --> needs to be done!
70     game.initCamera(0f20f90f, -15f0f0.1f200f);
71     
72     // show framerate --> this is optional
73     game.setDisplayFPS(true);
74     
75     // finally start the game
76     game.start(game);
77   }
78 }
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 ground texture.

Screenshot .