I'm making my first steps with OpenGL with LWJGL. I am currenlty constructing a... wait for it... CUBE. Quite creative...
Up to now i made it to three of its faces and thats where the Problems began. Sometimes its faces overlap each other. Googeling that Problem brought up DEPTH TESTING thing which i tried immediately unfortunately with no luck. The Problem remains the same. The faces are still "overlapping" eachother and it seems that this is somehow related to the drawing order.
Here is my complete code:
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;
public class Cube {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Display.setDisplayMode(new DisplayMode(1024, 768));
Display.create();
Display.setVSyncEnabled(true);
} catch (LWJGLException e) {
e.printStackTrace();
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30f, 1024f / 768f, 0.00001f, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_3D_COLOR);
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
//glDepthFunc(GL_ALWAYS); // The Type Of Depth Testing To Do
// Really Nice Perspective Calculations
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
//glShadeModel(GL_SMOOTH);
System.out.println(glGetString(GL_VERSION));
float speed = 0.0f;
float rotationX = 0;
float rotationY = 0;
float rotationZ = 0;
while(!Display.isCloseRequested()){
System.out.println(glGetError());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTranslatef(0, 0, speed);
if(Mouse.isButtonDown(0)){
rotationX +=1;
}
if(Mouse.isButtonDown(1)){
rotationY += 1;
}
if(Mouse.isButtonDown(2)){
rotationZ += 1;
}
rotationX %= 360;
rotationY %= 360;
rotationZ %= 360;
glPushMatrix();
glTranslatef(0, 0, -50);
glRotatef(rotationX, 1, 0, 0);
glRotatef(rotationY, 0, 1, 0);
glRotatef(rotationZ, 0, 0, 1);
glBegin(GL_QUADS);
glColor3f(1, 0, 0);
//Deckel
glVertex3f(0, 5, 0);
glVertex3f(5, 5, 0);
glVertex3f(5, 5, -5);
glVertex3f(0, 5, -5);
glColor3f(1, 1, 1);
//Front
glVertex3f(0, 0, 0);
glVertex3f(5, 0, 0);
glVertex3f(5, 5, 0);
glVertex3f(0, 5, 0);
glColor3f(0, 1, 0);
glVertex3f(0, 5, -5);
glVertex3f(5, 5, -5);
glVertex3f(5, 0, -5);
glVertex3f(0, 0,-5);
glEnd();
glPopMatrix();
if(Keyboard.isKeyDown(Keyboard.KEY_UP)){
speed += 0.05f;
}
if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)){
speed -= 0.05f;
}
Display.update();
Display.sync(60);
}
Display.destroy();
}
}
If you're using the code, you can rotate the "cube" by using the three mouse buttons
- Is there anything that i'm obviously doing wrong?
- Does this code work for anyone?
- Could this problem result from using this on Linux?, 'cause that's what i'm doing.