• Advertisement
Sign in to follow this  

[C++][DIRECTX9] Lightning has no effect?

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

Hello, I set up a quite simple scene including one textured cube. now when i add lightning i see no difference, there is no difference with a directional ligh or a ambient light nothing seems to work. here are some codefragments plz ask for more if needed. here is where i set up the render states
void setRenderStates()
{
	if(g_wireframe)
	{
		gp_d3dDev->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
	}
	else
	{
		gp_d3dDev->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID);
	}
	
	//z buffer
	gp_d3dDev->SetRenderState(D3DRS_ZENABLE, TRUE);
	
	//lightning
	gp_d3dDev->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
	gp_d3dDev->SetRenderState(D3DRS_LIGHTING, TRUE);
	gp_d3dDev->SetRenderState(D3DRS_AMBIENT, 0x00202020);
	
	//textures
	gp_d3dDev->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);
	gp_d3dDev->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
	gp_d3dDev->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE);
	gp_d3dDev->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	gp_d3dDev->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	gp_d3dDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
}

here is where i add the lights
HRESULT setLightning()
{
HRESULT hr;

	D3DXVECTOR3 Direction;
	D3DLIGHT9 LightSource;

	LightSource.Type = D3DLIGHT_DIRECTIONAL;
	LightSource.Range = 100.0f;
	LightSource.Diffuse.r = 0.5f;
	LightSource.Diffuse.g = 0.5f;
	LightSource.Diffuse.b = 0.5f;
	LightSource.Diffuse.a = 1.0f;

	Direction = D3DXVECTOR3(1.0f, 0.0f, 1.0f);
	D3DXVec3Normalize((D3DXVECTOR3*)&LightSource.Direction, &Direction);

	hr = gp_d3dDev->SetLight(0, &LightSource);
	if(FAILED(hr))
	{
		return(hr);
	}

	hr = gp_d3dDev->LightEnable(0, TRUE);
	if(FAILED(hr))
	{
		MessageBox(NULL, L"LightEnable Failed, make sure directional lights are supported", L"Error!", MB_OK);
		return(hr);
	}
	return(hr);
}

and here is where i create my cube
HRESULT createObject()
 {
 HRESULT hr;
 IDirect3DTexture9 *p_Texture;
 D3DMATERIAL9 material;

	D3DXVECTOR3 norm(0,0,-1);
	iVertex front[] =
	{
		{-0.5,-0.5,-0.5,norm,0xFFFFFFFF,0,1},
		{-0.5,0.5,-0.5,norm,0xFFFFFFFF,0,0},
		{0.5,-0.5,-0.5,norm,0xFFFFFFFF,1,1},
		{0.5,0.5,-0.5,norm,0xFFFFFFFF,1,0}
	};
	norm = D3DXVECTOR3(1,0,0);
	iVertex right[] =
	{
		{0.5,-0.5,-0.5,norm,0xFFFFFFFF,0,1},
		{0.5,0.5,-0.5,norm,0xFFFFFFFF,0,0},
		{0.5,-0.5,0.5,norm,0xFFFFFFFF,1,1},
		{0.5,0.5,0.5,norm,0xFFFFFFFF,1,0}
	};
	norm = D3DXVECTOR3(0,0,1);
	iVertex back[] =
	{
		{0.5,-0.5,0.5,norm,0xFFFFFFFF,0,1},
		{0.5,0.5,0.5,norm,0xFFFFFFFF,0,0},
		{-0.5,-0.5,0.5,norm,0xFFFFFFFF,1,1},
		{-0.5,0.5,0.5,norm,0xFFFFFFFF,1,0}
	};
	norm = D3DXVECTOR3(-1,0,0);
	iVertex left[] =
	{
		{-0.5,-0.5,0.5,norm,0xFFFFFFFF,0,1},
		{-0.5,0.5,0.5,norm,0xFFFFFFFF,0,0},
		{-0.5,-0.5,-0.5,norm,0xFFFFFFFF,1,1},
		{-0.5,0.5,-0.5,norm,0xFFFFFFFF,1,0}
	};
	norm = D3DXVECTOR3(0,1,0);
	iVertex top[] =
	{
		{-0.5,0.5,-0.5,norm,0xFF000000,0,1},
		{-0.5,0.5,0.5,norm,0xFF000000,0,0},
		{0.5,0.5,-0.5,norm,0x0F000000,1,1},
		{0.5,0.5,0.5,norm,0x0F000000,1,0}
	};
	norm = D3DXVECTOR3(0,-1,0);
	iVertex bottom[] =
	{
		{-0.5,-0.5,0.5,norm,0xFF000000,0,1},
		{-0.5,-0.5,-0.5,norm,0xFF000000,0,0},
		{0.5,-0.5,0.5,norm,0x0F000000,1,1},
		{0.5,-0.5,-0.5,norm,0x0F000000,1,0}
	};

	object = new iObject();

	hr = D3DXCreateTextureFromFile(gp_d3dDev, L".\\textures\\metal.jpg", &p_Texture);
	if(FAILED(hr))
	{
		g_appRun = false;
		g_exitCode = 110;
		return(hr);
	}

	ZeroMemory(&material,sizeof(D3DMATERIAL9));

	material.Diffuse.r = 1.0f; material.Ambient.r = 1.0f;
	material.Diffuse.g = 1.0f; material.Ambient.g = 1.0f;
	material.Diffuse.b = 1.0f; material.Ambient.b = 1.0f;
	material.Diffuse.a = 1.0f; material.Ambient.a = 1.0f;

	object->addPart(4,D3DPT_TRIANGLESTRIP,front,p_Texture,material);
	object->addPart(4,D3DPT_TRIANGLESTRIP,right,p_Texture,material);
	object->addPart(4,D3DPT_TRIANGLESTRIP,back,p_Texture,material);
	object->addPart(4,D3DPT_TRIANGLESTRIP,left,p_Texture,material);
	object->addPart(4,D3DPT_TRIANGLESTRIP,top,p_Texture,material);
	object->addPart(4,D3DPT_TRIANGLESTRIP,bottom,p_Texture,material);

	return(hr);
}

