001 package tutorial;
002 import javax.media.opengl.GL;
003 import javax.media.opengl.GLDrawable;
004
005 import com.sun.opengl.util.GLUT;
006
007 import de.rico.engine.effect.light.ogllight.DirectionalLight;
008 import de.rico.engine.effect.light.ogllight.PositionalLight;
009 import de.rico.engine.effect.light.ogllight.SpotLight;
010 import de.rico.engine.game.AbstractBaseGame;
011
012 /**
013 * A class that demonstrates how to add OpenGL lights to a scene.
014 * @author Frank Bruns
015 *
016 */
017 public class AddOpenGLLights extends AbstractBaseGame
018 {
019 DirectionalLight directional = null;
020
021 PositionalLight positional = null;
022
023 SpotLight spot = 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 AddOpenGLLights(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 and configure a basic green directional light
042 // --> more options possible!
043 this.directional = new DirectionalLight(0);
044 this.directional.setDiffuse(0f, 1f, 0f, 1f);
045 this.directional.setDirection(-1f, 0f, 0f);
046
047 // create and configure a basic red positional light
048 // --> more options possible!
049 this.positional = new PositionalLight(1);
050 this.positional.setDiffuse(1f, 0f, 0f, 1f);
051 this.positional.setPosition(20f,0f,0f);
052
053 // create and configure a basic white spot light
054 // --> more options possible!
055 this.spot = new SpotLight(2);
056 this.spot.setDiffuse(1f, 1f, 1f, 1f);
057 this.spot.setPosition(0f, 1.5f, 20f);
058 this.spot.setCutOff(20f);
059 this.spot.setExponent(75f);
060 }
061
062 /* (non-Javadoc)
063 * @see de.rico.engine.game.AbstractBaseGame#update(long)
064 */
065 @Override
066 public void update(long elapsedTime)
067 {
068 // lights don't need to be updated here!
069 }
070
071 /* (non-Javadoc)
072 * @see de.rico.engine.game.AbstractBaseGame#draw(javax.media.opengl.GL)
073 */
074 @Override
075 public void draw(GL gl)
076 {
077 // bind the lights
078 this.directional.bind(gl);
079 this.positional.bind(gl);
080 this.spot.bind(gl);
081
082 // draw a solid sphere that gets illuminated by the lights
083 // and represents any kind of scene geometry
084 new GLUT().glutSolidSphere(5.0, 40, 40);
085
086 // release the lights
087 this.spot.release(gl);
088 this.positional.release(gl);
089 this.directional.release(gl);
090 }
091
092 /**
093 * The usual main method. It's the entry point to the application.
094 * @param args argument string
095 */
096 public static void main(String args[])
097 {
098 // create a new game
099 AddOpenGLLights game = new AddOpenGLLights("Test Game", 100);
100
101 // initialise the camera --> needs to be done!
102 game.initCamera(0f, 0f, 20f, 0f, 0f, 0.1f, 200f);
103
104 // show framerate --> this is optional
105 game.setDisplayFPS(true);
106
107 // finally start the game
108 game.start(game);
109 }
110 }
|