Ok, you’re right. I should have done so in the first place…
Here you go. It is a little weird for it is a subset of a project but it works and produces the result I posted…:
import java.awt.*;
import java.awt.event.*;
import net.java.games.jogl.*;
public class JoglProblem {
static Animator animator = null;
static class Renderer implements GLEventListener, KeyListener {
static class Wall {
private float[] point0 = {0.0f, 0.0f, 0.0f};
private float[] point1 = {0.0f, 0.0f, 0.0f};
private float[] point2 = {0.0f, 0.0f, 0.0f};
private float[] point3 = {0.0f, 0.0f, 0.0f};
public Wall (float[] point0, float[] point1, float[] point2, float[] point3) {
this.point0 = point0;
this.point1 = point1;
this.point2 = point2;
this.point3 = point3;
}
public void draw (GL gl) {
gl.glVertex3f(point0[0], point0[1], point0[2]);
gl.glVertex3f(point1[0], point1[1], point1[2]);
gl.glVertex3f(point2[0], point2[1], point2[2]);
gl.glVertex3f(point3[0], point3[1], point3[2]);
}
}
private float xpos = 0.0f;
private float zpos = 0.0f;
private int heading = 180;
private Frame frame;
private Wall[] room = new Wall[16];
private boolean debug = true;
public Renderer (Frame f) {
float[] point0 = {-4.0f, -1.0f, 4.0f};
float[] point1 = {-4.0f, 1.0f, 4.0f};
float[] point2 = {-2.0f, -1.0f, 4.0f};
float[] point3 = {-2.0f, 1.0f, 4.0f};
float[] point4 = {-2.0f, -1.0f, 6.0f};
float[] point5 = {-2.0f, 1.0f, 6.0f};
float[] point6 = {-3.0f, -1.0f, 6.0f};
float[] point7 = {-3.0f, 1.0f, 6.0f};
float[] point8 = {-3.0f, -1.0f, 8.0f};
float[] point9 = {-3.0f, 1.0f, 8.0f};
float[] point10 = {-2.0f, -1.0f, 8.0f};
float[] point11 = {-2.0f, 1.0f, 8.0f};
float[] point12 = {-2.0f, -1.0f, 10.0f};
float[] point13 = {-2.0f, 1.0f, 10.0f};
float[] point14 = {0.0f, -1.0f, 10.0f};
float[] point15 = {0.0f, 1.0f, 10.0f};
float[] point16 = {2.0f, -1.0f, 10.0f};
float[] point17 = {2.0f, 1.0f, 10.0f};
float[] point18 = {2.0f, -1.0f, 8.0f};
float[] point19 = {2.0f, 1.0f, 8.0f};
float[] point20 = {3.0f, -1.0f, 8.0f};
float[] point21 = {3.0f, 1.0f, 8.0f};
float[] point22 = {3.0f, -1.0f, 6.0f};
float[] point23 = {3.0f, 1.0f, 6.0f};
float[] point24 = {2.0f, -1.0f, 6.0f};
float[] point25 = {2.0f, 1.0f, 6.0f};
float[] point26 = {2.0f, -1.0f, 4.0f};
float[] point27 = {2.0f, 1.0f, 4.0f};
float[] point28 = {4.0f, -1.0f, 4.0f};
float[] point29 = {4.0f, 1.0f, 4.0f};
float[] point30 = {-4.0f, -1.0f, 0.0f};
float[] point31 = {-4.0f, 1.0f, 0.0f};
float[] point32 = {4.0f, -1.0f, 0.0f};
float[] point33 = {4.0f, 1.0f, 0.0f};
frame = f;
room[0] = new Wall(point0, point1, point3, point2);
room[1] = new Wall(point2, point3, point5, point4);
room[2] = new Wall(point4, point5, point7, point6);
room[3] = new Wall(point6, point7, point9, point8);
room[4] = new Wall(point8, point9, point11, point10);
room[5] = new Wall(point10, point11, point13, point12);
room[6] = new Wall(point12, point13, point15, point14);
room[7] = new Wall(point14, point15, point17, point16);
room[8] = new Wall(point16, point17, point19, point18);
room[9] = new Wall(point18, point19, point21, point20);
room[10] = new Wall(point20, point21, point23, point22);
room[11] = new Wall(point22, point23, point25, point24);
room[12] = new Wall(point24, point25, point27, point26);
room[13] = new Wall(point26, point27, point29, point28);
room[14] = new Wall(point28, point29, point33, point32);
room[15] = new Wall(point30, point31, point1, point0);
}
public void display(GLDrawable gLDrawable) {
final GL gl = gLDrawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glRotatef((float)heading, 0.0f, 1.0f, 0.0f);
gl.glTranslatef(xpos, 0.0f, -zpos);
if (debug) System.out.println("Scene setup with " + gLDrawable.getGLU().gluErrorString(gl.glGetError()));
draw(gLDrawable);
if (debug) System.out.println("Scene drawn with " + gLDrawable.getGLU().gluErrorString(gl.glGetError()));
}
private void draw (GLDrawable gLDrawable) {
final GL gl = gLDrawable.getGL();
gl.glBegin(GL.GL_QUADS);
gl.glColor3f(0.0f, 0.0f, 1.0f);
for (int i = 0; i < room.length; i++) {
room[i].draw(gl);
}
if (debug) System.out.println("Before glEnd() with " + gLDrawable.getGLU().gluErrorString(gl.glGetError()));
gl.glEnd();
if (debug) System.out.println("glEnd() with " + gLDrawable.getGLU().gluErrorString(gl.glGetError()));
}
public void init(GLDrawable gLDrawable) {
final GL gl = gLDrawable.getGL();
gl.glShadeModel(GL.GL_SMOOTH);
gl.glClearColor(1.0f, 1.0f, 1.0f, 0.5f);
gl.glClearDepth(1.0f);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
gl.glDisable(GL.GL_CULL_FACE);
if (debug) System.out.println("Init finished with " + gLDrawable.getGLU().gluErrorString(gl.glGetError()));
}
public void reshape(GLDrawable gLDrawable, int x, int y, int width, int height) {
final GL gl = gLDrawable.getGL();
final GLU glu = gLDrawable.getGLU();
if (height <= 0)
height = 1;
final float h = (float)width / (float)height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 20.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
if (debug) System.out.println("Reshape finished with " + gLDrawable.getGLU().gluErrorString(gl.glGetError()));
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
animator.stop();
System.exit(0);
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
heading ++;
if (heading >= 181)
heading = -179;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
heading--;
if (heading <= -181)
heading = 179;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
float xdifference = ((float) (Math.sin(((double)heading / 180.0) * Math.PI) * 0.05f));; // Move On The X-Plane Based On Player Direction
float zdifference = ((float) (Math.cos(((double)heading / 180.0) * Math.PI) * 0.05f)); // Move On The Z-Plane Based On Player Direction
xpos += xdifference;
zpos += zdifference;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
float xdifference = ((float) (Math.sin(((double)heading / 180.0) * Math.PI) * 0.05f)); // Move On The X-Plane Based On Player Direction
float zdifference = ((float) (Math.cos(((double)heading / 180.0) * Math.PI) * 0.05f)); // Move On The Z-Plane Based On Player Direction
xpos -= xdifference;
zpos -= zdifference;
}
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) {}
}
public static void main(String[] args) {
Frame frame = new Frame("Jogl Problem");
GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
Renderer renderer = new Renderer(frame);
canvas.addGLEventListener(renderer);
canvas.addKeyListener(renderer);
frame.add(canvas);
frame.setSize(640, 480);
animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
animator.stop();
System.exit(0);
}
});
frame.show();
animator.start();
}
}
Greetz…
…Hoschi