Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / C++ / C# - DirectX 9 - Lights not working

Author
Message
bergice
17
Years of Service
User Offline
Joined: 5th Jun 2007
Location: Oslo,Norway
Posted: 28th Apr 2011 14:09 Edited at: 28th Apr 2011 14:11
Hi, I am trying to create a basic engine in DX9 but I'm sort of stuck at the lighting part.

The issue is that the lights have no effect on my meshes. The objects will just render to the ambient color.

I think the easiest way would be to just show you the different parts of the code that has something to say on the lights.

After creating the DX device i enable some different states:



That includes setting the ambient color, and enabling lights.

My custom vertex format is defined like this:



I then create a light using this code:



Then the mesh is loaded:



The actual rendering is done like this:




And thats about it. I think one possible reason is that when the mesh is loaded it doesn't load the normal data.
Thoughts?

Hi
Diggsey
19
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 30th Apr 2011 21:43
You're setting the ambient colour to white, so it will automatically light everything up fully. You have to set it to something darker than white to see the lights.

[b]
bergice
17
Years of Service
User Offline
Joined: 5th Jun 2007
Location: Oslo,Norway
Posted: 1st May 2011 23:51
I am aware of that.

It was just for testing purposes, when i set it to a lower value lights still won't show.

Hi
Diggsey
19
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 2nd May 2011 03:40
Hmm, the only things I can think of are:
- Normalise the mesh normals
- Normalise the light direction
- Use coloured lights to make it more obvious if a light is working at all
- Increase the light range

Also noticed a few unrelated things while looking at your code:
- manager->GetDevice( ) returns an IDirect3DDevice9* which is a COM object. You should always call AddRef on a COM object before returning it from a function, and the calling code should call Release when it has finished with it. Although it's not needed in this code, if your code is used alongside code which does follow COM rules it will be very difficult to manage the lifetimes of objects.

- It's potentially faster if you clear the Z-buffer and render target at the same time like so:
p_Device -> Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,32,128), 1.0f, 0);

[b]

Login to post a reply

Server time is: 2025-05-15 19:02:59
Your offset time is: 2025-05-15 19:02:59