summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2013-11-29 22:36:02 +0100
committerPeter Wu <lekensteyn@gmail.com>2013-11-29 22:36:02 +0100
commit6cc6461c316ff75aa71c577891fb484c046bf0c5 (patch)
treeac95867d97192ada4fdbcf8962f1a542aaba8a4d
parente77fa46c5daea3bb81b0551cf3f8adcd38a6cdb9 (diff)
download2iv60-robots-6cc6461c316ff75aa71c577891fb484c046bf0c5.tar.gz
Implement lighting (exercise 1.4)
Add "L" debugging key to toggle lighting. If it is buggy, see pitfall 14 at http://www.opengl.org/archives/resources/features/KilgardTechniques/oglpitfall/
-rw-r--r--src/RobotRace.java68
1 files changed, 65 insertions, 3 deletions
diff --git a/src/RobotRace.java b/src/RobotRace.java
index c755225..9dfda81 100644
--- a/src/RobotRace.java
+++ b/src/RobotRace.java
@@ -86,6 +86,12 @@ public class RobotRace extends Base {
private final Terrain terrain;
/**
+ * Whether lighting effects should be enabled or not. For testing purposes,
+ * defaults to true. It can be changed by pressing "L".
+ */
+ private boolean lightingEnabled = true;
+
+ /**
* Constructs this robot race by initializing robots, camera, track, and
* terrain.
*/
@@ -150,6 +156,35 @@ public class RobotRace extends Base {
gl.glEnable(GL_TEXTURE_2D);
gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
gl.glBindTexture(GL_TEXTURE_2D, 0);
+
+ // initialize lighting effects (lighting bit is not enabled here though)
+ initLighting();
+ }
+
+ /**
+ * Set lighting colors and effects, but do not enable them yet.
+ */
+ private void initLighting() {
+ // greyish color
+ float[] ambientRGBA = {0.2f, 0.2f, 0.2f, 1.0f};
+ // yellowish - "sunglow" RGB (255, 204, 51)
+ //float[] diffuseRGBA = {1.0f, 0.8f, 0.2f, 1.0f};
+ // red!
+ float[] diffuseRGBA = {1.0f, 0.0f, 0.0f, 1.0f};
+
+ // set the light-source colors
+ gl.glLightfv(GL_LIGHT0, GL_AMBIENT, ambientRGBA, 0);
+ gl.glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseRGBA, 0);
+
+ // global ambient light
+ //gl.glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientRGBA, 0);
+
+ // turn the light on (note: display func must set or unset LIGHTING)
+ gl.glEnable(GL_LIGHT0);
+ //gl.glEnable(GL_LIGHTING);
+
+ // blend the light with the material colors
+ gl.glEnable(GL_COLOR_MATERIAL);
}
/**
@@ -185,6 +220,26 @@ public class RobotRace extends Base {
glu.gluLookAt(camera.eye.x(), camera.eye.y(), camera.eye.z(),
camera.center.x(), camera.center.y(), camera.center.z(),
camera.up.x(), camera.up.y(), camera.up.z());
+
+ // Enable lighting effects
+ if (lightingEnabled) {
+ float[] lightPos = {
+ // light position (slightly away from top-left corner)
+ (float) camera.eye.x(),
+ (float) camera.eye.y() + 1f,
+ (float) camera.eye.z() - 1f,
+ // Light-source type, 0 sets a directional light which starts in
+ // (x,y,z) and points to the origin. 1 means positional where
+ // the light is located in (x,y,z) and shines in all directions.
+ 0
+ };
+
+ // set the light-source position
+ gl.glLightfv(GL_LIGHT0, GL_POSITION, lightPos, 0);
+ gl.glEnable(GL_LIGHTING);
+ } else {
+ gl.glDisable(GL_LIGHTING);
+ }
}
/**
@@ -300,20 +355,27 @@ public class RobotRace extends Base {
* implementation.
*/
public static void main(String args[]) {
+ System.out.println("JOGL version: "
+ + com.jogamp.opengl.JoglVersion.getInstance().getImplementationBuild());
+ final RobotRace robotRace = new RobotRace();
// Being able to exit by pressing Escape would be nice.
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
+ if (e.getID() != KeyEvent.KEY_PRESSED) {
+ return false;
+ }
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
System.err.println("Exiting...");
System.exit(0);
return true;
}
+ if (e.getKeyCode() == KeyEvent.VK_L) {
+ robotRace.lightingEnabled = !robotRace.lightingEnabled;
+ System.err.println("Lighting set to " + robotRace.lightingEnabled);
+ }
return false;
}
});
- System.out.println("JOGL version: "
- + com.jogamp.opengl.JoglVersion.getInstance().getImplementationBuild());
- RobotRace robotRace = new RobotRace();
}
}