Sign in to follow this  

D3D Lighting topic #1524

This topic is 4836 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

Hi Ok, this is driving me nuts for weeks now. I've read every lighting-thread on gamedev.net about a hundred times now, but still can't get my (point-) light to work. One reason might be that I don't understand the DX lighting system :D Ok, here we go (just in case you're curios, "gState" is my statemanager so I don't get any "redundant render state" warnings): gState.SetRenderState(D3DRS_LIGHTING, TRUE); // eh...yeah :) Here's my light:
D3DLIGHT9 lfFlashlight;
ZeroMemory(&ltFlashlight, sizeof(ltFlashlight));
	
	D3DXVECTOR3 vecLight(playerPosition.pos);
	vecLight.z += 2.0f;

	ltFlashlight.Type			= D3DLIGHT_POINT;
	ltFlashlight.Diffuse.r		= ltFlashlight.Ambient.r = 0.0f;
    ltFlashlight.Diffuse.g		= ltFlashlight.Ambient.g = 0.0f;
    ltFlashlight.Diffuse.b		= ltFlashlight.Ambient.b = 1.0f;
	ltFlashlight.Diffuse.a		= ltFlashlight.Ambient.a = 0.0f;
    ltFlashlight.Range			= 100.0f;
	ltFlashlight.Position		= vecLight;
	ltFlashlight.Attenuation0	= 0.0f;
	ltFlashlight.Attenuation1	= 1.0f;
	ltFlashlight.Attenuation2	= 0.0f;

	hRes = pD3DDevice->SetLight(0, &ltFlashlight); // S_OK
	hRes = pD3DDevice->LightEnable(0, TRUE); // S_OK
These are the texture states I set before rendering my level (BSP):
gState.SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
	gState.SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	gState.SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
	gState.SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
	gState.SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	gState.SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
This is blending the level texture with a lightmap - works fine without lighting. My vertex struct:
struct VERTEX 
	{
		D3DXVECTOR3 pos;
		TEXCOORDS surface;
		TEXCOORDS lightmap;
		D3DXVECTOR3 normal;
		byte color[4];
	};
Vertex declaration...
const D3DVERTEXELEMENT9 vertexDeclaration[] = 
	{
		{ 0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
		{ 0, 12, D3DDECLTYPE_FLOAT2,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		{ 0, 20, D3DDECLTYPE_FLOAT2,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 },
		{ 0, 28, D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,   0 },
		{ 0, 40, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR,    0 },
		D3DDECL_END()
	};
I draw the normals and they are correct, they're normalized. So, why isn't my light working? :( thanks in advance

Share this post


Link to post
Share on other sites
1) Have you set a D3D material to describe how your mesh recieves/reflects light ? if not, you should.

i.e. have you called IDirect3DDevice9::SetMaterial(), and what did you set the members of the D3DMATERIAL9 to ?


2) have you set a global ambient colour?, if not, try it [i.e. SetRenderState( D3DRS_AMBIENT, ???) ]

Share this post


Link to post
Share on other sites
1)
I tried:
D3DMATERIAL9 mat;
ZeroMemory(&mat, sizeof(D3DMATERIAL9));
mat.Ambient.r = mat.Diffuse.r = mat.Emissive.r = 1.0f;
mat.Ambient.g = mat.Diffuse.g = mat.Emissive.g = 1.0f;
mat.Ambient.b = mat.Diffuse.b = mat.Emissive.b = 1.0f;

pD3DDevice->SetMaterial(&mat);


(Actually I don't even know what I'm setting here :-D)
But it seems as if this material is only applied to parts of my level where no (valid) texture is set.
For a value of RGB(1,1,1) these parts have a yellowish color, RGB(0,0,0) makes them black and so on.
But the textures themselves don't change at all.

2)

Yep:
gState.SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(255,255,255));

Share this post


Link to post
Share on other sites
Quote:
Original post by FoxHunter2
This turns everything completely black :(

That's progress [smile]
- Are you sure you're filling your vertices with the correct color values?
- Try increasing the range of the light a little
- Try setting its alpha to 1 (honestly, I don't remember if alpha made any difference for lights, but trying won't hurt)

Share this post


Link to post
Share on other sites
Set the lightmap texture into stage 0 and set the colorop of stage 0 to D3DTOP_ADD (check the cap bits to see if you can use this first). Set texture stage 1 to the texture map, and leave its colorop as modulate. Also set the emissive property of the material to black, as you don't want the triangles emitting any light, you only want them to reflect light.

Share this post


Link to post
Share on other sites
Excuse my ignorance, can you post some code?


if(face.lmId >= 0) {
pD3DDevice->SetTexture(0, m_lightmaps[face.lmId]);
}
pD3DDevice->SetTexture(1, m_textures[face.textureId]);


and the texture states:

gState.SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_ADD);
gState.SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
gState.SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
gState.SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
gState.SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_DIFFUSE);


This makes the level look extremely ugly and erroneous.

Share this post


Link to post
Share on other sites

gState.SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_ADD);
gState.SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
gState.SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
gState.SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
gState.SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
gState.SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);



See, you want to add the vertex light to the lightmap, that's what stage 0 is set to do. Stage 1 then modulates the summed light with the color map.

Share this post


Link to post
Share on other sites
Sorry for my delayed answer, I've been quite busy last week.

Here's the result of changing the texture stages the way to suggested:


As you see, this looks ugly and lighting seems to be off nonetheless :(

regards

Share this post


Link to post
Share on other sites

This topic is 4836 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this