Sign in to follow this  
mrWodoo

Quad normals

Recommended Posts

Hello, I have little problem with normals. I don't know how to compute them :(

Here is my vertex-struct
[code]struct D3DVERTEX
{
float fX, fY, fZ;
DWORD dwColor;
D3DVECTOR Normal;
};[/code]

And here are my vertices(?)
[code]D3DVERTEX aQuad[] = {{1.0f,-1.0f,1.0f, 0xffffff00},
{3.0f,-1.0f,1.0f,0xffffff00},
{1.0f, 1.0f,1.0f,0xffffff00},
{3.0f, 1.0f,1.0f,0xffffff00},

{3.0f, -1.0f, 1.0f, 0xffffff00},
{3.0f, 1.0f, 1.0f, 0xffffff00},
{3.0f, -1.0f, 3.0f, 0xffffff00},
{3.0f, 1.0f, 3.0f, 0xffffff00},

{1.0f, -1.0f, 1.0f, 0xffffff00},
{1.0f, 1.0f, 1.0f, 0xffffff00},
{1.0f, -1.0f, 3.0f, 0xffffff00},
{1.0f, 1.0f, 3.0f, 0xffffff00},

{1.0f, -1.0f, 1.0f, 0xffffff00},
{3.0f, -1.0f, 1.0f, 0xffffff00},
{1.0f, -1.0f, 3.0f, 0xffffff00},
{3.0f, -1.0f, 3.0f, 0xffffff00},

{1.0f, 1.0f, 1.0f, 0xffffff00},
{3.0f, 1.0f, 1.0f, 0xffffff00},
{1.0f, 1.0f, 3.0f, 0xffffff00},
{3.0f, 1.0f, 3.0f, 0xffffff00},

{1.0f,-1.0f,3.0f,0xffffff00},
{3.0f,-1.0f,3.0f,0xffffff00},
{1.0f, 1.0f,3.0f,0xffffff00},
{3.0f, 1.0f,3.0f,0xffffff00},
};[/code]

Share this post


Link to post
Share on other sites
Just write the normals behind the position and the color.

The first four vertices in aQuad are the front of the cube and the normal has to stand vertical on the quad.
This means that the normal must be parallel with the z-axis (if the up-vector is the y-axis).
So the first four points would be:

[code]
{1.0f, -1.0f, 1.0f, 0xffffff00, D3DXVECTOR3(0, 0, -1) },
{3.0f, -1.0f, 1.0f, 0xffffff00, D3DXVECTOR3(0, 0, -1) },
{1.0f, 1.0f, 1.0f, 0xffffff00, D3DXVECTOR3(0, 0, -1) },
{3.0f, 1.0f, 1.0f, 0xffffff00, D3DXVECTOR3(0, 0, -1) },
[/code]
The next four are the right side of the quad, thus the normal would be D3DXVECTOR3(1, 0, 0);

I am not really good in explaining this, but I hope it helped.

Share this post


Link to post
Share on other sites
In case of a single quad, find two independent vectors that lie within the plane and compute their cross-product. If your quad is made up from the points A,B,C and D, you can use the two vectors (A-B) and (A-C) for example.

If your normal seems to come out wrong remember that you might want to normalize it (hence the name, I guess) and that the cross-product is non-commutative: cross(x,y) = -cross(y,x)

If you don't know the cross-product just check Wikipedia. Its implementation is pretty straight-forward.

- Michael

Share this post


Link to post
Share on other sites
[quote name='Michael N.' timestamp='1306677089' post='4817103']
...
If your normal seems to come out wrong remember that you might want to normalize it (hence the name, I guess) ...
[/quote]
The length of a vector computed by the cross-product is
| [b]a[/b] x [b]b[/b] | == | [b]a[/b] | * | [b]b[/b] | * sin( <[b]a[/b],[b]b[/b]> )
and hence direct proportional to the length of the both argument vectors and the sine of the angle between the both vectors. Because one often only needs the unscaled direction for e.g. lighting, setting a normal vector to its unit length is usual.

I'm not a mathematician, but I'd explain it so: The term "normal" here means a direction w.r.t. a reference, namely that it is orthogonal e.g. to a plane or curve. It falls into the same category as tangent, bi-tangent (for a plane) or bi-normal (for a curve) do. The term "normalization" means to apply a [url="http://en.wikipedia.org/wiki/Norm_(mathematics)"]norm[/url] to a vector (in this case). The norm we usually apply is the [url="http://en.wikipedia.org/wiki/Norm_(mathematics)#Euclidean_norm"]Euclidean Norm[/url].

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this