PDA

View Full Version : Draw frustrum in opengl

debonair
06-05-2014, 10:39 PM
I want to draw frustrum outlines. What will be my coordinates for these frustrum vertices ?

carsten neumann
06-06-2014, 11:42 AM
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?

debonair
06-06-2014, 02:09 PM
I want light frustrum. here is my code of geometry shader:

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.

carsten neumann
06-06-2014, 03:42 PM
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.

debonair
06-06-2014, 03:52 PM
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
trans[i]=trans[i]/trans[i].w;

carsten neumann
06-07-2014, 02:11 AM
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

debonair
06-08-2014, 07:36 PM
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.