diff options
author | Peter Wu <lekensteyn@gmail.com> | 2013-12-18 16:31:03 +0100 |
---|---|---|
committer | Peter Wu <lekensteyn@gmail.com> | 2013-12-18 16:31:24 +0100 |
commit | bd872119e982f9deb55b1400d24e56ebfa9160a3 (patch) | |
tree | 3d31a757268aa9e4a0db418e989d6344101577dd | |
parent | a575b891596b268686f6a68207169d0500d4f604 (diff) | |
download | 2iv60-robots-bd872119e982f9deb55b1400d24e56ebfa9160a3.tar.gz |
2.1 Implement test track drawing
-rw-r--r-- | src/BetterBase.java | 10 | ||||
-rw-r--r-- | src/RaceTrack.java | 70 |
2 files changed, 71 insertions, 9 deletions
diff --git a/src/BetterBase.java b/src/BetterBase.java index 2e233ee..9ae499a 100644 --- a/src/BetterBase.java +++ b/src/BetterBase.java @@ -3,6 +3,7 @@ import com.jogamp.opengl.util.gl2.GLUT; import javax.media.opengl.GL2; import javax.media.opengl.glu.GLU; import java.awt.Color; +import robotrace.Vector; /** * Base class that provides basic bindings to the ugly JOGL interface. This @@ -46,4 +47,13 @@ abstract class BetterBase { float[] rgba = color.getRGBComponents(null); gl.glColor3fv(rgba, 0); } + + /** + * Pass a vector as a vertex to OpenGL. + */ + static public void glVertex(Vector vector) { + gl.glVertex3d(vector.x(), + vector.y(), + vector.z()); + } } diff --git a/src/RaceTrack.java b/src/RaceTrack.java index 78e6e89..d40c529 100644 --- a/src/RaceTrack.java +++ b/src/RaceTrack.java @@ -1,20 +1,26 @@ import robotrace.Vector; import static java.lang.Math.*; +import static javax.media.opengl.GL2.*; /** * Implementation of a race track that is made from Bezier segments. */ -class RaceTrack { +class RaceTrack extends BetterBase { /** - * Halfwidth of the ellipse. + * Half-width of the ellipse. */ - protected static final int ELLIPSE_A = 10; + protected static final double ELLIPSE_A = 10; /** - * Halfheight of the ellipse. + * Half-height of the ellipse. */ - protected static final int ELLIPSE_B = 14; + protected static final double ELLIPSE_B = 14; + + /** + * Number of segments for the race track. + */ + private final double SEGMENTS = 180; /** * Array with control points for the O-track. @@ -52,7 +58,7 @@ class RaceTrack { // The test track is selected if (0 == trackNr) { - // code goes here ... + drawTestTrack(); } else if (1 == trackNr) { // The O-track is selected // code goes here ... } else if (2 == trackNr) { // The L-track is selected @@ -94,9 +100,55 @@ class RaceTrack { * describes the tangent vector of point p. */ Vector p = getPoint(t); - - return new Vector(-p.y()/(ELLIPSE_A * ELLIPSE_A), - p.x()/(ELLIPSE_B * ELLIPSE_B), + return new Vector(-p.y() / (ELLIPSE_A * ELLIPSE_A), + p.x() / (ELLIPSE_B * ELLIPSE_B), 0).normalized(); } + + private void drawTestTrack() { + /* A track segment looks like: + * B----------------------------D + * / : /| + * / G- - - - - - - - - - - - -/--H + * / / + * A----------------------------C + * | | + * E----------------------------F + * ^-- 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. + */ + // previous points + Vector point_A = null, point_B = null, point_E = null, point_G = null; + + for (double i = 0; i < SEGMENTS; ++i) { + double t = i * 2 * PI / SEGMENTS; + Vector point_C = getPoint(t); + Vector lanes_len = getTangent(t).cross(point_C).normalized().scale(4); + Vector point_D = point_C.add(lanes_len); + // 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) { + gl.glBegin(GL_QUAD_STRIP); + glVertex(point_E); + glVertex(point_F); + glVertex(point_A); + glVertex(point_C); + glVertex(point_B); + glVertex(point_D); + glVertex(point_G); + glVertex(point_H); + gl.glEnd(); + } + + // save points for next draw round + point_E = point_F; + point_A = point_C; + point_B = point_D; + point_G = point_H; + } + } } |