Sign in to follow this  

I hate lights

This topic is 4481 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Whats wrong with my lights? All my polys are just rendered plain black!
//Globals
D3DLIGHT9 Light;

//[...]

//Init
ZeroMemory( &Light, sizeof(Light) );
pD3DDevice->SetRenderState(D3DRS_LIGHTING,true);
pD3DDevice->SetRenderState(D3DRS_AMBIENT,RGB(1.0f,1.0f,1.0f));
Light.Type      = D3DLIGHT_POINT;
Light.Ambient   = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
Light.Diffuse   = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
Light.Specular  = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);

//I also tried like this:
//Light.Diffuse.r = 1.0f;
//Light.Diffuse.g = 1.0f;
//Light.Diffuse.b = 1.0f;

Light.Position  = D3DXVECTOR3(0.0f, 3.0f, -1.0f);
Light.Range     = 1000.0f;
Light.Falloff   = 1.0f;
Light.Attenuation0 = 1.0f;
Light.Attenuation1 = 0.0f;
Light.Attenuation2 = 0.0f;
pD3DDevice->SetLight(0, &Light);
pD3DDevice->LightEnable(0, true);

//[...]

//Render all thingZ


I have made all possible tutorials but found no answers! the only thing I see when doing this is my vertex-shaded teapot, and even if I comment all the shaders things, the lights wont work!

Share this post


Link to post
Share on other sites
Light.Position = D3DXVECTOR3(0.0f, 3.0f, -1.0f);

Where is your direction?

Also, if you are using the sampleFramework and HLSL (.fx) then the fixed-function (D3DLight) is disabled automatically.

Share this post


Link to post
Share on other sites
He's using a point light so he shouldn't need one.

What are the material settings for your polys?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ok so I need a material for EACH of my polygons?
And do I also need to set normals in my FVFs?

Share this post


Link to post
Share on other sites
Yes, you do need normals and materials. One material should be enough, it just means all your polygons will have the same color and such.

Make sure you calculate the normals correctly too, you can't just add them to the FVF. I think if you use a d3dxmesh it can do this for you.

Share this post


Link to post
Share on other sites
Ok now i've made a material and it works!
I'm now getting all my polys in color.

But stiiillllll... The light wont work!

I'll put back my code because I might have updated it:

//Globals
D3DLIGHT9 Light;

//in bool init() :

ZeroMemory( &Light, sizeof(Light) );
pD3DDevice->SetRenderState(D3DRS_LIGHTING, true);
//pD3DDevice->SetRenderState(D3DRS_AMBIENT,RGB(0.0f,1.0f,1.0f));
Light.Type = D3DLIGHT_POINT;

D3DCOLORVALUE LightColor;
LightColor.a=LightColor.r=LightColor.b=1.0f;
Light.Ambient = LightColor; //D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
Light.Diffuse = LightColor; //D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
Light.Specular = LightColor; //D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);

Light.Position = D3DXVECTOR3(0.0f, 3.0f, -1.0f);
Light.Range = 5.0f;
Light.Falloff = 1.0f;
Light.Attenuation0 = 1.0f;
Light.Attenuation1 = 0.0f;
Light.Attenuation2 = 0.0f;
pD3DDevice->SetLight(0, &Light);
pD3DDevice->LightEnable(0, true);

D3DCOLORVALUE MaterialColor;
MaterialColor.a=MaterialColor.r=MaterialColor.g=1.0f;

D3DMATERIAL9 DefaultMaterial;
DefaultMaterial.Ambient=
DefaultMaterial.Diffuse=
DefaultMaterial.Emissive=
DefaultMaterial.Specular=MaterialColor;
DefaultMaterial.Power=1.0f;
pD3DDevice->SetMaterial(&DefaultMaterial);






Ok now you can forget about the vertex shader thing, I have removed it completely to be sure it doesn't influence the fixed function pipeline.

Still I dont see my mistakes, I've made all steps:
1-Enable lights
2-Create light structure
3-Set as light 0
4-Enable light 0

Any ideas?

Note that I am using a ID3DXMesh, a plane and a heightmap to test my light, the mesh should have normals (I put D3DFVF_NORMAL in my CloneMeshFVF) but not the other objects, but anyway I should not be able to see anything from a certain distance even without normals I guess..

Share this post


Link to post
Share on other sites
In directx SDK 9.0 you can find a 'Lighting' sample. In sample there are point, directional, and spot lights, all moving and working 100%. Look at it... if you don't have it send me a message I'll send it to you via mail.

Share this post


Link to post
Share on other sites
Hum... I can't seem to find your lighting sample... I have only HLSL things.

Was I in the good directory?
C:\Program Files\Microsoft DirectX 9.0 SDK (August 2005)\Samples\C++\Direct3D

BTW my email should be in my profile

Share this post


Link to post
Share on other sites
nop it is in original dirctx 9.0 sdk, the first one without updates. anyway I'll send it to you...

Share this post


Link to post
Share on other sites
If you see the colors specified in the material, and lighting is enabled, that should mean the light is working.

