宣告:
//Mesh objects 網格物件
LPD3DXMESH g_teapot=NULL;
LPD3DXMESH g_cube=NULL;
LPD3DXMESH g_sphere=NULL;//球體
LPD3DXMESH g_torus=NULL;//圓環
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, FALSE);
//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_teapot->DrawSubset(0); //繪製茶壺
// Draw Cube.
D3DXMatrixTranslation(&g_WorldMatrix, -2.0f, -2.0, 0.0f); // 平移矩陣
g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix); // 從模型空間到世界空間的世界轉換
g_cube->DrawSubset(0); // 繪製盒子
// Draw Sphere.
D3DXMatrixTranslation(&g_WorldMatrix, 2.0f, 2.0, 0.0f); // 平移矩陣
g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix); // 從模型空間到世界空間的世界轉換
g_sphere->DrawSubset(0); // 繪製球體
// Draw Torus.
D3DXMatrixTranslation(&g_WorldMatrix, -2.0f, 2.0, 0.0f); // 平移矩陣
g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix); // 從模型空間到世界空間的世界轉換
g_torus->DrawSubset(0); // 繪製圓環
// End the scene. Stop rendering.
g_D3DDevice->EndScene();
// Display the scene.
g_D3DDevice->Present(NULL, NULL, NULL, NULL);
}
void Shutdown()
{
if(g_D3DDevice != NULL) g_D3DDevice->Release();
if(g_D3D != NULL) g_D3D->Release();
if(g_teapot != NULL) { g_teapot->Release(); g_teapot = NULL; }
if(g_cube != NULL) { g_cube->Release(); g_cube = NULL; }
if(g_sphere != NULL) { g_sphere->Release(); g_sphere = NULL; }
if(g_torus != NULL) { g_torus->Release(); g_torus = NULL; }
// g_D3DDevice = NULL;
//g_D3D = NULL;
}