Sign in to follow this  
MourningDew

Normals And Lighting

Recommended Posts

MourningDew    130
Hey there guys. Is there anybody who could give me a quick overview of how to determine the normal of a vertex so that the lighting in DirectX treats it properly. Say I have a cube that is defined as: g_Building[0].position = D3DXVECTOR3(-1.0f, 1.0f,-1.0f); g_Building[1].position = D3DXVECTOR3( 1.0f, 1.0f,-1.0f); g_Building[2].position = D3DXVECTOR3(-1.0f,-1.0f,-1.0f); g_Building[3].position = D3DXVECTOR3( 1.0f,-1.0f,-1.0f); g_Building[4].position = D3DXVECTOR3(-1.0f, 1.0f, 1.0f); g_Building[5].position = D3DXVECTOR3(-1.0f,-1.0f, 1.0f); g_Building[6].position = D3DXVECTOR3( 1.0f, 1.0f, 1.0f); g_Building[7].position = D3DXVECTOR3( 1.0f,-1.0f, 1.0f); g_Building[8].position = D3DXVECTOR3(-1.0f, 1.0f, 1.0f); g_Building[9].position = D3DXVECTOR3( 1.0f, 1.0f, 1.0f); g_Building[10].position = D3DXVECTOR3(-1.0f, 1.0f,-1.0f); g_Building[11].position = D3DXVECTOR3( 1.0f, 1.0f,-1.0f); g_Building[12].position = D3DXVECTOR3(-1.0f,-1.0f, 1.0f); g_Building[13].position = D3DXVECTOR3(-1.0f,-1.0f,-1.0f); g_Building[14].position = D3DXVECTOR3( 1.0f,-1.0f, 1.0f); g_Building[15].position = D3DXVECTOR3( 1.0f,-1.0f,-1.0f); g_Building[16].position = D3DXVECTOR3( 1.0f, 1.0f,-1.0f); g_Building[17].position = D3DXVECTOR3( 1.0f, 1.0f, 1.0f); g_Building[18].position = D3DXVECTOR3( 1.0f,-1.0f,-1.0f); g_Building[19].position = D3DXVECTOR3( 1.0f,-1.0f, 1.0f); g_Building[20].position = D3DXVECTOR3(-1.0f, 1.0f,-1.0f); g_Building[21].position = D3DXVECTOR3(-1.0f,-1.0f,-1.0f); g_Building[22].position = D3DXVECTOR3(-1.0f, 1.0f, 1.0f); g_Building[23].position = D3DXVECTOR3(-1.0f,-1.0f, 1.0f); Where those are the vertecies of the cube (It's using a vertex list, not an index thing) How do I make the light work properly on that? Thanks very muchness...

Share this post


Link to post
Share on other sites
haegarr    7372
Verices by itself have no normals, since they are 1D entities in a 3D world. What one does is to determine the normals of the adjacent faces, to do some computation on it (see below) and to declare the results as the normals of the vertices.

The simplest thing is to assign the normal of the face _as is_ to the vertices of that face. That in fact means that the normal is the same at each place in space covered by the face. Since neighbouring faces in a cube will have their own, distinct normals, the normals change suddenly from face to face, and so the edges and corners appear sharp.

Another basic way is to average the normals of all adjacent faces before assigning them to the respective vertices. In such a case the normals will vary from vertex to vertex. The gfx API is able to interpolate these normals when rendering the face, so that the edges and corners of your cube look something smooth (as long as not looking from the side!).

For a single face you can compute the normal "by hand" or let the API doing it for you. (I think also D3D can compute normals on the fly? Don't know, since I'm an OpenGL user.)

If computing "by hand" one usually computes the cross product of the vectors given by the edges of the face. E.g. for a triangular face with the points A, B, and C:
v := (B-A) x (C-A) ; n := v / |v|
where v is the vector from the cross product, and n (your normal) the normalized v.
For a quadrangular face w/ the points A, B, C, and D (your case), one should use diagonal vectors for this purpose:
v := (C-A) x (D-B) ; n := v / |v|

In all cases (say also if you want your API doing the computations) you have to guarantee the correct order in which vertices are supplied, or else the normals will point into the opposite direction they should. Either supply them all in CW or CCW.

Share this post


Link to post
Share on other sites
haegarr    7372
Yes, I mean point C minus point A, yielding in the difference vector. In geometric math it is often written as the line AC. (Line A to C is computed as point C minus A, not vice-versa.)

I wrote it w.r.t. a vector algebra that distinguishes explicitely between points and (direction) vectors. I've often answered problems that have arised from not distingushing between these kinds of vectors, since API's can be misunderstood that there is no such difference.

You probably will enter this problem if coming to transformations, where you will notice that there is a difference of transforming points or direction vectors, although the transformation matrices (if used) are the same.

If already interested in more about that, post it here. I'll look at it.

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