Hi, I found a piece of code that does pretty much the same thing except it works. But I don't really know what the difference to my code is.
import org.lwjgl.*;
import org.lwjgl.opengl.*;
import org.lwjgl.opengl.glu.*;
public class GLApp {
public static boolean finished; // App will exit when finished is true (when finishedKey is hit: see mainloop())
public static String windowTitle = "My Test";
public static DisplayMode DM, origDM; // hold display mode we set, and the display mode when app first executes
public static int displayWidth = 800;
public static int displayHeight = 600;
public static int displayColorBits = 24;
public static int displayFrequency = 60;
public static int depthBufferBits = 24; // bits per pixel in the depth buffer
public static boolean fullScreen = false; // full screen or floating window
public static float aspectRatio = 0; // aspect ratio of Display (will default to displayWidth/displayHeight)
public static int viewportX, viewportY; // viewport position (will default to 0,0)
public static int viewportW, viewportH; // viewport size (will default to screen width, height)
static float rotation = 0f; // to rotate cubes (just to put something on screen)
public static void main(String args[]) {
GLApp demo = new GLApp();
demo.run();
}
public void run() {
init();
while (!finished) {
if (!Display.isVisible()) {
try {
Thread.sleep(200L);
} catch(Exception ex) {}
} else if (Display.isCloseRequested()) {
finished = true;
}
mainLoop();
Display.update();
}
cleanup();
System.exit(0);
}
public void mainLoop() {
render();
}
public void render() {
// select model view for subsequent transforms
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
// clear depth buffer and color
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
// rotate, scale and draw cube
rotation += .3;
GL11.glPushMatrix();
GL11.glLoadIdentity();
GL11.glRotatef(rotation, (float)1, (float)1, (float)1);
GL11.glColor4f(0f, .5f, 1f, 1f);
renderCube();
GL11.glPopMatrix();
}
public void cleanup() {
Display.destroy();
}
public void init() {
initDisplay();
initOpenGL();
}
public void initOpenGL() {
GL11.glClearDepth(1.0f); // Depth Buffer Setup
GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing // Need this ON if using textures
GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do
GL11.glClearColor(0f, 0f, 0f, 0f); // Black Background
GL11.glViewport(viewportX, viewportY, viewportW, viewportH);
setPerspective();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
public boolean initDisplay () {
origDM = Display.getDisplayMode();
System.out.println("GLApp.initDisplay(): Current display mode is " + origDM);
if ( (DM = getDisplayMode(displayWidth, displayHeight, displayColorBits, displayFrequency)) == null) {
if ( (DM = getDisplayMode(800, 600, 32, 60)) == null) {
if( (DM = getDisplayMode(800, 600, 24, 60)) == null) {
if ( (DM = getDisplayMode(800, 600, 16, 60)) == null) {
if ( (DM = getDisplayMode(origDM.getWidth(), origDM.getHeight(), origDM.getBitsPerPixel(), origDM.getFrequency())) == null) {
System.out.println("GLApp.initDisplay(): Can't find a compatible Display Mode!!!");
}
}
}
}
}
try {
System.out.println("GLApp.initDisplay(): Setting display mode to " + DM + " with pixel depth = " + depthBufferBits);
Display.setDisplayMode(DM);
} catch (Exception exception) {
System.err.println("GLApp.initDisplay(): Failed to create display: " + exception);
System.exit(1);
}
// Initialize the Window
try {
Display.create(new PixelFormat(0, depthBufferBits, 0)); // set bits per buffer: alpha, depth, stencil
Display.setTitle(windowTitle);
Display.setFullscreen(fullScreen);
Display.setVSyncEnabled(true);
System.out.println("GLApp.initDisplay(): Created OpenGL window.");
} catch (Exception exception1) {
System.err.println("GLApp.initDisplay(): Failed to create OpenGL window: " + exception1);
System.exit(1);
}
// Set viewport width/height and Aspect ratio.
if (aspectRatio == 0f) {
aspectRatio = (float)DM.getWidth() / (float)DM.getHeight();
}
// viewport may not match shape of screen. Adjust to fit.
viewportH = DM.getHeight(); // viewport Height
viewportW = (int) (DM.getHeight() * aspectRatio); // Width
if (viewportW > DM.getWidth()) {
viewportW = DM.getWidth();
viewportH = (int) (DM.getWidth() * (1 / aspectRatio));
}
// center viewport in screen
viewportX = (int) ((DM.getWidth()-viewportW) / 2);
viewportY = (int) ((DM.getHeight()-viewportH) / 2);
return true;
}
public static DisplayMode getDisplayMode(int w, int h, int colorBits, int freq) {
try {
DisplayMode adisplaymode[] = Display.getAvailableDisplayModes();
DisplayMode dm = null;
for (int j = 0; j < adisplaymode.length; j++) {
dm = adisplaymode[j];
if (dm.getWidth() == w && dm.getHeight() == h && dm.getBitsPerPixel() == colorBits &&
dm.getFrequency() == freq) {
return dm;
}
}
}
catch (LWJGLException e) {
System.out.println("GLApp.getDisplayMode(): Exception " + e);
}
return null;
}
public static void setPerspective() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(30f, aspectRatio, 1f, 30f);
GLU.gluLookAt(0f, 0f, 15f, 0f, 0f, 0f, 0f, 1f, 0f);
}
public static void renderCube() {
GL11.glBegin(GL11.GL_QUADS);
// Front Face
GL11.glColor3f(1f, 0f, 0f);
GL11.glVertex3f(-1.0f, -1.0f, 1.0f);
GL11.glVertex3f( 1.0f, -1.0f, 1.0f);
GL11.glVertex3f( 1.0f, 1.0f, 1.0f);
GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
GL11.glColor3f(0f, 1f, 0f);
GL11.glVertex3f(-1.0f, -1.0f, -1.0f);
GL11.glVertex3f(-1.0f, 1.0f, -1.0f);
GL11.glVertex3f( 1.0f, 1.0f, -1.0f);
GL11.glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
GL11.glColor3f(0f, 0f, 1f);
GL11.glVertex3f(-1.0f, 1.0f, -1.0f);
GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
GL11.glVertex3f( 1.0f, 1.0f, 1.0f);
GL11.glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
GL11.glColor3f(1f, 1f, 0f);
GL11.glVertex3f(-1.0f, -1.0f, -1.0f);
GL11.glVertex3f( 1.0f, -1.0f, -1.0f);
GL11.glVertex3f( 1.0f, -1.0f, 1.0f);
GL11.glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
GL11.glColor3f(1f, 0f, 1f);
GL11.glVertex3f( 1.0f, -1.0f, -1.0f);
GL11.glVertex3f( 1.0f, 1.0f, -1.0f);
GL11.glVertex3f( 1.0f, 1.0f, 1.0f);
GL11.glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
GL11.glColor3f(0f, 1f, 1f);
GL11.glVertex3f(-1.0f, -1.0f, -1.0f);
GL11.glVertex3f(-1.0f, -1.0f, 1.0f);
GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
GL11.glVertex3f(-1.0f, 1.0f, -1.0f);
GL11.glEnd();
}
}
Why does this code switch between GL_PROJECTION and GL_MODEL_VIEW all the time???