How do I find out the ?scale? of the OGL window?

Started by CuppoJava, March 20, 2005, 21:36:59

Previous topic - Next topic

CuppoJava

Hi,
I'm doing a 2D game in LWJGL, and so far so good. But I'm having trouble with the actual scale of the window...
I'm drawing my sprites right now with GL_QUADS, with a width and height of 1, and its already taking up about 20% of the screen. But a little tweaking can work around that.

But now I'm drawing the HUD and I need to draw a shape at EXACTLY which and which pixels on the screen. How can I do that?

Right now I'm using:
   GLU.gluPerspective(45f,
       (float)width/height,
       0.1f,
       100.0f);

Whatever that means... I using perspective because I want to be able to "zoom" in and out. I think orthographic will not let me do that. But anyways, I have no idea what i'm doing besides pluggin in other people's code to see what is does. Does anyone know of a good website that covers these basics?

jam007


CuppoJava

Wow is that the full OpenGL red book?

Do you have a link to the rest too? I was going to buy those books until you gave me the link.

Thx a lot

napier

If you want to render exactly to screen pixels, you'll probably want ortho mode.  If you want to zoom in and out of an ortho mode rendering (like a map view) you can scale the ortho up and down easily enough.  By definition there's no perspective in ortho mode, though you can render your scene in 3D, then switch to ortho and draw a HUD on top of the scene.

I have an ortho mode demo here that may help:

http://potatoland.org/code/gl

Here's some functions to setup ortho and draw a flat image over the current scene:

   public static void setOrthoOn()
    {
        // prepare to render in 2D
        GL11.glDisable(GL11.GL_DEPTH_TEST);     // so text stays on top of scene
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glPushMatrix();                    // preserve perspective view
        GL11.glLoadIdentity();                  // clear the perspective matrix
        GL11.glOrtho(viewportX,viewportX+viewportW,viewportY,viewportY+viewportH,-1,1);  // turn on 2D
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glPushMatrix();			// Preserve the Modelview Matrix
        GL11.glLoadIdentity();			// clear the Modelview Matrix
    }

    public static void setOrthoOff()
    {
        // restore the original positions and views
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glPopMatrix();
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glPopMatrix();
        GL11.glEnable(GL11.GL_DEPTH_TEST);		// turn Depth Testing back on
    }

    public static void drawImageQuad(int textureHandle, int x, int y, float w, float h) {
        // activate the specified texture
        GL11.glBindTexture(GL11.GL_TEXTURE_2D,textureHandle);
        // prepare to render in 2D
        setOrthoOn();
        // draw the image textured onto a quad
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glTexCoord2f(0f, 0f);
        GL11.glVertex3f( (float)x, (float)y, (float)0);
        GL11.glTexCoord2f(1f, 0f);
        GL11.glVertex3f( (float)x+w, (float)y, (float)0);
        GL11.glTexCoord2f(1f, 1f);
        GL11.glVertex3f( (float)x+w, (float)y+h, (float)0);
        GL11.glTexCoord2f(0f, 1f);
        GL11.glVertex3f( (float)x, (float)y+h, (float)0);
        GL11.glEnd();
        // restore the previous perspective and model views
        setOrthoOff();
    }
penGL/Java/LWJGL demos and code: http://potatoland.org/code/gl

Zero

Quote from: "CuppoJava"Wow is that the full OpenGL red book?

Do you have a link to the rest too? I was going to buy those books until you gave me the link.

Thx a lot

That is not the most current book.  They have the 1.1 redbook pdf available at opengl.org.

But the latest printing is 1.4 and the books number is in line with the opengl version number.
1.4 book has stuff on multitexturing and the 1.1 book doesn't (since opengl1.1 could not) and all kinds of other little changes.
You might want to wait for the 2.0 version to come out.... but then they haven't released a 1.5 version yet.... so maybe not.  I do recommend getting the red book though.

PS. i have written a function that does what you want. I will post it later tonight.

CuppoJava

Hi,
Ortho does indeed sound like the way to go. I'll try out all of the code pasted and see if I get anywhere with it.

One last question about perspective though..
Is there a way I can tell what are the world coordinates of the screen boundaries?

ie. I want to know exactly how tall the window is in world units.

Anonymous

You can 'un-project' (GLU.gluUnProject()) the screen points back into world coordinates.  Unproject the corners points of your window (ie. 0,0 and 640,480) to find the world coords for those points.  

You have to decide what Z you want to use for the world coords, since the screen has no depth, then unproject to that Z position.  

I have a utility class here:    

   http://potatoland.org/code/gl/GLApp.java

Look at these functions for sample code:

   /**
     * For given screen xy, return the world xyz coords in a float array.  Assume
     * Z position is 0.
     */
    public static float[] getWorldCoordsAtScreen(int x, int y)

    /**
     * Find the Z depth of the origin in the projected world view. Used by unproject()
     */
    public static float getZDepthAtOrigin()

    /**
     * Return world coordinates for a given point on the screen.  The screen
     * point xyz is 'un-projected' back into world coordinates using the
     * current model and projection matrices, and the current viewport settings.
     */
    public static void unProject(float x, float y, float z, float[] resultf)

    /**
     * Return screen coordinates for a given point in world space.  The world
     * point xyz is 'projected' into screen coordinates using the current model
     * and projection matrices, and the current viewport settings.
     */
    public static void project(float x, float y, float z, float[] resultf)

napier

That post above was from me.  Seems like I got logged out midway...
penGL/Java/LWJGL demos and code: http://potatoland.org/code/gl

Cuppo

Thanks for that napier.

It seems I'm in good shape now, my engine's gonna be pretty perdy soon.

Thanks again everyone.