diff options
author | Peter Wu <lekensteyn@gmail.com> | 2013-11-14 21:12:08 +0100 |
---|---|---|
committer | Peter Wu <lekensteyn@gmail.com> | 2013-11-14 21:12:08 +0100 |
commit | 943e0d8bb6f808966f9ec783a5826fe2840c6545 (patch) | |
tree | 5f61dbaf88f612513b1fd80e6c2ab8307a2d2f09 | |
parent | e78861e07e4a106f5057f62fe06fa71a55fbc390 (diff) | |
download | 2iv60-robots-943e0d8bb6f808966f9ec783a5826fe2840c6545.tar.gz |
Improved exercise 1.1 (axis)
Documented steps better, reduce code duplication.
-rw-r--r-- | src/RobotRace.java | 77 |
1 files changed, 52 insertions, 25 deletions
diff --git a/src/RobotRace.java b/src/RobotRace.java index 28d4ca5..f9c1a0b 100644 --- a/src/RobotRace.java +++ b/src/RobotRace.java @@ -205,37 +205,64 @@ public class RobotRace extends Base { gl.glScalef(1f, 1f, 2f); // Translated, rotated, scaled box. - glut.glutWireCube(1f); + glut.glutWireCube(1f); } - - + + /** + * Draw a colored arrow from left to right. + * + * @param r Red color scale (0 to 1). + * @param g Green color scale (0 to 1). + * @param b Blue color scale (0 to 1). + */ + private void drawColoredArrow(float r, float g, float b) { + gl.glPushMatrix(); + + // change color + gl.glColor3f(r, g, b); + + // draw a thin line from the origin to the right. + gl.glTranslatef(0.5f, 0, 0); + gl.glScalef(1f, 0.01f, 0.01f); + glut.glutSolidCube(1f); + // restore scale for clarity. + gl.glScalef(1f, 1/0.01f, 1/0.01f); + + // draw a cone on the end of the line that has a head that has a radius + // which is three times larger than the line segment. + gl.glTranslatef(0.5f, 0, 0); + // turn head to the right (rotate 90 degree from the Y-axis) + gl.glRotatef(90, 0, 1, 0); + glut.glutSolidCone(.03f, .1f, 10, 10); + + // restore previous matrix + gl.glPopMatrix(); + } + /** * Draws the x-axis (red), y-axis (green), z-axis (blue), * and origin (yellow). */ public void drawAxisFrame() { - gl.glPushMatrix(); - gl.glPushMatrix(); - gl.glPushMatrix(); - - gl.glColor3f(0f, 0f, 1f); - gl.glScalef(0.5f, 0.5f, 4f); - glut.glutSolidCube(0.5f); - - gl.glPopMatrix(); - - gl.glColor3f(0f, 1f, 0f); - gl.glScalef(0.5f, 4f, 0.5f); - glut.glutSolidCube(0.5f); - - gl.glPopMatrix(); - - gl.glColor3f(1f, 0f, 0f); - gl.glScalef(4f, 0.5f, 0.5f); - glut.glutSolidCube(0.5f); - - gl.glPopMatrix(); - + // X-axis: normal orientation + drawColoredArrow(1, 0, 0); + + // Y-axis: rotate 90 degree clockwise in the Z-axis + gl.glRotatef(90, 0, 0, 1); + drawColoredArrow(0, 1, 0); + gl.glRotatef(-90, 0, 0, 1); + + // Z-axis: rotate 90 degree in the XY ais + gl.glRotatef(-90, 0, 1, 0); + drawColoredArrow(0, 0, 1); + gl.glRotatef(90, 0, 1, 0); + + // yellow sphere of 0.03m (with ten divisions) + gl.glColor3f(1, 1, 0); + glut.glutSolidSphere(0.05f, 10, 10); + + // reset color + gl.glColor3f(0, 0, 0); } /** |