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

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

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;

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 on other sites
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 on other sites
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

Share on other sites
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).

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 on other sites
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.

Share on other sites
You are setting the lighting, but it looks like you are not setting any materials or anything.

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

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

Share on other sites

//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 on other sites
Quote:
 Original post by BornToCodeYou 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....

1. 1
2. 2
Rutin
21
3. 3
JoeJ
18
4. 4
5. 5

• 14
• 40
• 23
• 13
• 13
• Forum Statistics

• Total Topics
631719
• Total Posts
3001886
×