Archived

This topic is now archived and is closed to further replies.

kVandaele

(SOLVED) D3DLIGHT_DIRECTIONAL - no effect

Recommended Posts

I'm making a space sim and I want two lights: directional light to represent the sun, and ambient light to make sure the not-lighted-by-the-sun side isn't black. To do this I implemented a version of Andy Pike's lighting code, then heavily modified it over and over hoping I can get it to work... I've successfully implemented the D3DLIGHT_POINT light, included (below) as commented code. I've viewed the meshes in MeshView (they're .x files) and they have normals. I also added a material, and changed virtually all of its settings trying to get something useful but nada. I also read another thread here (search function works today, yay!) after which I enabled automatic normalizing of normals to see if that helped (obviously it didn't): m_pD3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE). I get no errors in my log (so all the HRESULTS check out fine). I added the material using the code from the DX C++ help files... Also, in their Tutorial 4: Creating and Using Lights they setup #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL). So I checked my Mesh class and found I'm using #define MESH_D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1) so that's not it either. I don't know what's going on... Any help?
bool cApp::InitLights(void* Ptr){
	cApp *App = (cApp*)Ptr;

	LogInfo("<br>Initialise Material:");
	D3DMATERIAL8 mtrl;
	ZeroMemory( &mtrl, sizeof(D3DMATERIAL8) );
	mtrl.Diffuse.r = mtrl.Ambient.r = 1.0f;
	mtrl.Diffuse.g = mtrl.Ambient.g = 1.0f;
	mtrl.Diffuse.b = mtrl.Ambient.b = 1.0f;
	mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;
	mtrl.Specular.r = 1.0f;
	mtrl.Specular.g = 1.0f;
	mtrl.Specular.b = 1.0f;
	mtrl.Specular.a = 1.0f;
	mtrl.Power = 0.0f;
	if(FAILED(App->m_Graphics.GetDeviceCOM()->SetMaterial(&mtrl))){
		LogError("<li>SetMaterial Failed");
		return false;
	}else{
		LogInfo("<li>SetMaterial OK");
	}

	LogInfo("<br>Initialise Lights:");

	D3DLIGHT8 d3dLight;

	//Initialize the light structure.

	ZeroMemory(&d3dLight, sizeof(D3DLIGHT8));

	d3dLight.Type = D3DLIGHT_DIRECTIONAL;
	
	d3dLight.Ambient.r = 1.0f;
	d3dLight.Ambient.g = 1.0f;
	d3dLight.Ambient.b = 1.0f;
	
	D3DXVECTOR3 Dir = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);
	D3DXVec3Normalize((D3DXVECTOR3*)&d3dLight.Direction, &Dir);
/*	d3dLight.Type = D3DLIGHT_POINT;
	
	d3dLight.Position.x = 0.0f;
	d3dLight.Position.y = 10.0f;
	d3dLight.Position.z = 0.0f;

	d3dLight.Attenuation0 = 1.0f; 
	d3dLight.Attenuation1 = 0.0f; 
	d3dLight.Attenuation2 = 0.0f; 
	d3dLight.Range = 1000.0f;	

	d3dLight.Diffuse.r = 1.0f;
	d3dLight.Diffuse.g = 1.0f;
	d3dLight.Diffuse.b = 1.0f;
	
	d3dLight.Ambient.r = 0.0f;
	d3dLight.Ambient.g = 0.0f;
	d3dLight.Ambient.b = 0.0f;
	
	d3dLight.Specular.r = 0.0f;
	d3dLight.Specular.g	= 0.0f;
	d3dLight.Specular.b	= 0.0f;
*/
	//Assign the point light to our device in poisition (index) 0

	if(FAILED(App->m_Graphics.GetDeviceCOM()->SetLight(0, &d3dLight))){
		LogError("<li>SetLight Failed");
		return false;
	}else{
		LogInfo("<li>SetLight OK");
	}

	//Enable our point light in position (index) 0

	if(FAILED(App->m_Graphics.GetDeviceCOM()->LightEnable(0, TRUE))){
		LogError("<li>LightEnable Failed");
		return false;
	}else{
		LogInfo("<li>LightEnable OK");
	}

	//Turn on lighting

    if(FAILED(App->m_Graphics.GetDeviceCOM()->SetRenderState(D3DRS_LIGHTING, TRUE))){
		LogError("<li>SetRenderState: D3DRS_LIGHTING Failed");
		return false;
	}else{
		LogInfo("<li>SetRenderState: D3DRS_LIGHTING OK");
	}

	//Set ambient light level

	if(FAILED(App->m_Graphics.GetDeviceCOM()->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(60, 60, 60)))){
		LogError("<li>SetRenderState: D3DRS_AMBIENT Failed");
		return false;
	}else{
		LogInfo("<li>SetRenderState: D3DRS_AMBIENT OK");
	}

	//TEMP

	if(FAILED(App->m_Graphics.GetDeviceCOM()->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE))){
		LogError("<LI>can't normalize");
		return false;
	}else{
		LogInfo("<li>normalized");
	}

	return true;
}
[edited by - kVandaele on May 19, 2004 10:56:05 AM]

Share this post


Link to post
Share on other sites
You never set the light''s diffuse color, so all you''ll ever get is your ambient level.... other than that it seems okay.

You also need to ensure your texturestagestate''s include a modulate with DIFFUSE which holds the results of the lighting calculation.

// Use lighting * texture as color

// Use material alpha * texture alpha as alpha

pDev->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);
pDev->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
pDev->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE);
pDev->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_MODULATE);
pDev->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);
pDev->SetTextureStageState(0,D3DTSS_ALPHAARG2,D3DTA_DIFFUSE);
pDev->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_DISABLE);
pDev->SetTextureStageState(1,D3DTSS_ALPHAOP,D3DTOP_DISABLE);

Share this post


Link to post
Share on other sites
that did it... but I had to disable the light''s ambient lighting as well as add the diffuse component. Why would a directional light have ambient lighting anyway? (i mean, then what is the ambient renderstate for? ... whatever)

thanks a lot. I''ll check out those renderstates (they look like some might be in the graphics engine though - meaning not in the light initialization)

Share this post


Link to post
Share on other sites