Question

im working on a project to learn to render 3d shapes. to get the depth right i am trying to use glEnable(GL_DEPTH_TEST). as expected it will not render anything with ony that addition so i added glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT), but still i do not get a single thing rendered. here is a simplified version of my code.

public class Main {

public static void main(String[] args) {

    initDisplay();
    gameLoop();
    cleanUp();
}
public static void initDisplay(){
    try {

        Display.setDisplayMode(new DisplayMode(500,500));
        Display.create();

    } catch (LWJGLException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}


public static void gameLoop(){
    float x = 0;
    float y = 0;
    float z = -20;
    float cubeX = 0;
    float cubeY = 0;
    float cubeZ = 0;
    float apothem = 1;
    float aspect = Display.getWidth()/Display.getHeight();
    Camera cam = new Camera(aspect,70,0.0f, 1000);
    Cube cube = new Cube(1);
    while(!Display.isCloseRequested() && Display.isActive()){
        if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){
            x+=.003;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)){
            x-=.003;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_UP)){
            z-=.003;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)){
            z+=.003;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_D)){
            cubeZ+=.03;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_A)){
            cubeZ-=.03;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_W)){
            cubeX+=.03;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_S)){
            cubeX-=.03;
        }
        cam.setX(x);
        cam.setY(y);
        cam.setZ(z);

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glLoadIdentity();
        cam.useView();
        cube.render(cubeX,cubeY,cubeZ);
        Display.update();
    }

}

this is the class used to create and render a cube

public class Cube {
float apothem;
Cube(float apothem){
    this.apothem = apothem;
}
public void render(float x, float y, float z){

    glPushMatrix();

        glRotatef(x,1,0,0);
        glRotatef(y,0,1,0);
        glRotatef(z,0,0,1);
        glBegin(GL_QUADS);
            //front face
            glColor3f(.22f, .64f, .14f);
            glVertex3d(-apothem,apothem,apothem);
            glVertex3d(apothem,apothem,apothem);
            glVertex3d(apothem,-apothem,apothem);
            glVertex3d(-apothem,-apothem,apothem);

            //back face
            glColor3f(.23f, .26f, .64f);
            glVertex3d(-apothem,apothem,-apothem);
            glVertex3d(apothem,apothem,-apothem);
            glVertex3d(apothem,-apothem,-apothem);
            glVertex3d(-apothem,-apothem,-apothem);

            //left face
            glColor3f(.72f, .16f, .39f);
            glVertex3d(-apothem,apothem,-apothem);
            glVertex3d(-apothem,apothem,apothem);
            glVertex3d(-apothem,-apothem,apothem);
            glVertex3d(-apothem,-apothem,-apothem);

            //right face
            glColor3f(.53f, .21f, .84f);
            glVertex3d(apothem,-apothem,apothem);
            glVertex3d(apothem,-apothem,-apothem);
            glVertex3d(apothem,apothem,-apothem);
            glVertex3d(apothem,apothem,apothem);

            //top face
            glColor3f(.42f, .99f, .37f);
            glVertex3d(-apothem,apothem,apothem);
            glVertex3d(-apothem,apothem,-apothem);
            glVertex3d(apothem,apothem,-apothem);
            glVertex3d(apothem,apothem,apothem);

            //bottom face
            glColor4f(.44f, .11f, .74f,25f);
            glVertex3d(apothem,-apothem,-apothem);
            glVertex3d(apothem,-apothem,apothem);
            glVertex3d(-apothem,-apothem,apothem);
            glVertex3d(-apothem,-apothem,-apothem);
       glEnd();
   glPopMatrix();




}

} this is the camera im using to view things

private float x,y,z;
private float rx,ry,rz;

private float aspect;
private float fov;
private float zNear, zFar;

Camera(float aspect, float fov, float zNear, float zFar){
    x = 0;
    y = 0;
    z = 0;
    rx = 0;
    ry = 0;
    rz = 0;
    this.aspect = aspect;
    this.fov = fov;
    this.zNear = zNear;
    this.zFar = zFar;
    initProjection();

}
private void initProjection(){
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(fov, aspect, zNear, zFar);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_DEPTH_TEST);
}
public void useView(){
    glRotatef(rx,1,0,0);
    glRotatef(ry,0,1,0);
    glRotatef(rz,0,0,1);
    glTranslatef(x,y,z);
}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top