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 import de.rico.engine.input.InputManager;
010
011 /**
012 * A class that shows how to determine whether or not the mouse is located
013 * over a 3d model.
014 * @author Frank Bruns
015 *
016 */
017 public class CheckMouseOverModel extends AbstractBaseGame
018 {
019 private ModelMS3DAscii model = null;
020
021 private InputManager inputManager = null;
022
023 private PositionalLight light = null;
024
025 /**
026 * Constructor for the new game.
027 * @param title title for the window bar
028 * @param maxFps maximum possible framerate
029 */
030 public CheckMouseOverModel(String title, int maxFps)
031 {
032 super(title, maxFps);
033 }
034
035 /* (non-Javadoc)
036 * @see de.rico.engine.game.AbstractBaseGame#initResources(javax.media.opengl.GLDrawable, javax.media.opengl.GL, int, int)
037 */
038 @Override
039 public void initResources(GLDrawable gld, GL gl, int width, int height)
040 {
041 // create the input manager object that allows you to
042 // retrieve the current mouse coordinates
043 this.inputManager = new InputManager(this.getOGLCanvas());
044
045 this.model = new ModelMS3DAscii(BoundingVolume.BOX);
046 this.model.load("./models/ms3d/recke/recke.txt", 0.1f);
047 this.model.setTexUnit0("./models/ms3d/recke/recke.jpg", true, false, false);
048
049 // create a simple white light to illuminate the scene
050 this.light = new PositionalLight(0);
051 this.light.setPosition(0f, 50f, 100f);
052 }
053
054 /* (non-Javadoc)
055 * @see de.rico.engine.game.AbstractBaseGame#update(long)
056 */
057 @Override
058 public void update(long elapsedTime)
059 {
060 // update the models
061 this.model.update(elapsedTime);
062
063 // retrieve the current mouse screen coordinates in pixels
064 int x = this.inputManager.getMouseX();
065 int y = this.inputManager.getMouseY();
066
067 // check if the mouse coordinates are located over the model.
068 // Actually it is tested, if the coordinates are over the bounding
069 // volume of the model, so there can be inaccuracies if the
070 // model bounding volume doesn't fit that well to its dimensions.
071 // Before actually checking the mouse coordinates, I recommend
072 // to check if the model isn't occluded by another one.
073 boolean mouseOverModel = false;
074
075 if(this.model.isOccluded() == false)
076 mouseOverModel = this.model.isMouseOverMesh(x, y);
077
078 System.out.println("Is the mouse positioned over the model?: "+mouseOverModel);
079
080 // Instead of simply asking whether the mouse is located over the
081 // model's bounding volume you can also ask where exactly in
082 // 3d space the mouse is intersecting with the bounding volume
083 // of the mesh. Just use the getMouseOverMesh(x,y) method in that case.
084
085 // For more information on finding intersection points, please have a closer
086 // look at the methods of the CollisionFinder class.
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 model
099 this.model.draw(gl);
100
101 // release the light again
102 this.light.release(gl);
103 }
104
105 /**
106 * The usual main method. It's the entry point to the application.
107 * @param args argument string
108 */
109 public static void main(String args[])
110 {
111 // create a new game
112 CheckMouseOverModel game = new CheckMouseOverModel("Test Game", 100);
113
114 // initialise the camera --> needs to be done!
115 game.initCamera(0f, 7.5f, 20f, 0f, 0f, 0.1f, 200f);
116
117 // show framerate --> this is optional
118 game.setDisplayFPS(true);
119
120 // finally start the game
121 game.start(game);
122 }
123 }
|