glOrtho matrix equivalent

Problems with Canvas, OpenGL, etc...

glOrtho matrix equivalent

Hi, I need to store a copy of projection matrix in OpenGL ES 1.0
I tried to create my method for generating parallel projection matrix:
Code: Select all
`   /**    * Generates and returns parallel projection matrix.    * @param left - left surface position,    * @param right - right surface position,    * @param bottom - bottom surface position,    * @param top - top surface position,    * @param near - distance to near clipping plane,    * @param far - distance to far clipping plane.    * @return 4x4 parallel projection matrix.    */   public static final float[] parallelProjection(         float left, float right, float bottom, float top, float near, float far) {      // Calculate essential matrix elements.      float x1y1 = 2.0f / (right - left);      float x2y2 = 2.0f / (top - bottom);      float x3y3 = -2.0f / (far - near);      float x4y1 = - (right + left) / (right - left);      float x4y2 = - (top + bottom) / (top - bottom);      float x4y3 = - (far + near) / (far - near);      // 4x4 projection matrix.      return new float[] {            x1y1, 0.0f, 0.0f, x4y1,            0.0f, x2y2, 0.0f, x4y2,            0.0f, 0.0f, x3y3, x4y3,            0.0f, 0.0f, 0.0f, 1.0f,      };   }`

The problem is, that instead of square I get a deformed line.
Here' my rendering code:
Code: Select all
`      // Set up 2D projection.      gl.glMatrixMode(GL10.GL_PROJECTION);            //gl.glOrthof(.0f, GameRenderer.width, GameRenderer.height, .0f, 0.0f, 1.0f);            gl.glMultMatrixf(Matrix3D.parallelProjection(.0f, GameRenderer.width, GameRenderer.height, .0f, 0.0f, 1.0f), 0);            `

If I uncomment the Ortho method and comment mine, everything works fine.
Any idea what might be wrong?
Ghost
Developer

Posts: 26
Joined: Fri Feb 11, 2011 2:50 pm

Re: glOrtho matrix equivalent

I think, and this is a long shot since I'm batting a 0.0 so far, that opengl expects column-major ordering in the array you're passing to glMultMatrix.

Try something like

Code: Select all
`{x1y1, 0.0, 0.0, 0.0,0.0, x2y2, 0.0, 0.0,0.0, 0.0, x3y3, 0.0,x4y1,x4y2,x4y3,1.0}`

Worked for me.

PS got my Optimus! yay!
theRet
Freshman

Posts: 6
Joined: Sun Jul 10, 2011 8:53 pm

Re: glOrtho matrix equivalent

Whoa, it actually worked, and I'm very confused to why.
I came from DirectX background and I was fully aware about translation coordinate position in matrix, but after reading pages like this:
http://www.opengl.org/sdk/docs/man/xhtml/glOrtho.xml
I figured that openGl switched positions.
I actually tried even your suggested solution before, but it didn't work at that time (probably due to my lazines).
Anyway, thanks.

P. S. If anyone knows why official documentation provides misleading information, please tell me.
Ghost
Developer

Posts: 26
Joined: Fri Feb 11, 2011 2:50 pm

Re: glOrtho matrix equivalent

Thanks and glad to hear you got 'er working.
The specifics as to why glMultMatrix applies the column-major ordering are here http://www.opengl.org/sdk/docs/man/xhtml/glMultMatrix.xml
theRet
Freshman

Posts: 6
Joined: Sun Jul 10, 2011 8:53 pm