Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 14th, 2012  |  syntax: None  |  size: 17.09 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. DirectX9 Lighting C   Lighting not working
  2. #include "DirectXGame.h"
  3.  
  4. DirectXGame::DirectXGame(void)
  5. {
  6.     //init to zero good pratice
  7.  
  8.     m_pD3DObject=0;
  9.     m_pD3DDevice=0;
  10.     m_currTime=0;
  11.     m_prevTime=0;
  12.  
  13.     ZeroMemory(&m_D3Dpp,sizeof(m_D3Dpp));
  14.     FOV=D3DXToRadian(65.0f);
  15.     aspectRatio=800/600;
  16.     nearPlane=1.0f;
  17.     farPlane=1000.0f;
  18. }
  19.  
  20. DirectXGame::~DirectXGame(void)
  21. {
  22. }
  23.  
  24. //create class for input
  25. DirectXInput DXClass;
  26. void DirectXGame::Initialize(HWND hWnd ,HINSTANCE hInst,bool bWindowed) // create the material struct)//
  27. {
  28.     // grab the window width and height fronm the HWND
  29.     RECT r;
  30.     GetWindowRect(hWnd, &r);
  31.     m_nWidth = r.right - r.left;
  32.     m_nHeight = r.bottom - r.top;
  33.     m_bVSync = false;
  34.  
  35.     // Create the D3D Object
  36.     m_pD3DObject = Direct3DCreate9(D3D_SDK_VERSION);
  37.  
  38.     // Create our presentation parameters for our D3D Device
  39.     m_D3Dpp.hDeviceWindow = hWnd; // Handle to the window
  40.     m_D3Dpp.Windowed = bWindowed; // Windowed or Full-screen?
  41.     m_D3Dpp.BackBufferCount = 1; // Number of back-buffers
  42.     m_D3Dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // Back-buffer pixel format
  43.     m_D3Dpp.BackBufferWidth = m_nWidth; // Back-buffer width
  44.     m_D3Dpp.BackBufferHeight = m_nHeight; // Back-buffer height
  45.     m_D3Dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // Swap effectm_bVSync ? D3DPRESENT_INTERVAL_DEFAULT :
  46.     m_D3Dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  47.     m_D3Dpp.FullScreen_RefreshRateInHz = bWindowed ? 0 : D3DPRESENT_RATE_DEFAULT;
  48.     m_D3Dpp.EnableAutoDepthStencil = TRUE; // Enable depth and stencil buffer
  49.     m_D3Dpp.AutoDepthStencilFormat = D3DFMT_D24S8; // Depth/Stencil buffer bit format
  50.     m_D3Dpp.Flags = D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL; // Discard the depth/stencil buffer upon Present()
  51.     m_D3Dpp.MultiSampleQuality = 0; // MSAA quality
  52.     m_D3Dpp.MultiSampleType = D3DMULTISAMPLE_NONE; // MSAA type
  53.  
  54.     // Check the device's capabilities
  55.     DWORD deviceBehaviorFlags = 0;
  56.     m_pD3DObject->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &m_D3DCaps);
  57.  
  58.     // Determine vertex processing mode
  59.     if(m_D3DCaps.DevCaps & D3DCREATE_HARDWARE_VERTEXPROCESSING)
  60.     {
  61.         deviceBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
  62.     }
  63.     else
  64.     {
  65.         deviceBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  66.     }
  67.  
  68.     // if hardware vertex processing is on, check for pure
  69.     if(m_D3DCaps.DevCaps & D3DCREATE_PUREDEVICE && deviceBehaviorFlags & D3DCREATE_HARDWARE_VERTEXPROCESSING)
  70.     {
  71.         deviceBehaviorFlags |= D3DCREATE_PUREDEVICE;
  72.     }
  73.  
  74.     // Create D3D Device
  75.     m_pD3DObject->CreateDevice(
  76.         D3DADAPTER_DEFAULT, // Default display adapter
  77.         D3DDEVTYPE_HAL, // Device type to use
  78.         hWnd, // Handle to our window
  79.         deviceBehaviorFlags, // D3DCREATE_HARDWARE_VERTEXPROCESSINGVertex Processing Behavior Flags (PUREDEVICE, HARDWARE_VERTEXPROCESSING, SOFTWARE_VERTEXPROCESSING)
  80.         &m_D3Dpp, // Presentation parameters
  81.         &m_pD3DDevice); // Return a created D3D Device
  82.  
  83.     //=================================================================//
  84.     // Create/Load Sprite & Font D3D and COM objects
  85.  
  86.     //Create font
  87.     D3DXCreateFont(m_pD3DDevice, 23, 0, FW_BOLD, 0, true,
  88.         DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
  89.         DEFAULT_PITCH | FF_DONTCARE, TEXT("Times New Roman"),
  90.         &m_pD3DFont);
  91.  
  92.     //for font 1 "Name"
  93.     RECT rct;
  94.     rct.left=2;
  95.     rct.right=780;
  96.     rct.top=10;
  97.     rct.bottom=rct.top+20;
  98.  
  99.     //MATRIX
  100.     eyePos.x = 0;
  101.     eyePos.y = 2;
  102.     eyePos.z = -10;
  103.     lookAt.x = 0;
  104.     lookAt.y = 0;
  105.     lookAt.z = 0;
  106.     upVec.x = 0;
  107.     upVec.y = 1;
  108.     upVec.z = 0;
  109.  
  110.     D3DXMatrixLookAtLH( &view_Matrix, &eyePos, &lookAt, &upVec);
  111.  
  112.     m_pD3DDevice->SetTransform( D3DTS_VIEW, &view_Matrix);
  113.  
  114.     // projection matrix
  115.     D3DXMatrixPerspectiveFovLH( &pro_Matrix, FOV, aspectRatio, nearPlane, farPlane);
  116.     m_pD3DDevice->SetTransform( D3DTS_PROJECTION, &pro_Matrix);
  117.  
  118.     // MATRIX: VertexElement Declaration
  119.     D3DVERTEXELEMENT9 declaration[] =
  120.     {
  121.         {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
  122.         {0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
  123.         {0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
  124.         D3DDECL_END()
  125.     };
  126.  
  127.     //LPDIRECT3DVERTEXDECLARATION9 m_pVtxDeclObject;
  128.     // Create vertex declaration
  129.     m_pD3DDevice->CreateVertexDeclaration(declaration,&vertDec);
  130.  
  131.     ///---CUBE: Vertex and Indicies :START---///
  132.     // Load vertex info, listed per cube face quads
  133.     // Front
  134.     m_cubeVerts[0].position = D3DXVECTOR3(-1.0f, -1.0f, -1.0f);
  135.     m_cubeVerts[1].position = D3DXVECTOR3(-1.0f, 1.0f, -1.0f);
  136.     m_cubeVerts[2].position = D3DXVECTOR3(1.0f, 1.0f, -1.0f);
  137.     m_cubeVerts[3].position = D3DXVECTOR3(1.0f, -1.0f, -1.0f);
  138.     D3DXVec3Normalize(&m_cubeVerts[0].normal, &D3DXVECTOR3(0.0f, 0.0f, -1.0f));
  139.     D3DXVec3Normalize(&m_cubeVerts[1].normal, &D3DXVECTOR3(0.0f, 0.0f, -1.0f));
  140.     D3DXVec3Normalize(&m_cubeVerts[2].normal, &D3DXVECTOR3(0.0f, 0.0f, -1.0f));
  141.     D3DXVec3Normalize(&m_cubeVerts[3].normal, &D3DXVECTOR3(0.0f, 0.0f, -1.0f));
  142.     m_cubeVerts[0].uv = D3DXVECTOR2(0.0f, 1.0f);
  143.     m_cubeVerts[1].uv = D3DXVECTOR2(0.0f, 0.0f);
  144.     m_cubeVerts[2].uv = D3DXVECTOR2(1.0f, 0.0f);
  145.     m_cubeVerts[3].uv = D3DXVECTOR2(1.0f, 1.0f);
  146.  
  147.     // Back
  148.     m_cubeVerts[4].position = D3DXVECTOR3(-1.0f, -1.0f, 1.0f);
  149.     m_cubeVerts[5].position = D3DXVECTOR3(1.0f, -1.0f, 1.0f);
  150.     m_cubeVerts[6].position = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
  151.     m_cubeVerts[7].position = D3DXVECTOR3(-1.0f, 1.0f, 1.0f);
  152.     D3DXVec3Normalize(&m_cubeVerts[4].normal, &D3DXVECTOR3(0.0f, 0.0f, 1.0f));
  153.     D3DXVec3Normalize(&m_cubeVerts[5].normal, &D3DXVECTOR3(0.0f, 0.0f, 1.0f));
  154.     D3DXVec3Normalize(&m_cubeVerts[6].normal, &D3DXVECTOR3(0.0f, 0.0f, 1.0f));
  155.     D3DXVec3Normalize(&m_cubeVerts[7].normal, &D3DXVECTOR3(0.0f, 0.0f, 1.0f));
  156.     m_cubeVerts[4].uv = D3DXVECTOR2(1.0f, 1.0f);
  157.     m_cubeVerts[5].uv = D3DXVECTOR2(0.0f, 1.0f);
  158.     m_cubeVerts[6].uv = D3DXVECTOR2(0.0f, 0.0f);
  159.     m_cubeVerts[7].uv = D3DXVECTOR2(1.0f, 0.0f);
  160.  
  161.     // Top
  162.     m_cubeVerts[8].position = D3DXVECTOR3(-1.0f, 1.0f, -1.0f);
  163.     m_cubeVerts[9].position = D3DXVECTOR3(-1.0f, 1.0f, 1.0f);
  164.     m_cubeVerts[10].position = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
  165.     m_cubeVerts[11].position = D3DXVECTOR3(1.0f, 1.0f, -1.0f);
  166.     D3DXVec3Normalize(&m_cubeVerts[8].normal, &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  167.     D3DXVec3Normalize(&m_cubeVerts[9].normal, &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  168.     D3DXVec3Normalize(&m_cubeVerts[10].normal, &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  169.     D3DXVec3Normalize(&m_cubeVerts[11].normal, &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  170.     m_cubeVerts[8].uv = D3DXVECTOR2(0.0f, 1.0f);
  171.     m_cubeVerts[9].uv = D3DXVECTOR2(0.0f, 0.0f);
  172.     m_cubeVerts[10].uv = D3DXVECTOR2(1.0f, 0.0f);
  173.     m_cubeVerts[11].uv = D3DXVECTOR2(1.0f, 1.0f);
  174.  
  175.     // Bottom
  176.     m_cubeVerts[12].position = D3DXVECTOR3(-1.0f, -1.0f, -1.0f);
  177.     m_cubeVerts[13].position = D3DXVECTOR3(1.0f, -1.0f, -1.0f);
  178.     m_cubeVerts[14].position = D3DXVECTOR3(1.0f, -1.0f, 1.0f);
  179.     m_cubeVerts[15].position = D3DXVECTOR3(-1.0f, -1.0f, 1.0f);
  180.     D3DXVec3Normalize(&m_cubeVerts[12].normal, &D3DXVECTOR3(0.0f, -1.0f, 0.0f));
  181.     D3DXVec3Normalize(&m_cubeVerts[13].normal, &D3DXVECTOR3(0.0f, -1.0f, 0.0f));
  182.     D3DXVec3Normalize(&m_cubeVerts[14].normal, &D3DXVECTOR3(0.0f, -1.0f, 0.0f));
  183.     D3DXVec3Normalize(&m_cubeVerts[15].normal, &D3DXVECTOR3(0.0f, -1.0f, 0.0f));
  184.     m_cubeVerts[12].uv = D3DXVECTOR2(1.0f, 1.0f);
  185.     m_cubeVerts[13].uv = D3DXVECTOR2(0.0f, 1.0f);
  186.     m_cubeVerts[14].uv = D3DXVECTOR2(0.0f, 0.0f);
  187.     m_cubeVerts[15].uv = D3DXVECTOR2(1.0f, 0.0f);
  188.  
  189.     // Left
  190.     m_cubeVerts[16].position = D3DXVECTOR3(-1.0f, -1.0f, 1.0f);
  191.     m_cubeVerts[17].position = D3DXVECTOR3(-1.0f, 1.0f, 1.0f);
  192.     m_cubeVerts[18].position = D3DXVECTOR3(-1.0f, 1.0f, -1.0f);
  193.     m_cubeVerts[19].position = D3DXVECTOR3(-1.0f, -1.0f, -1.0f);
  194.     D3DXVec3Normalize(&m_cubeVerts[16].normal, &D3DXVECTOR3(-1.0f, 0.0f, 0.0f));
  195.     D3DXVec3Normalize(&m_cubeVerts[17].normal, &D3DXVECTOR3(-1.0f, 0.0f, 0.0f));
  196.     D3DXVec3Normalize(&m_cubeVerts[18].normal, &D3DXVECTOR3(-1.0f, 0.0f, 0.0f));
  197.     D3DXVec3Normalize(&m_cubeVerts[19].normal, &D3DXVECTOR3(-1.0f, 0.0f, 0.0f));
  198.     m_cubeVerts[16].uv = D3DXVECTOR2(0.0f, 1.0f);
  199.     m_cubeVerts[17].uv = D3DXVECTOR2(0.0f, 0.0f);
  200.     m_cubeVerts[18].uv = D3DXVECTOR2(1.0f, 0.0f);
  201.     m_cubeVerts[19].uv = D3DXVECTOR2(1.0f, 1.0f);
  202.  
  203.     // Right
  204.     m_cubeVerts[20].position = D3DXVECTOR3(1.0f, -1.0f, -1.0f);
  205.     m_cubeVerts[21].position = D3DXVECTOR3(1.0f, 1.0f, -1.0f);
  206.     m_cubeVerts[22].position = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
  207.     m_cubeVerts[23].position = D3DXVECTOR3(1.0f, -1.0f, 1.0f);
  208.     D3DXVec3Normalize(&m_cubeVerts[20].normal, &D3DXVECTOR3(1.0f, 0.0f, 0.0f));
  209.     D3DXVec3Normalize(&m_cubeVerts[21].normal, &D3DXVECTOR3(1.0f, 0.0f, 0.0f));
  210.     D3DXVec3Normalize(&m_cubeVerts[22].normal, &D3DXVECTOR3(1.0f, 0.0f, 0.0f));
  211.     D3DXVec3Normalize(&m_cubeVerts[23].normal, &D3DXVECTOR3(1.0f, 0.0f, 0.0f));
  212.     m_cubeVerts[20].uv = D3DXVECTOR2(0.0f, 1.0f);
  213.     m_cubeVerts[21].uv = D3DXVECTOR2(0.0f, 0.0f);
  214.     m_cubeVerts[22].uv = D3DXVECTOR2(1.0f, 0.0f);
  215.     m_cubeVerts[23].uv = D3DXVECTOR2(1.0f, 1.0f);
  216.  
  217.     // Load index info, refers into index into verts array to compose triangles
  218.     // Note: A clockwise winding order of verts will show the front face.
  219.  
  220.     // Front
  221.     m_cubeIndices[0] = 0; m_cubeIndices[1] = 1; m_cubeIndices[2] = 2; // Triangle 0
  222.     m_cubeIndices[3] = 0; m_cubeIndices[4] = 2; m_cubeIndices[5] = 3; // Triangle 1
  223.  
  224.     // Back
  225.     m_cubeIndices[6] = 4; m_cubeIndices[7] = 5; m_cubeIndices[8] = 6; // Triangle 2
  226.     m_cubeIndices[9] = 4; m_cubeIndices[10] = 6; m_cubeIndices[11] = 7; // Triangle 3
  227.  
  228.     // Top
  229.     m_cubeIndices[12] = 8; m_cubeIndices[13] = 9; m_cubeIndices[14] = 10; // Triangle 4
  230.     m_cubeIndices[15] = 8; m_cubeIndices[16] = 10; m_cubeIndices[17] = 11; // Triangle 5
  231.  
  232.     // Bottom
  233.     m_cubeIndices[18] = 12; m_cubeIndices[19] = 13; m_cubeIndices[20] = 14; // Triangle 6
  234.     m_cubeIndices[21] = 12; m_cubeIndices[22] = 14; m_cubeIndices[23] = 15; // Triangle 7
  235.  
  236.     // Left
  237.     m_cubeIndices[24] = 16; m_cubeIndices[25] = 17; m_cubeIndices[26] = 18; // Triangle 8
  238.     m_cubeIndices[27] = 16; m_cubeIndices[28] = 18; m_cubeIndices[29] = 19; // Triangle 9
  239.  
  240.     // Right
  241.     m_cubeIndices[30] = 20; m_cubeIndices[31] = 21; m_cubeIndices[32] = 22; // Triangle 10
  242.     m_cubeIndices[33] = 20; m_cubeIndices[34] = 22; m_cubeIndices[35] = 23; // Triangle 11
  243.     ///---CUBE: Vertex and Indicies :END---///
  244.  
  245.     //create buffers
  246.     // create a vertex buffer interface called i_buffer
  247.     m_pD3DDevice->CreateVertexBuffer(4*6*sizeof(Vertex),
  248.         D3DUSAGE_WRITEONLY,
  249.         0,
  250.         D3DPOOL_MANAGED,
  251.         &VB,
  252.         NULL);
  253.  
  254.     void* pVertices;
  255.     // lock VB and load the vertices into it
  256.     VB->Lock(0, 0,&pVertices, 0);
  257.  
  258.     // send array
  259.     memcpy(pVertices, m_cubeVerts, 4*6*sizeof(Vertex));
  260.  
  261.     //unlock
  262.     VB->Unlock();
  263.  
  264.     ///////////////////////////////////////////////////
  265.  
  266.     m_pD3DDevice->CreateIndexBuffer(3*12*sizeof(WORD), // 3 and 12
  267.         D3DUSAGE_WRITEONLY,
  268.         D3DFMT_INDEX16,
  269.         D3DPOOL_MANAGED,
  270.         &IB,
  271.         NULL);
  272.  
  273.     void* pIndices;
  274.     // lock index
  275.     IB->Lock(0,0,&pIndices,0);
  276.     //send array of indices to vram
  277.     memcpy(pIndices, m_cubeIndices, 3*12*sizeof(WORD));
  278.  
  279.     //unlock index
  280.     IB->Unlock();
  281.  
  282.     D3DLIGHT9 light;
  283.     D3DMATERIAL9 m_Mat;
  284.  
  285.     ZeroMemory(&light, sizeof(light));
  286.     // clear out the light struct for use
  287.     light.Type = D3DLIGHT_POINT;
  288.     // make the light type 'directional light'
  289.     light.Diffuse.r = 0.5f;
  290.     light.Diffuse.g = 0.5f;
  291.     light.Diffuse.b = 0.5f;
  292.     light.Diffuse.a = 1.0f;
  293.     light.Ambient.r = 0.2f;
  294.     light.Ambient.g = 0.2f;
  295.     light.Ambient.b = 1.0f;
  296.     light.Specular.r = 1.0f;
  297.     light.Specular.g = 1.0f;
  298.     light.Specular.b = 1.0f;
  299.     // set the lighting position
  300.     light.Position.x = 30;
  301.     light.Position.y = 10;
  302.     light.Position.z = -10;
  303.     light.Range = 900.0f;
  304.     light.Attenuation0 = 0.0f;
  305.     light.Attenuation1 = 0.125f;
  306.     light.Attenuation2 = 0.0f;
  307.     m_pD3DDevice->SetLight(0, &light);
  308.     // send the light struct properties to light #0
  309.     m_pD3DDevice->LightEnable(0, TRUE);
  310.     // turn on light #0
  311.     ZeroMemory(&m_Mat, sizeof(m_Mat));
  312.     // clear out the struct for use
  313.     m_Mat.Diffuse.r = 1.0f;
  314.     m_Mat.Diffuse.g = 0.0f;
  315.     m_Mat.Diffuse.b = 0.0f;
  316.     m_Mat.Diffuse.a = 0.0f;
  317.     // set diffuse color to white
  318.     m_Mat.Ambient.r = 0.2f;
  319.     m_Mat.Ambient.g = 0.2f;
  320.     m_Mat.Ambient.b = 0.2f;
  321.     m_Mat.Ambient.a = 1.0f;
  322.     // set ambient color to white
  323.     m_Mat.Specular.r = 1.0f;
  324.     m_Mat.Specular.g =1.0f;
  325.     m_Mat.Specular.b =1.0f;
  326.     m_Mat.Specular.a =1.0f;
  327.     m_Mat.Power = 100.0f;
  328.  
  329.     m_pD3DDevice->SetMaterial(&m_Mat);
  330.  
  331.     // Create a texture using the with "test.tga" from the sprite labs.
  332.     //Apply tri-linear filtering to the texture, by modifying the sampler states by calling the device's SetSamplerState().
  333.     D3DXCreateTextureFromFile(m_pD3DDevice, L"test.png",&m_Texture);
  334.  
  335.     //Apply tri-linear filtering to the texture, by modifying the sampler states by calling the device's SetSamplerState().
  336.  
  337.     m_pD3DDevice->SetSamplerState( 0,D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  338.     m_pD3DDevice->SetSamplerState( 0,D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
  339.     m_pD3DDevice->SetSamplerState( 0,D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  340. }
  341.  
  342. void DirectXGame::Update(HWND hWnd, bool& bWindowed, double dt)
  343. {
  344. }
  345.  
  346. void DirectXGame::Render()
  347. {
  348.     // if our d3d device was not craeted return
  349.     if(!m_pD3DDevice)
  350.         return;
  351.  
  352.     m_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
  353.     D3DCOLOR_XRGB(100,149,237), 1, 0); // 100,149,237 00255
  354.  
  355.     // Begin the scene
  356.     if( SUCCEEDED( m_pD3DDevice->BeginScene() ) )
  357.     {
  358.         m_pD3DDevice->SetVertexDeclaration( vertDec);
  359.  
  360.         //set cube postion
  361.         D3DXMATRIX translation, rotation, scale, world, position;
  362.         float index = 0.0f; index+=0.05f;
  363.         D3DXMatrixTranslation(&translation,0,0,0);
  364.         D3DXMatrixRotationY(&rotation, timeGetTime()/1000);
  365.         D3DXMatrixScaling(&scale,1,1, 1.0f);
  366.  
  367.         world = scale*rotation*translation;
  368.  
  369.         m_pD3DDevice->SetTransform(D3DTS_WORLD, &world);
  370.  
  371.         // select the vertex and index buffers to use
  372.         m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
  373.         m_pD3DDevice->SetRenderState( D3DRS_SPECULARENABLE, TRUE );
  374.         m_pD3DDevice->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_XRGB(60,60,60));
  375.         m_pD3DDevice->SetStreamSource(0, VB, 0, sizeof(Vertex));
  376.         m_pD3DDevice->SetIndices(IB);
  377.         m_pD3DDevice->SetMaterial(&m_Mat);
  378.         m_pD3DDevice->SetTexture( 0,m_Texture);
  379.  
  380.         m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,//D3DPRIMITIVETYPE Type
  381.             0,/*BaseVertexIndex*/
  382.             0, //* MinIndex*/
  383.             24, /*NumVertices*/
  384.             0, /*StartIndex*/
  385.             12);//PrimitiveCount
  386.  
  387.         ///////////////////////////////////////////////////////////////////////////
  388.  
  389.         //Font 1 "Name"
  390.         RECT rct;
  391.         rct.bottom = m_nHeight;
  392.         rct.top=2;
  393.         rct.left=658;
  394.         rct.right=m_nWidth;
  395.  
  396.         //for the font2 FPS
  397.         RECT rect;
  398.         rect.bottom = m_nHeight;
  399.         rect.top =2;
  400.         rect.left = 10;
  401.         rect.right = m_nWidth;
  402.  
  403.         wchar_t buffer[64];
  404.         swprintf_s(buffer, 64,L"Frames Per Second: %d", m_FPS);
  405.         m_pD3DFont->DrawText(0, buffer, -1, &rect, DT_TOP | DT_NOCLIP, D3DCOLOR_ARGB(255,255,255,255));
  406.  
  407.         // Create a colour for the text ( blue)
  408.         D3DCOLOR fontColor = D3DCOLOR_ARGB(255,0,0,255);
  409.  
  410.         //draw the text
  411.         m_pD3DFont->DrawText(NULL, L"Mariana Serrato", -1, &rct, 0, fontColor ); // move test to far right
  412.  
  413.         // End the scene
  414.         m_pD3DDevice->EndScene();
  415.  
  416.         // present the back buffer to the screen
  417.         m_pD3DDevice->Present(NULL, NULL, NULL, NULL);
  418.  
  419.         //calculate Frames Per Second
  420.         m_currTime = timeGetTime();
  421.         static int fspCounter = 0;
  422.         if(m_currTime - m_prevTime >= 1000.0f)
  423.         {
  424.             m_prevTime = m_currTime;
  425.             m_FPS = fspCounter;
  426.             fspCounter =0;
  427.         } else {
  428.             ++fspCounter;
  429.         }
  430.     }
  431. }
  432.  
  433. void DirectXGame::Shutdown()
  434. {
  435.     SAFE_RELEASE(VB);
  436.     SAFE_RELEASE(IB);
  437.     SAFE_RELEASE(vertDec);
  438.     SAFE_RELEASE(m_pD3DFont);
  439.     SAFE_RELEASE(m_Texture);
  440.     SAFE_RELEASE(m_Sprite);
  441.     SAFE_RELEASE(m_pD3DDevice);
  442.     SAFE_RELEASE(m_pD3DObject);
  443. }
  444.        
  445. D3DPRESENT_PARAMETERS   m_D3Dpp;
  446. ID3DXFont*              m_pD3DFont; //Font
  447. LPDIRECT3DTEXTURE9      m_Texture;
  448. ID3DXSprite*            m_Sprite;//Sprite
  449. D3DXVECTOR3             texCenter,eyePos,lookAt,upVec;
  450. D3DCAPS9                m_D3DCaps; //D3D Device Caps
  451. D3DLIGHT9               m_Light;
  452. D3DMATERIAL9            m_Mat;
  453. D3DVERTEXELEMENT9       m_Element;
  454. D3DXMATRIX              view_Matrix, pro_Matrix;
  455.  
  456. // cube
  457. Vertex m_cubeVerts[24];
  458. WORD m_cubeIndices[36];
  459. IDirect3DVertexBuffer9* VB;
  460. IDirect3DIndexBuffer9* IB;  
  461. IDirect3DVertexDeclaration9* vertDec;
  462.  
  463. bool m_bVSync;
  464. char buffer [256];
  465. int m_nWidth, m_nHeight;
  466. int m_FPS,alfa[5],mFPS,mMilliSecPerFrame;
  467. float m_currTime,m_prevTime,FOV, aspectRatio, nearPlane, farPlane;
  468. DWORD D3DUSASE_DYNAMIC,D3DUSASE_WRITEONLY;
  469.        
  470. m_pD3DDevice->setRenderState(D3DRS_LIGHTING, TRUE);