Search code examples
javaopengl3dlwjgl

gluPerspective() displaying a black screen?


I have a simple application that displays a 3D cube that rotates on all axes, and a camera controlled via mouse that allows you to adjust the angle you're looking at the cube.

The entire application works exactly as I want when I use glOrtho, but once I change it to gluPerspective I only get a black screen.

Here is my method to initialize openGL:

public void initializeOpenGL()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, ((float)WIDTH) / ((float)HEIGHT), 0.01f, 500f);
    //glOrtho(0, 640, 480, 0, 600, -1); //displays everything correctly
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_DEPTH_TEST);
}

And then here is my loop method:

public void begin()
{
    float degrees = 0;
    Mouse.setGrabbed(false);
    while(!Display.isCloseRequested())
    {
        moveCamera(); //calculates the changes in mouse position            
        GL11.glLoadIdentity();
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        camera.lookThrough(); //applies the changes
        glTranslatef((float)WIDTH/2,(float)HEIGHT/2,10f);
        glRotatef(degrees++,1f,1f,1f);
        glScalef(100,100,100);

        drawQuad();

        Display.update();
        Display.sync(60);
    }
    Display.destroy();
    System.exit(0);     
}

Solution

  • I created an image displaying where the camera looks: https://i.sstatic.net/rIPhm.png As you can see the camera looks along the -z axis

    -> you should try translating the quad to something like:

    glTranslatef(0,0, -10f );
    

    Instead of moving the quad to the middle of the Display you move it half the size of the Display in the nowhere of your Landscape

    SHORT VERSION: Gist: https://gist.github.com/Chronove/11da12b2635bfc040981

    package me.tutorial;
    
    import static org.lwjgl.opengl.GL11.*;
    import static org.lwjgl.util.glu.GLU.gluPerspective;
    
    import org.lwjgl.opengl.Display;
    import org.lwjgl.opengl.DisplayMode;
    
    /**
     * @author Chronove
     */
    public class Example {
    
        public static void main(String[] args) throws Exception {
    
            int WIDTH  = 600;
            int HEIGHT = 400;
    
            // INIT
            Display.setDisplayMode(new DisplayMode(WIDTH,HEIGHT));
            Display.create();
    
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
    
            gluPerspective(45.0f,(float)(WIDTH / HEIGHT),0.1f,500f);
    
            glMatrixMode(GL_MODELVIEW);
            glEnable(GL_DEPTH_TEST);
    
            float degrees = 0;
    
            while(!(Display.isCloseRequested())){
    
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                glLoadIdentity();
    
                glTranslatef(0,0,-10f);
                glRotatef(degrees++,0f,0f,1f);
    
                // drawQuad();
                {
    
                    glBegin(GL_QUADS);
    
                    glVertex3f(-1f,-1f,0f);
                    glVertex3f( 1f,-1f,0f);
                    glVertex3f( 1f, 1f,0f);
                    glVertex3f(-1f, 1f,0f);
    
                    glEnd();
    
                }
    
                Display.update();
                Display.sync(60);
    
            }
    
            Display.destroy();
            System.exit(0);
    
        }
    
    }