GL units and screen coordinates

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

GL units and screen coordinates

Flow
Hey all,

I'm trying to setup my JOGL environment such as my GL Units ans screen coordinates are the same, but I can't get it.

I read a few other posts on different websites, and I tried this:
public void init(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        GLU glu = new GLU();

        // Set the clear color to black
        gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluOrtho2D(0.0, 0.0, width, height);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
}

Small precision: width and height are the size of my JFrame in pixels (900x700 in this case)!

With this init method, my GL units and screen coordinates should match, shouldn't they?

Thanks a lot!
Reply | Threaded
Open this post in threaded view
|

Re: GL units and screen coordinates

Wade Walker
Administrator
Hi Flow,

The problem might be how you're calling gluOrtho2D. It's defined like this

gluOrtho2D(left, right, bottom, top)

where the four arguments are the clipping lines. X coordinates between left and right will be in the viewport, as will y coordinates between bottom and top. So to do a one-to-one mapping you'd want either this

glu.gluOrtho2D(0.0, width, 0.0, height);    // origin at bottom left

or this

glu.gluOrtho2D(-width/2.0, width/2.0, -height/2.0, height/2.0);    // origin at center
Reply | Threaded
Open this post in threaded view
|

Re: GL units and screen coordinates

Flow
Hi Wade, that's exactly what I was trying to do :)

But I was using a zomming in/out feature based in a float which was used like this : in my display() method I've this code :
gl.glVertex3f(0.0f, 0.0f, zoom);

This feature is not working anymore with this option. Is there any solution to this problem?

Thanks again!
Reply | Threaded
Open this post in threaded view
|

Re: GL units and screen coordinates

gouessej
Administrator
Modify the parameters of gluOrtho2D when you zoom.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: GL units and screen coordinates

Flow
Thanks to you!
Reply | Threaded
Open this post in threaded view
|

Re: GL units and screen coordinates

Wade Walker
Administrator
In reply to this post by Flow
Julien is right as usual  Normally I do something like this:

gl.glMatrixMode( GL.GL_PROJECTION );
gl.glLoadIdentity();
glu.gluOrtho2D( -(fObjectUnitsPerPixel * iWidth) / 2.0f,
                 (fObjectUnitsPerPixel * iWidth) / 2.0f,
                -(fObjectUnitsPerPixel * iHeight) / 2.0f,
                 (fObjectUnitsPerPixel * iHeight) / 2.0f );

gl.glMatrixMode( GL.GL_MODELVIEW );
gl.glViewport( 0, 0, iWidth, iHeight );
gl.glLoadIdentity();
gl.glTranslatef( fViewTranslateX, fViewTranslateY, 0.0f );

When fObjectUnitsPerPixel = 1.0, there's no zoom. Decreasing fObjectUnitsPerPixel zooms you in, and increasing it zooms you out. fViewTranslateX and fViewTranslateY are used to pan the viewport around.
Reply | Threaded
Open this post in threaded view
|

Re: GL units and screen coordinates

Flow
Hey again!

I've try your solution, but the problem is that the zooming is the same when my fObjectUnitsPerPixel is equal to -1, 0 or 1, but when it's another value (i.e. 10 or 5), nothing is draw.

I've just remplace fObjectUnitsPerPixel by my zooming value (which is increased or decreased by 1 each time mouse wheel is moved).

Is it normal?

Thanks to you two!
Reply | Threaded
Open this post in threaded view
|

Re: GL units and screen coordinates

Wade Walker
Administrator
Handling fObjectUnitsPerPixel can be tricky. For example, if fObjectUnitsPerPixel = 1, that means that a triangle that's 10 object units across (e.g. (0,0), (10,0), (0,10)) will be ten pixels across on screen. But it changes quickly:

fObjectUnitsPerPixel  triangle size in pixels
---------------------------------------------
10.0                  1
2.0                   5
1.0                   10
0.5                   20
0.1                   100
0.01                  1000
0.0                   infinity
<0.0                  meaningless

A fObjectUnitsPerPixel value less than zero makes no sense, because you can't have -1 object units per pixel -- objects can't have negative size!

So you need to make sure that fObjectUnitsPerPixel stays between some reasonable bounds (say between 0.001 and 100), and that it changes in variable-size steps (bigger steps when you're far away, smaller steps when you're closer). Usually I do this to zoom out one step:

double dZoomStep = fObjectUnitsPerPixel / sfZoomStepDivisor;
fObjectUnitsPerPixel += dZoomStep;

To zoom in, just subtract dZoomStep instead of adding it. sfZoomStepDivisor is a constant (greater than 1) that you can adjust depending on your application. Start with it set to 5 or so and see how it works for you.
Reply | Threaded
Open this post in threaded view
|

Re: GL units and screen coordinates

Flow
I'll try that when I'll be back home, thanks a lot for your great advices