Plane equation
Has anyone got a good function to draw a plane given its equation
ax+by+cz+d = 0
right now Im looping across
x and y then calculating z to draw lines, but this draws the plane all skewed and stuff.
ax+by+cz+d = 0
right now Im looping across
x and y then calculating z to draw lines, but this draws the plane all skewed and stuff.
Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
(a, b, c) is the normal, and d is the distance from the origin along that norrmal. That should be plenty to figure out where to put a quad...
It is I just dont know how,
Id have to find two perpendicular vectors to that normal and that means they could be at any orientation as long as they lie on the plane.
Id have to find two perpendicular vectors to that normal and that means they could be at any orientation as long as they lie on the plane.
Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
so, pick a vector v that's not (nearly) parallel or antiparallel to the norml.
Your two vectors are p = normal x v and q = normal x p.
Your two vectors are p = normal x v and q = normal x p.
what does not (nearly) parallel mean?
not (nearly parallel)
or
(not nearly) parallel
not (nearly parallel)
or
(not nearly) parallel
Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
not parallel, or nearly parallel, or antiparallel, or nearly antiparallel.
basically, try a fixed vector eg. (1, 0, 0), if the dot product is too close to 1 or 1, use a different fixed vector eg. (0, 1, 0) instead.
basically, try a fixed vector eg. (1, 0, 0), if the dot product is too close to 1 or 1, use a different fixed vector eg. (0, 1, 0) instead.
Take 2 points on the plane (try with x and y as 0, and y and z as 0, for example), and take the difference from them. There you have 1 vector. Take the cross product between that vector and the normal. There you have 2 vectors.
My approach is similar to akb825. I'd take the cross of the normal with the xaxis to get the plane's y, and then the cross of those two to get the plane's x.
Then, if you want to draw the plane as a grid, I'd create a matrix using those three axes, glLoadMatrix it, and then draw the plane. Easy enough.
Code:
vec3 x, y;
y.cross( vec3( 1,0,0 ), planeNormal );
y.normalize();
x.cross( y, planeNormal );
x.normalize();
Then, if you want to draw the plane as a grid, I'd create a matrix using those three axes, glLoadMatrix it, and then draw the plane. Easy enough.
know its a 4x4 matrix, but where does x, y and z from the calculation go?
Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Finding the up and right vectors based on a normal. First start with the right vector as it will always be on the XY plane. I.E. z component is 0 (I'm talking cartesian space.)
The normal on the XY plane is just the normal above with the nz set to zero.
Now just find the perpendicular to the NOP to find the right vector.
Now find the up vector on the plane.
Voila you should now have two unit vectors at right angles to themselves in the plane's orientation. Given a point on the plane all you need to do is scale the up and right vectors to get points on the plane.
Note 1: You'll need to specially handle the right vector calculation when the normal is [0, 0, 1] because NOP will be [0,0,0] which has no perpendicular.
Note 2: You'll have to be careful on the right vector calculation. Above is one way but it may be the wrong way. I.E. x = y and y = x instead.
Note 3: You'll have ot careful on the cross product. A cross B gives a different vector than B cross A. Basically it points the other direction.
Note 4: You may need to adjust the normal so that it points in a predictable direction if you find yourself getting flustered. I.E. Always pointing up if mainly upward or east if easterly or south if northerly.
Code:
// Normal, up, and right vectors
Point n, u, r;
nx = plane.a;
ny = plane.b;
nz = plane.c;
//abc not necessarily unit in a sloppy system.
Normalize(&n);
The normal on the XY plane is just the normal above with the nz set to zero.
Code:
//Normal on plane:
Point nop;
nopx = nx;
nopy = ny;
nopz = 0;
// make it unit length.
Normalize(&nop);
Now just find the perpendicular to the NOP to find the right vector.
Code:
r.x = nop.y;
r.y = nop.x;
r.z = 0;
Now find the up vector on the plane.
Code:
CrossProduct(&n, &r, &u);
Voila you should now have two unit vectors at right angles to themselves in the plane's orientation. Given a point on the plane all you need to do is scale the up and right vectors to get points on the plane.
Note 1: You'll need to specially handle the right vector calculation when the normal is [0, 0, 1] because NOP will be [0,0,0] which has no perpendicular.
Note 2: You'll have to be careful on the right vector calculation. Above is one way but it may be the wrong way. I.E. x = y and y = x instead.
Note 3: You'll have ot careful on the cross product. A cross B gives a different vector than B cross A. Basically it points the other direction.
Note 4: You may need to adjust the normal so that it points in a predictable direction if you find yourself getting flustered. I.E. Always pointing up if mainly upward or east if easterly or south if northerly.
You got to be careful here when plane normal is in the same direction as (1,0,0) but that's just nitpicking.
TomorrowPlusX Wrote:My approach is similar to akb825. I'd take the cross of the normal with the xaxis to get the plane's y, and then the cross of those two to get the plane's x.
Code:
vec3 x, y;
y.cross( vec3( 1,0,0 ), planeNormal );
y.normalize();
x.cross( y, planeNormal );
x.normalize();
Then, if you want to draw the plane as a grid, I'd create a matrix using those three axes, glLoadMatrix it, and then draw the plane. Easy enough.
unknown Wrote:know its a 4x4 matrix, but where does x, y and z from the calculation go?
Code:
(rx, ry, rz, 0
ux, uy, uz, 0
nx, ny, nx, 0
0, 0, 0, 1)
Ok thanks a lot.
TommorowPlusX: Where do you get the vec3 stuff or did you write it?
TommorowPlusX: Where do you get the vec3 stuff or did you write it?
Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Just for clarification, Zekaric's matrix is in column major form, which is how OpenGL processes it. If you want it in row major form (how you usually write it), just take the transpose. Also note that you will also need to translate for the d after that.
unknown Wrote:Ok thanks a lot.
TommorowPlusX: Where do you get the vec3 stuff or did you write it?
It's adapted from sample code from this guy: http://www.humus.ca/index.php?page=3D
I added a lot, such as a plane class ( relevant here ), AABB class, and integer variants, and various other tidbits like helper functions.
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
Rendering a reflection about an arbitrary plane  TomorrowPlusX  3  6,018 
Jan 14, 2008 07:51 AM Last Post: TomorrowPlusX 

pls help ....am in desperate need of code (illuminating equation)  shru_ani  2  3,265 
Oct 30, 2006 04:32 PM Last Post: Najdorf 

Math: mirroring a view frustum about a plane  TomorrowPlusX  1  4,114 
Aug 19, 2006 03:30 PM Last Post: Jones 

Drawing an Infinite Plane  unknown  3  4,021 
May 5, 2006 12:54 PM Last Post: Zekaric 

Math question: deriving rotation by plane intersection  TomorrowPlusX  6  5,639 
Jan 24, 2006 07:28 AM Last Post: TomorrowPlusX 