Login  Register

Re: GL units and screen coordinates

Posted by Wade Walker on Apr 29, 2011; 2:13pm
URL: https://forum.jogamp.org/GL-units-and-screen-coordinates-tp2826205p2879348.html

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.