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

Started by
10 comments, last by basgoossen 17 years, 8 months ago
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.
It's all about 1's and 0' beïng in the right place at the right time.
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

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

this would be my vertex format.

//an iVertex is the smallest part of this engine, its only a dot in spacestruct 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
It's all about 1's and 0' beïng in the right place at the right time.
Your stage 0 colorop says "selectarg1", where arg1 is texture. This won't light anything. Lighting comes through as diffuse. You want "modulate" as the colorop, and texture and diffuse as the colorargs (your args are already fine).
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.
It's all about 1's and 0' beïng in the right place at the right time.
Have a look at this old thread and see what's different from my setup and yours. It's an example of swapping between vertex and material colors for lighting.
You are setting the lighting, but it looks like you are not setting any materials or anything.
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
:)
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);




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....
It's all about 1's and 0' beïng in the right place at the right time.

This topic is closed to new replies.

Advertisement