From c8025eb89306b4c7a47ca8236444d715e7cdfdca Mon Sep 17 00:00:00 2001 From: Frank v/d Haterd Date: Wed, 8 Jan 2014 16:43:18 +0100 Subject: Robot textures and track --- src/BetterBase.java | 8 ++++++++ src/RaceTrack.java | 50 +++++++++++++++++++++++++++++++++++---------- src/Robot.java | 59 +++++++++++++++++++++++++++++++++++++++++++---------- src/RobotRace.java | 21 +++++++++++++++++-- 4 files changed, 114 insertions(+), 24 deletions(-) diff --git a/src/BetterBase.java b/src/BetterBase.java index 9145d15..bed9fc5 100644 --- a/src/BetterBase.java +++ b/src/BetterBase.java @@ -4,6 +4,7 @@ import javax.media.opengl.GL2; import javax.media.opengl.glu.GLU; import java.awt.Color; import robotrace.Vector; +import static javax.media.opengl.GL2.*; /** * Base class that provides basic bindings to the ugly JOGL interface. This @@ -65,4 +66,11 @@ abstract class BetterBase { vector.y(), vector.z()); } + + /** + * Unbind the textures so the scene is drawn correctly. + */ + static public void unbindTextures() { + gl.glBindTexture(GL_TEXTURE_2D, 0); + } } diff --git a/src/RaceTrack.java b/src/RaceTrack.java index d2bcaa5..3865b3d 100644 --- a/src/RaceTrack.java +++ b/src/RaceTrack.java @@ -42,14 +42,13 @@ class RaceTrack extends BetterBase { * Array with control points for the custom track. */ private Vector[] controlPointsCustomTrack; + private final RobotRace race; /** * Constructs the race track, sets up display lists. */ - public RaceTrack() { - // code goes here ... - - + public RaceTrack(RobotRace race) { + this.race = race; } /** @@ -148,29 +147,58 @@ class RaceTrack extends BetterBase { Vector norm_inside = norm_outside.scale(-1).normalized(); Vector norm_up = Vector.Z; - gl.glBegin(GL_QUAD_STRIP); + // Set brick texture + race.getBrickTexture().bind(gl); + + // Draw track walls + gl.glBegin(GL_QUADS); setColor(Color.RED); // inside bottom glNormal(norm_inside); + gl.glTexCoord2f(0, 0); glVertex(point_E); + gl.glTexCoord2f(1, 0); glVertex(point_F); setColor(Colors.PALE_TURQOISE); // inside top glNormal(norm_up.add(norm_inside).normalized()); - glVertex(point_A); + gl.glTexCoord2f(1, 1); glVertex(point_C); - // outside top - glNormal(norm_up.add(norm_outside).normalized()); - glVertex(point_B); - glVertex(point_D); - setColor(Color.RED); + gl.glTexCoord2f(0, 1); + glVertex(point_A); + // outside bottom glNormal(norm_outside); + gl.glTexCoord2f(0, 0); glVertex(point_G); + gl.glTexCoord2f(1, 0); glVertex(point_H); + // outside top + glNormal(norm_up.add(norm_outside).normalized()); + gl.glTexCoord2f(1, 1); + glVertex(point_D); + gl.glTexCoord2f(0, 1); + glVertex(point_B); + gl.glEnd(); + + race.getTrackTexture().bind(gl); + + // Draw track itself + gl.glBegin(GL_QUADS); + glNormal(Vector.Z); + gl.glTexCoord2f(0, 0); + glVertex(point_A); + gl.glTexCoord2f(1, 0); + glVertex(point_C); + gl.glTexCoord2f(1, 1); + glVertex(point_D); + gl.glTexCoord2f(0, 1); + glVertex(point_B); gl.glEnd(); } + unbindTextures(); + // save points for next draw round point_E = point_F; point_A = point_C; diff --git a/src/Robot.java b/src/Robot.java index cffbffc..51fad15 100644 --- a/src/Robot.java +++ b/src/Robot.java @@ -102,8 +102,14 @@ class Robot extends BetterBase { // Draw the robot, everything is relative to the center of torso. // Static parts (that do not animate): + race.getTorsoTexture().bind(gl); drawTorso(); + + //race.getHeadTexture().bind(gl); drawHead(); + + unbindTextures(); + // only draw the shoulder parts in normal, full body mode if (!asStickFigure) { drawShoulderTop(); @@ -116,7 +122,7 @@ class Robot extends BetterBase { // draw left and right arms drawArm(false); drawArm(true); - + // // The following function call exist to make a point clear. Adding // comments for the sake of having comments is silly, pointless and an @@ -144,7 +150,8 @@ class Robot extends BetterBase { * when drawing a stick figure, in that case the boneColor constant will * be used. */ - private void drawBeam(float x, float y, float z, Direction dir, Color color) { + private void drawBeam(float x, float y, float z, Direction dir, Color color, + boolean isTextured) { if (asStickFigure) { // for a stick figure, draw a thin figure without colors switch (dir) { @@ -176,7 +183,13 @@ class Robot extends BetterBase { // turn a cube into a small beam gl.glScalef(x, y, z); - drawCube(); + // Depending on if it needs to be textured, use our own method + // or the glut solid cube. + if (isTextured) { + drawCube(); + } else { + glut.glutSolidCube(1); + } // return to previous scales gl.glScalef(1 / x, 1 / y, 1 / z); @@ -219,44 +232,68 @@ class Robot extends BetterBase { gl.glBegin(GL_QUADS); // Front face glNormal(norm_towards); + gl.glTexCoord2f(0, 0); glVertex(point_A); + gl.glTexCoord2f(1, 0); glVertex(point_B); + gl.glTexCoord2f(0, 1); glVertex(point_C); + gl.glTexCoord2f(1, 1); glVertex(point_D); // Right face glNormal(norm_right); + gl.glTexCoord2f(0, 0); glVertex(point_B); + gl.glTexCoord2f(1, 0); glVertex(point_F); + gl.glTexCoord2f(0, 1); glVertex(point_G); + gl.glTexCoord2f(1, 1); glVertex(point_C); // Back face glNormal(norm_outwards); + gl.glTexCoord2f(0, 0); glVertex(point_F); + gl.glTexCoord2f(1, 0); glVertex(point_G); + gl.glTexCoord2f(0, 1); glVertex(point_H); + gl.glTexCoord2f(1, 1); glVertex(point_E); // Left face glNormal(norm_left); + gl.glTexCoord2f(0, 0); glVertex(point_A); + gl.glTexCoord2f(1, 0); glVertex(point_G); + gl.glTexCoord2f(0, 1); glVertex(point_H); + gl.glTexCoord2f(1, 1); glVertex(point_D); // Top face glNormal(norm_up); + gl.glTexCoord2f(0, 0); glVertex(point_D); + gl.glTexCoord2f(1, 0); glVertex(point_C); + gl.glTexCoord2f(0, 1); glVertex(point_G); + gl.glTexCoord2f(1, 1); glVertex(point_H); // Bottom face glNormal(norm_down); + gl.glTexCoord2f(0, 0); glVertex(point_A); + gl.glTexCoord2f(1, 0); glVertex(point_B); + gl.glTexCoord2f(0, 1); glVertex(point_F); + gl.glTexCoord2f(1, 1); glVertex(point_E); gl.glEnd(); } @@ -275,16 +312,16 @@ class Robot extends BetterBase { */ private void drawTorso() { // Scale the torso to specified values - drawBeam(torsoWidth, depth, torsoHeight, Direction.Z, Color.LIGHT_GRAY); + drawBeam(torsoWidth, depth, torsoHeight, Direction.Z, Color.LIGHT_GRAY, true); if (asStickFigure) { // draw the bone connecting the arms (visible for stick figure) gl.glTranslatef(0, 0, torsoHeight / 2); - drawBeam(torsoWidth + armWidth * 3, boneSize, boneSize, Direction.X, null); + drawBeam(torsoWidth + armWidth * 3, boneSize, boneSize, Direction.X, null, false); gl.glTranslatef(0, 0, -torsoHeight); // draw the bone connecting the legs (visible for stick figure) - drawBeam(.5f * torsoWidth + legWidth / 2, boneSize, boneSize, Direction.X, null); + drawBeam(.5f * torsoWidth + legWidth / 2, boneSize, boneSize, Direction.X, null, false); // return to torso center gl.glTranslatef(0, 0, torsoHeight / 2); @@ -332,7 +369,7 @@ class Robot extends BetterBase { // upper leg half gl.glTranslatef(0, 0, -legLength / 4); - drawBeam(legWidth, legWidth, legLength / 2, Direction.Z, Color.DARK_GRAY); + drawBeam(legWidth, legWidth, legLength / 2, Direction.Z, Color.DARK_GRAY, false); // knee gl.glTranslatef(0, 0, -legLength / 4); @@ -340,7 +377,7 @@ class Robot extends BetterBase { // lower leg half gl.glTranslatef(0, 0, -legLength / 4); - drawBeam(legWidth, legWidth, legLength / 2, Direction.Z, Color.DARK_GRAY); + drawBeam(legWidth, legWidth, legLength / 2, Direction.Z, Color.DARK_GRAY, false); // for the stick figure, the "foot" is just an extension of the leg line // and therefore stays at the same y-position @@ -351,7 +388,7 @@ class Robot extends BetterBase { } // draw foot! gl.glTranslatef(0, foot_y, -legLength / 4 - footHeight / 2); - drawBeam(footWidth, footLength, footHeight, Direction.Z, Colors.GRAYISH); + drawBeam(footWidth, footLength, footHeight, Direction.Z, Colors.GRAYISH, false); // Restore position gl.glPopMatrix(); @@ -376,7 +413,7 @@ class Robot extends BetterBase { // here is your arm (start drawing from the elbow) gl.glTranslatef(0, 0, -armLength / 2); - drawBeam(armWidth, armWidth, armLength, Direction.Z, Colors.ARM_GRAY_COLOR); + drawBeam(armWidth, armWidth, armLength, Direction.Z, Colors.ARM_GRAY_COLOR, false); if (!asStickFigure) { // Give me a big hand! @@ -398,7 +435,7 @@ class Robot extends BetterBase { // position centered above the torso for the neck gl.glTranslatef(0f, 0f, torsoHeight / 2 + neckHeight / 2); float neckRadius = headRadius / 2.5f; - drawBeam(neckRadius, neckRadius, neckHeight, Direction.Z, Colors.LAVENDER); + drawBeam(neckRadius, neckRadius, neckHeight, Direction.Z, Colors.LAVENDER, false); // continue moving to the center of the head gl.glTranslatef(0f, 0f, neckHeight / 2 + headRadius / 2); diff --git a/src/RobotRace.java b/src/RobotRace.java index 7a7689b..ee8abb5 100644 --- a/src/RobotRace.java +++ b/src/RobotRace.java @@ -1,4 +1,5 @@ +import com.jogamp.opengl.util.texture.Texture; import java.awt.Color; import java.awt.Desktop; import java.awt.KeyEventDispatcher; @@ -169,7 +170,7 @@ public class RobotRace extends Base { } // Initialize the race track - raceTrack = new RaceTrack(); + raceTrack = new RaceTrack(this); // Initialize the camera camera = new Camera(gs, raceTrack, robots); @@ -486,7 +487,23 @@ public class RobotRace extends Base { // reset color gl.glColor3f(0, 0, 0); } - + + public Texture getTorsoTexture() { + return this.torso; + } + + public Texture getHeadTexture() { + return this.head; + } + + public Texture getBrickTexture() { + return this.brick; + } + + public Texture getTrackTexture() { + return this.track; + } + /** * Main program execution body, delegates to an instance of the RobotRace * implementation. -- cgit v1.2.1