glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_FOG);
Controller joystick = ControllerEnvironment.getDefaultEnvironment().getControllers()[0];
{
FloatBuffer fogColours = BufferUtils.createFloatBuffer(4);
fogColours.put(new float[]{fogColor.r, fogColor.g, fogColor.b, fogColor.a});
glClearColor(fogColor.r, fogColor.g, fogColor.b, fogColor.a);
fogColours.flip();
glFog(GL_FOG_COLOR, fogColours);
glFogi(GL_FOG_MODE, GL_LINEAR);
glHint(GL_FOG_HINT, GL_NICEST);
glFogf(GL_FOG_START, fogNear);
glFogf(GL_FOG_END, fogFar);
glFogf(GL_FOG_DENSITY, 0.005f);
}
int floorTexture = glGenTextures();
{
InputStream in = null;
try {
in = new FileInputStream("res/images/floor.png");
PNGDecoder decoder = new PNGDecoder(in);
ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth() * decoder.getHeight());
decoder.decode(buffer, decoder.getWidth() * 4, Format.RGBA);
buffer.flip();
glBindTexture(GL_TEXTURE_2D, floorTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA,
GL_UNSIGNED_BYTE, buffer);
glBindTexture(GL_TEXTURE_2D, 0);
} catch (FileNotFoundException ex) {
System.err.println("Failed to find the texture files.");
Display.destroy();
System.exit(1);
} catch (IOException ex) {
System.err.println("Failed to load the texture files.");
Display.destroy();
System.exit(1);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
int ceilingDisplayList = glGenLists(1);
glNewList(ceilingDisplayList, GL_COMPILE);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(-gridSize, ceilingHeight, -gridSize);
glTexCoord2f(gridSize * 10 * tileSize, 0);
glVertex3f(gridSize, ceilingHeight, -gridSize);
glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
glVertex3f(gridSize, ceilingHeight, gridSize);
glTexCoord2f(0, gridSize * 10 * tileSize);
glVertex3f(-gridSize, ceilingHeight, gridSize);
glEnd();
glEndList();
int wallDisplayList = glGenLists(1);
glNewList(wallDisplayList, GL_COMPILE);
glBegin(GL_QUADS);
// North wall
glTexCoord2f(0, 0);
glVertex3f(-gridSize, floorHeight, -gridSize);
glTexCoord2f(0, gridSize * 10 * tileSize);
glVertex3f(gridSize, floorHeight, -gridSize);
glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
glVertex3f(gridSize, ceilingHeight, -gridSize);
glTexCoord2f(gridSize * 10 * tileSize, 0);
glVertex3f(-gridSize, ceilingHeight, -gridSize);
// West wall
glTexCoord2f(0, 0);
glVertex3f(-gridSize, floorHeight, -gridSize);
glTexCoord2f(gridSize * 10 * tileSize, 0);
glVertex3f(-gridSize, ceilingHeight, -gridSize);
glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
glVertex3f(-gridSize, ceilingHeight, +gridSize);
glTexCoord2f(0, gridSize * 10 * tileSize);
glVertex3f(-gridSize, floorHeight, +gridSize);
// East wall
glTexCoord2f(0, 0);
glVertex3f(+gridSize, floorHeight, -gridSize);
glTexCoord2f(gridSize * 10 * tileSize, 0);
glVertex3f(+gridSize, floorHeight, +gridSize);
glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
glVertex3f(+gridSize, ceilingHeight, +gridSize);
glTexCoord2f(0, gridSize * 10 * tileSize);
glVertex3f(+gridSize, ceilingHeight, -gridSize);
// South wall
glTexCoord2f(0, 0);
glVertex3f(-gridSize, floorHeight, +gridSize);
glTexCoord2f(gridSize * 10 * tileSize, 0);
glVertex3f(-gridSize, ceilingHeight, +gridSize);
glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
glVertex3f(+gridSize, ceilingHeight, +gridSize);
glTexCoord2f(0, gridSize * 10 * tileSize);
glVertex3f(+gridSize, floorHeight, +gridSize);
glEnd();
glEndList();
int floorDisplayList = glGenLists(1);
glNewList(floorDisplayList, GL_COMPILE);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(-gridSize, floorHeight, -gridSize);
glTexCoord2f(0, gridSize * 10 * tileSize);
glVertex3f(-gridSize, floorHeight, gridSize);
glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
glVertex3f(gridSize, floorHeight, gridSize);
glTexCoord2f(gridSize * 10 * tileSize, 0);
glVertex3f(gridSize, floorHeight, -gridSize);
glEnd();
glEndList();
int objectDisplayList = glGenLists(1);
glNewList(objectDisplayList, GL_COMPILE);
{
double topPoint = 0.75;
glBegin(GL_TRIANGLES);
glColor4f(1, 1, 0, 1f);
glVertex3d(0, topPoint, -5);
glColor4f(0, 0, 1, 1f);
glVertex3d(-1, -0.75, -4);
glColor4f(0, 0, 1, 1f);
glVertex3d(1, -.75, -4);
glColor4f(1, 1, 0, 1f);
glVertex3d(0, topPoint, -5);
glColor4f(0, 0, 1, 1f);
glVertex3d(1, -0.75, -4);
glColor4f(0, 0, 1, 1f);
glVertex3d(1, -0.75, -6);
glColor4f(1, 1, 0, 1f);
glVertex3d(0, topPoint, -5);
glColor4f(0, 0, 1, 1f);
glVertex3d(1, -0.75, -6);
glColor4f(0, 0, 1, 1f);
glVertex3d(-1, -.75, -6);
glColor4f(1, 1, 0, 1f);
glVertex3d(0, topPoint, -5);
glColor4f(0, 0, 1, 1f);
glVertex3d(-1, -0.75, -6);
glColor4f(0, 0, 1, 1f);
glVertex3d(-1, -.75, -4);
glEnd();
glColor4f(1, 1, 1, 1);
}
glEndList();
getDelta();
lastFPS = getTime();
while (running) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int delta = getDelta();
glBindTexture(GL_TEXTURE_2D, floorTexture);
glEnable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glCallList(floorDisplayList);
glCallList(ceilingDisplayList);
glCallList(wallDisplayList);
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glBindTexture(GL_TEXTURE_2D, 0);
glCallList(objectDisplayList);
glLoadIdentity();
glRotatef(rotation.x, 1, 0, 0);
glRotatef(rotation.y, 0, 1, 0);
glRotatef(rotation.z, 0, 0, 1);
glTranslatef(position.x, position.y, position.z);
if (Mouse.isGrabbed()) {
float mouseDX = Mouse.getDX() * mouseSpeed * 0.16f;
float mouseDY = Mouse.getDY() * mouseSpeed * 0.16f;
if (rotation.y + mouseDX >= 360) {
rotation.y = rotation.y + mouseDX - 360;
} else if (rotation.y + mouseDX < 0) {
rotation.y = 360 - rotation.y + mouseDX;
} else {
rotation.y += mouseDX;
}
if (rotation.x - mouseDY >= maxLookDown && rotation.x - mouseDY <= maxLookUp) {
rotation.x += -mouseDY;
} else if (rotation.x - mouseDY < maxLookDown) {
rotation.x = maxLookDown;
} else if (rotation.x - mouseDY > maxLookUp) {
rotation.x = maxLookUp;
}
}
boolean keyUp = Keyboard.isKeyDown(Keyboard.KEY_UP) || Keyboard.isKeyDown(Keyboard.KEY_W);
boolean keyDown = Keyboard.isKeyDown(Keyboard.KEY_DOWN) || Keyboard.isKeyDown(Keyboard.KEY_S);
boolean keyLeft = Keyboard.isKeyDown(Keyboard.KEY_LEFT) || Keyboard.isKeyDown(Keyboard.KEY_A);
boolean keyRight = Keyboard.isKeyDown(Keyboard.KEY_RIGHT) || Keyboard.isKeyDown(Keyboard.KEY_D);
boolean flyUp = Keyboard.isKeyDown(Keyboard.KEY_SPACE);
boolean flyDown = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT);
boolean moveFaster = Keyboard.isKeyDown(Keyboard.KEY_LCONTROL);
boolean moveSlower = Keyboard.isKeyDown(Keyboard.KEY_TAB);
joystick.poll();
for (Component c : joystick.getComponents()) {
double dy = 0, dx = 0;
if (c.getName().equals("x")) {
joystickSpeed = (int) ((-c.getPollData() + 1.1) * 10);
if (c.getPollData() < -.004) {
keyLeft = true;