实现画茶壶,圆环,盒子,球体

声明:

//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;
}

你可能感兴趣的:(实现)