Add different types of OpenGL lights

There are three important classes for adding lights to a 3D scene with the engine. Each one represents a special style of lighting. "DirectionalLight" is used for directional lights, which have no position but merely a direction. "PositionalLight" is used for positional lights that have a defined position and shed light into all directions. "SpotLight" is used for spot lights that have a defined positon, too, but limit the radius of illumination to some sort of cone of light. After an object of a light class has been instantiated with a unique light number that ranges from 0 to 7, it needs to be properly configured according to the imaginations of the user. Lights do not need to be updated in the update() method but instead be bound and released in the draw() method. The following code shows how to add representatives of each light to the scene and apply them to a solid sphere geometry object that represents any kind of possible meshes in a game.


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(0f1f0f1f);
045     this.directional.setDirection(-1f0f0f);
046     
047     // create and configure a basic red positional light
048     // --> more options possible!
049     this.positional = new PositionalLight(1);
050     this.positional.setDiffuse(1f0f0f1f);
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(1f1f1f1f);
057     this.spot.setPosition(0f1.5f20f);
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.04040);
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(0f0f20f0f0f0.1f200f);
103     
104     // show framerate --> this is optional
105     game.setDisplayFPS(true);
106     
107     // finally start the game
108     game.start(game);
109   }
110 }
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. The green looking part of the sphere is caused by the directional light that shines down on the positive x-axis. The red light is caused by a positional light which has been placed somewhere near the right side of the sphere. Finally there is white spot light that shines directly down the negative z-axis by default. The areas of the sphere that don't get illuminated remain dark.

Screenshot .