Skip to main content
GameDev.net gamedev.net
🔒 Locked

Trouble with diffuse lighting

Started by doug25 Sep 2, 2011 at 5:54 PM 9 replies 1.6k views
Original Post
doug25
doug25
Hello,

I am confused as to why I can't get diffuse lighting to work.

It looks as if I've set all the right render states and enabled lighting correctly but I just can't spot the problem.

I enable lighting and call DrawSubset

I've set the material to use for the model:

D3DXMATERIAL mtrl;
ZeroMemory(&mtrl.MatD3D, sizeof(D3DMATERIAL9));
mtrl.MatD3D.Diffuse = d3d::WHITE;
mtrl.MatD3D.Ambient = mtrl.MatD3D.Diffuse;
mtrl.MatD3D.Specular = d3d::WHITE;

and call Device->SetMaterial

I have computed vertex normals for each vertex

I've compared my code with an example of diffuse lighting and can't see anything i've done different that would affect the lighting

I enable the light before rendering is done using:

Device->SetLight(0, &dirLight1);
Device->LightEnable(0, true);

and set up the light in a function

D3DLIGHT9 d3d::InitDirectionalLight()
{
D3DLIGHT9 light; // create the light struct

ZeroMemory(&light, sizeof(light)); // clear out the light struct for use
light.Type = D3DLIGHT_DIRECTIONAL; // make the light type 'directional light'
light.Diffuse = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f); // set the light's color
light.Direction = D3DXVECTOR3(-1.0f, -0.3f, -1.0f);

return light;
}

Any idea what's wrong?

The other thing is I'm using an ortho projection but I checked it against a perspective projection
and the diffuse lighting doesn't work with that either.
Doug
B_old
B_old
What problem are you seeing?
I'm not really familiar with the fixed function pipeline, but I advice that you pick different colors for ambient and diffuse in your material. Otherwise everything is looking exactly the same and you couldn't see the lighting even though it is working.
doug25
doug25

What problem are you seeing?
I'm not really familiar with the fixed function pipeline, but I advice that you pick different colors for ambient and diffuse in your material. Otherwise everything is looking exactly the same and you couldn't see the lighting even though it is working.


The problem is that I can't see the lighting as you say. I'm rendering a cube and all the colours of the sides look the same, no shading that would be expected from diffuse lighting.

I tried what you suggested but the sides still are the same colour with no blending.

I'm calling
Device->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
before Draw subset as the documentation says to do so that the material is taken into consideration with the lighting
Doug
B_old
B_old
Try setting the material source for all of the D3DRS_AMBIENTMATERIALSOURCE, D3DRS_DIFFUSEMATERIALSOURCE, D3DRS_SPECULARMATERIALSOURCE varieties.
What color are you seeing on the cube? The ambient or the diffuse color?
doug25
doug25

Try setting the material source for all of the D3DRS_AMBIENTMATERIALSOURCE, D3DRS_DIFFUSEMATERIALSOURCE, D3DRS_SPECULARMATERIALSOURCE varieties.
What color are you seeing on the cube? The ambient or the diffuse color?


I'm seeing the ambient color.

I've tried setting D3DRS_AMBIENTMATERIALSOURCE to D3DMCS_MATERIAL and the same for specular. i'm still getting no diffuse lighting. i'll check out those render states in the documentation or do you have an idea what to set them to ?
Doug
B_old
B_old
Can you move the camera around the cube, to check whether the lighting is coming from another direction?
Try setting the lights colors for ambient and specular as well.
doug25
doug25

Can you move the camera around the cube, to check whether the lighting is coming from another direction?
Try setting the lights colors for ambient and specular as well.


I can move the camera around, but there's still no diffuse lighting happening. i've tried changing the colors for specular ambient and diffuse but with no desired result. I did find that if I set SOFTARE VERTEXPROCESSING on then everything goes white but changing colors and renderstates for that hasn't had any effect, it's weird.
Doug
B_old
B_old
Right now I'm out of ideas. Have you checked that the normals are included in the vertex layout?
Now this is hardly constructive, but have you considered using shaders? It won't magically solve your problem, but it gives you a lot more flexibility later when all is up and running. Probably even easier to debug.
doug25
doug25

Right now I'm out of ideas. Have you checked that the normals are included in the vertex layout?
Now this is hardly constructive, but have you considered using shaders? It won't magically solve your problem, but it gives you a lot more flexibility later when all is up and running. Probably even easier to debug.


I've manually computed normals and added them to each vertex, I'm pretty sure the normals are correct as I've tried lighting with a loaded mesh that has normal data too which only shows ambient. I did actually try a diffuse shader instead but i'm quite new to shaders and the mesh went invisible sometimes. I might have another go, but i'm quite set on finding a solution without having to use one. thanks
Doug
doug25
doug25
I've got the lighting working now but only from certain angles. I had to not call Device->SetRenderState(::D3DRS_NORMALIZENORMALS, true); that was the problem. quite difficult to spot if you ask me!

Any idea why it only works from certain angles ?

sometimes a mesh turns completely white(lit) from some angles and other angles it works fine.
Doug
doug25
doug25
Problem Solved! I found the answer on directxtutorial.com. I was using a cube with 8 vertices and needed to use one with 4 vertices per side. so it was something to do with the vertex normals being incorrect.
Doug

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.