package com.idonedid.game;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.input.Keyboard;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
public class Prime {
private boolean done = false;
private boolean fullscreen = false;
private final String windowTitle = "NeHe's OpenGL Lesson 6 for LWJGL (Texture Mapping)";
private boolean f1 = false;
private DisplayMode displayMode;
private float xrot; // X Rotation ( NEW )
private float yrot; // Y Rotation ( NEW )
private float zrot; // Z Rotation ( NEW )
private Texture texture; // Storage For One Texture ( NEW )
private float zPos;
public static void main(String args[]) {
boolean fullscreen = false;
if(args.length>0) {
if(args[0].equalsIgnoreCase("fullscreen")) {
fullscreen = true;
}
}
Prime app = new Prime();
app.run(fullscreen);
}
public void run(boolean fullscreen) {
this.fullscreen = fullscreen;
try {
init();
while (!done) {
mainloop();
render();
Display.update();
}
cleanup();
}
catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
private void mainloop() throws LWJGLException {
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { // Exit if Escape is pressed
done = true;
}
if(Display.isCloseRequested()) { // Exit if window is closed
done = true;
}
if(Keyboard.isKeyDown(Keyboard.KEY_W))
{
zPos += 0.1f;
}
if (!Keyboard.isKeyDown(Keyboard.KEY_W))
{
zPos = 0f;
}
}
private void switchMode() {
fullscreen = !fullscreen;
try {
Display.setFullscreen(fullscreen);
}
catch(Exception e) {
e.printStackTrace();
}
}
private boolean render() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
GL11.glLoadIdentity(); // Reset The Current Modelview Matrix
texture.bind();
GL11.glTranslatef(0.0f, 0.0f, zPos); // Move Into The Screen 5 Units
GL11.glRotatef(xrot, 1.0f, 0.0f, 0.0f); // Rotate On The X Axis
GL11.glRotatef(yrot, 0.0f, 1.0f, 0.0f); // Rotate On The Y Axis
GL11.glRotatef(zrot, 0.0f, 0.0f, 1.0f); // Rotate On The Z Axis // Select Our Texture
GL11.glBegin(GL11.GL_QUADS);
// Front Face
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
// Back Face
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
// Top Face
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
// Bottom Face
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
// Right face
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
// Left Face
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
GL11.glEnd();
//xrot += 0.3f; // X Axis Rotation
//yrot += 0.2f; // Y Axis Rotation
//zrot += 0.4f; // Z Axis Rotation
return true;
}
private void createWindow() throws Exception {
Display.setFullscreen(fullscreen);
Display.setDisplayMode(new DisplayMode(800, 600));
Display.setTitle(windowTitle);
Display.create();
}
private void init() throws Exception {
createWindow();
loadTextures();
initGL();
}
private void loadTextures() throws Exception {
texture = loadTexture("dirt.png");
}
private void initGL() {
GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping
GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
GL11.glClearDepth(1.0f); // Depth Buffer Setup
GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do
GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
GL11.glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
// Really Nice Perspective Calculations
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
}
private void cleanup() {
Display.destroy();
}
/**
* Texture loading using DevIL
* Example created by Mark Bernard
*/
private Texture loadTexture(String path) throws Exception
{
return TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(path));
}
}
因此,本质上,当立方体处于其默认位置时,它看起来与它应该的完全一样。如果我将 Z 位置更改为 1f 或 0f 以外的任何值,它就会变得不可见。请帮助我理解并解决这个问题。