views:

133

answers:

2

Hi all,

(Mac OS X, Cocoa, C/C++, OpenGL, somewhat mathematically challenged.)

Project is a real-time (30fps) poker game, with a 2D view. The OpenGL view is set up for Orthographic projection using glOrtho; the camera is always looking directly down at the cards/table. To date, it's essentially been a 2D image composition engine.

My playing cards are pre-built textures (with alpha), rendered thusly:

glBegin(GL_TRIANGLE_STRIP);
    glTexCoord2f(0.0, 0.0);
    glVertex3d(startDraw_X, startDraw_Y, 0.0);

    glTexCoord2f(1.0, 0.0);
    glVertex3d(endDraw_X, startDraw_Y, 0.0);

    glTexCoord2f(0.0, 1.0);
    glVertex3d(startDraw_X, endDraw_Y, 0.0);

    glTexCoord2f(1.0, 1.0);
    glVertex3d (endDraw_X, endDraw_Y, 0.0);
glEnd();

This works well, and provides 1:1 pixel mapping, for high image quality.

QUESTION: how can I (cheaply, quickly) "flip" or distort my card using OpenGL? The rotation part isn't hard, but I'm having trouble with the perspective aspect.

Specifically, I have in mind the horizontal flipping action of UIViews in iPhone applications (e.g. Music). The UIView will appear to flip by shrinking horizontally, with the appearance of perspective (the "rear" coordinates of the view will shrink vertically.)

Thanks for any help or tips.

+2  A: 

Instead of using glOrtho to set up an orthographic projection, use glFrustum to set up a perspective projection.

Jerry Coffin
+1. You can work to create a fake-perspective warp in the tex coords in your ortho space, or you can just leverage the power of GL that's already there and make it 3D. Then the perspective will even look right! :)
quixoto
Thanks for the help guys. By using glFrustrum, do you mean that I should do this specifically for this one-card draw, and then reset to glOrtho for all other drawing?
SirRatty
Given that the rest of your drawing is apparently pretty much 2D, it probably won't make much difference either way. Since you're setting up and calling `glFrustum` for part of it, I'd probably use it throughout.
Jerry Coffin
OK, I admit to being quite confused as to what I need to do. As it stands, the whole shebang is currently 2D. I use the z axis in terms of layering/stacking objects on top of each other. I only want a perspective effect for a single flipped card, but do not want perspective for any other elements. Similarly, they have to retain their current positions and appearances. Cheers.
SirRatty
@SirRatty: if you need to assure identical appearance otherwise, then yes you probably want to continue using glOrtho for the other drawing. If changes of a few pixels are acceptable, and you've used reasonably accurate z-values, then leaving the matrix from glFrustum won't make much difference, simply because most of it's close to flat anyway (though a stack of chips can be tall enough that it might make a difference there).
Jerry Coffin
A: 

Since you game is 2D, I assume it is a top-view camera, right ?

In this case, you can setup your projection matrix as a projection one, put your camera at the center of the table, at a decent height (> 1.2 x width of table depending on the FoV) and not change anything else.

You may have to set your card's (and other game items) height a lot closer from the table than what they may currently be (since in ortho, a delta of 10 meters along Z won't be seen).

Mixing ortho and perspective is technically possible, but I don't quite see the advantage. However, you may have special assets in your game that require special treatments...

Calvin1602