diff options
-rw-r--r-- | src/BetterBase.java | 9 | ||||
-rw-r--r-- | src/RaceTrack.java | 45 | ||||
-rw-r--r-- | src/RobotRace.java | 8 |
3 files changed, 50 insertions, 12 deletions
diff --git a/src/BetterBase.java b/src/BetterBase.java index 9ae499a..9145d15 100644 --- a/src/BetterBase.java +++ b/src/BetterBase.java @@ -56,4 +56,13 @@ abstract class BetterBase { vector.y(), vector.z()); } + + /** + * Use given vector as normal. + */ + static public void glNormal(Vector vector) { + gl.glNormal3d(vector.x(), + vector.y(), + vector.z()); + } } diff --git a/src/RaceTrack.java b/src/RaceTrack.java index 69d45ed..012047a 100644 --- a/src/RaceTrack.java +++ b/src/RaceTrack.java @@ -1,4 +1,5 @@ +import java.awt.Color; import robotrace.Vector; import static java.lang.Math.*; import static javax.media.opengl.GL2.*; @@ -80,6 +81,16 @@ class RaceTrack extends BetterBase { } /** + * Returns the position of the curve at 0 <= {@code t} <= 1 and + * 1 <= laneNo <= (number of robots). + */ + public Vector getPointForLane(double t, double laneNo) { + Vector p = getPoint(t); + Vector lanes_len = new Vector(p.x(), p.y(), 0).normalized().scale(4); + return p.add(lanes_len); + } + + /** * Returns the tangent of the curve at 0 <= {@code t} <= 1. */ public Vector getTangent(double t) { @@ -107,13 +118,13 @@ class RaceTrack extends BetterBase { private void drawTestTrack() { /* A track segment looks like: - * B----------------------------D + * B----------------------------D "outside top" * / : /| - * / G- - - - - - - - - - - - -/--H + * / G- - - - - - - - - - - - -/--H "outside bottom" * / / - * A----------------------------C + * A----------------------------C "inside top" * | | - * E----------------------------F + * E----------------------------F "inside bottom" * ^-- t = t0 ^-- t = t0 + 1 * Assume point A the inner point of the race track. Draw quads from * EF (starting point) to AC, BD, GH. @@ -121,28 +132,38 @@ class RaceTrack extends BetterBase { // previous points Vector point_A = null, point_B = null, point_E = null, point_G = null; - setColor(Colors.PALE_TURQOISE); - - for (double i = 0; i < SEGMENTS; ++i) { - double t = i * 2 * PI / SEGMENTS; + for (double i = 0; i <= SEGMENTS; ++i) { + double t = i / SEGMENTS; Vector point_C = getPoint(t); - Vector lanes_len = new Vector(point_C.x(), point_C.y(), 0).normalized().scale(4); - Vector point_D = point_C.add(lanes_len); + Vector point_D = getPointForLane(t, 4); // Z=1 to Z=-1 Vector point_F = point_C.subtract(new Vector(0, 0, 2)); Vector point_H = point_D.subtract(new Vector(0, 0, 2)); // initially, there are no "previous" vectors to use as start. if (i > 0) { + Vector norm_outside = new Vector(point_E.x(), point_E.y(), 0).normalized(); + Vector norm_inside = norm_outside.scale(-1).normalized(); + Vector norm_up = Vector.Z; + gl.glBegin(GL_QUAD_STRIP); + setColor(Color.RED); + // inside bottom + glNormal(norm_inside); glVertex(point_E); glVertex(point_F); + setColor(Colors.PALE_TURQOISE); + // inside top + glNormal(norm_up.add(norm_inside).normalized()); glVertex(point_A); glVertex(point_C); - setColor(Colors.CHOCOLATE); + // outside top + glNormal(norm_up.add(norm_outside).normalized()); glVertex(point_B); glVertex(point_D); - setColor(Colors.PALE_TURQOISE); + setColor(Color.RED); + // outside bottom + glNormal(norm_outside); glVertex(point_G); glVertex(point_H); gl.glEnd(); diff --git a/src/RobotRace.java b/src/RobotRace.java index ba042af..fb4ab1e 100644 --- a/src/RobotRace.java +++ b/src/RobotRace.java @@ -202,6 +202,10 @@ public class RobotRace extends Base { // too dark and the effect disappears, so make diffuse more bright float[] diffuseRGBA = {.7f, .7f, .7f, 1.0f}; + // default is 100% specular, but that causes the surfaces pointing to + // the normal to be fully white. + gl.glLightfv(GL_LIGHT0, GL_SPECULAR, new float[] {0, 0, 0, 0.5f}, 0); + // set the light-source colors gl.glLightfv(GL_LIGHT0, GL_AMBIENT, ambientRGBA, 0); gl.glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseRGBA, 0); @@ -334,8 +338,12 @@ public class RobotRace extends Base { // Draw the robots drawRobots(); + // use color of race track, not lighting + gl.glEnable(GL_COLOR_MATERIAL); // Draw race track raceTrack.draw(gs.trackNr); + // restore + gl.glDisable(GL_COLOR_MATERIAL); // Draw terrain terrain.draw(); |