Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

DirectX 8 Lighting

Hi,
I am having trouble getting lights to work on textured
meshes. I use my own loading program which I will attach.
And the render code I will attach too. I don't understand
why it won't work though. Can anyone point out the basic
things that need to be set to get a light to effect a
tectured mesh?

********************Mesh Loader***********************
bool OBJECT::Load(char *FileName,char *TextureName)
{
// loads all the information into the object and creates
everything
// even includes tecture mapping support.

LPD3DXBUFFER pD3DXMtrlBuffer = NULL;
LPD3DXBUFFER pAdjacencyBuffer = NULL;

// Load the mesh from the specified file

if(FAILED(D3DXLoadMeshFromX(FileName,D3DXMESH_SYSTEMMEM,lpd3dDevice,
&pAdjacencyBuffer,&pD3DXMtrlBuffer,&numMaterials,
&mesh) ))
{
return false;
}

// Optimize the mesh for performance
if( FAILED(mesh->OptimizeInplace(
D3DXMESHOPT_COMPACT |
D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,

(DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL, NULL,
NULL ) ) )
{
if (pAdjacencyBuffer)
pAdjacencyBuffer->Release();
if (pD3DXMtrlBuffer)
pD3DXMtrlBuffer->Release();
return false;
}

// get the properties of the mesh for materials and texturing
D3DXMATERIAL* d3dxMaterials =
(D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
meshMtls = new D3DMATERIAL8[numMaterials];
meshTexs = new LPDIRECT3DTEXTURE8[numMaterials];

for(DWORD i=0; i<numMaterials; i++)
{
// Copy the material
meshMtls[i] = d3dxMaterials[i].MatD3D;

// Set the ambient color for the material (D3DX
does not do this)
meshMtls[i].Ambient = meshMtls[i].Diffuse;

// Create the texture
meshTexs[i] = NULL;

D3DXCreateTextureFromFile(lpd3dDevice,TextureName,&meshTexs[i]);
}

// for practical purposes let's set the number of polys
and verts
poly_count = mesh->GetNumFaces();
vert_count = mesh->GetNumVertices();

// clean up and leave
pD3DXMtrlBuffer->Release();
pAdjacencyBuffer->Release();

numObjects++;

return true;
}
**********************End Loader**********************
**********************Mesh Renderer*******************
void OBJECT::Render(void)
{
// Renders the object in the way you want it

// This places the light into active mode and renders it
// Plug it into the render engine
lpd3dDevice->SetLight(ID,&theLight);
lpd3dDevice->LightEnable(ID,TRUE);

// Lock the usuals for rendering, if visible
if (visible)
{
lpd3dDevice->SetTransform(D3DTS_WORLD,&objMat);

for(DWORD i=0; i<numMaterials; i++)
{
// Lock usual textures for each poly
lpd3dDevice->SetMaterial(&meshMtls[i]);
lpd3dDevice->SetTexture(0,meshTexs[i]);

// Draw the mesh subset
mesh->DrawSubset(i);
}
}
}

Comments

  • illuminatorilluminator Member Posts: 15
    Hi,

    1. Did you enable lighting for your device?
    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );

    2. What is the FVF of your mesh? For lighting you need normals.

    3. What kind of light is it? Check light range.

    4. please don not worry about my poor english ;)




    cu,

    illuminator
    (Novus ordo seclorum)

  • GozarGozarianGozarGozarian Member Posts: 17
    Hi,
    I do the g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE ); at the initial setup of the lighting. So that is done correctly.
    I think the problem is the FVF. I can render primitives just fine. Can you tell me how to set the FVF with a mesh? I can't find the code in the examples right now. I would just need the function name of how to do it, I already have my own FVF structure setup.

    Thanks,
    Rory

    : Hi,
    :
    : 1. Did you enable lighting for your device?
    : g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
    :
    : 2. What is the FVF of your mesh? For lighting you need normals.
    :
    : 3. What kind of light is it? Check light range.
    :
    : 4. please don not worry about my poor english ;)
    :
    :
    :
    :
    : cu,
    :
    : illuminator
    : (Novus ordo seclorum)
    :
    :

  • illuminatorilluminator Member Posts: 15
    Hi,

    use this function of D3DX Library if creating a mesh

    HRESULT D3DXCreateMeshFVF(
    DWORD NumFaces,
    DWORD NumVertices,
    DWORD Options,
    DWORD FVF,
    LPDIRECT3DDEVICE8 pDevice,
    LPD3DXMESH* ppMesh
    );

    or this if you have a mesh of a x-file without a FVF containing normals.

    HRESULT CloneMeshFVF(
    DWORD Options,
    DWORD FVF,
    LPDIRECT3DDEVICE8 pD3DDevice,
    LPD3DXMESH* ppCloneMesh
    );
    (found in ID3DXBaseMesh Interface)

    FVF must contain normals if using lighting.

    cu,

    illuminator
    (Novus ordo seclorum)

  • GozarGozarianGozarGozarian Member Posts: 17
    Thanks for the help. Looks like it was just the normals were missing from the FVF of the mesh. Once I added them and recomputed the normals everything was lit correctly.

    Rory

    : Hi,
    :
    : use this function of D3DX Library if creating a mesh
    :
    : HRESULT D3DXCreateMeshFVF(
    : DWORD NumFaces,
    : DWORD NumVertices,
    : DWORD Options,
    : DWORD FVF,
    : LPDIRECT3DDEVICE8 pDevice,
    : LPD3DXMESH* ppMesh
    : );
    :
    : or this if you have a mesh of a x-file without a FVF containing normals.
    :
    : HRESULT CloneMeshFVF(
    : DWORD Options,
    : DWORD FVF,
    : LPDIRECT3DDEVICE8 pD3DDevice,
    : LPD3DXMESH* ppCloneMesh
    : );
    : (found in ID3DXBaseMesh Interface)
    :
    : FVF must contain normals if using lighting.
    :
    : cu,
    :
    : illuminator
    : (Novus ordo seclorum)
    :
    :

Sign In or Register to comment.