# Light Normals ---

## Recommended Posts

Sup guys, I'm having some trouble with light normals and just don't 'get' how to set them in my custom vertex struct... I know their purpose and even the way you have to set them(perpendicular to the surface) but like I said I don't know how! Can anyone explain in detail how it's done and give some examples? Thanks(a load).

##### Share on other sites
StoneDevil    122
right now i only remember managed directx, its been a couple years since i did unmanaged dx. but in managed you create a group of vertices for example:

CustomeVertex.PositionNormalTexture[] m_vertes = new Customvertex.PositionNormalTexture[size];

inside the vertex stucture there is a vector3 for position and a vector 3 for the normal and also uv coords....

im not completely sure about unmanaged...if i can rmemeber you have to define your vertex structure and inside there you can define a normal vector.

im not sure if that helps....specify which version of directX and maybe you can get more answers

##### Share on other sites
Thanks for trying to help me dude, but unmanaged and managed seem to be a bit too different for me to understand or work by... Also Im using DX9.

##### Share on other sites
MJP    19754
Is it that you don't get how to actually set the normal values in your vertex buffer, or that you don't get how to compute the normal values for a given mesh?

##### Share on other sites
StoneDevil    122
from what i can remmeber its something like this....search online...i found this on msdn site...just added normals to the vertex structure.

struct CUSTOMVERTEX
{
float x,y,z;
float nx, ny, nz;
};

CUSTOMVERTEX Vertices[] =
{
{ 0.0, 0.0, 0.0, 0.0, 1.0, 0.0},
{-5.0, 5.0, 0.0, 0.0, 1.0, 0.0},
{-3.0, 7.0, 0.0, 0.0, 1.0, 0.0},
{ 0.0, 10.0, 0.0, 0.0, 1.0, 0.0},
{ 3.0, 7.0, 0.0, 0.0, 1.0, 0.0},
{ 5.0, 5.0, 0.0, 0.0, 1.0, 0.0},
};

d3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 4 );

##### Share on other sites
Thanks again for all your help guys, the problem is that I don't know how to set them.

StoneDevil: That can't be a right example :) thanks though!

##### Share on other sites
StoneDevil    122
why dont you just post the section of your code where you are trying to "set" the normals...and also where you are rendering your mesh.

##### Share on other sites
Buckeye    10747
If you just want a normal that's perpendicular to a face (3 vertices, a triangle):

D3DXVECTOR3 v0, v1, v2; // three vertices forming a face
D3DXVECTOR3 normal;

D3DXVECTOR3 vn0 = v1-v0;
D3DXVECTOR3 vn1 = v2-v0;

D3DXVec3Cross( &normal, &vn1, &vn0 );

If the normal is pointing in rather than out, then:

D3DXVec3Cross( &normal, &vn0, &vn1 );

If you form a mesh and want all the normals to be smoothed, look at:

D3DXComputeNormals();

##### Share on other sites
Thanks again! The thing is guys that I'm NOT owrking with a mesh here I'm still doing the basics because I keep getting confused with this normal thing.

Well the following is my custom vertex, flexible vertex format and the primitive respectively --->

[icode]
//The FVF --->
#define CustomFVF (D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_NORMAL)

//Custom Vertex --->
struct CustomV
{

FLOAT x, y, z;
FLOAT u, v;
D3DVECTOR vnormal;

};

//Primitive --->
CustomV Primitive[] =
{

{-3.0f, 3.0f, 1.0f, 0, 0, 0.0f, 1.0f, 0.0f, },
{3.0f, 3.0f, 1.0f, 1, 0, 0.0f, 1.0f, 0.0f, },
{-3.0f, -3.0f, 1.0f, 0, 1, 0.0f, 1.0f, 0.0f, },
{3.0f, -3.0f, 1.0f, 1, 1, 0.0f, 1.0f, 0.0f, },

};
[/icode]

This though was just a guess(the normals).

Thanks.

##### Share on other sites
exoity    208
Since you are drawing a standard quad, you are going to want the normal to be (0, 0, 1) meaning the normal would come towards you.

{-3.0f,  3.0f, 1.0f, 	0, 0, 	0.0f, 0.0f, 1.0f, },{ 3.0f,  3.0f, 1.0f, 	1, 0, 	0.0f, 0.0f, 1.0f, },{-3.0f, -3.0f, 1.0f, 	0, 1, 	0.0f, 0.0f, 1.0f, },{ 3.0f, -3.0f, 1.0f, 	1, 1, 	0.0f, 0.0f, 1.0f, },

Also, the takes to do code is [ \ code].

##### Share on other sites
Buckeye    10747
If you have the vertices you want, use the D3DXVec3Cross function described above to determine the normals.

Also, be careful with your vertex format. With your FVF, your vertex structure should be { x,y,z,normal,u,v }. Put the texcoords after the normal vector. The FVF definition does not determine the order of the variables. It only tells the device to expect position, normal and texcoord info.

