views:

352

answers:

1

I need the view to show the road polygon (a rectangle 3.f * 100.f) with a vanishing point for a road being at 3/4 height of the viewport and the nearest road edge as a viewport's bottom side. See Crazy Taxi game for an example of what I wish to do.

I'm using iPhone SDK 3.1.2 default OpenGL ES project template.

I setup the projection matrix as follows:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustumf(-2.25f, 2.25f, -1.5f, 1.5f, 0.1f, 1000.0f);

Then I use glRotatef to adjust for landscape mode and setup camera.

glMatrixMode(GL_MODELVIEW);

glLoadIdentity(); glRotatef(-90, 0.0f, 0.0f, 1.0f);

const float cameraAngle = 45.0f * M_PI / 180.0f; gluLookAt(0.0f, 2.0f, 0.0f, 0.0f, 0.0f, 100.0f, 0.0f, cos(cameraAngle), sin(cameraAngle));

My road polygon triangle strip is like this:

static const GLfloat roadVertices[] = {

-1.5f, 0.0f, 0.0f, 1.5f, 0.0f, 0.0f, -1.5f, 0.0f, 100.0f, 1.5f, 0.0f, 100.0f, };

And I can't seem to find the right parameters for gluLookAt. My vanishing point is always at the center of the screen.

+2  A: 

You want to do this in genuine 3D? basically your look at point is the center of the screen. What you want it to be is a quarter of the screen below the horizon.

You can work it out by inverting a final position by projection and by view (essentially unprojecting).

Assuming the screen stretches from -1.0f to 1.0f in x and -1.0f to 1.0f in y then you want the horizon to go to (0.0f, 0.5f). This means you need to look down by quarter of the screen (ie -0.5f). For a given z (we'll randomly chose 0.5f). So if we take your view as you've already defined and an "identity" matrix as well as your "projection" matrix you can then pass this through gluUnproject as follows:

GLdouble eyeX, eyeY, eyeZ;
gluUnproject( 0.0f, -0.5f, 0.5f, identity, view, projection, &eyeX, &eyeY, &eyeZ );

You can then plug your eye values back into gluLookAt and you will now be looking at a point below the road surface.

I'm sure there are myriad other ways of doing it but that one should work...

Goz