1 IUnknown--> ID3DXBUFFER D3D泛型接口:

GetBufferPointer

Retrieves a pointer to the data in the buffer.

GetBufferSize

Retrieves the total size of the data in the buffer.

使用完之后需要进行释放:Release()

2XFILE

//从文件中加载xfile文件

  1. HRESULT D3DXLoadMeshFromX(
  2. __in LPCTSTR pFilename, //xfile文件名
  3. __in DWORD Options, //加载可选项
  4. __in LPDIRECT3DDEVICE9 pD3DDevice, //设备
  5. __out LPD3DXBUFFER *ppAdjacency, //临街信息,DWORD数组
  6. __out LPD3DXBUFFER *ppMaterials, //材质信息
  7. __out LPD3DXBUFFER *ppEffectInstances, //
  8. __out DWORD *pNumMaterials, //材质数目
  9. __out LPD3DXMESH *ppMesh
  10. );
  1. //材质结构:ppMaterials结构中第I项 对应第I个子集
  1. typedef struct D3DXMATERIAL {
  2. D3DMATERIAL9 MatD3D;
  3. LPSTR pTextureFilename;
  4. } D3DXMATERIAL, *LPD3DXMATERIAL;
  1. 加载时,首先加载XFILE文件,然后遍历纹理数据,将纹理数据加载,在渲染的时候,先设置纹理和纹理
  1. 3生成顶点法线
  1. 方便使用光照,网格的法向量计算方法
  1. HRESULT D3DXComputeNormals(
  2. __inout LPD3DXBASEMESH pMesh, //输出的网格数据
  3. __in const DWORD *pAdjacency //临街数据
  4. );
  1. pMesh参数中顶点格式必须包含D3DFVF_NORMAL,所以需要从原先的mesh数据copy一份过来
  1. 4.渐进网格
  1. 类似于渐进纹理数据,通过边折叠技术对网格进行简化,并且这种操作时可逆转的
  1. 渐进网格从已有的网格数据中产生:
  1. D3DXGeneratePMesh(
  2. LPD3DXMESH pMesh,
  3. CONST DWORD* pAdjacency, //邻接信息dword数组
  4. CONST D3DXATTRIBUTEWEIGHTS *pVertexAttributeWeights, //指定相应顶点属性权值 涉及被移除的概率
  5. CONST FLOAT *pVertexWeights, //指定顶点权值,涉及被移除的概率
  6. DWORD MinValue, //面片简化的最少值,如果设置为1 表示尽可能的少
  7. DWORD Options, //决定上面的参数为定点数或者面片数
  8. LPD3DXPMESH* ppPMesh);
  1. D3DXATTRIBUTEWEIGHTS 顶点属相权值结构,暂且使用默认值
  1. D3DXPMesh提供的操作接口:
  1. GetMaxFaces() //最多的面片数目上限
  1. GetMinFaces() //最少的面片数目下限
  1. GetMaxVertexs() //最多的顶点数目上限
  1. GetMinVertexs() //最少的顶点数目下限
  1. SetNumFaces() // 设置面片数目,
  1. SetNumVertexs() //设置顶点数目
  1. trimByFaces(newfacemin , newfacemax, --- ., --) // 重新设置顶点的最大,最小限制
  1. trimByVetex(newfacemin , newfacemax, --- ., --) // 重新设置顶点的最大,最小限制
  1. 5外接体:可以用于检测碰撞以及可见性
  1. //外接球
  1. D3DXComputeBoundingSphere(
  2. CONST D3DXVECTOR3 *pFirstPosition, // pointer to first position 指向顶点的第一个位置
    DWORD NumVertices, 数组中顶点数目
  3. DWORD dwStride, // count in bytes to subsequent position vectors 每个顶点字节数
  4. D3DXVECTOR3 *pCenter, 外界球中心
  5. FLOAT *pRadius); 外接球半径
  1. // 外界体
  1. D3DXComputeBoundingBox(
  2. CONST D3DXVECTOR3 *pFirstPosition, // pointer to first position,指向顶点的第一个位置
  3. DWORD NumVertices, //顶点数组中的顶点数目
  4. DWORD dwStride, // count in bytes to subsequent position vectors 每个顶点的字节数
  5. D3DXVECTOR3 *pMin, //返回的
  6. D3DXVECTOR3 *pMax);
  1. //////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // File: boundingvolumes.cpp
  4. //
  5. // Author: Frank Luna (C) All Rights Reserved
  6. //
  7. // System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0
  8. //
  9. // Desc: Demonstrates how to use D3DXComputeBoundingSphere and D3DXComputeBoundingBox.
  10. //
  11. // -The spacebar key switches between rendering the mesh's bounding sphere and box.
  12. //
  13. //////////////////////////////////////////////////////////////////////////////////////////////////
  14.  
  15. #include "d3dUtility.h"
  16. #include <vector>
  17.  
  18. //
  19. // Globals
  20. //
  21.  
  22. IDirect3DDevice9* Device = ;
  23.  
  24. const int Width = ;
  25. const int Height = ;
  26.  
  27. ID3DXMesh* Mesh = ;
  28. ID3DXPMesh* pMesh = ;
  29. std::vector<D3DMATERIAL9> Mtrls();
  30. std::vector<IDirect3DTexture9*> Textures();
  31.  
  32. ID3DXMesh* SphereMesh = ;
  33. ID3DXMesh* BoxMesh = ;
  34.  
  35. bool RenderBoundingSphere = true;
  36.  
  37. //
  38. // Prototypes
  39. //
  40.  
  41. bool ComputeBoundingSphere(ID3DXMesh* mesh, d3d::BoundingSphere* sphere);
  42. bool ComputeBoundingBox(ID3DXMesh* mesh, d3d::BoundingBox* box);
  43.  
  44. //
  45. // Framework functions
  46. //
  47. bool Setup()
  48. {
  49. HRESULT hr = ;
  50.  
  51. //
  52. // Load the XFile data.
  53. //
  54. ID3DXBuffer* adjBuffer = ;
  55. ID3DXBuffer* mtrlBuffer = ;
  56. DWORD numMtrls = ;
  57.  
  58. hr = D3DXLoadMeshFromX(
  59. "bigship1.x",
  60. D3DXMESH_MANAGED,
  61. Device,
  62. &adjBuffer,
  63. &mtrlBuffer,
  64. ,
  65. &numMtrls,
  66. &Mesh);
  67.  
  68. if(FAILED(hr))
  69. {
  70. ::MessageBox(, "D3DXLoadMeshFromX() - FAILED", , );
  71. return false;
  72. }
  73.  
  74. //
  75. // Extract the materials, load textures.
  76. //
  77.  
  78. if( mtrlBuffer != && numMtrls != )
  79. {
  80. D3DXMATERIAL* mtrls = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer();
  81.  
  82. for(int i = ; i < numMtrls; i++)
  83. {
  84. // the MatD3D property doesn't have an ambient value set
  85. // when its loaded, so set it now:
  86. mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse;
  87.  
  88. // save the ith material
  89. Mtrls.push_back( mtrls[i].MatD3D );
  90.  
  91. // check if the ith material has an associative texture
  92. if( mtrls[i].pTextureFilename != )
  93. {
  94. // yes, load the texture for the ith subset
  95. IDirect3DTexture9* tex = ;
  96. D3DXCreateTextureFromFile(
  97. Device,
  98. mtrls[i].pTextureFilename,
  99. &tex);
  100.  
  101. // save the loaded texture
  102. Textures.push_back( tex );
  103. }
  104. else
  105. {
  106. // no texture for the ith subset
  107. Textures.push_back( );
  108. }
  109. }
  110. }
  111. d3d::Release<ID3DXBuffer*>(mtrlBuffer); // done w/ buffer
  112.  
  113. //
  114. // Optimize the mesh.
  115. //
  116.  
  117. hr = Mesh->OptimizeInplace(
  118. D3DXMESHOPT_ATTRSORT |
  119. D3DXMESHOPT_COMPACT |
  120. D3DXMESHOPT_VERTEXCACHE,
  121. (DWORD*)adjBuffer->GetBufferPointer(),
  122. , , );
  123.  
  124. d3d::Release<ID3DXBuffer*>(adjBuffer); // done w/ buffer
  125.  
  126. if(FAILED(hr))
  127. {
  128. ::MessageBox(, "OptimizeInplace() - FAILED", , );
  129. return false;
  130. }
  131.  
  132. //
  133. // Compute Bounding Sphere and Bounding Box.
  134. //
  135.  
  136. d3d::BoundingSphere boundingSphere;
  137. d3d::BoundingBox boundingBox;
  138.  
  139. ComputeBoundingSphere(Mesh, &boundingSphere);
  140. ComputeBoundingBox(Mesh, &boundingBox);
  141.  
  142. D3DXCreateSphere(
  143. Device,
  144. boundingSphere._radius,
  145. ,
  146. ,
  147. &SphereMesh,
  148. );
  149.  
  150. D3DXCreateBox(
  151. Device,
  152. boundingBox._max.x - boundingBox._min.x,
  153. boundingBox._max.y - boundingBox._min.y,
  154. boundingBox._max.z - boundingBox._min.z,
  155. &BoxMesh,
  156. );
  157.  
  158. //
  159. // Set texture filters.
  160. //
  161.  
  162. Device->SetSamplerState(, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  163. Device->SetSamplerState(, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  164. Device->SetSamplerState(, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
  165.  
  166. //
  167. // Set Lights.
  168. //
  169.  
  170. D3DXVECTOR3 dir(1.0f, -1.0f, 1.0f);
  171. D3DXCOLOR col(1.0f, 1.0f, 1.0f, 1.0f);
  172. D3DLIGHT9 light = d3d::InitDirectionalLight(&dir, &col);
  173.  
  174. Device->SetLight(, &light);
  175. Device->LightEnable(, true);
  176. Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
  177. Device->SetRenderState(D3DRS_SPECULARENABLE, true);
  178.  
  179. //
  180. // Set camera.
  181. //
  182.  
  183. D3DXVECTOR3 pos(4.0f, 12.0f, -20.0f);
  184. D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
  185. D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
  186.  
  187. D3DXMATRIX V;
  188. D3DXMatrixLookAtLH(
  189. &V,
  190. &pos,
  191. &target,
  192. &up);
  193.  
  194. Device->SetTransform(D3DTS_VIEW, &V);
  195.  
  196. //
  197. // Set projection matrix.
  198. //
  199.  
  200. D3DXMATRIX proj;
  201. D3DXMatrixPerspectiveFovLH(
  202. &proj,
  203. D3DX_PI * 0.5f, // 90 - degree
  204. (float)Width / (float)Height,
  205. 1.0f,
  206. 1000.0f);
  207. Device->SetTransform(D3DTS_PROJECTION, &proj);
  208.  
  209. return true;
  210. }
  211.  
  212. void Cleanup()
  213. {
  214. d3d::Release<ID3DXMesh*>(Mesh);
  215.  
  216. for(int i = ; i < Textures.size(); i++)
  217. d3d::Release<IDirect3DTexture9*>( Textures[i] );
  218.  
  219. d3d::Release<ID3DXMesh*>(SphereMesh);
  220. d3d::Release<ID3DXMesh*>(BoxMesh);
  221. }
  222.  
  223. bool Display(float timeDelta)
  224. {
  225. if( Device )
  226. {
  227. //
  228. // Update: Rotate the mesh.
  229. //
  230.  
  231. static float y = 0.0f;
  232. D3DXMATRIX yRot;
  233. D3DXMatrixRotationY(&yRot, y);
  234. y += timeDelta;
  235.  
  236. if( y >= 6.28f )
  237. y = 0.0f;
  238.  
  239. D3DXMATRIX World = yRot;
  240.  
  241. Device->SetTransform(D3DTS_WORLD, &World);
  242.  
  243. //
  244. // Render
  245. //
  246.  
  247. Device->Clear(, , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, );
  248. Device->BeginScene();
  249.  
  250. // draw the mesh
  251. for(int i = ; i < Mtrls.size(); i++)
  252. {
  253. Device->SetMaterial( &Mtrls[i] );
  254. Device->SetTexture(, Textures[i]);
  255. Mesh->DrawSubset(i);
  256. }
  257.  
  258. //
  259. // Draw bounding volume in blue and at 10% opacity
  260. D3DMATERIAL9 blue = d3d::BLUE_MTRL;
  261. blue.Diffuse.a = 0.10f; // 10% opacity
  262.  
  263. Device->SetMaterial(&blue);
  264. Device->SetTexture(, ); // disable texture
  265.  
  266. Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
  267. Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
  268. Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
  269.  
  270. if( RenderBoundingSphere )
  271. SphereMesh->DrawSubset();
  272. else
  273. BoxMesh->DrawSubset();
  274.  
  275. Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
  276.  
  277. Device->EndScene();
  278. Device->Present(, , , );
  279. }
  280. return true;
  281. }
  282.  
  283. //
  284. // WndProc
  285. //
  286. LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  287. {
  288. switch( msg )
  289. {
  290. case WM_DESTROY:
  291. ::PostQuitMessage();
  292. break;
  293.  
  294. case WM_KEYDOWN:
  295. if( wParam == VK_ESCAPE )
  296. ::DestroyWindow(hwnd);
  297.  
  298. if( wParam == VK_SPACE )
  299. RenderBoundingSphere = !RenderBoundingSphere;
  300.  
  301. break;
  302. }
  303. return ::DefWindowProc(hwnd, msg, wParam, lParam);
  304. }
  305.  
  306. //
  307. // WinMain
  308. //
  309. int WINAPI WinMain(HINSTANCE hinstance,
  310. HINSTANCE prevInstance,
  311. PSTR cmdLine,
  312. int showCmd)
  313. {
  314. if(!d3d::InitD3D(hinstance,
  315. Width, Height, true, D3DDEVTYPE_HAL, &Device))
  316. {
  317. ::MessageBox(, "InitD3D() - FAILED", , );
  318. return ;
  319. }
  320.  
  321. if(!Setup())
  322. {
  323. ::MessageBox(, "Setup() - FAILED", , );
  324. return ;
  325. }
  326.  
  327. d3d::EnterMsgLoop( Display );
  328.  
  329. Cleanup();
  330.  
  331. Device->Release();
  332.  
  333. return ;
  334. }
  335.  
  336. bool ComputeBoundingSphere(ID3DXMesh* mesh, d3d::BoundingSphere* sphere)
  337. {
  338. HRESULT hr = ;
  339.  
  340. BYTE* v = ;
  341. mesh->LockVertexBuffer(, (void**)&v);
  342.  
  343. hr = D3DXComputeBoundingSphere(
  344. (D3DXVECTOR3*)v,
  345. mesh->GetNumVertices(),
  346. D3DXGetFVFVertexSize(mesh->GetFVF()),
  347. &sphere->_center,
  348. &sphere->_radius);
  349.  
  350. mesh->UnlockVertexBuffer();
  351.  
  352. if( FAILED(hr) )
  353. return false;
  354.  
  355. return true;
  356. }
  357.  
  358. bool ComputeBoundingBox(ID3DXMesh* mesh, d3d::BoundingBox* box)
  359. {
  360. HRESULT hr = ;
  361.  
  362. BYTE* v = ;
  363. mesh->LockVertexBuffer(, (void**)&v);
  364.  
  365. hr = D3DXComputeBoundingBox(
  366. (D3DXVECTOR3*)v,
  367. mesh->GetNumVertices(),
  368. D3DXGetFVFVertexSize(mesh->GetFVF()),
  369. &box->_min,
  370. &box->_max);
  371.  
  372. mesh->UnlockVertexBuffer();
  373.  
  374. if( FAILED(hr) )
  375. return false;
  376.  
  377. return true;
  378. }

DirectX基础学习系列8 渐进网格以及外接体的更多相关文章

  1. directX基础学习系列7 网格(自己创建)

    D3DXMesh 以及 D3DXPMesh都是从ID3DXBaseMesh类中集成,mesh基本是对三角单元进行操作 ID3DXBaseMesh主要函数: HRESULT DrawSubset( DW ...

  2. DirectX 基础学习系列5 纹理映射

    1 纹理坐标 类似BMP图像坐标系,左上为原点 纹理坐标为了规范化,范围限定在[0,1]之间,使用纹理的时候,需要修改顶点结构 struct ColorVetex { float x, y,z; fl ...

  3. DirectX基础学习系列1

    1.3 基础 1.3.1表面 表面接口:     IDirect3DSurface9 获得表面信息:GetDesc(D3DSURFACE_DESC) 获得表面接口指针 :LockRect( D3DLO ...

  4. DirectX 基础学习系列6 字体

    DIRECTX9自带ID3DXFONT类 内部调用GDI的接口,效率一般,但能够处理一些复杂的字体 HRESULT D3DXCreateFontIndirect( LPDIRECT3DDEVICE9 ...

  5. DirectX基础学习系列5 融合技术

    7.1融合方程 1概念 融合技术将当前光栅化像素的颜色与以前已光栅化并处于同一个位置的像素颜色进行合成,即将当前要进行光栅化的三角形单元与已写入后台的像素进行融合 2需要遵循的原则: (1)先绘制不需 ...

  6. DirectX基础学习系列4 颜色和光照

    4.1颜色表示 RGB颜色:D3DCOLOR  可以用宏D3DCOLOR_ARGB(a,r,g,b)  D3DCOLOR_XRGB(255,r,g,b) 另外一种浮点表示:D3DCOLORVALUE, ...

  7. DirectX基础学习系列2

    补充第一章矩阵内容 向量 1 3D空间向量,包含浮点数类型坐标 D3DXVECTOR-->D3DXVECTOR3 2向量的长度 D3DXVector3Length(const D3DXVECTO ...

  8. Linux基础学习系列目录导航

    Linux基础学习-通过VM安装RHEL7.4 Linux基础学习-命令行与图形界面切换 Linux基础学习-基本命令 Linux基础学习-RHEL7.4之YUM更换CentOS源 Linux基础学习 ...

  9. Bootstrap基础学习 ---- 系列文章

    [Bootstrap基础学习]05 Bootstrap学习总结 [Bootstrap基础学习]04 Bootstrap的HTML和CSS编码规范 [Bootstrap基础学习]03 Bootstrap ...

随机推荐

  1. [原]网站跨站点脚本,Sql注入等攻击的处理

    从360安全论坛里找到的一段代码,经过整理封装,直接在站点Global.asax文件或写一个HttpModule来拦截恶意请求即可: http://bbs.webscan.360.cn/forum.p ...

  2. wp8 导航方法

    全局跳转 (App.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Tools/SpatialQueryCha ...

  3. laravel数据库查询是use方法的使用

    ){                return $query->where('effectivetime','<',date('Y-m-d'));             }else{ ...

  4. poj 1019

    懂了 题意是给一串 1 12 123 1234 12345 123456 ....这样的数字问第 i个数字是多少 Sample Input 2 8 3 Sample Output 2 2 #inclu ...

  5. 【codevs1993】草地排水 最大流

    [codevs1993]草地排水 题目描述 Description 在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段 ...

  6. Winedt打开tex文件报错error reading的解决方案

    我刚装就发现winedt打开一些.tex文件时会出现reading error,然后看不到任何文字(网上有人讨论打开是乱码的问题,但是我的是完全看不到任何东西),我的系统winxp,网上有人说好像是和 ...

  7. hdu 1228

    Description 读入两个小于100的正整数A和B,计算A+B. 需要注意的是:A和B的每一位数字由对应的英文单词给出.    Input 测试输入包含若干测试用例,每个测试用例占一行,格式为& ...

  8. uva494 Kindergarten Counting Game

    #include<bits/stdc++.h>using namespace std;int main(){ int n=0; char a; int flag=1; while((sca ...

  9. js:数据结构笔记10--图和图算法

    图:是由边和定点的集合组成:  按照图的定点对是否有序可以分为:有向图和无向图:  路径:所有顶点都由边连接构成:路径长度为第一个定点到最后一个顶点之间的数量:  环:指向自身的顶点,长度为0:圈:至 ...

  10. js:数据结构笔记5--链表

    数组: 其他语言的数组缺陷:添加/删除数组麻烦: js数组的缺点:被实现为对象,效率低: 如果要实现随机访问,数组还是更好的选择: 链表: 结构图: 基本代码: function Node (elem ...