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...
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
精彩评论