problem with glOrtho & glViewport

Hi there!

As stated in the topic i don’t get the whole trick about these two. My aim is to display a rectangular texture with an orthographic projection, which should then be handled like pictues in photoshop for example (e.g. panning, zooming in % and so on). This works particulary with this code:


gl.glViewport(0, 0, img_width, img_height);
float zoom_x = zoom_borders_x*(1.0f/zoom);
zoom_x/=2.0f;
float zoom_y = zoom_borders_y*(1.0f/zoom);
zoom_y/=2.0f;
	
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
		
gl.glOrtho(-zoom_x, zoom_x,-zoom_y, zoom_y, .0f, 1000.0);

gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();

where zoom_borders_* are initially set to img_height and img_width.
When zoom = 1.0 the displayed texture has exact the size of the image, great.

But now the whole area of the JFrame should be useable, which is not in this case, e.g. when I
pan the image to the right it disappears before the right end of the JFrame cause of the glViewport-setting,
same behaviour when I zoom in…

Anyone got an idea how i could use the whole JFrame without loosing the aspect-ratio and the right pixel-representation (one pixel of the image
= 1 pixel on the screen). I tried it with multiplying the values in glViewport, but this doesn’t seem to work correctly :frowning:

thanks for reading, hope my problem is understandable and maybe solveable

greetings

basti

The viewport will always cap the area that is rendered into. No way around it.

The viewport should be (0, 0, w h).
You should transform the projection-matrix to display the image in the proper size.

You might want to start with glOrtho, with values like (0, w, 0, h, -1, +1)
then use the pixels-positions as vertex-positions. The other math shouldn’t be too hard.

Thanks for the quick answer! I’ll try that out later, heading to the club right now 8)
Hope I’ll figure out the math needed to correct the projection matrix. It’s fundamental
that the displayed section of the image keeps it size and position when the JFrame is changes size.

greetings

basti

This works just fine, thank you again ;D

greetings

basti