Question

I am trying to set up perspective projection in OpenGL such that it maps exactly to the screen pixels, like we can do in orthographic projections. I can do this for specific screen sizes, for example 640x960, but cannot do it for any screen size. Below is the code I have used to set it up for 640x960 size

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
Matrix4f perspective( Matrix4f::perspective( 60.0f, ( size.width / size.height ), 0.5f, 1500.f ) );
Matrix4f lookAt( Matrix4f::lookAt( size.width / 2.0f, size.height / 2.0f, size.height / 1.1566f, size.width / 2.0f, size.height / 2.0f, 0.0f, 0.0f, 1.0f, 0.0f ) );
Matrix4f m( ( perspective * lookAt ).translate( -( size.width / 2.0f ), -( size.height / 2.0f ), -( size.height/ 1.1566f ) ) );
glLoadMatrixf( m.get() );

This works for 640x960 resolution, but does not work for any other like 1024x768. In other resolutions it does not exactly map to the screen pixels.

I want to set up perspective projection because I want to provide an ability to rotate the sprites along x and y axes too.

Was it helpful?

Solution 2

Seems like the problem was with the multiplication order. The above code now works fine with any resolution.

OTHER TIPS

I am trying to set up perspective projection in OpenGL such that it maps exactly to the screen pixels

By its very nature this kind of mapping will hold for only one certain depth. For all others it simply can't map view X,Y to viewport X,Y

I want to set up perspective projection because I want to provide an ability to rotate the sprites along x and y axes too.

Nothing stops you from doing that with an ortho projection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top