# OpenGL Problem with World - View - Projection

This topic is 2023 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I'm programming an app for iOS with OpenGL ES 2.0 but the application it isn't draw anything

I did my own math library for matrix and vector's operations and works good.

I compute the View Matrix like DirectX in Right Hand System
MATRIX* ComputeViewMatrixRH(MATRIX *pOut, const VECTOR3 *pEye, const VECTOR3 *pLookAt, const VECTOR3 *pUp)
{
VECTOR3 zAxis = *pEye - *pLookAt;
Vector3Normalize(&zAxis, &zAxis);

VECTOR3 xAxis;
Vector3Cross(&xAxis, pUp, &zAxis);
Vector3Normalize(&xAxis, &xAxis);

VECTOR3 yAxis;
Vector3Cross(&yAxis, &zAxis, &xAxis);

pOut->m[0][0] = xAxis.x; pOut->m[0][1] = yAxis.x; pOut->m[0][2] = zAxis.x; pOut->m[0][3] = 0.0f;
pOut->m[1][0] = xAxis.y; pOut->m[1][1] = yAxis.y; pOut->m[1][2] = zAxis.y; pOut->m[1][3] = 0.0f;
pOut->m[2][0] = xAxis.z; pOut->m[2][1] = yAxis.z; pOut->m[2][2] = zAxis.z; pOut->m[2][3] = 0.0f;

pOut->m[3][0] = Vector3Dot(&xAxis, pEye);
pOut->m[3][1] = Vector3Dot(&yAxis, pEye);
pOut->m[3][2] = Vector3Dot(&zAxis, pEye);
pOut->m[3][3] = 1.0f;

return pOut;
}


The perspective Matrix I compute like DirectX in Right Hand System

MATRIX* ComputeProjectionMatrixRH(MATRIX* pOut, float fovY, float aspectRatio, float zn, float zf)
{
float yScale = 1/tan(fovY/2);
float xScale = yScale / aspectRatio;

pOut->m[0][0] = xScale; pOut->m[0][1] = 0.0f;   pOut->m[0][2] = 0.0f;        pOut->m[0][3] =  0.0f;
pOut->m[1][0] = 0.0f;   pOut->m[1][1] = yScale; pOut->m[1][2] = 0.0f;        pOut->m[1][3] =  0.0f;
pOut->m[2][0] = 0.0f;   pOut->m[2][1] = 0.0f;   pOut->m[2][2] = zf/(zn-zf);  pOut->m[2][3] = -1.0f;
pOut->m[3][0] = 0.0f;   pOut->m[3][1] = 0.0f;   pOut->m[3][2] = zn*zf/(zn-zf);pOut->m[3][3] =  0.0f;

return pOut;
}


Now I'm confused with that because some books and pages in Internet show the projection matrix different. I tested all matrix but anything works.

For example other perspective projection matrix for right hand system is this:

1/tan(alpha/2)*aspect,        0        ,     0      ,   0
0              ,   1/tan(alpha/2),     0      ,   0
0              ,        0        ,(f+n)/(f-n) ,   1
0              ,        0        , 2*f*n/(n-f),   0


What's the correct matrix?? Why many matrices for perspective projection???

Thanks!

##### Share on other sites
Projection matrices are different between D3D and GL because the range of the Z coordinate in NDC (post projection) space is different.
D3D z ranges from 0 to 1, whereas GL made the silly decision to make z range from -1 to 1.

I'll check that

##### Share on other sites

One way i test my matrix stuff and making sure that everything is correct is do all the transformation myself including put them in screen coordinate and actually see if the values that i am getting back makes sense. Are they are mapping to proper screen pixels. If they are then you should be good. Since the card is doing the exact same thing.

Edited by BornToCode

##### Share on other sites

I have some code on my other laptop that successfully converts left handed matrices from Direct3D to right handed matrices for OpenGL.  I could share that if you want.

But if you just need to get a projection matrix, try looking at open source implementations of gluLookAt and gluPerspective.

OpenGL compatible matrix class: http://www.songho.ca/opengl/gl_matrix.html

I added that last one just in case.  Also, keep in mind that OpenGL's matrices are column major and not row major like Direct3D's.  Sorry I can't type more, I'm heading out of the door in just a few minutes.

Shogun.

##### Share on other sites
Hi,

I checked a lot matrices but anything works for me.

In this page http://maniacdev.com/2009/05/opengl-gluperspective-function-in-iphone-opengl-es/ the perspective matrix is different to the other http://www.songho.ca/opengl/gl_matrix.html

I don't understand why :(

Anyway I'll check that with more detail.

Thanks!!

##### Share on other sites

Hi,

I solved the problem! :)

I had the view matrix wrong! The components of the last vector was positive and the correct values are negative.

And the correct Perspective Matrix for the OpenGL is this:

pOut->m[0][0] = xScale; pOut->m[0][1] = 0.0f;   pOut->m[0][2] =  0.0f;           pOut->m[0][3] =  0.0f;
pOut->m[1][0] = 0.0f;   pOut->m[1][1] = yScale; pOut->m[1][2] =  0.0f;           pOut->m[1][3] =  0.0f;
pOut->m[2][0] = 0.0f;   pOut->m[2][1] = 0.0f;   pOut->m[2][2] = (zf+zn)/(zn-zf); pOut->m[2][3] = -1.0f;
pOut->m[3][0] = 0.0f;   pOut->m[3][1] = 0.0f;   pOut->m[3][2] = 2*zf*zn/(zn-zf); pOut->m[3][3] =  0.0f;


Thanks everyone!!

1. 1
Rutin
31
2. 2
3. 3
4. 4
5. 5

• 13
• 22
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632962
• Total Posts
3009511
• ### Who's Online (See full list)

There are no registered users currently online

×