• Create Account

## Need to obtain frustum corners coordinates

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Naked Shooter  Members

152
Like
0Likes
Like

Posted 21 October 2012 - 03:39 PM

Hi. I'm trying to get the frustum vertices by using the perspective projection matrix's inverse. I'm trying to take a cube with the vertices { (-1,-1,-1), (1,-1,-1) ... } and multiply each vertex by the inverse of the perspective matrix, hoping to get the frustum. This isn't working, however. I am aware that there's a lighthouse3d tutorial, and I just might bite the bullet and go through it, but I'm just curious why this isn't working.

glm::mat4 projection(0);
glm::mat4 projectionInv(0);
//the frustum in normalized coordinates
vector<glm::vec4> NDCCube;
NDCCube.push_back(glm::vec4(-1.0f, -1.0f, -1.0f, 1.0f));
NDCCube.push_back(glm::vec4(1.0f, -1.0f, -1.0f, 1.0f));
NDCCube.push_back(glm::vec4(1.0f, -1.0f, 1.0f, -0.0f));
NDCCube.push_back(glm::vec4(-1.0f, -1.0f, 1.0f, -0.0f));
NDCCube.push_back(glm::vec4(-1.0f, 1.0f, -1.0f, 1.0f));
NDCCube.push_back(glm::vec4(1.0f, 1.0f, -1.0f, 1.0f));
NDCCube.push_back(glm::vec4(1.0f, 1.0f, 1.0f, -0.0f));
NDCCube.push_back(glm::vec4(-1.0f, 1.0f, 1.0f, -0.0f));
/* Get the current PROJECTION from OpenGL */
glGetFloatv(GL_PROJECTION_MATRIX, (GLfloat*)(glm::value_ptr(projection)));
//calculate the inverse of projection matrix. Hopefully, this'll get us the frustum when multiplied
//by coords of a cube.
projectionInv = glm::inverse(projection);

//push back vertices
for(int i = 0; i < 8; i++)
{
glm::vec4 tempvec;
tempvec = projectionInv * NDCCube.at(i); //multiply by projection matrix inverse to obtain frustum vertex
frustumVertices.push_back(Vector3(tempvec.x, tempvec.y, tempvec.z));
}


Edited by Naked Shooter, 21 October 2012 - 04:16 PM.

### #2Ohforf sake  Members

2048
Like
0Likes
Like

Posted 22 October 2012 - 12:21 AM

The projection matrix uses a projective space with homogeneous coordinates. This means that you have a 4th component. To get a vector from your normal 3 dimensional space to this projective space you attach a 4th component and set it to 1. In your case it is 0 for some vectors which is incorrect.
After the transformation with the inverse projection matrix the homogeneous vector must be transformed back from projective space to your normal 3 dimensional space:
tempVec /= tempVec.w

Also note that OpenGL returns the matrix column major IIRC and glm _might_ store it row major (I have never used glm). In this case you would have to transpose the matrix.

### #3larspensjo  Members

1561
Like
0Likes
Like

Posted 22 October 2012 - 01:05 AM

Also note that OpenGL returns the matrix column major IIRC and glm _might_ store it row major (I have never used glm). In this case you would have to transpose the matrix.

I can confirm that glm stores it in the same order as OpenGL (column major).
Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

### #4Naked Shooter  Members

152
Like
0Likes
Like

Posted 22 October 2012 - 04:46 AM

Yes, that works! I knew it had something to do with w.. Thank you Ohforf!!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.