##### Share on other sites
Hmm, thanks again guys! I think I now understand normals also the blue square problem is gone because of what buckeye said and my texture shows.

But the problem hasn't died sadly, the light still doesn't work... The following is my light function --->

VOID InitLights(){ D3DVECTOR LightDir = {0.0f, 0.0f, 0.0f}; ZeroMemory(&Light, sizeof(Light)); ZeroMemory(&LightM, sizeof(LightM)); Light.Type = D3DLIGHT_DIRECTIONAL; Light.Diffuse.r = 0.5f; Light.Diffuse.g = 0.5f; Light.Diffuse.b = 0.5f; Light.Diffuse.a = 1.0f; Light.Direction = LightDir; LightM.Diffuse.r = LightM.Ambient.r = 1.0f; LightM.Diffuse.g = LightM.Ambient.g = 1.0f; LightM.Diffuse.b = LightM.Ambient.b = 1.0f; LightM.Diffuse.a = LightM.Ambient.a = 1.0f; D3DDev->SetLight(0, &Light); D3DDev->LightEnable(0, true); D3DDev->SetMaterial(&LightM);}

Any issues that you guys can see with it?

Thanks.

##### Share on other sites
MJP    19754
Quote:
 Original post by pcbrainbuster D3DVECTOR LightDir = {0.0f, 0.0f, 0.0f};

That's not a valid direction vector. The pipeline may convert it into something usable (I'm not sure, I never use the fixed-function lighting stuff), but it's probably better if you set it to something usable from the start. In particular, if you want your triangles to be lit you should set the light direction to be approximately opposite of your vertex normals (such as <0, 0, -1>).

##### Share on other sites
jollyjeffers    1570
Your direction needs attention - I don't believe the FF pipe will subst in a meaningful value for you.

Also, I suspect your setting the material's ambient to <1,1,1,1> will effectively disable your lighting. Leave it as just setting the diffuse to <1,1,1,1>.

hth
Jack

##### Share on other sites
MJP: Yay it worked :) the problem was that well what you said :)

JollyJefers: Attenuation would be the problem if I wasn't using a directionallight ;) Directional lights go on forever...

Every other Humanoid: When you're setting the light well your setting the light right? But when you're setting the material you're setting how the light will reflect. But what I don't understand is why the ambient member is in both the light and material struct. I mean shouldn't it only be in the material one?

Thanks.

##### Share on other sites
MJP    19754
Quote:
 Original post by pcbrainbuster JollyJefers: Attenuation would be the problem if I wasn't using a directionallight ;) Directional lights go on forever...

He didn't say "attenuation", he was saying "attention". Basically he was pointing out the same thing that I was. [wink]

##### Share on other sites
Thanks MJP... For making me feel stupid lol. But seriously sorry JolyJefers for doin' that.

(questions still apply)

Thanks.

##### Share on other sites
I've just constructed a cube and am not sure if I completed the normals properly can you guys check please?

CustomV Primitive[] ={ //Front Side {-3.0f, 3.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0, 0, }, {3.0f, 3.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1, 0, }, {-3.0f, -3.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0, 1, }, {3.0f, -3.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1, 1, }, //Right Side {3.0f, 3.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0, 0, }, {3.0f, 3.0f, 6.0f, 1.0f, 0.0f, 0.0f, 1, 0, }, {3.0f, -3.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0, 1, }, {3.0f, -3.0f, 6.0f, 1.0f, 0.0f, 0.0f, 1, 1, }, //Left Side {-3.0f, 3.0f, 6.0f, -1.0f, 0.0f, 0.0f, 0, 0, }, {-3.0f, 3.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1, 0, }, {-3.0f, -3.0f, 6.0f, -1.0f, 0.0f, 0.0f, 0, 1, }, {-3.0f, -3.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1, 1, }, //Back Side {3.0f, 3.0f, 6.0f, 0.0f, 0.0f, -1.0f, 0, 0, }, {-3.0f, 3.0f, 6.0f, 0.0f, 0.0f, -1.0f, 1, 0, }, {3.0f, -3.0f, 6.0f, 0.0f, 0.0f, -1.0f, 0, 1, }, {-3.0f, -3.0f, 6.0f, 0.0f, 0.0f, -1.0f, 1, 1, }, //Top Side {3.0f, 3.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0, 0, }, {-3.0f, 3.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1, 0, }, {3.0f, 3.0f, 6.0f, 0.0f, 1.0f, 0.0f, 0, 1, }, {-3.0f, 3.0f, 6.0f, 0.0f, 1.0f, 0.0f, 1, 1, }, //Bottom Side {-3.0f, -3.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0, 0, }, {3.0f, -3.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1, 0, }, {-3.0f, -3.0f, 6.0f, 0.0f, -1.0f, 0.0f, 0, 1, }, {3.0f, -3.0f, 6.0f, 0.0f, -1.0f, 0.0f, 1, 1, },}

(questions still apply)

Thanks.

## Create an account

Register a new account