Archived

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

Taralieth

Problem with lighting

Recommended Posts

Hey, can someone help me with getting this light to work. I can't find waht's wrong with it. When i turn the light on, my textured cube turns all black. This is the structure used to store my cube vertices.
typedef struct {
	FLOAT x, y, z;
	FLOAT nx, ny, nz;
	DWORD colour;
	FLOAT tu, tv;
} sCUSTOMVERTEX;
#define VERTEXFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1)
sCUSTOMVERTEX Verts[] = {
	// front face

	{ -100,-100,100, 0,0,-1, 0xFFFFFFFF, 0,1 },
	{ 100,-100,100, 0,0,-1, 0xFFFFFFFF, 1,1 },
	{ -100,100,100, 0,0,-1, 0xFFFFFFFF, 0,0 },
	{ 100,100,100, 0,0,-1, 0xFFFFFFFF, 1,0 },

	// right face

	{ 100,-100,100, 1,0,0, 0xFFFFFFFF, 1,1 },
	{ 100,-100,-100, 1,0,0, 0xFFFFFFFF, 0,1 },
	{ 100,100,100, 1,0,0, 0xFFFFFFFF, 1,0 },
	{ 100,100,-100, 1,0,0, 0xFFFFFFFF, 0,0 },

	// back face

	{ 100,-100,-100, 0,0,1, 0xFFFFFFFF, 0,1 },
	{ -100,-100,-100, 0,0,1, 0xFFFFFFFF, 1,1 },
	{ 100,100,-100, 0,0,1, 0xFFFFFFFF, 0,0 },
	{ -100,100,-100, 0,0,1, 0xFFFFFFFF, 1,0 },

	// left face

	{ -100,-100,-100, -1,0,0, 0xFFFFFFFF, 1,1 },
	{ -100,-100,100, -1,0,0, 0xFFFFFFFF, 0,1 },
	{ -100,100,-100, -1,0,0, 0xFFFFFFFF, 1,0 },
	{ -100,100,100, -1,0,0, 0xFFFFFFFF, 0,0 },
};
  

This is the initialization code. I think I'm setting something wrong here or forgetting something.
	D3DDISPLAYMODE d3ddm;
	D3DPRESENT_PARAMETERS d3dpp;
	VOID *Ptr;
	D3DXMATRIX matView, matProj;
	
	D3DLIGHT9 light;
	D3DXVECTOR3 lightvector;

	m_pD3D = Direct3DCreate9(D3D_SDK_VERSION); // initialize the direct3d object

	// returns false if the object is still NULL

	if(m_pD3D == NULL) {
		return FALSE;
	}
	
	// gets the display mode and fills the d3ddm structure.

	if(FAILED(m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm))) {
		return FALSE;
	}

	// clear the d3dpp structure and fill it w/ necessary info

	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = d3ddm.Format;

	// initialize the device object

	if(FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, 
								   D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
								   &d3dpp, &m_pDevice))) {
		return FALSE;
	}


	ZeroMemory(&light, sizeof(light));
	light.Type = D3DLIGHT_POINT;
	light.Diffuse.r = 1.0f;
	light.Diffuse.g = 1.0f;
	light.Diffuse.b = 1.0f;
	light.Range = 1000.0f;
	light.Position = D3DXVECTOR3(0.0f,0.0f,-200.0f);
	light.Attenuation0 = 1.0f;

	if(FAILED(m_pDevice->SetLight(0,&light))) {
		return FALSE;
	}

	if(FAILED(m_pDevice->LightEnable(0, TRUE))) {
		return FALSE;
	}

	m_pDevice->SetRenderState(D3DRS_AMBIENT, 0x00202020 );
	// Set the rendering states

	m_pDevice->SetRenderState(D3DRS_LIGHTING, TRUE);

	// set the projection matrix

	D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4.0f, (float)m_Width/m_Height , 1.0f, 1000.0f);
	m_pDevice->SetTransform(D3DTS_PROJECTION, &matProj);

	//set the view matrix

	D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0,0,-700), &D3DXVECTOR3(0,0,0), &D3DXVECTOR3(0,1,0));
	m_pDevice->SetTransform(D3DTS_VIEW, &matView);

	// create the vertex buffer and copy the vertices into the vertex buffer

	m_pDevice->CreateVertexBuffer(sizeof(Verts), 0, VERTEXFVF, D3DPOOL_DEFAULT, &m_pVB, NULL);
	m_pVB->Lock(0, 0, (void**)&Ptr, 0);
	memcpy(Ptr, Verts, sizeof(Verts));
	m_pVB->Unlock();

	if(FAILED(D3DXCreateTextureFromFile(m_pDevice, "data/crate.bmp", &m_pTexture))) {
		return FALSE;
	}
	m_pDevice->SetTexture(0, m_pTexture);
	m_pDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
	m_pDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	m_pDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT);
	m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
	m_pDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
    m_pDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
}
 
edit: put the code into a source tag and fixed some mistakes in copying.

Share this post


Link to post
Share on other sites
Well, when i change it to true, everythign is black. if i change it to false, i can see the texture but the lighting is off obviously.

well, i forgot to change it back to true when i copied it to the message.

[edited by - Taralieth on July 3, 2003 12:04:40 AM]

Share this post


Link to post
Share on other sites
Your vertex normals for the front and back faces are pointing the opposite direction that they should.


[edited by - don on July 4, 2003 1:58:06 AM]

Share this post


Link to post
Share on other sites
omg you''re right! i feel stupid. I just switched back to Direct3D after trying out OpenGL for a while, and i forgot that they have different positive directions for the z axis. Well, thx to everyone who answered.

Share this post


Link to post
Share on other sites