创建物体
2013-08-17 18:06
267 查看
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; } }
相关文章推荐
- 怎样用ZBrush中的shadowbox创建物体
- 【学习17】Box2D创建物体和碰撞检测
- Unity3D 学习教程 4 创建物体
- ZBrush中的shadowbox该怎么创建物体
- 在三维空间里创建10个物体,并可以自由缩放,移动视角
- Unity 鼠标点击屏幕创建物体
- OpenCV之imgproc 模块. 图像处理(5)在图像中寻找轮廓 计算物体的凸包 创建包围轮廓的矩形和圆形边界框 为轮廓创建可倾斜的边界框和椭圆 轮廓矩 多边形测试
- COCOS2DX学习之Box2d物理引擎使用之------动态物体的创建
- Unity学习笔记-2D物体的创建与销毁等基本操作
- Three.js学习创建物体
- UE4创建3D场景中的UI方法及在UI上面显示三维物体
- 用小方块组成圆环(使动态创建物体有父物体 指向中心 )
- unity使用单例创建物体
- 在3D物体上创建UI
- 【Unity3D】如何用实现MC中整齐创建物体的效果?
- Unity实现类似我的世界(Minecraft)的创建物体以及销毁物体的功能
- unity编辑模式下创建若干子物体父物体
- 在Hololens创建可编辑物体的第一步:包围盒计算
- Unity 3D使用GameObject创建一个简单的可移动物体
- Cocos2d-x之物理世界(创建运动的物体)