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

Thread: Draw frustrum in opengl

  1. #1
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    207

    How to draw frustrum in opengl

    I want to draw frustrum outlines. What will be my coordinates for these frustrum vertices ?
    Last edited by debonair; 06-06-2014 at 10:21 AM.

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    749
    They are trivial in NDC (normalized device coordinates): there the frustum becomes a cube with coordinates (-1, -1, -1) (1, 1, 1). By applying e.g. the inverse projection transformation you can bring these points back to eye space.
    However, you are at best going to see lines along the edge of your screen, because that is where by definition these points project. Perhaps you want something different?

  3. #3
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    207
    I want light frustrum. here is my code of geometry shader:

    Code :
     vec4 vertices[]={
     
    {-1, -1, -1, 1}, { 1, -1, -1, 1}, { 1,  1, -1, 1},  {-1,  1, -1, 1},
     
    {-1, -1, 1, 1},	{ 1, -1, 1, 1},	{ 1,  1, 1, 1},  {-1,  1, 1, 1}
    };
     
    	mat4 inverse1;
    	inverse1=inverse(LightWorldViewProjection);
     
    	vec4 trans[8];
    	int i;
    	for(i=0;i<8;i++)
    	{
    		trans[i]=vertices[i]*inverse1;
    		trans[i]=trans[i]/trans[i].w;
    	}
    	gl_Position=trans[0];
    	data.color=vec3(1.0,0.0,0.0);
    	EmitVertex();
     
    	gl_Position=trans[1];
    	data.color=vec3(1.0,0.0,0.0);
    	EmitVertex();
    	gl_Position=trans[2];
    	data.color=vec3(1.0,0.0,0.0);
    	EmitVertex();
    	gl_Position=trans[3];
    	data.color=vec3(1.0,0.0,0.0);
    	EmitVertex();
    	gl_Position=trans[4];
    	data.color=vec3(1.0,0.0,0.0);
    	EmitVertex();
    	gl_Position=trans[5];
    	data.color=vec3(1.0,0.0,0.0);
    	EmitVertex();
    	gl_Position=trans[6];
    	data.color=vec3(1.0,0.0,0.0);
    	EmitVertex();
    	gl_Position=trans[7];
    	data.color=vec3(1.0,0.0,0.0);
    	EmitVertex();
    EndPrimitive();

    It does not draw anything.
    Last edited by debonair; 06-06-2014 at 01:17 PM.

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    749
    Hmm, shouldn't the vertices (after transforming from light source NDC to world coords) undergo the normal modelview and projection of your camera?
    Also, is all your code set up to adhere to the vertex * matrix convention, because IIRC that influences the order in which the various transformations apply.

  5. #5
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    207
    Thanks a lot. I forgot to apply camera transformation.
    It works now
    One doubt, after applying projection matrix, vertices will be in clip coordinate space and not in NDC right? but we are taking NDC as input. Also can you please explain why need to
    Code :
     trans[i]=trans[i]/trans[i].w;
    Last edited by debonair; 06-06-2014 at 03:04 PM.

  6. #6
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    749
    after applying projection matrix, vertices will be in clip coordinate space and not in NDC
    Yes, you are correct, I've gotten a bit sloppy with the coordinate spaces
    The difference between clip space and NDC is the perspective division, which you also have to account for when transforming back, hence the division by .w

  7. #7
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    207
    I am still confused with how it works. Initially we are taking normalized coordinates and multiplying it with inverse projection matrix to get coordinates into view space, but in the reverse step ( converting view space to NDC) we multiply it with projection matrix and then divide by 'w' which is not the same while going NDC to view space.

    Can you please explain how it works?

    Thanks.

Posting Permissions

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