如何更改投影以显示我的对象?
我有一个尺寸为(W,H)并且位于(-W * 0.5,-H * 0.5)的2D对象(GL_QUAD)。
我不想调整对象的大小,因为我需要能够选择该对象上相对于纹理的点。 (即 openGL 中的 (x,y) 转换为纹理上的 (x,y) - 无需缩放)。
我想要做的是更改相机属性(投影、角度等),以便对象,无论大小如何,都显示填充屏幕的最大轴。 (长宽比适合?)
我可以比较屏幕尺寸与对象尺寸,没有问题。我可以毫无问题地更改我的相机矩阵(模型视图和投影)。但我不知道如何计算查看整个对象所需的变换。
我在想:
- 使用 gluProject()平移相机 (0,0,-1)
- 项目对象的 4 个坐标,
- 测试屏幕矩形内是否存在点。
- 如果没有,请转到 1。
但这似乎...计算量很大。我有屏幕坐标和对象坐标 - 我是否应该能够计算每个对象的 4 个角之间的线所描述的立体角并将其设置为该立体角?
谢谢,
[编辑]
这是另一个不优雅的解决方案:
void View::getCamera(eq::Matrix4f& camera)
{
camera = eq::Matrix4f::IDENTITY;
camera = camera * _rotation;
camera.set_translation( _translation );
float longAxis = 1.0f;
// Normalize Scale
@try {
float tH = (float)getHeight(); // texture coords
float tW = (float)getWidth();
float sH = getBaseFrustum().getWall().getHeight(); // screen coords
float sW = getBaseFrustum().getWall().getWidth();
float rW = tW / sW;
float rH = tH / sH;
if (rH > rW) {
longAxis *= tH*2.0f;
} else {
longAxis *= tW*2.0f;
}
}
@catch (...) {
//Nothing?
NSLog(@"Couldn't get Height or Width");
}
// Normalize Scale to Screen/Image Size
camera.scale( eq::Vector3f(_scale/longAxis,-_scale/longAxis,1.0f) );
}
我不觉得这是一个很好的解决方案,但是......
I have an 2D object (GL_QUAD) with size (W,H) and sitting at (-W*0.5, -H*0.5).
I'd rather not resize the object, because I need to be able to select points on that object relative to the texture. (i.e. (x,y) in openGL translates to (x,y) on the texture - no scaling needed).
What I want to do is change the camera properties (projection, angle, etc) so that the object, regardless of size, shows up filling the largest axis of the screen. (Aspect Fit?)
I can compare the screen size vs. object size no problem. I can change my camera matrix no problem (modelview and projection). But I don't know how to calculate the transformation I need in order to view the whole object.
I was thinking of:
- translate camera (0,0,-1)
- project object's 4 coords with gluProject()
- test if points exist within screen rect.
- if not, goto 1.
But that seems... computationally so intensive. I have the screen coords, and the object coords - shouldn't I be able to calculate the solid angle that lines between the 4 corners of each object describe and set it to that?
Thanks,
[EDIT]
Here's another non-elegant solution:
void View::getCamera(eq::Matrix4f& camera)
{
camera = eq::Matrix4f::IDENTITY;
camera = camera * _rotation;
camera.set_translation( _translation );
float longAxis = 1.0f;
// Normalize Scale
@try {
float tH = (float)getHeight(); // texture coords
float tW = (float)getWidth();
float sH = getBaseFrustum().getWall().getHeight(); // screen coords
float sW = getBaseFrustum().getWall().getWidth();
float rW = tW / sW;
float rH = tH / sH;
if (rH > rW) {
longAxis *= tH*2.0f;
} else {
longAxis *= tW*2.0f;
}
}
@catch (...) {
//Nothing?
NSLog(@"Couldn't get Height or Width");
}
// Normalize Scale to Screen/Image Size
camera.scale( eq::Vector3f(_scale/longAxis,-_scale/longAxis,1.0f) );
}
I don't feel like it's a great solution, however...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来你的东西都是二维的,真的不需要透视投影的好处,在这种情况下,正交投影会更容易使用。正交投影使得安装四边形来填充屏幕变得非常容易。
http://www.opengl.org/sdk/docs/man/ xhtml/glOrtho.xml
It sounds like your stuff is all 2D and really doesn't need the benefits of a perspective projection, in such a case an orthogonal projection would be much easier to use. An orthogonal projection makes it trivially easy to fit a quad to fill the screen.
http://www.opengl.org/sdk/docs/man/xhtml/glOrtho.xml