Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Having some trouble with my projection matrix.

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2013
    Posts
    5

    Having some trouble with my projection matrix.

    I'm just trying to create an orthographic projection matrix, but I am having some serious problems here. I do not have access to glOrtho, so please don't tell me to use that. I am using LWJGL, and the Matrix4f class is column major so I am pretty sure that this code should not be behaving like it is
    Code :
    	float left = 0;
    	float right = 1;
    	float bottom = 1;
    	float top = 0;
    	float near = 0f;
    	float far = 100f;
     
    	projectionMatrix.m00 = 2 / (right - left);
    	projectionMatrix.m11 = 2 / (top - bottom);
    	projectionMatrix.m22 = -2 / (far - near);
    	projectionMatrix.m30 = -((right + left) / (right - left));
    	projectionMatrix.m31 = -((top + bottom) / (top - bottom));
    	projectionMatrix.m32 = -((far + near) / (far - near));

    I can't upload an image to this post because this account is new, but it draws my quad in the top left of the screen with the bottom right of the quad in the center of the screen.

    When I try to change the values so that it is correctly setup to my screen, as shown below, nothing shows up at all.
    Code :
    	float left = 0;
    	float right = Engine.windowWidth;
    	float bottom = Engine.windowHeight;
    	float top = 0;
    	float near = 0f;
    	float far = 100f;
     
    	projectionMatrix.m00 = 2 / (right - left);
    	projectionMatrix.m11 = 2 / (top - bottom);
    	projectionMatrix.m22 = -2 / (far - near);
    	projectionMatrix.m30 = -((right + left) / (right - left));
    	projectionMatrix.m31 = -((top + bottom) / (top - bottom));
    	projectionMatrix.m32 = -((far + near) / (far - near));

    Changing the right and bottom values slowly move the quad off the screen to the left. I have been struggling with this for a while now and I feel like it's something really stupid that I am missing.

  2. #2
    Junior Member Newbie
    Join Date
    Jun 2013
    Posts
    5
    I didn't figure it out, but I kind of maybe fixed it?
    Code :
    		float left = -(Engine.windowWidth / 2);
    		float right = Engine.windowWidth / 2;
    		float bottom = Engine.windowHeight / 2;
    		float top = -(Engine.windowHeight / 2);
    		float near = 0f;
    		float far = 1f;
    This maps the center to 0, 0, which is not what I want.
    Last edited by Dooskington; 08-25-2013 at 12:17 PM.

  3. #3
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    495
    You appear to be trying to incorporate the window dimensions into the projection matrix, but that's handled by the viewport transformation.

    After transformation by the projection matrix and division by w, -1,-1 is mapped to x,y and 1,1 to x+width,y+height, where x, y, width and height are the values passed to glViewport().

    Also, your code fragments don't show any settings for the bottom row; projectionMatrix.m33 is set to 1, right?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •