## Use the mesh container to render a lot of meshes (with shadows)

The following code demonstrates how to use a mesh container to simplify the rendering of a lot of meshes and to draw shadows.

`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.MeshContainer; 009 import de.rico.engine.geometry.modelloader.ms3d.ModelMS3DAscii; 010 011 /** 012 * A class that demonstrates how to use a mesh container for a simplified 013 * drawing of meshes. If a mesh container is used the assigned meshes don't 014 * need to be updated and drawn seperatedly. 015 * @author Frank Bruns 016 * 017 */ 018 public class UseMeshContainer extends AbstractBaseGame 019 { 020 private MeshContainer container = null; 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 UseMeshContainer(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 several models 041 042 ModelMS3DAscii model1 = new ModelMS3DAscii(BoundingVolume.BOX); 043 model1.load("./models/ms3d/recke/recke.txt", 0.1f); 044 model1.setTexUnit0("./models/ms3d/recke/recke.jpg", true, false, false); 045 model1.setShadowEnabled(true); 046 047 ModelMS3DAscii model2 = new ModelMS3DAscii(BoundingVolume.BOX); 048 model2.load("./models/ms3d/recke/recke.txt", 0.1f); 049 model2.setTexUnit0("./models/ms3d/recke/recke.jpg", true, false, false); 050 model2.setShadowEnabled(true); 051 model2.translateX(-3.5f); // translate 052 053 ModelMS3DAscii model3 = new ModelMS3DAscii(BoundingVolume.BOX); 054 model3.load("./models/ms3d/recke/recke.txt", 0.1f); 055 model3.setTexUnit0("./models/ms3d/recke/recke.jpg", true, false, false); 056 model3.setShadowEnabled(true); 057 model3.translateX(3.5f); // translate 058 059 // create a mesh container object 060 this.container = new MeshContainer(); 061 062 // add the models to the mesh container 063 this.container.add(model1); 064 this.container.add(model2); 065 this.container.add(model3); 066 067 // create a simple white light to illuminate the scene 068 this.light = new PositionalLight(0); 069 this.light.setPosition(0f, 50f, 100f); 070 } 071 072 /* (non-Javadoc) 073 * @see de.rico.engine.game.AbstractBaseGame#update(long) 074 */ 075 @Override 076 public void update(long elapsedTime) 077 { 078 // update every models just by calling the mesh 079 // container's update() method 080 this.container.update(elapsedTime); 081 } 082 083 /* (non-Javadoc) 084 * @see de.rico.engine.game.AbstractBaseGame#draw(javax.media.opengl.GL) 085 */ 086 @Override 087 public void draw(GL gl) 088 { 089 // bind the light for illumination of the scene 090 this.light.bind(gl); 091 092 // draw the meshes with shadow matrix shadows. The 093 // specified light will be used as a reference light 094 // for shadowing 095 this.container.drawWithShadowMatrix(gl, this.light); 096 097 // othter possible options are... 098 099 // draw the models without shadows 100 //this.container.draw(gl); 101 102 // draw the meshes with the specified transparency value 103 //this.container.drawTransparent(gl, 0.3f); 104 105 // release the light again 106 this.light.release(gl); 107 } 108 109 /** 110 * The usual main method. It's the entry point to the application. 111 * @param args argument string 112 */ 113 public static void main(String args[]) 114 { 115 // create a new game 116 UseMeshContainer game = new UseMeshContainer("Test Game", 100); 117 118 // initialise the camera --> needs to be done! 119 game.initCamera(0f, 5f, 20f, 0f, 0f, 0.1f, 200f); 120 121 // show framerate --> this is optional 122 game.setDisplayFPS(true); 123 124 // finally start the game 125 game.start(game); 126 } 127 }`
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.
.