LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: johnnyh on September 01, 2011, 19:12:52

Title: Problem with gluPerspective
Post by: johnnyh on September 01, 2011, 19:12:52
hello,

I am just trying to print a quad on the screen with LWJGL for several hours... but it doesnt seem to work ! I put near to 1 and far to 1000 because it is said that the gluPerspective transform its into -1 and -1000. So my pyramid of vision should contain the **** quad i am drawing (z=-20) !!!





package testlwjgl4;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;


public class TestLWJGL4 {
    public void start(){
        try {
    Display.setDisplayMode(new DisplayMode(800,600));
    Display.create();
} catch (LWJGLException e) {
    e.printStackTrace();
    System.exit(0);
}
       
        init();
       
        while(!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
            refresh();
        }
       
        Display.destroy();
        }
    ////////////////////////////////////////////////////////////////////////////
    public void init(){
        GL11.glMatrixMode(GL11.GL_PROJECTION);
            GL11.glLoadIdentity();
            GLU.gluPerspective(70, (float)800/600, 1, 1000);
            //GL11.glOrtho(0, 800, 600, 0,1,1000 );
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glLoadIdentity();
    }
   
    public void refresh(){
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glColor3f(1f,0.5f,1.0f);
        GL11.glBegin(GL11.GL_QUADS);
            GL11.glVertex3f(400-30,300-30,-20);
            GL11.glVertex3f(400+30,300-30,-20);
            GL11.glVertex3f(400+30,300+30,-20);
            GL11.glVertex3f(400-30,300+30,-20);
        GL11.glEnd();
        Display.update();
    }
   

    public static void main(String[] args) {
        TestLWJGL4 test = new TestLWJGL4();
        test.start();
    }
}


Title: Re: Problem with gluPerspective
Post by: Chuck on September 01, 2011, 22:13:31
Your problem is that you're drawing well off-center -- remember (0,0,z) is the center of your projection  -- and very close to the origin.  In other words, your quad is way off to the upper right and would still fill the screen if you moved it center.  gluPerspective puts the apex of the frustum pyramid at (0,0,0) and points it down the z axis, widening in the y direction at the angle you set for your FOV, and in the X direction by the FOV * aspect.  So at -20, you don't have a lot of screen real estate to work with.

Try this instead, which works:


        GL11.glBegin(GL11.GL_QUADS);
            GL11.glVertex3f(-3,-3,-20);
            GL11.glVertex3f( 3,-3,-20);
            GL11.glVertex3f( 3, 3,-20);
            GL11.glVertex3f(-3, 3,-20);
        GL11.glEnd();



You might also give the NeHe tutorials a try, which sets up a perspective projection from word go.  The old LWJGL ports have bit-rotted, but I've ported the first 7 so far and lavishly commented them here at https://bitbucket.org/chuck/lwjgl-sandbox/src/14473ab062f8/src/main/java/tutorials/nehe/
Title: Re: Problem with gluPerspective
Post by: johnnyh on September 02, 2011, 00:03:23
Thx Chuck for your response.

Well now if I would like to keep the same quad as before but i would like to watch it. Should i use glRotatef for that working on the Projection Matrix ? Or on the ModelView Matrix ?
Title: Re: Problem with gluPerspective
Post by: Chuck on September 02, 2011, 00:22:07
If you want to rotate an object, you work on the ModelView matrix.  You pretty much never want to rotate or translate the projection matrix unless you're doing something truly funky like stereographic projections.

Lesson04.java has a couple rotating objects, so I'd take a look at that (everything from 4 on involves some kind of animation)