Archived

This topic is now archived and is closed to further replies.

Weston

Not again... Vertex normals and lights

Recommended Posts

I didn't expect lighting to be so frusterating. So far, my program has a quad that faces the camera. It has a texture on it and also has a transparent area. This program is just to get me familiar with lights. However, everything works until I tried to add in lights. I assumer it's because of my normals not being set up correctly. Take a look and see if I'm missing anything. (I'm not going to put in the entire source code, so don't worry about it) Program details: Camera position: (0, 0, -500) Camera target: (0, 0, 0) World orientation: (0, 1, 0) Light type: Point Quad is centered on the origin facing the camera and rotates on the Z axis. It has a texture on it. Everything works perfectly until I tried to add lighting. And this is some of my initialization function:
struct VERTEX3D
{
	D3DXVECTOR3 Position;
	D3DXVECTOR3 Normal;		
	FLOAT u, v;
};
//

// My verticies

VERTEX3D Verts[4];
Verts[0].Position = D3DXVECTOR3(-100.0f, -100.0f, 0.0f);
Verts[0].Normal = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
Verts[0].u = 0.0f;
Verts[0].v = 1.0f;
Verts[1].Position = D3DXVECTOR3(-100.0f, 100.0f, 0.0f);
Verts[1].Normal = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
Verts[1].u = 0.0f;
Verts[1].v = 0.0f;
Verts[2].Position = D3DXVECTOR3(100.0f, -100.0f, 0.0f);
Verts[2].Normal = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
Verts[2].u = 1.0f;
Verts[2].v = 1.0f;
Verts[3].Position = D3DXVECTOR3(100.0f, 100.0f, 0.0f);
Verts[3].Normal = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
Verts[3].u = 1.0f;
Verts[3].v = 0.0f;
//

// Light structure

D3DLIGHT8 d3dlight;
//

//////////////////////////////////////////////////////////

//

// Set render states

D3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
D3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
D3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
D3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
D3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
D3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
D3DDevice->SetRenderState(D3DRS_ALPHAREF, 0x01);
D3DDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
//

//////////////////////////////////////////////////////////

//

// Set up the light

d3dlight.Type = D3DLIGHT_POINT;
d3dlight.Position = D3DXVECTOR3(0.0f, 20.0f, -200.0f);
d3dlight.Diffuse.r = d3dlight.Ambient.r = 1.0f;
d3dlight.Diffuse.g = d3dlight.Ambient.g = 1.0f;
d3dlight.Diffuse.b = d3dlight.Ambient.b = 1.0f;
d3dlight.Diffuse.a = d3dlight.Ambient.a = 1.0f;
d3dlight.Attenuation0 = 1.0f;
d3dlight.Range = 1000.0f;

D3DDevice->SetLight(0, &d3dlight);
D3DDevice->LightEnable(0, TRUE);
I probably have my normals set wrong. I've tried different combinations, but nothing works so far. I couldn't find any good tutorials on calculating them either. So, do you see anything wrong, or should I just skip lighting until I need it? I want to get out of this chapter in the book soon. [edited by - Weston on July 25, 2003 3:35:24 PM] [edited by - Weston on July 25, 2003 6:07:02 PM]

Share this post


Link to post
Share on other sites
Well, your vertices all have the same position

EDIT- I just checked it again and they don't. I could've sworn they didn't. What FVF flags are you using?


[edited by - FlamePixel on July 25, 2003 5:21:49 PM]

Share this post


Link to post
Share on other sites
All the normals are pointing straight up the y axis, therefore no light is "reflecting" towards the camera. The normals should be (0,0,-1), this way the light is "reflecting" towards you.

I should probably be working now...

Share this post


Link to post
Share on other sites
I have the D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 flags, so it should be just fine.

I also changed the vectors to (0.0f, 0.0f, -1.0f) but still no image shows up.

Do I have to specify anything in the texture to allow lighting, or should that jsut be automatic?

Thanks for your suggestions.

Share this post


Link to post
Share on other sites
Do you have a material set up? Ambient light is indiffernt to normals etc - try setting that to someting obvious and work until you at least see that. Then you know your renderstates/materials/matrices are correct at least!

Share this post


Link to post
Share on other sites
No book said I have to set up a material before setting a texture. It works now. Thanks to all that replied!

[edited by - Weston on July 26, 2003 1:49:29 AM]

Share this post


Link to post
Share on other sites