DirectX lighting question

Started by
1 comment, last by Kazael 15 years, 5 months ago
I'm trying to get some basic lighting up for a school project and before I add it to my actual project I was testing stuff out in the direct X sample programs, and I'm getting an odd bug where the primitive gets lighting but the .x file ( the tiger from the samples ) dose not, even though the 2 objects are right on top of each other. here is some of the code maybe any of u can spot the cause in here other wise this code is from the basic mesh and basic lighting samples that come with the DirectX SDK. 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() / 300.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() ) ) { 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 ); } // Render the vertex buffer contents g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( g_pMesh ) ); g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 * 50 - 2 ); // End the scene g_pd3dDevice->EndScene(); } // Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); }
Advertisement
The mesh probably doesn't have any normals. See here for how to compute them.
Thank you for the fast reply, I haven't a chance to try it out yet but it sounds promising.

This topic is closed to new replies.

Advertisement