• Create Account

## Problem with World - View - Projection

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1cHimura  Members

Posted 29 March 2013 - 09:44 PM

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!

### #2Hodgman  Moderators

Posted 30 March 2013 - 12:03 AM

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.

### #3cHimura  Members

Posted 30 March 2013 - 06:50 PM

I'll check that

### #4BornToCode  Members

Posted 31 March 2013 - 12:53 AM

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, 31 March 2013 - 01:31 AM.

### #5blueshogun96  Members

Posted 01 April 2013 - 05:25 PM

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.

Follow Shogun3D on the official website: http://shogun3d.net

"Yo mama so fat, she can't be frustum culled." - yoshi_lol

"One objection to a “critique of C#” would be that you can’t talk about C# without talking about the whole “.Net experience”. However, one can approach the topic of Hitler without a complete discussion of Nationalist Socialism, so I feel justified." - Steve White.

### #6cHimura  Members

Posted 03 April 2013 - 12:37 AM

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!!

### #7cHimura  Members

Posted 06 April 2013 - 07:35 PM

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!!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.