實現畫茶壺,圓環,盒子,球體 (2)

露水上的青蛙發表於2013-08-01

主要程式碼如下:

宣告

//定義材質
D3DMATERIAL9 g_material;
//定義光源
D3DLIGHT9 g_light;

bool InitializeObjects()
{
 // Set the projection matrix.
 D3DXMatrixPerspectiveFovLH(&g_projection, 45.0f,
  WINDOW_WIDTH/WINDOW_HEIGHT, 0.1f, 1000.0f);

 g_D3DDevice->SetTransform(D3DTS_PROJECTION, &g_projection);

 // Set default rendering states.  啟動光照
 //g_D3DDevice->SetRenderState(D3DRS_LIGHTING, true);
 g_D3DDevice->SetRenderState(D3DRS_AMBIENT,
  D3DCOLOR_COLORVALUE(0.3f,0.3f,0.3f,1.0f));

 //Setup the light source設定光源屬性
 g_light.Type = D3DLIGHT_DIRECTIONAL; //型別為方向光
 g_light.Direction= D3DXVECTOR3(0.0f,0.0f,1.0f);//光在世界座標系中傳播方向的向量

 g_light.Diffuse.r = g_light.Diffuse.g = 1;// 該光源所發出的漫射光的顏色(rgba)
 g_light.Diffuse.b = g_light.Diffuse.a = 1;
 g_light.Specular.r = g_light.Specular.g = 1;// 該光源所發出的鏡面光的顏色(rgba)
 g_light.Specular.b = g_light.Specular.a = 1;

 g_D3DDevice->SetLight(0,&g_light);// 設定該光源
 g_D3DDevice->LightEnable(0,true);// 啟用該光源

 // Setup the material properties for the teapot.
 ZeroMemory(&g_material,sizeof(D3DMATERIAL9));// 將g_material物件的記憶體塊清零
 g_material.Diffuse.r = g_material.Ambient.r = 0.6f;
 g_material.Diffuse.g = g_material.Ambient.g = 0.6f;
 g_material.Diffuse.b = g_material.Ambient.b = 0.7f;
 g_material.Specular.r = 0.4f;
 g_material.Specular.g = 0.4f;
 g_material.Specular.b = 0.4f;
 g_material.Power = 8.0f;

 //Create the objects  建立茶壺
    if(FAILED(D3DXCreateTeapot(g_D3DDevice,&g_teapot,NULL)))
  return false;
    //建立盒子
 if(FAILED(D3DXCreateBox(g_D3DDevice,2,2,2,&g_cube,NULL)))
  return false;
 //建立球體
    if(FAILED(D3DXCreateSphere(g_D3DDevice,1.5,25,25,
  &g_sphere,NULL)))
  return false;
 //建立圓環
    if(FAILED(D3DXCreateTorus(g_D3DDevice,0.5f,1.2f,25,25,
  &g_torus,NULL)))
  return false;

 // Define camera information.
 D3DXVECTOR3 cameraPos(0.0f, 0.0f, -8.0f);//攝像機的位置
 D3DXVECTOR3 lookAtPos(0.0f, 0.0f, 0.0f); //觀察點
 D3DXVECTOR3 upDir(0.0f, 1.0f, 0.0f);     //以上方向為準

 // Build view matrix. 建立檢視矩陣
 D3DXMatrixLookAtLH(&g_ViewMatrix, &cameraPos,
  &lookAtPos, &upDir);

 return true;
}

void RenderScene()
{
 // Clear the back buffer.清空後臺快取為指定色
 g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
  D3DCOLOR_XRGB(0,0,0), 1.0f, 0);

 // Begin the scene.  Start rendering. 啟動繪製
 g_D3DDevice->BeginScene();

 // Apply the view (camera).從世界空間到檢視空間的檢視轉換
 g_D3DDevice->SetTransform(D3DTS_VIEW, &g_ViewMatrix);

 //Draw teapot
 D3DXMatrixTranslation(&g_WorldMatrix,2.0f,-2.0,0.0f);//平移矩陣
 g_D3DDevice->SetTransform(D3DTS_WORLD,&g_WorldMatrix);//從模型空間到世界空間的世界轉換
 g_D3DDevice->SetMaterial(&g_material);  // 設定材質
 g_teapot->DrawSubset(0); //繪製茶壺

 // Draw Cube.
 D3DXMatrixTranslation(&g_WorldMatrix, -2.0f, -2.0, 0.0f); // 平移矩陣
 g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix);   // 從模型空間到世界空間的世界轉換
 g_D3DDevice->SetMaterial(&g_material);  // 設定材質
 g_cube->DrawSubset(0);    // 繪製盒子

 // Draw Sphere.
 D3DXMatrixTranslation(&g_WorldMatrix, 2.0f, 2.0, 0.0f);   // 平移矩陣
 g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix);   // 從模型空間到世界空間的世界轉換
 g_D3DDevice->SetMaterial(&g_material);  // 設定材質
 g_sphere->DrawSubset(0);  // 繪製球體

 // Draw Torus.
 D3DXMatrixTranslation(&g_WorldMatrix, -2.0f, 2.0, 0.0f);  // 平移矩陣
 g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix);   // 從模型空間到世界空間的世界轉換
 g_D3DDevice->SetMaterial(&g_material);  // 設定材質
 g_torus->DrawSubset(0);   // 繪製圓環

 // End the scene.  Stop rendering.
 g_D3DDevice->EndScene();

 // Display the scene.
 g_D3DDevice->Present(NULL, NULL, NULL, NULL);
}

相關文章