   lighting a mesh (everything is black)
  im trying to apply a lighting effect to a mesh in directx9.im following the directx tutorials that came with the february 2006 SDK.everything is fine until i try to add the SetupLights() function in my Render() function.  Everything becomes black(more like grey i can still see traces of the stripes from the tiger texture).  Even if i have ambient lighting.  Why does it show the model fine when there are no lights but it darkens when i try to add a light? here is my code:note: this isnt the full application im trying to write.  Im trying to do the same thing in my full blown application but i just threw this together so other things dont mess with my results and i can figure out whats goin on quicker.  Besides I dont think you guys want to have to sort through all of that anyways.  If it helps i can post the full code.#include <Windows.h>#include <mmsystem.h>#include <d3dx9.h>#pragma warning( disable : 4996 ) // disable deprecated warning #include <strsafe.h>#pragma warning( default : 4996 ) //-----------------------------------------------------------------------------// Global variables//-----------------------------------------------------------------------------LPDIRECT3D9   g_pD3D = NULL; // Used to create the D3DDeviceLPDIRECT3DDEVICE9  g_pd3dDevice= NULL; // Our rendering deviceLPD3DXMESH    g_pMesh= NULL; // Our mesh object in sysmemD3DMATERIAL9* g_pMeshMaterials = NULL; // Materials for our meshLPDIRECT3DTEXTURE9*g_pMeshTextures  = NULL; // Textures for our meshDWORD    g_dwNumMaterials = 0L;   // Number of mesh materials//#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1|D3DFVF_NORMAL)//#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL)//-----------------------------------------------------------------------------// Name: InitD3D()// Desc: Initializes Direct3D//-----------------------------------------------------------------------------HRESULT InitD3D( HWND hWnd ){    // Create the D3D object.    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )   return E_FAIL;    // Set up the structure used to create the D3DDevice. Since we are now    // using more complex geometry, we will create a device with a zbuffer.    D3DPRESENT_PARAMETERS d3dpp;ZeroMemory( &d3dpp, sizeof(d3dpp) );    d3dpp.Windowed = TRUE;    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;    d3dpp.EnableAutoDepthStencil = TRUE;    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;    // Create the D3DDevice    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,   D3DCREATE_SOFTWARE_VERTEXPROCESSING,   &d3dpp, &g_pd3dDevice ) ) )    {   return E_FAIL;    }// Turn off culling    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );// Turn off D3D lighting    //g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );    // Turn on the zbuffer    g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );    // Turn on ambient lightingg_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0xffffffff );    return S_OK;}//-----------------------------------------------------------------------------// Name: InitGeometry()// Desc: Load the mesh and build the material and texture arrays//-----------------------------------------------------------------------------HRESULT InitGeometry(){    LPD3DXBUFFER pD3DXMtrlBuffer;    // Load the mesh from the specified file    if( FAILED( D3DXLoadMeshFromX( "Tiger.x", D3DXMESH_SYSTEMMEM, g_pd3dDevice, NULL, &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials, &g_pMesh ) ) )    {   // If model is not in current folder, try parent folder   if( FAILED( D3DXLoadMeshFromX( "..\\Tiger.x", D3DXMESH_SYSTEMMEM,  g_pd3dDevice, NULL,  &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials,  &g_pMesh ) ) )   {  MessageBox(NULL, "Could not find tiger.x", "Meshes.exe", MB_OK);  return E_FAIL;   }    }    // We need to extract the material properties and texture names from the// pD3DXMtrlBuffer    D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();    g_pMeshMaterials = new D3DMATERIAL9[g_dwNumMaterials];    if( g_pMeshMaterials == NULL )   return E_OUTOFMEMORY;    g_pMeshTextures  = new LPDIRECT3DTEXTURE9[g_dwNumMaterials];    if( g_pMeshTextures == NULL )   return E_OUTOFMEMORY;    for( DWORD i=0; i<g_dwNumMaterials; i++ )    {   // Copy the material   g_pMeshMaterials = d3dxMaterials.MatD3D;   // Set the ambient color for the material (D3DX does not do this)   g_pMeshMaterials.Ambient = g_pMeshMaterials.Diffuse;   g_pMeshTextures = NULL;   if( d3dxMaterials.pTextureFilename != NULL &&   lstrlen(d3dxMaterials.pTextureFilename) > 0 )   {  // Create the texture  if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice,    d3dxMaterials.pTextureFilename,    &g_pMeshTextures ) ) )  { // If texture is not in current folder, try parent folder const TCHAR* strPrefix = TEXT("..\\"); TCHAR strTexture[MAX_PATH]; StringCchCopy( strTexture, MAX_PATH, strPrefix ); StringCchCat( strTexture, MAX_PATH, d3dxMaterials.pTextureFilename ); // If texture is not in current folder, try parent folder if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice,   strTexture,   &g_pMeshTextures ) ) ) {MessageBox(NULL, "Could not find texture map", "Meshes.exe", MB_OK); }  }   }    }    // Done with the material buffer    pD3DXMtrlBuffer->Release();    return S_OK;}//-----------------------------------------------------------------------------// Name: Cleanup()// Desc: Releases all previously initialized objects//-----------------------------------------------------------------------------VOID Cleanup(){    if( g_pMeshMaterials != NULL )    delete[] g_pMeshMaterials;    if( g_pMeshTextures )    {   for( DWORD i = 0; i < g_dwNumMaterials; i++ )   {  if( g_pMeshTextures ) g_pMeshTextures->Release();   }   delete[] g_pMeshTextures;    }    if( g_pMesh != NULL )   g_pMesh->Release();   if( g_pd3dDevice != NULL )   g_pd3dDevice->Release();    if( g_pD3D != NULL )   g_pD3D->Release();}//-----------------------------------------------------------------------------// Name: SetupMatrices()// Desc: Sets up the world, view, and projection transform matrices.//-----------------------------------------------------------------------------VOID SetupMatrices(){    // Set up world matrix    D3DXMATRIXA16 matWorld;    D3DXMatrixRotationY( &matWorld, timeGetTime()/1000.0f );    g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );    // Set up our view matrix. A view matrix can be defined given an eye point,    // a point to lookat, and a direction for which way is up. Here, we set the    // eye five units back along the z-axis and up three units, look at the// origin, and define "up" to be in the y-direction.    D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );    D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );    D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );    D3DXMATRIXA16 matView;    D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );    g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );    // For the projection matrix, we set up a perspective transform (which    // transforms geometry from 3D view space to 2D viewport space, with    // a perspective divide making objects smaller in the distance). To build    // a perpsective transform, we need the field of view (1/4 pi is common),    // the aspect ratio, and the near and far clipping planes (which define at    // what distances geometry should be no longer be rendered).    D3DXMATRIXA16 matProj;    D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );}//-----------------------------------------------------------------------------// Name: SetupLights()// Desc: Sets up the 04Lights and materials for the scene.//-----------------------------------------------------------------------------VOID SetupLights(){    // Set up a material. The material here just has the diffuse and ambient    // colors set to yellow. Note that only one material can be used at a time.    D3DMATERIAL9 mtrl;    ZeroMemory( &mtrl, sizeof(D3DMATERIAL9) );    mtrl.Diffuse.r = mtrl.Ambient.r = 1.0f;    mtrl.Diffuse.g = mtrl.Ambient.g = 1.0f;    mtrl.Diffuse.b = mtrl.Ambient.b = 0.0f;    mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;    g_pd3dDevice->SetMaterial( &mtrl );    // Set up a white, directional light, with an oscillating direction.    // Note that many Lights may be active at a time (but each one slows down    // the rendering of our scene). However, here we are just using one. Also,    // we need to set the D3DRS_LIGHTING renderstate to enable lighting    D3DXVECTOR3 vecDir;    D3DLIGHT9 light;    ZeroMemory( &light, sizeof(D3DLIGHT9) );    light.Type  = D3DLIGHT_DIRECTIONAL;    light.Diffuse.r  = 1.0f;    light.Diffuse.g  = 1.0f;    light.Diffuse.b  = 1.0f;    vecDir = D3DXVECTOR3(cosf(timeGetTime()/350.0f),1.0f,sinf(timeGetTime()/350.0f) );    D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir );    light.Range  = 1000.0f;    g_pd3dDevice->SetLight( 0, &light );    g_pd3dDevice->LightEnable( 0, TRUE );    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );    // Finally, turn on some ambient light.    g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x00202020 );}//-----------------------------------------------------------------------------// Name: Render()// Desc: Draws the scene//-----------------------------------------------------------------------------VOID Render(){    // Clear the backbuffer and the zbuffer    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );   // Begin the scene    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )    {// Setup the Lights and materials   //SetupLights();   // Setup the world, view, and projection matrices   SetupMatrices();   // Meshes are divided into subsets, one for each material. Render them in   // a loop   for( DWORD i=0; i<g_dwNumMaterials; i++ )   {  // Set the material and texture for this subset  g_pd3dDevice->SetMaterial( &g_pMeshMaterials );  g_pd3dDevice->SetTexture( 0, g_pMeshTextures );// Draw the mesh subset  g_pMesh->DrawSubset( i );   }   // End the scene   g_pd3dDevice->EndScene();    }    // Present the backbuffer contents to the display    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );}//-----------------------------------------------------------------------------// Name: MsgProc()// Desc: The window's message handler//-----------------------------------------------------------------------------LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){    switch( msg )    {   case WM_DESTROY:  Cleanup();  PostQuitMessage( 0 );  return 0;    }    return DefWindowProc( hWnd, msg, wParam, lParam );}//-----------------------------------------------------------------------------// Name: WinMain()// Desc: The application's entry point//-----------------------------------------------------------------------------INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ){    // Register the window class    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,   GetModuleHandle(NULL), NULL, NULL, NULL, NULL,  "D3D Tutorial", NULL };    RegisterClassEx( &wc );    // Create the application's window    HWND hWnd = CreateWindow( "D3D Tutorial", "Julius is a GAWD", WS_OVERLAPPEDWINDOW, 100, 100, 600, 800,NULL, NULL, wc.hInstance, NULL );    // Initialize Direct3D    if( SUCCEEDED( InitD3D( hWnd ) ) )    {    // Create the scene geometry   if( SUCCEEDED( InitGeometry() ) )   {  // Show the window  ShowWindow( hWnd, SW_SHOWDEFAULT );  UpdateWindow( hWnd );  // Enter the message loop  MSG msg;   ZeroMemory( &msg, sizeof(msg) );  while( msg.message!=WM_QUIT )  { if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) {TranslateMessage( &msg );DispatchMessage( &msg ); } elseRender();  }   }    }    UnregisterClass( "D3D Tutorial", wc.hInstance );    return 0;}
Quote:Why does it show the model fine when there are no lights but it darkens when i try to add a light? Because without the light it just uses the vertex colours, but by turning on the light you allow it to be affected by the actual lighting calculation, i.e. it could be far away from the light hence it would be darker,Paul   ;  well after looking through the code i think the light should be hitting the tiger mesh...vecDir = D3DXVECTOR3(cosf(timeGetTime()/350.0f),1.0f,sinf(timeGetTime()/350.0f) );It should be spinning everywhere.  Unless it is out of range or inside the tiger it should light it up.  At least a little.  And even then i turn on ambient light.   ;  Tiger.x does not have any normals specified. If you load Teapot.x from the \Media\misc folder you will see that the lighting works fine. If you still would like to use Tiger.x you have to clone the mesh and compute normals:g_pMesh->CloneMeshFVF(D3DXMESH_MANAGED, D3DFVF_XYZ|D3DFVF_NORMAL, g_pd3dDevice, &g_pNewMesh);D3DXComputeNormals(g_pNewMesh, NULL);