创建物体

来源:互联网 发布:学校软件 编辑:程序博客网 时间:2024/05/01 11:47



HRESULT WINAPI D3DXCreateTeapot(  LPDIRECTD3DDEVICE9 pDevice,  LPD3DXMESH* ppMesh,  LPD3DXBUFFER* ppAdjacency  );    
    

       如同程序清单3.1所示,pDevice是Direct3D设备对象,ppMesh是一个指向LPD3DXMESH对象的指针,LPD3DXBUFFER是函数返回时用于存储三角形索引的数组。就目前而言,最后一个参数总设为NULL(空)。本书稍后将更详细地介绍三角形和三角形索引等内容。

       除了茶壶之外,还可以使用D3DXCreateSphere()函数创建球体,使用D3DXCreateBox()函数创建盒子,使用D3DXCreateTorus()函数创建圆环,使用D3DXCreateCylinder()函数创建圆柱体,使用D3DXCreatePolygon()函数创建多边形。一旦使用Direct3D函数之一创建了网格对象,那么所要做的全部工作就是渲染该形状,这只需调用LPD3DXMESH对象的DrawSubset()函数即可

HRESULT DrawSubset(DWORD AttribId);

       参数AttribId是想要绘制的索引。对本书而言,该参数总为0,因为创建的形状主要由一个网面构成。如果物体由多个网面组成,那么就可以使用索引绘制那个特定的网面。例如,如果有一个模型分为三个网面(一个头部用到的网面,一个是身体用到的网面,一个是腿部用到的网面),那么就可以对头部使用索引0,对身体使用索引1,对腿部使用索引2。在此,本书将不需要考虑这一点。当学习模型加载和动画时,也许会将X模型加载到由多个子集构成的Direct3D中。最有可能的是,本书将只包含一个子集,并且在调用该函数时,该参数值总设为0。



bool InitializeObjects(){   // 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 backbuffer.   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(){   // Release all resources.   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; }}


原创粉丝点击