From 943e0d8bb6f808966f9ec783a5826fe2840c6545 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Thu, 14 Nov 2013 21:12:08 +0100 Subject: Improved exercise 1.1 (axis) Documented steps better, reduce code duplication. --- src/RobotRace.java | 77 ++++++++++++++++++++++++++++++++++++------------------ 1 file 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); } /** -- cgit v1.2.1