Results 1 to 7 of 7

Thread: Draw frustrum in opengl

  1. #1
    Senior Member Regular Contributor
    Join Date
    Apr 2012
    Posts
    318

    How to draw frustrum in opengl

    I want to draw frustrum outlines. What will be my coordinates for these frustrum vertices ?

  2. #2
    Newbie Frequent Contributor
    Join Date
    Mar 2016
    Posts
    896
    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
    Senior Member Regular Contributor
    Join Date
    Apr 2012
    Posts
    318
    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.

  4. #4
    Newbie Frequent Contributor
    Join Date
    Mar 2016
    Posts
    896
    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
    Senior Member Regular Contributor
    Join Date
    Apr 2012
    Posts
    318
    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;

  6. #6
    Newbie Frequent Contributor
    Join Date
    Mar 2016
    Posts
    896
    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
    Senior Member Regular Contributor
    Join Date
    Apr 2012
    Posts
    318
    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.

Similar Threads

  1. Frustrum culling
    By bobtedbob in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-20-2013, 11:32 AM
  2. Which are the frustrum's dimensions?
    By exoide in forum OpenGL: General
    Replies: 2
    Last Post: 10-22-2009, 01:04 PM
  3. How to draw a frustrum from a given position
    By kreyszig in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 09-30-2008, 01:03 PM
  4. frustrum culling
    By Masm in forum OpenGL: Advanced Coding
    Replies: 10
    Last Post: 02-27-2002, 12:07 AM
  5. How to set a frustrum?
    By in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 07-26-2000, 11:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean