简体   繁体   English

OpenGL透视投影来映射屏幕像素

[英]OpenGL perspective projection to map the screen pixels

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. 我试图在OpenGL中设置透视投影,使其精确映射到屏幕像素,就像我们在正交投影中所做的那样。 I can do this for specific screen sizes, for example 640x960, but cannot do it for any screen size. 我可以针对特定的屏幕尺寸执行此操作,例如640x960,但不能对任何屏幕尺寸执行此操作。 Below is the code I have used to set it up for 640x960 size 下面是我用来设置640x960大小的代码

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. 这适用于640x960分辨率,但不适用于任何其他像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. 我想设置透视投影,因为我想提供沿x和y轴旋转精灵的能力。

I am trying to set up perspective projection in OpenGL such that it maps exactly to the screen pixels 我试图在OpenGL中设置透视投影,使其精确映射到屏幕像素

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 对于所有其他人,它根本无法将视图X,Y映射到视口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. 我想设置透视投影,因为我想提供沿x和y轴旋转精灵的能力。

Nothing stops you from doing that with an ortho projection. 没有什么能阻止你用正射投影做到这一点。

Seems like the problem was with the multiplication order. 似乎问题在于乘法顺序。 The above code now works fine with any resolution. 以上代码现在适用于任何分辨率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM