# two-point perspective projection

Printable View

• 06-02-2009, 02:24 AM
dsasha
two-point perspective projection
Hello,
I am trying to impelement a two-point perspective projection and don't really know how to fill custom matrix for such a case.
The problem is as follows.
1. My camera is placed at camZ offset and camY height and looking at the origin point, which in turn the rotation center of the zx surface about Y axis.
2. All objects are disposed on this zx surface.
3. I need to create two-point perspective in order to eliminate any distortion along Y axis. In this case lines parallel to Y axis will remain parallel.
I could not find any code examples written in openGL.
Does anyone know how to do it?

Thank you in advance

alex
• 06-02-2009, 07:44 AM
Xmas
Re: two-point perspective projection
You would get that result by pointing the camera along the Z axis at the point (0, 0, camY) and only using the lower part of the rendered image, centered around the origin. To get that as your full image, you need to skew the view frustum.
Assuming your positive Y axis is up and negative Z is forward, this is the camera matrix you need to use:
1 0 0 0
0 1 -camY/camZ 0
0 0 1 -camZ
0 0 0 1

Code :

```// Instead of your current camera matrix setup: GLfloat cam[] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, -camY/camZ, -camZ, 0, 0, 0, 0, 1 }; glLoadMatrixf(cam);```
• 06-03-2009, 01:56 AM
dsasha
Re: two-point perspective projection
Hi Georg,
Thank you for reply. I tried to implement your suggestion and lost the building at all:)
The code I previously had was as follows:
Code :

``` glViewport(0, 0, w, h);   fAspect = (GLfloat)w / (GLfloat)h;   // Reset the coordinate system before modifying glMatrixMode(GL_PROJECTION); glLoadIdentity(); GLfloat zNear, zFar; zFar = 1000; // Set the clipping volume glFrustumf(-5*fAspect, 5*fAspect, -5, 5, 10, zFar); ugluLookAtf(0, camY, camZ, 0, camY, 0, 0, 1, 0);   //GLfloat cam[] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, -camY/camZ, -camZ, 0, 0, 0, 0, 1 }; //glLoadMatrixf(cam);   glMatrixMode(GL_MODELVIEW); glLoadIdentity(); DrawBuilding();```

If I replace ugluLookAtf() by matrix you suggested:
GLfloat cam[] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, -camY/camZ, -camZ, 0, 0, 0, 0, 1 };
glLoadMatrixf(cam);
I don't see the building.

I also moved glLoadMatrixf before glFrustum and received my building cut. Moving around didn't show the whole building.
What can be wrong?

Thank you for attention.
The positive Z is toward viewer, Y is up.
• 06-03-2009, 03:43 AM
Xmas
Re: two-point perspective projection
Your use of ugluLookAtf doesn't fit your initial description as you're looking at the point (0, camY, 0), not the origin.

The code I suggested was intended to replace the initial setup of the modelview matrix, not the projection matrix. glLoadMatrix replaces the current matrix, so the way you use it overwrites the projection set up with glFrustum.
In general, the view/camera transformation (not to be confused with the projection) should be part of the modelview matrix.

Code :

```glMatrixMode(GL_PROJECTION); glLoadIdentity(); GLfloat zNear, zFar; zFar = 1000; // Set the clipping volume glFrustumf(-5*fAspect, 5*fAspect, -5, 5, 10, zFar);   glMatrixMode(GL_MODELVIEW); #if TWO_POINT GLfloat cam[] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, -camY/camZ, -camZ, 0, 0, 0, 0, 1 }; glLoadMatrixf(cam); #else glLoadIdentity(); ugluLookAtf(0, camY, camZ, 0, camY, 0, 0, 1, 0); #endif   DrawBuilding();```