Maybe it's not working at all, but I'd guess it's just not working the way you expect. I would try messing with the settings: increase the range, set the "ambient" value to zero, and decrease the attenuation for starters, just to see if it has any effect.

Share this post


Link to post
Share on other sites
I don't think thats the problem because it is still lit with RGB 0 0 0 for the light...


PS : TX streamer, i'll look at these. (but I don't think i've ever installed the "basic" sdk o_O)

Share this post


Link to post
Share on other sites
Quote:
Original post by Trillian
I don't think thats the problem because it is still lit with RGB 0 0 0 for the light...


PS : TX streamer, i'll look at these. (but I don't think i've ever installed the "basic" sdk o_O)


Too bad :) there are samples that you can't find in any DirectX 9.0a-b-c "Update".

You're welcome [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by Trillian
the mesh should have normals (I put D3DFVF_NORMAL in my CloneMeshFVF) but not the other objects

Specifying D3DFVF_NORMAL in an ID3DXMesh::CloneMeshFVF() call will NOT generate normals for your object. It just allocates space (per vertex) for a normal vector. What exactly it'll be filled with is anyones guess [smile]

Try calling D3DXComputeNormals() after the cloning process.

Correct normals are required for fixed-function lighting.

hth
Jack

Share this post


Link to post
Share on other sites
Now I have made a new file to start from zero implementing lights and my directional light WORKS!!!!

BUT! (I knoe you guys hate "but"s)

I made a D3DXMesh cylinder and when lit, it flashes really annoyingly.

now I have decided to put my ENTIRE script (almost)!


LPD3DXMESH Cylinder = 0;

D3DMATERIAL9 DefaultMaterial;
D3DLIGHT9 Light;

bool Setup()
{
pD3DDevice->SetRenderState(D3DRS_ZENABLE, true );
pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
pD3DDevice->SetRenderState(D3DRS_LIGHTING, true);
pD3DDevice->SetRenderState(D3DRS_AMBIENT,RGB(0,0,0));
pD3DDevice->SetRenderState(D3DRS_SPECULARENABLE,true);
return true;
}


bool Init()
{
ZeroMemory( &Light, sizeof(Light) );
pD3DDevice->SetRenderState(D3DRS_LIGHTING, true);
//pD3DDevice->SetRenderState(D3DRS_AMBIENT,RGB(0.0f,1.0f,1.0f));
Light.Type = D3DLIGHT_DIRECTIONAL;

Light.Ambient = D3DYCOLOR_WHITE * 0.6f;
Light.Diffuse = D3DYCOLOR_WHITE;
Light.Specular = D3DYCOLOR_WHITE * 0.3f;

//Light.Position = D3DXVECTOR3(0.0f, 3.0f, -1.0f);
Light.Direction = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
Light.Range = 0.0f;
Light.Falloff = 0.0f;
Light.Attenuation0 = 1.0f;
Light.Attenuation1 = 0.0f;
Light.Attenuation2 = 0.0f;
pD3DDevice->SetLight(0, &Light);
pD3DDevice->LightEnable(0, true);
pD3DDevice->LightEnable(0, TRUE);

DefaultMaterial.Ambient = D3DYCOLOR_WHITE;
DefaultMaterial.Diffuse = D3DYCOLOR_WHITE;
DefaultMaterial.Emissive = D3DYCOLOR_BLACK;
DefaultMaterial.Specular = D3DYCOLOR_WHITE;
DefaultMaterial.Power=5.0f;
pD3DDevice->SetMaterial(&DefaultMaterial);

pD3DDevice->SetRenderState(D3DRS_LIGHTING, true);
pD3DDevice->SetLight(0, &Light);
pD3DDevice->LightEnable(0, true);


LPD3DXMESH TempMesh = 0;
D3DXCreateCylinder(pD3DDevice, 2.0f, 2.0f, 4.0f, 40, 40 ,&TempMesh, 0);
TempMesh->CloneMeshFVF(0,D3DFVF_XYZ | D3DFVF_NORMAL,pD3DDevice,&Cylinder);
SAFE_RELEASE(TempMesh);
D3DXComputeNormals(Cylinder,NULL);

return true;
}





bool Update(float timeDelta)
{
pD3DDevice->Clear( 0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL,D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,0.0f), 1.0f, 0 );

/***********************/
pD3DDevice->BeginScene();
/***********************/

Cylinder->DrawSubset(0);

/*********************/
pD3DDevice->EndScene();
/*********************/

pD3DDevice->Present( NULL, NULL, NULL, NULL );

return true;
}

bool Destroy()
{
SAFE_RELEASE(Cylinder);
return true;
}




but then that dumb cylinder flashes (mostly on the two circlar sides).

You guys know what causes this?

EDIT -> System:
Intel Pentium 4 CPU 3.00GHz (2CPUs), ~3.00GHz
Microsoft windows XP home edition
512MB RAM
ATI RADEON 9200
DirectX 9.0c (DXSDK august 2005)

Share this post


Link to post
Share on other sites
Sign in to follow this