ok the cube renders and its rotating like it did before i added lightning. do i need to recalculate lightning every frame using a command? or should this do? please help me out, its driving me mad.

Share this post


Link to post
Share on other sites
Advertisement
What vertex format are you using? More specifically, is it *definitely* set when you actually submit the rendering?

The only bit of code that looks suspect to me is:

material.Diffuse.r = 1.0f; material.Ambient.r = 1.0f;
material.Diffuse.g = 1.0f; material.Ambient.g = 1.0f;
material.Diffuse.b = 1.0f; material.Ambient.b = 1.0f;
material.Diffuse.a = 1.0f; material.Ambient.a = 1.0f;


Setting the Ambient colour to 1.0 should force everything to be white. Diffuse is multiplicative and Ambient is additive...

Try changing the Ambient to be 0.0 and see if it works. It's been a while since I've done any fixed-function coding though, so I'm not 100% sure that'll be it.

hth
Jack

Share this post


Link to post
Share on other sites
this would be my vertex format.


//an iVertex is the smallest part of this engine, its only a dot in space
struct iVertex
{
float x,y,z; //vertex's postion
D3DXVECTOR3 normal; //vertex's normal
DWORD color; //vertex's color
float tu,tv; //texture coordinates
};
const DWORD iFVF = (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1);



changing material diffuse and ambient numbers doesn't change anything

Share this post


Link to post
Share on other sites
oke i'm pretty new to this all as you might have noticed. now changing colorop to modulate changes my cube to black deep black like 0xFF000000. i still can see it since my background isn't. I guess the object isn't lit by the lightsource? but what am i doing wrong here.

Share this post


Link to post
Share on other sites
const DWORD iFVF = (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1);


This
line
tells
D3D
that
your
vertices
are
allready
lit
take
the
the
D3DFVF_DIFFUSE
off
and
DWORD color; //vertex's color
from
the
iVertex
declaration.
That
will
work
:)

Share this post


Link to post
Share on other sites
ALREADY_LIT_VERTICES:


//an iVertex is the smallest part of this engine, its only a dot in space
struct iVertex
{
float x,y,z; //vertex's postion
D3DXVECTOR3 normal; //vertex's normal
DWORD color; //vertex's color
float tu,tv; //texture coordinates
};
const DWORD iFVF = (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1);

------------------------------------------------------

USER_LIT_VERTICES:

//an iVertex is the smallest part of this engine, its only a dot in space
struct iVertex
{
float x,y,z; //vertex's postion
D3DXVECTOR3 normal; //vertex's normal
float tu,tv; //texture coordinates
};
const DWORD iFVF = (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1);




Share this post


Link to post
Share on other sites
Quote:
Original post by BornToCode
You are setting the lighting, but it looks like you are not setting any materials or anything.


look at the creation of the cube, here's where i set up the material.

I stripped the diffuse color, but nothing changes. i found out i had the compiler in release mode and switched it back to debug (using d3d debug runtime). but now it seems like were back to the start and nothing happend, i see just a fully lit cube (all sides lighted at the same brightnes). and when i switch to release mode the cube is black again. strange....

Share this post


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

  • Advertisement