Jump to content
  • Advertisement
Sign in to follow this  
Xeile

Lighting a texture

This topic is 4889 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 got some trouble lighting my ground texture. If I don't use a texture all goes fine, you can see clearly some lighted area and an unlighted one. But when I apply a texture to the geometry the lighting effect doesn't work anymore and you see the entire textured geometry lite (so no lighted and unlighted areas anymore, like it does in without applying a texture to the geometry). Can anyone help me with this?

Share this post


Link to post
Share on other sites
Advertisement
I'm using DirectX (else I would have posted it in OpenGL forum).

Here is the source of the render function:

VOID Render()
{
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
// Begin the scene
if(SUCCEEDED(g_pd3dDevice->BeginScene()))
{
SetupMatrices();
SetupLights();
// Samplerstages here..

// Texture crap here
g_pd3dDevice->SetTexture(0, g_pTB1);
g_pd3dDevice->SetTexture(1, g_pTB2);
g_pd3dDevice->SetTexture(2, g_pTB3);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
g_pd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);
g_pd3dDevice->SetTextureStageState(2, D3DTSS_TEXCOORDINDEX, 0);

SetTSS(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); // just select arg1
SetTSS(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); // which is texture color
SetTSS(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); // just select arg1

SetTSS(1, D3DTSS_COLOROP, D3DTOP_BLENDDIFFUSEALPHA); // Use alpha from stage 0 to blend
SetTSS(1, D3DTSS_COLORARG1, D3DTA_CURRENT); // and the stage 0's color
SetTSS(1, D3DTSS_COLORARG2, D3DTA_TEXTURE);
SetTSS(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); // Just keep alpha stage active
SetTSS(1, D3DTSS_ALPHAARG1, D3DTA_CURRENT);

if(bGrid)
{
SetTSS(2, D3DTSS_COLOROP, D3DTOP_BLENDTEXTUREALPHA);
SetTSS(2, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
SetTSS(2, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);

SetTSS(3, D3DTSS_COLOROP, D3DTOP_DISABLE);
SetTSS(3, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
}
else
{
SetTSS(2, D3DTSS_COLOROP, D3DTOP_DISABLE);
SetTSS(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
}

for(int i = 0; i < MapX * MapY; i++)
g_pMesh->DrawSubset(0);

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

}



Here is the SetupLight function:

VOID SetupLights()
{
D3DMATERIAL9 d3dMaterial;
ZeroMemory( &d3dMaterial, sizeof(d3dMaterial) );

d3dMaterial.Diffuse.r = d3dMaterial.Ambient.r = 1.0f;
d3dMaterial.Diffuse.g = d3dMaterial.Ambient.g = 1.0f;
d3dMaterial.Diffuse.b = d3dMaterial.Ambient.b = 1.0f;
d3dMaterial.Power = 0;

g_pd3dDevice->SetMaterial(&d3dMaterial);

D3DLIGHT9 d3dLight;

//Initialize the light structure.
ZeroMemory(&d3dLight, sizeof(D3DLIGHT9));

//Set up a white point light at (0, 0, -10).
d3dLight.Type = D3DLIGHT_POINT;

d3dLight.Diffuse.r = d3dLight.Diffuse.g = d3dLight.Diffuse.b = 1.0f;
d3dLight.Ambient.r = d3dLight.Ambient.g = d3dLight.Ambient.b = 0.5f;

d3dLight.Specular.r = 0.0f;
d3dLight.Specular.g = 0.0f;
d3dLight.Specular.b = 0.0f;

d3dLight.Position = D3DXVECTOR3(8.0f, 8.0f, 4.0f);

d3dLight.Attenuation0 = 1.0f;
d3dLight.Attenuation1 = 0.0f;
d3dLight.Attenuation2 = 0.0f;
d3dLight.Range = 8.0f;

//Assign the point light to our device in poisition (index) 0
g_pd3dDevice->SetLight(0, &d3dLight);

//Enable our point light in position (index) 0
g_pd3dDevice->LightEnable(0, TRUE);

//Turn on lighting
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE);

//Set ambient light level
g_pd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(32, 32, 32));

}



I make use of the mesh->Drawsubset command.

Share this post


Link to post
Share on other sites
Sorry. I wasn't paying attention to the forum.

I believe the way texture stages and lighting work is that the lighting is calculated per-vertex and then saved in vertex color, which is then interpolated. So one of your texture stages needs to be set to modulate the output fragment by the interpolated vertex color - I believe it's D3DTA_DIFFUSE.

Hope that helps.

Tom

Share this post


Link to post
Share on other sites
Quote:
Original post by ParadigmShift
Sorry. I wasn't paying attention to the forum.

I believe the way texture stages and lighting work is that the lighting is calculated per-vertex and then saved in vertex color, which is then interpolated. So one of your texture stages needs to be set to modulate the output fragment by the interpolated vertex color - I believe it's D3DTA_DIFFUSE.

Hope that helps.

Tom


Yep, it did the job. Thank you.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!