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 2 of 2

Thread: Projection Planes and the Z-Axis

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    9

    Projection Planes and the Z-Axis

    Code :
    VertexData data[ 18 ] = {
    		{ { 255,   0,   0, 255 }, { -0.5f, 0.0f,  0.5f, 1.f } },	// front
    		{ {   0, 255,   0, 255 }, {  0.5f, 0.0f,  0.5f, 1.f } },
    		{ {   0,   0, 255, 255 }, {  0.0f, 0.5f,  0.0f, 1.f } },
    		{ { 255,   0,   0, 255 }, {  0.5f, 0.0f,  0.5f, 1.f } },	// right
    		{ {   0, 255,   0, 255 }, {  0.5f, 0.0f, -0.5f, 1.f } },
    		{ {   0,   0, 255, 255 }, {  0.0f, 0.5f,  0.0f, 1.f } },
    		{ { 255,   0,   0, 255 }, {  0.5f, 0.0f, -0.5f, 1.f } },	// back
    		{ {   0, 255,   0, 255 }, { -0.5f, 0.0f, -0.5f, 1.f } },
    		{ {   0,   0, 255, 255 }, {  0.0f, 0.5f,  0.0f, 1.f } },
    		{ { 255,   0,   0, 255 }, { -0.5f, 0.0f, -0.5f, 1.f } },	// left
    		{ {   0, 255,   0, 255 }, { -0.5f, 0.0f,  0.5f, 1.f } },
    		{ {   0,   0, 255, 255 }, {  0.0f, 0.5f,  0.0f, 1.f } },
    		{ { 255,   0,   0, 255 }, {  0.5f, 0.0f,  0.5f, 1.f } },	// base 1
    		{ {   0, 255,   0, 255 }, { -0.5f, 0.0f,  0.5f, 1.f } },
    		{ {   0,   0, 255, 255 }, { -0.5f, 0.0f, -0.5f, 1.f } },
    		{ { 255,   0,   0, 255 }, { -0.5f, 0.0f, -0.5f, 1.f } },	// base 2
    		{ {   0, 255,   0, 255 }, {  0.5f, 0.0f, -0.5f, 1.f } },
    		{ {   0,   0, 255, 255 }, {  0.5f, 0.0f,  0.5f, 1.f } }
    	};

    I have a pyramid built out of the vertices above. It basically ranges from [-0.5, 0.5]. When I setup an orthographic projection such and view matrix such as this:
    Code :
    glm::mat4 Projection = vmath::ortho( -1.f, 1.f, -1.f, 1.f, -2.f, 10.f );
    glm::mat4 View = vmath::translate( 0.f, 0.f, 0.f );

    I can see the pyramid fine, but this confuses me. The second to last parameter of ortho() is -2 for the near plane, which I assume means the near plane is at z = -2. I thought OpenGL uses a right-handed coordinate system where the +Z points out of the screen. So how can my far plane be at +10 and I still see the pyramid. What's also strange is that if I make the far plane -10, then I can see the pyramid, but it is very close up, almost as if I'm the bottom of the pyramid and looking up at the tip.

    I'm not sure about what to set the left, right, top and bottom planes to. I only chose [-1,1] just because I assumed that I would be able to see my pyramid within that range because it is within those boundaries.

    For perspective projection, I see nothing. I've tested with different values but sometimes I will get an upside down pyramid or some very strange shape that does not look like a pyramid.

    Maybe my projection matrices are incorrect?
    Code :
    glm::mat4 vmath::ortho( GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat znear, GLfloat zfar )
    {
    	GLfloat x1 =  2 / ( right - left );
    	GLfloat w1 = -( right + left ) / ( right - left );
     
    	GLfloat y2 =  2 / ( top - bottom );
    	GLfloat w2 = -( top + bottom ) / ( top - bottom );
     
    	GLfloat z3 = -2 / ( zfar - znear );
    	GLfloat w3 = -( zfar + znear ) / ( zfar - znear );
     
    	glm::mat4 orthogonalMatrix(
    		x1,  0.f, 0.f,  w1,
    		0.f,  y2, 0.f,  w2,
    		0.f, 0.f,  z3,  w3,
    		0.f, 0.f, 0.f, 1.f );
     
    	return orthogonalMatrix;
    }
     
     
    glm::mat4 vmath::perspective( GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat znear, GLfloat zfar )
    {
    	GLfloat x1 = ( 2 * znear ) / ( right - left );
    	GLfloat z1 = ( right + left ) / ( right - left );
     
    	GLfloat y2 = ( 2 * znear ) / ( top - bottom );
    	GLfloat z2 = ( top + bottom ) / ( top - bottom );
     
    	GLfloat z3 = -( zfar + znear ) / ( zfar - znear );
    	GLfloat w3 = -( 2 * zfar * znear ) / ( zfar - znear );
     
    	glm::mat4 perspectiveMatrix(
    		x1,  0.f,   z1, 0.f,
    		0.f,  y2,   z2, 0.f,
    		0.f, 0.f,   z3,  w3,
    		0.f, 0.f, -1.f, 0.f );
     
    	return perspectiveMatrix;
    }

    This is how I am multiplaying my matrices:

    Code :
    glm::mat4 ModelViewProjection = Projection * ( Model * View );

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Singapore
    Posts
    800
    Your order of matrix multiplication should be
    glm::mat4 ModelViewProjection = Projection * View * Model
    The multiplication goes right to left.
    See if this helps.
    Regards,
    Mobeen

Posting Permissions

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