glOrtho matrix equivalent

Problems with Canvas, OpenGL, etc...

glOrtho matrix equivalent

Postby Ghost » Fri Jul 15, 2011 10:47 am

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
Developer
 
Posts: 26
Joined: Fri Feb 11, 2011 2:50 pm

Top

Re: glOrtho matrix equivalent

Postby theRet » Sat Jul 16, 2011 6:26 am

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
Freshman
 
Posts: 6
Joined: Sun Jul 10, 2011 8:53 pm

Re: glOrtho matrix equivalent

Postby Ghost » Sat Jul 16, 2011 3:58 pm

Congrats on your new purchase ;)

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
Developer
 
Posts: 26
Joined: Fri Feb 11, 2011 2:50 pm

Re: glOrtho matrix equivalent

Postby theRet » Sat Jul 16, 2011 4:04 pm

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
Freshman
 
Posts: 6
Joined: Sun Jul 10, 2011 8:53 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 3 guests