Jump to content
  • Advertisement
Sign in to follow this  
cHimura

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,
 
I'm programming an app for iOS with OpenGL ES 2.0 but the application it isn't draw anything sad.png
 
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 this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
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 this post


Link to post
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

 

gluLookAt: http://iphonedevelopment.blogspot.com/2008/12/glulookat.html

 

gluPerspective: http://maniacdev.com/2009/05/opengl-gluperspective-function-in-iphone-opengl-es/

 

gluOrtho: http://stackoverflow.com/questions/7131037/how-do-you-implement-glortho-for-opengles-2-0-with-or-without-tx-ty-tz-values-f

 

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. sad.png

 

Shogun.

Share this post


Link to post
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!!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!