DX lighting test

Started by
5 comments, last by Armadon 18 years, 8 months ago
Here I am again, seeking help in cyberspace. I have created a application which creates a box and lightning. The box rotates but it gets "stuck" on the backround which creates an annoying effect. The code can be found here: http://codedump.nutextonline.com/view/88 Uses dx9. Edit: Link for the program itself(.rar). Here
Advertisement
Your problem is that you're not clearing the backbuffer each frame. As a result, the contents of the previous frame are passed on to the next one, except where you draw over it.

You need to add a call to
g_pD3DDevice->Clear() every frame, right after BeginScene().

Hope this helps :).
Sirob Yes.» - status: Work-O-Rama.
edit: found it.(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_RGBA(0,0,0,255), 1.0f, 0);

I removed the D3DCLEAR_ZBUFFER and for some reason it worked :D

I do g_pD3DDevice->(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_RGBA(0,0,0,255), 1.0f, 0);

Right before begin scene and that is done every frame but as you can see it does not help.
ok thanks. I did the clear before the beginscene and it works :D. Well now I am trying to add ambient lighting as I see I need to work a bit more on lighting.
So I will pop another question.

I try to add ambient g_pD3DDevice->SetRenderState(D3DRS_AMBIENT,
D3DCOLOR_COLORVALUE(0.0f, 0.5f, 0.5f, 0.8f));

I add it on the line 176 but I can't see any changes, why?

And thanks for your quick answers, appreciate it very much.
When you create your device if you did not request a ZBuffer then do not clear it.I think that you already know that and that is the reason you were getting the artifact but i would suggest you used the zbuffer otherwisew you will need to draw everything back to front and it can get kind of annoying especially when you have a bunch of Objects at Different Z locations.
Ohh ok, learning dx so I guess I will come to that part. Thanks for the info.

I also tried to add a directional light but, well it just won't work.
here is the code:
D3DLIGHT9 Light;



ZeroMemory(&ALight, sizeof(ALight));

ALight.Type= D3DLIGHT_DIRECTIONAL;
// Set the diffuse and ambient colors to yellow
ALight.Diffuse.r = ALight.Ambient.r = 1.0f;
ALight.Diffuse.g = ALight.Ambient.g = 1.0f;
ALight.Diffuse.b = ALight.Ambient.b = 0.0f;

D3DXVECTOR3 Dir = D3DXVECTOR3(0.0f, 500.0f, 0.0f);
D3DXVec3Normalize((D3DXVECTOR3*)&ALight.Direction, &Dir);


// Set and enable the light
g_pD3DDevice->SetLight(0, &Light);
g_pD3DDevice->SetLight(1, &ALight);
g_pD3DDevice->LightEnable(0, TRUE);
g_pD3DDevice->LightEnable(1, TRUE);

Shouldn't that enable both lights?
Hi there Todilo,
How are you doing?

[The Problem]
Directional light problem?

[The Solution]
The directional light doesn't have a range.
Directional Light:
"Light is a directional light source. This is equivalent to using a point light source at an infinite distance."

So let's look at your code :)

Your light Direction
D3DXVECTOR3 Dir = D3DXVECTOR3(0.0f, 500.0f, 0.0f);
Change that to
D3DXVECTOR3 Dir = D3DXVECTOR3(0.0f, 1.0f, 0.0f); //Unit vector :)

Assign light to the device
g_pd3dDevice->SetLight( 0, &light );

The following code fragment enables the light by calling
g_pd3dDevice->LightEnable( 0, TRUE);

The following code fragment tells Direct3D to render lights by calling
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );

For testing purposes enable ambient lighting
g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x00202020 );

For further reading on lights and materials, I would suggest the SDK docs which are pretty clear :)

Lights and Material

I hope this helps a bit :)

This topic is closed to new replies.

Advertisement