GiovanniFreda

I am trying to light a simple mesh created by calling d3dxCreateBox();

I can render the mesh without lighting and it will be white but the moment i turn lighting on it wont light...


Code follows... sry for formatting...
LPD3DXMESH g_Cube;

LPDIRECT3DDEVICE9 InitializeDevice ( HWND han_WindowToBindTo);
void DrawScene (LPDIRECT3DDEVICE9 p_dx_Device);
void SetUpCamera (LPDIRECT3DDEVICE9 p_dx_Device);

LPDIRECT3DDEVICE9 InitializeDevice(HWND han_WindowToBindTo)
{
LPDIRECT3D9 p_dx_Object;
LPDIRECT3DDEVICE9 p_dx_Device;

p_dx_Object = Direct3DCreate9(D3D_SDK_VERSION);
if (p_dx_Object == NULL)
MessageBox(han_WindowToBindTo,_T("DirectX Runtime library not installed!"),_T("InitializeDevice()"),MB_OK);

D3DPRESENT_PARAMETERS dx_PresParams;
ZeroMemory( &dx_PresParams, sizeof(dx_PresParams) );
dx_PresParams.Windowed = TRUE;
dx_PresParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
dx_PresParams.BackBufferFormat = D3DFMT_UNKNOWN;

if (FAILED(p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, han_WindowToBindTo, D3DCREATE_HARDWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device)))
MessageBox(han_WindowToBindTo,_T("Failed to create device!"),_T("InitializeDevice()"),MB_OK);

p_dx_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
D3DXCreateLine(p_dx_Device, &pLine);

D3DXCreateBox(p_dx_Device,2,2,2,&g_Cube,0);
// Fill in a light structure defining our light
D3DLIGHT9 light;
ZeroMemory( &light, sizeof(D3DLIGHT9) );
light.Type = D3DLIGHT_POINT;
light.Diffuse.r = 1.0f;
light.Diffuse.g = 1.0f;
light.Diffuse.b = 1.0f;

// Point lights have no direction but do have a position
//Set light position to the camera's point
light.Position = D3DXVECTOR3(0,0,-10);

// Tell the device about the light and turn it on
light.Attenuation0 = 0.1f;
light.Range=200.0f;

// Tell the device about the light and turn it on
p_dx_Device->SetRenderState( D3DRS_LIGHTING, TRUE );
p_dx_Device->SetLight( 0, &light );
p_dx_Device->LightEnable( 0, TRUE );
return p_dx_Device;
}

void SetUpCamera(LPDIRECT3DDEVICE9 p_dx_Device)
{
D3DXVECTOR3 m_EyePos(0, 0, -10);
D3DXVECTOR3 m_TargetPos = D3DXVECTOR3(0,0,0);
D3DXVECTOR3 m_UpVector(0, 1, 0);
D3DXMATRIXA16 m_View;
D3DXMATRIX m_Projection;
D3DXMatrixLookAtLH(&m_View, &m_EyePos, &m_TargetPos, &m_UpVector);
p_dx_Device->SetTransform(D3DTS_VIEW, &m_View);
D3DXMatrixPerspectiveFovLH(&m_Projection, D3DX_PI/4, 500/500, 1, 50);
p_dx_Device->SetTransform(D3DTS_PROJECTION, &m_Projection);

}

void DrawScene(LPDIRECT3DDEVICE9 p_dx_Device)
{
p_dx_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(72,61,139), 1.0f, 0);
p_dx_Device->BeginScene();

for (int curFace = 0; curFace < g_Cube->GetNumFaces(); curFace++)
{
p_dx_Device->SetFVF(g_Cube->GetFVF());
g_Cube->DrawSubset(curFace);
}

p_dx_Device->EndScene();
p_dx_Device->Present(NULL, NULL, NULL, NULL);
}


Re: Game Technologies: DirectX 101 Cant Light Mesh...Help..

Etienne2005

 

The Mesh you create as Vertex position only...
You need to create a clone of the mesh and add Diffuse and Normal to all your vertex

And then Calculate those Normals

Depending on what you want to show you have to select a colorOP to selectArg1
if  Diffuse is your ColorArg1

Or if you want texture (ColorArg1)  MODULATE (ColorOP) Diffuse (ColorArg2)

Light affect Vertex by using the Normal of your vertex to tell what is the angle of illumination of the surface

That vertex color calculated is call Diffuse and depend on the Diffuse of your Vertex also

It's even more complicated then this...you have to set Ambience Light value(a Render state) too

And the final result depend on all those value

You can search the DX help on Light to get the formula

All light ADD up, but Ambient is a multiplication...

I hope a MVP will give you (and me) a better explanation but it's a start :-)

 





Re: Game Technologies: DirectX 101 Cant Light Mesh...Help..

Wessam Bahnassi

D3DXCreateBox() adds normal information to the generated mesh (position+normals only). I suggest you start with a directional light as that is simpler to setup properly than a point light, and don't forget to setup your material as well.




Re: Game Technologies: DirectX 101 Cant Light Mesh...Help..

Pieter Germishuys

Wessam, hit it right on the nail,
I did not see any materials specified, the material gives the box some properties and Direct3D needs to know how the light needs to interact with the box.

But ye, I agree with Wessam