Drawing a wireframe representation of a frustum - Java-Games
This is a discussion on Drawing a wireframe representation of a frustum - Java-Games ; Hi,
I'm building a tool in DirectX for editing my 3D Scenes. I want to add a
feature that draws the viewing cone (frustum) of a virtual camera in my
scenes, just like in 3DsMax where you can select 'Show ...
-
Drawing a wireframe representation of a frustum
Hi,
I'm building a tool in DirectX for editing my 3D Scenes. I want to add a
feature that draws the viewing cone (frustum) of a virtual camera in my
scenes, just like in 3DsMax where you can select 'Show Cone' under the
camera settings.
Thing is, I have no clue how to exstract the 8 vertex positions that form
the frustum. Suppose I have the View Matrix and the Perspective Matrix, how
do I extract the 8 corners in vertex coordinates?
Peter
-
Re: Drawing a wireframe representation of a frustum
> I'm building a tool in DirectX for editing my 3D Scenes. I want to add a
> feature that draws the viewing cone (frustum) of a virtual camera in my
> scenes, just like in 3DsMax where you can select 'Show Cone' under the
> camera settings.
hi,
with some simple algebraic steps you get your aim:
every matrix is a 4x4 matrix!
P .. current projection matrix
M .. current modelview matrix - accumulate model and view matrix
C .. current clipping matrix
C = P*M
extract clipping planes from C
left: n=(c_41+c_11, c_34+c_12, c_43+c_13), d=(-c_44-c_14)
right: n=(c_41-c_11, c_34-c_12, c_43-c_13), d=(-c_44+c_14)
top: n=(c_41-c_21, c_34-c_22, c_43-c_23), d=(-c_44+c_24)
bottom: n=(c_41+c_21, c_34+c_22, c_43+c_23), d=(-c_44-c_24)
near: n=(c_41+c_31, c_34+c_32, c_43+c_33), d=(-c_44-c_34)
far: n=(c_41-c_31, c_34-c_32, c_43-c_33), d=(-c_44+c_34)
compute the 8 intersection points per 3 intersecting clipping planes!
see: http://mathworld.wolfram.com/Plane-P...ersection.html
i hope this little code fragment could help you:
it's based on OpenGL, so i think you have to swtich between the different
matrix definitions of OpenGL and Direct3D (exchange array indices):
OpenGL 4x4 Matrix: - column vectors first
[ m[0] m[4] m[ 8] m[12] ]
[ m[1] m[5] m[ 9] m[13] ]
[ m[2] m[6] m[10] m[14] ]
[ m[3] m[7] m[11] m[15] ]
i think in Direct3D it is the other way round - row vectors first:
[ m[ 0] m[ 1] m[ 2] m[ 3] ]
[ m[ 4] m[ 5] m[ 6] m[ 7] ]
[ m[ 8] m[ 9] m[10] m[11] ]
[ m[12] m[13] m[14] m[15] ]
float p[16]; // current projection matrix
float m[16]; // current modelview matrix, rem. OpenGL does not distinct
between model matrix and viewing matrix, hence modelview matrix!
float c[16]; // temporary matrix
c[ 0] = m[ 0]*p[ 0] + m[ 1]*p[ 4] + m[ 2]*p[ 8] + m[ 3]*p[12];
c[ 1] = m[ 0]*p[ 1] + m[ 1]*p[ 5] + m[ 2]*p[ 9] + m[ 3]*p[13];
c[ 2] = m[ 0]*p[ 2] + m[ 1]*p[ 6] + m[ 2]*p[10] + m[ 3]*p[14];
c[ 3] = m[ 0]*p[ 3] + m[ 1]*p[ 7] + m[ 2]*p[11] + m[ 3]*p[15];
c[ 4] = m[ 4]*p[ 0] + m[ 5]*p[ 4] + m[ 6]*p[ 8] + m[ 7]*p[12];
c[ 5] = m[ 4]*p[ 1] + m[ 5]*p[ 5] + m[ 6]*p[ 9] + m[ 7]*p[13];
c[ 6] = m[ 4]*p[ 2] + m[ 5]*p[ 6] + m[ 6]*p[10] + m[ 7]*p[14];
c[ 7] = m[ 4]*p[ 3] + m[ 5]*p[ 7] + m[ 6]*p[11] + m[ 7]*p[15];
c[ 8] = m[ 8]*p[ 0] + m[ 9]*p[ 4] + m[10]*p[ 8] + m[11]*p[12];
c[ 9] = m[ 8]*p[ 1] + m[ 9]*p[ 5] + m[10]*p[ 9] + m[11]*p[13];
c[10] = m[ 8]*p[ 2] + m[ 9]*p[ 6] + m[10]*p[10] + m[11]*p[14];
c[11] = m[ 8]*p[ 3] + m[ 9]*p[ 7] + m[10]*p[11] + m[11]*p[15];
c[12] = m[12]*p[ 0] + m[13]*p[ 4] + m[14]*p[ 8] + m[15]*p[12];
c[13] = m[12]*p[ 1] + m[13]*p[ 5] + m[14]*p[ 9] + m[15]*p[13];
c[14] = m[12]*p[ 2] + m[13]*p[ 6] + m[14]*p[10] + m[15]*p[14];
c[15] = m[12]*p[ 3] + m[13]*p[ 7] + m[14]*p[11] + m[15]*p[15];
// now you can extract the planes of the viewing frustum
// plane: normal (x,y,z); distance (d)
// left: (c[ 3]+c[ 0], c[ 7]+c[ 4], c[11]+c[ 8]); (-c[15]-c[12])
// right: (c[ 3]-c[ 0], c[ 7]-c[ 4], c[11]-c[ 8]); (-c[15]+c[12])
// top: (c[ 3]-c[ 1], c[ 7]-c[ 5], c[11]-c[ 9]); (-c[15]+c[13])
// bottom: (c[ 3]+c[ 1], c[ 7]+c[ 5], c[11]+c[ 9]); (-c[15]-c[13])
// near: (c[ 3]+c[ 2], c[ 7]+c[ 6], c[11]+c[10]); (-c[15]-c[14])
// far: (c[ 3]-c[ 2], c[ 7]-c[ 6], c[11]-c[10]); (-c[15]+c[14])
// compute intersection points of every 3 planes!
// see: http://mathworld.wolfram.com/Plane-P...ersection.html
regards
godfired
-
Re: Drawing a wireframe representation of a frustum
Thanks a lot for your help. I will try it out.
Peter
"Gottfried Eibner" <_gottfried._eibner@_univie._ac._at> schreef in bericht
news:410f72e9$0$11742$3b214f66@usenet.univie.ac.at...
> > I'm building a tool in DirectX for editing my 3D Scenes. I want to add a
> > feature that draws the viewing cone (frustum) of a virtual camera in my
> > scenes, just like in 3DsMax where you can select 'Show Cone' under the
> > camera settings.
>
> hi,
> with some simple algebraic steps you get your aim:
> every matrix is a 4x4 matrix!
>
> P .. current projection matrix
> M .. current modelview matrix - accumulate model and view matrix
> C .. current clipping matrix
>
> C = P*M
>
> extract clipping planes from C
>
> left: n=(c_41+c_11, c_34+c_12, c_43+c_13), d=(-c_44-c_14)
> right: n=(c_41-c_11, c_34-c_12, c_43-c_13), d=(-c_44+c_14)
> top: n=(c_41-c_21, c_34-c_22, c_43-c_23), d=(-c_44+c_24)
> bottom: n=(c_41+c_21, c_34+c_22, c_43+c_23), d=(-c_44-c_24)
> near: n=(c_41+c_31, c_34+c_32, c_43+c_33), d=(-c_44-c_34)
> far: n=(c_41-c_31, c_34-c_32, c_43-c_33), d=(-c_44+c_34)
>
> compute the 8 intersection points per 3 intersecting clipping planes!
> see: http://mathworld.wolfram.com/Plane-P...ersection.html
>
>
> i hope this little code fragment could help you:
> it's based on OpenGL, so i think you have to swtich between the different
> matrix definitions of OpenGL and Direct3D (exchange array indices):
>
> OpenGL 4x4 Matrix: - column vectors first
> [ m[0] m[4] m[ 8] m[12] ]
> [ m[1] m[5] m[ 9] m[13] ]
> [ m[2] m[6] m[10] m[14] ]
> [ m[3] m[7] m[11] m[15] ]
>
> i think in Direct3D it is the other way round - row vectors first:
> [ m[ 0] m[ 1] m[ 2] m[ 3] ]
> [ m[ 4] m[ 5] m[ 6] m[ 7] ]
> [ m[ 8] m[ 9] m[10] m[11] ]
> [ m[12] m[13] m[14] m[15] ]
>
> float p[16]; // current projection matrix
> float m[16]; // current modelview matrix, rem. OpenGL does not
distinct
> between model matrix and viewing matrix, hence modelview matrix!
> float c[16]; // temporary matrix
>
> c[ 0] = m[ 0]*p[ 0] + m[ 1]*p[ 4] + m[ 2]*p[ 8] + m[ 3]*p[12];
> c[ 1] = m[ 0]*p[ 1] + m[ 1]*p[ 5] + m[ 2]*p[ 9] + m[ 3]*p[13];
> c[ 2] = m[ 0]*p[ 2] + m[ 1]*p[ 6] + m[ 2]*p[10] + m[ 3]*p[14];
> c[ 3] = m[ 0]*p[ 3] + m[ 1]*p[ 7] + m[ 2]*p[11] + m[ 3]*p[15];
>
> c[ 4] = m[ 4]*p[ 0] + m[ 5]*p[ 4] + m[ 6]*p[ 8] + m[ 7]*p[12];
> c[ 5] = m[ 4]*p[ 1] + m[ 5]*p[ 5] + m[ 6]*p[ 9] + m[ 7]*p[13];
> c[ 6] = m[ 4]*p[ 2] + m[ 5]*p[ 6] + m[ 6]*p[10] + m[ 7]*p[14];
> c[ 7] = m[ 4]*p[ 3] + m[ 5]*p[ 7] + m[ 6]*p[11] + m[ 7]*p[15];
>
> c[ 8] = m[ 8]*p[ 0] + m[ 9]*p[ 4] + m[10]*p[ 8] + m[11]*p[12];
> c[ 9] = m[ 8]*p[ 1] + m[ 9]*p[ 5] + m[10]*p[ 9] + m[11]*p[13];
> c[10] = m[ 8]*p[ 2] + m[ 9]*p[ 6] + m[10]*p[10] + m[11]*p[14];
> c[11] = m[ 8]*p[ 3] + m[ 9]*p[ 7] + m[10]*p[11] + m[11]*p[15];
>
> c[12] = m[12]*p[ 0] + m[13]*p[ 4] + m[14]*p[ 8] + m[15]*p[12];
> c[13] = m[12]*p[ 1] + m[13]*p[ 5] + m[14]*p[ 9] + m[15]*p[13];
> c[14] = m[12]*p[ 2] + m[13]*p[ 6] + m[14]*p[10] + m[15]*p[14];
> c[15] = m[12]*p[ 3] + m[13]*p[ 7] + m[14]*p[11] + m[15]*p[15];
>
> // now you can extract the planes of the viewing frustum
> // plane: normal (x,y,z); distance (d)
>
> // left: (c[ 3]+c[ 0], c[ 7]+c[ 4], c[11]+c[ 8]); (-c[15]-c[12])
> // right: (c[ 3]-c[ 0], c[ 7]-c[ 4], c[11]-c[ 8]); (-c[15]+c[12])
> // top: (c[ 3]-c[ 1], c[ 7]-c[ 5], c[11]-c[ 9]); (-c[15]+c[13])
> // bottom: (c[ 3]+c[ 1], c[ 7]+c[ 5], c[11]+c[ 9]); (-c[15]-c[13])
> // near: (c[ 3]+c[ 2], c[ 7]+c[ 6], c[11]+c[10]); (-c[15]-c[14])
> // far: (c[ 3]-c[ 2], c[ 7]-c[ 6], c[11]-c[10]); (-c[15]+c[14])
>
> // compute intersection points of every 3 planes!
> // see: http://mathworld.wolfram.com/Plane-P...ersection.html
> regards
> godfired
Similar Threads
-
By Application Development in forum Graphics
Replies: 1
Last Post: 02-10-2007, 05:08 AM
-
By Application Development in forum Graphics
Replies: 3
Last Post: 12-26-2006, 03:48 PM
-
By Application Development in forum Graphics
Replies: 3
Last Post: 06-20-2006, 09:36 PM
-
By Application Development in forum Java-Games
Replies: 1
Last Post: 12-26-2004, 09:03 PM
-
By Application Development in forum Graphics
Replies: 0
Last Post: 07-02-2004, 12:37 PM