Archived

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

Mike Lue

Lightening problem

Recommended Posts

I am tring to create a light effect on a 3D triangle.(The code below is what I have written) but the result when running this program is that I have only a black screen with nothing on it! Can anyone please to help to check my code! Thanks very much!(by the way, I basicly copy out the code from Microsoft Tutorial) //I initialize my d3d here: // Create the D3D object. if( NULL == ( m_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) ) return E_FAIL; // Get the current desktop display mode, so we can set up a back // buffer of the same format D3DDISPLAYMODE d3ddm; if( FAILED( m_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) ) return E_FAIL; // Set up the structure used to create the D3DDevice D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = d3ddm.Format; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16; // Create the D3DDevice if( FAILED( m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &m_pd3dDevice ) ) ) { return E_FAIL; } // Turn off culling m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); // Turn on the zbuffer m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE ); /////////////////////////////////////////////////////////// //I create my vertex buffer here: // Create the vertex buffer. if( FAILED( m_pd3dDevice->CreateVertexBuffer( 3*2*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &m_pVB ) ) ) { return E_FAIL; } // Fill the vertex buffer. We are algorithmically generating a cylinder // here, including the normals, which are used for lighting. CUSTOMVERTEX* pVertices; if( FAILED( m_pVB->Lock( 0, 0, (BYTE**)&pVertices, 0 ) ) ) return E_FAIL; pVertices[0].position = D3DXVECTOR3( -1.0f,-1.0f, -2.0f ); pVertices[0].normal = D3DXVECTOR3( 1.0f, -2.0f, 2.0f ); pVertices[1].position = D3DXVECTOR3( -1.0f,-1.0f, -2.0f ); pVertices[1].normal = D3DXVECTOR3( 0.0f, 2.0f, 0.0f ); pVertices[2].position = D3DXVECTOR3( -1.0f,-1.0f, -2.0f ); pVertices[2].normal = D3DXVECTOR3( 0.0f,0.0f,0.0f ); m_pVB->Unlock(); return S_OK; //////////////////////////////////////////////////////////////// //I setup my light here: // 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. D3DMATERIAL8 mtrl; ZeroMemory( &mtrl, sizeof(D3DMATERIAL8) ); mtrl.Diffuse.r = mtrl.Ambient.r = 1.0f; mtrl.Diffuse.g = mtrl.Ambient.g = 1.0f; mtrl.Diffuse.b = mtrl.Ambient.b = 1.0f; mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f; m_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; D3DLIGHT8 light; ZeroMemory( &light, sizeof(D3DLIGHT8) ); light.Type = D3DLIGHT_DIRECTIONAL; light.Diffuse.r = 1.0f; light.Diffuse.g = 1.0f; light.Diffuse.b = 1.0f; vecDir = D3DXVECTOR3(cosf(/*/timeGetTime()*/44/350.0f), 1.0f, sinf(timeGetTime()/350.0f) ); D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir ); light.Range = 1000.0f; m_pd3dDevice->SetLight( 0, &light ); m_pd3dDevice->LightEnable( 0, TRUE ); m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE ); // Finally, turn on some ambient light. m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x00202020 ); ////////////////////////////////////////////////////////////// //I render my scene here: // Clear the backbuffer to a black color m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 ); // Begin the scene m_pd3dDevice->BeginScene(); // Setup the world, view, and projection matrices SetupMatrix(); SetLight(); // Render the vertex buffer contents m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) ); m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX ); m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 ); // End the scene m_pd3dDevice->EndScene(); // Present the backbuffer contents to the display m_pd3dDevice->Present( NULL, NULL, NULL, NULL ); ///////////////////////////////////////////////////////////// Sorry about such a long code,but I really need your code to fix it because I am an absolute newbie to D3D. Thanks for your great help once again!!!!!!

Share this post


Link to post
Share on other sites
Create a new project and copy tutorial code into it.
Make sure this project works as intended.
Make your modifications one-by-one. After each, check that your program still works as intended.
When it stops working, undo the latest changes and make sure it works again.
There you''ll have the piece of code that''s causing you trouble.

Tip: if you only see a black screen, change clear color to red or something similar. This way you''ll know whether you are not drawing anything or your lights don''t light what you''ve drawn.

Share this post


Link to post
Share on other sites
why do the 3 vertices have the same position?
why do you create a 6-vertex VB and draw a 3-vertex triangle?
are your sure your light is pointing through the tri but not somewhere in Mars?

Share this post


Link to post
Share on other sites