D3DXMesh 以及 D3DXPMesh都是从ID3DXBaseMesh类中集成,mesh基本是对三角单元进行操作

ID3DXBaseMesh主要函数:

HRESULT DrawSubset( DWORD AttribId);

HRESULT GetIndexBuffer( LPDIRECT3DINDEXBUFFER9 * ppIB);

HRESULT GetVertexBuffer( LPDIRECT3DVERTEXBUFFER9 * ppVB);

HRESULT LockIndexBuffer( DWORD Flags, LPVOID * ppData);

HRESULT UnlockIndexBuffer();

HRESULT CloneMesh( DWORD Options, CONST D3DVERTEXELEMENT9 * pDeclaration, LPDIRECT3DDEVICE9pDevice, LPD3DXMESH * ppCloneMesh);

HRESULT CloneMeshFVF( DWORD Options, DWORD FVF, LPDIRECT3DDEVICE9 pDevice, LPD3DXMESH * ppCloneMesh);

  1. DWORD GetOptions();获得当前网格的选项

DWORD GetFVF();

DWORD GetNumVertices();

DWORD GetNumBytesPerVertex();

DWORD GetNumFaces();

 

1 子集和属性缓存

子集:mesh由子集构成,一个子集是网格中相同属性的三角新单元,每个子集有子集属性ID

属性缓存:三角形单元属性ID存储位置,和网格索引中三角形单元的数目相同,属性缓存项与网格索引缓存中的数据一一对应。

获得索引的办法:

HRESULT LockAttributeBuffer( DWORD Flags, DWORD ** ppData);

HRESULT UnlockAttributeBuffer();

2绘制

HRESULT DrawSubset( DWORD AttribId);

3优化

对网格中的索引和顶点进行重组

  1. HRESULT OptimizeInplace(
  2. DWORD Flags, 优化标记
  3. CONST DWORD * pAdjacencyIn, 指向未经优化的网格临街信息数组的指针
  4. DWORD * pAdjacencyOut, 指向优化后的网格临街信息数组的指针 3 *  GetNumFaces()
  5. DWORD * pFaceRemap, 优化过程中 原始面片被移动到新的面片位置 GetNumFaces()
    LPD3DXBUFFER * ppVertexRemap 顶点被移动到新的顶点位置 GetNumFaces()
    );
  1. //优化标记
  1. typedef enum D3DXMESHOPT
  2. {
  3. D3DXMESHOPT_COMPACT = 0x01000000,移除没用的顶点和索引
  4. D3DXMESHOPT_ATTRSORT = 0x02000000, 根据属性重新排序
  5. D3DXMESHOPT_VERTEXCACHE = 0x04000000, 提高顶点高速缓存的命中率
  6. D3DXMESHOPT_STRIPREORDER = 0x08000000, 对索引进行重组
  7. D3DXMESHOPT_IGNOREVERTS = 0x10000000,
  8. D3DXMESHOPT_DONOTSPLIT = 0x20000000,
  9. D3DXMESHOPT_DEVICEINDEPENDENT = 0x40000000,
  10. } D3DXMESHOPT, *LPD3DXMESHOPT;
  1. 另一个优化函数:会修改原始网格数据
  1. HRESULT Optimize( DWORD Flags, CONST
  2. DWORD * pAdjacencyIn, DWORD *
  3. pAdjacencyOut, DWORD * pFaceRemap,
  4. LPD3DXBUFFER * ppVertexRemap, LPD3DXMESH *
  5. ppOptMesh);

4属性表

属性表经过D3DXMESHOPT_ATTRSORT标志排序后,相同属性的三角形面片的属性信息和顶点列表就会存储在连续的区域中,可以通过 函数:

HRESULT GetAttributeTable( D3DXATTRIBUTERANGE * pAttribTable, DWORD * pAttribTableSize)

来获得网格中属性信息列表,属性信息列表的结构:

  1. typedef struct D3DXATTRIBUTERANGE {
  2. DWORD AttribId; 属性ID
  3. DWORD FaceStart; 索引缓存中的其实位置 3* FaceStart
    DWORD FaceCount; 面片数目
  4. DWORD VertexStart;顶点缓存其实位置
  5. DWORD VertexCount;
  6. } D3DXATTRIBUTERANGE, *LPD3DXATTRIBUTERANGE;
  1. 属性表获取:
  1. GetAttributeTable0,&num);
  1. D3DXATTRIBUTERANGE table = new D3DXATTRIBUTERANGE[num];
  1. GetAttributeTabletable ,&num);
  1. 设置属性表:
  1. HRESULT SetAttributeTable( CONST D3DXATTRIBUTERANGE *
  2. pAttribTable, DWORD cAttribTableSize);
  1. 5邻接信息
  1. 每个三角形面片有三个边,最多三个邻接信息,无效邻接信息为-1或者ULONG_MAX
  1. 邻接数组大小为Get FaceNun*3
  1. 获得邻接数据函数:
  1. HRESULT GenerateAdjacency( FLOAT Epsilon, DWORD * pAdjacency);
  1. Epsilon:指定两个点距离多近可以算作同一个点
  1. pAdjacencyDWORD数组
  1. 6网格顶点复制
  1. HRESULT CloneMeshFVF( DWORD Options, DWORD FVF, LPDIRECT3DDEVICE9 pDevice, LPD3DXMESH * ppCloneMesh);
  1. Options:创建网格副本是的可选项
  1. FVF:灵活顶点格式
  1. DWORD GetOptions();获得当前网格的选项
  1. 7创建网格
  1. HRESULT D3DXCreateMesh( DWORD NumFaces,
  2. DWORD NumVertices, DWORD Options,
  3. CONST LPD3DVERTEXELEMENT9 * pDeclaration,
  4. LPDIRECT3DDEVICE9 pD3DDevice, LPD3DXMESH *
  5. ppMesh);
  1. HRESULT D3DXCreateMeshFVF( DWORD NumFaces,
  2. DWORD NumVertices, DWORD Options,
  3. DWORD FVF, LPDIRECT3DDEVICE9
  4. pD3DDevice, LPD3DXMESH * ppMesh);
  1. typedef struct D3DVERTEXELEMENT9 {
  2. WORD Stream;
  3. WORD Offset;
  4. BYTE Type;
  5. BYTE Method;
  6. BYTE Usage;
  7. BYTE UsageIndex;
  8. } D3DVERTEXELEMENT9, *LPD3DVERTEXELEMENT9;
  1.  
  1. 8网格创建与绘制
  1. 1创建网格对象
  1. 2将面片数据写入网格缓存
  1. 3指定每个面片所属的子集
  1. 4生成邻接信息,优化网格
  1. 5绘制网格
  1.  
  1. #include "d3dUtility.h"
  2. #include <fstream>
  3. #include <vector>
  4.  
  5. //
  6. // Globals
  7. //
  8.  
  9. IDirect3DDevice9* Device = 0;
  10.  
  11. const int Width = 640;
  12. const int Height = 480;
  13.  
  14. ID3DXMesh* Mesh = 0;
  15. const DWORD NumSubsets = 3;
  16. IDirect3DTexture9* Textures[3] = {0, 0, 0};// texture for each subset
  17.  
  18. std::ofstream OutFile; // used to dump mesh data to file
  19.  
  20. //
  21. // Prototypes
  22. //
  23.  
  24. void dumpVertices(std::ofstream& outFile, ID3DXMesh* mesh);
  25. void dumpIndices(std::ofstream& outFile, ID3DXMesh* mesh);
  26. void dumpAttributeBuffer(std::ofstream& outFile, ID3DXMesh* mesh);
  27. void dumpAdjacencyBuffer(std::ofstream& outFile, ID3DXMesh* mesh);
  28. void dumpAttributeTable(std::ofstream& outFile, ID3DXMesh* mesh);
  29.  
  30. //
  31. // Classes and Structures
  32. //
  33. struct Vertex
  34. {
  35. Vertex(){}
  36. Vertex(float x, float y, float z,
  37. float nx, float ny, float nz, float u, float v)
  38. {
  39. _x = x; _y = y; _z = z;
  40. _nx = nx; _ny = ny; _nz = nz;
  41. _u = u; _v = v;
  42. }
  43.  
  44. float _x, _y, _z, _nx, _ny, _nz, _u, _v;
  45.  
  46. static const DWORD FVF;
  47. };
  48. const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
  49.  
  50. //
  51. // Framework functions
  52. //
  53. bool Setup()
  54. {
  55. HRESULT hr = 0;
  56.  
  57. //
  58. // We are going to fill the empty mesh with the geometry of a box,
  59. // so we need 12 triangles and 24 vetices.
  60. //
  61.  
  62. hr = D3DXCreateMeshFVF(
  63. 12,
  64. 24,
  65. D3DXMESH_MANAGED,
  66. Vertex::FVF,
  67. Device,
  68. &Mesh);
  69.  
  70. if(FAILED(hr))
  71. {
  72. ::MessageBox(0, "D3DXCreateMeshFVF() - FAILED", 0, 0);
  73. return false;
  74. }
  75.  
  76. //
  77. // Fill in vertices of a box
  78. //
  79. Vertex* v = 0;
  80. Mesh->LockVertexBuffer(0, (void**)&v);
  81.  
  82. // fill in the front face vertex data
  83. v[0] = Vertex(-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
  84. v[1] = Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
  85. v[2] = Vertex( 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);
  86. v[3] = Vertex( 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
  87.  
  88. // fill in the back face vertex data
  89. v[4] = Vertex(-1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
  90. v[5] = Vertex( 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f);
  91. v[6] = Vertex( 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f);
  92. v[7] = Vertex(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f);
  93.  
  94. // fill in the top face vertex data
  95. v[8] = Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
  96. v[9] = Vertex(-1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);
  97. v[10] = Vertex( 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f);
  98. v[11] = Vertex( 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
  99.  
  100. // fill in the bottom face vertex data
  101. v[12] = Vertex(-1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f);
  102. v[13] = Vertex( 1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f);
  103. v[14] = Vertex( 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f);
  104. v[15] = Vertex(-1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f);
  105.  
  106. // fill in the left face vertex data
  107. v[16] = Vertex(-1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f);
  108. v[17] = Vertex(-1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
  109. v[18] = Vertex(-1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f);
  110. v[19] = Vertex(-1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f);
  111.  
  112. // fill in the right face vertex data
  113. v[20] = Vertex( 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f);
  114. v[21] = Vertex( 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
  115. v[22] = Vertex( 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f);
  116. v[23] = Vertex( 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f);
  117.  
  118. Mesh->UnlockVertexBuffer();
  119.  
  120. //
  121. // Define the triangles of the box
  122. //
  123. WORD* i = 0;
  124. Mesh->LockIndexBuffer(0, (void**)&i);
  125.  
  126. // fill in the front face index data
  127. i[0] = 0; i[1] = 1; i[2] = 2;
  128. i[3] = 0; i[4] = 2; i[5] = 3;
  129.  
  130. // fill in the back face index data
  131. i[6] = 4; i[7] = 5; i[8] = 6;
  132. i[9] = 4; i[10] = 6; i[11] = 7;
  133.  
  134. // fill in the top face index data
  135. i[12] = 8; i[13] = 9; i[14] = 10;
  136. i[15] = 8; i[16] = 10; i[17] = 11;
  137.  
  138. // fill in the bottom face index data
  139. i[18] = 12; i[19] = 13; i[20] = 14;
  140. i[21] = 12; i[22] = 14; i[23] = 15;
  141.  
  142. // fill in the left face index data
  143. i[24] = 16; i[25] = 17; i[26] = 18;
  144. i[27] = 16; i[28] = 18; i[29] = 19;
  145.  
  146. // fill in the right face index data
  147. i[30] = 20; i[31] = 21; i[32] = 22;
  148. i[33] = 20; i[34] = 22; i[35] = 23;
  149.  
  150. Mesh->UnlockIndexBuffer();
  151.  
  152. //
  153. // Specify the subset each triangle belongs to, in this example
  154. // we will use three subsets, the first two faces of the cube specified
  155. // will be in subset 0, the next two faces will be in subset 1 and
  156. // the the last two faces will be in subset 2.
  157. //
  158. DWORD* attributeBuffer = 0;
  159. Mesh->LockAttributeBuffer(0, &attributeBuffer);
  160.  
  161. for(int a = 0; a < 4; a++)
  162. attributeBuffer[a] = 0;
  163.  
  164. for(int b = 4; b < 8; b++)
  165. attributeBuffer[b] = 1;
  166.  
  167. for(int c = 8; c < 12; c++)
  168. attributeBuffer[c] = 2;
  169.  
  170. Mesh->UnlockAttributeBuffer();
  171.  
  172. //
  173. // Optimize the mesh to generate an attribute table.
  174. //
  175.  
  176. std::vector<DWORD> adjacencyBuffer(Mesh->GetNumFaces() * 3);
  177. Mesh->GenerateAdjacency(0.0f, &adjacencyBuffer[0]);
  178.  
  179. hr = Mesh->OptimizeInplace(
  180. D3DXMESHOPT_ATTRSORT |
  181. D3DXMESHOPT_COMPACT |
  182. D3DXMESHOPT_VERTEXCACHE,
  183. &adjacencyBuffer[0],
  184. 0, 0, 0);
  185.  
  186. //
  187. // Dump the Mesh Data to file.
  188. //
  189.  
  190. OutFile.open("Mesh Dump.txt");
  191.  
  192. dumpVertices(OutFile, Mesh);
  193. dumpIndices(OutFile, Mesh);
  194. dumpAttributeTable(OutFile, Mesh);
  195. dumpAttributeBuffer(OutFile, Mesh);
  196. dumpAdjacencyBuffer(OutFile, Mesh);
  197.  
  198. OutFile.close();
  199.  
  200. //
  201. // Load the textures and set filters.
  202. //
  203.  
  204. D3DXCreateTextureFromFile(
  205. Device,
  206. "brick0.jpg",
  207. &Textures[0]);
  208.  
  209. D3DXCreateTextureFromFile(
  210. Device,
  211. "brick1.jpg",
  212. &Textures[1]);
  213.  
  214. D3DXCreateTextureFromFile(
  215. Device,
  216. "checker.jpg",
  217. &Textures[2]);
  218.  
  219. Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  220. Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  221. Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
  222.  
  223. //
  224. // Disable lighting.
  225. //
  226.  
  227. Device->SetRenderState(D3DRS_LIGHTING, false);
  228.  
  229. //
  230. // Set camera.
  231. //
  232.  
  233. D3DXVECTOR3 pos(0.0f, 0.f, -4.0f);
  234. D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
  235. D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
  236.  
  237. D3DXMATRIX V;
  238. D3DXMatrixLookAtLH(
  239. &V,
  240. &pos,
  241. &target,
  242. &up);
  243.  
  244. Device->SetTransform(D3DTS_VIEW, &V);
  245.  
  246. //
  247. // Set projection matrix.
  248. //
  249.  
  250. D3DXMATRIX proj;
  251. D3DXMatrixPerspectiveFovLH(
  252. &proj,
  253. D3DX_PI * 0.5f, // 90 - degree
  254. (float)Width / (float)Height,
  255. 1.0f,
  256. 1000.0f);
  257. Device->SetTransform(D3DTS_PROJECTION, &proj);
  258.  
  259. return true;
  260. }
  261.  
  262. void Cleanup()
  263. {
  264. d3d::Release<ID3DXMesh*>(Mesh);
  265. d3d::Release<IDirect3DTexture9*>(Textures[0]);
  266. d3d::Release<IDirect3DTexture9*>(Textures[1]);
  267. d3d::Release<IDirect3DTexture9*>(Textures[2]);
  268. }
  269.  
  270. bool Display(float timeDelta)
  271. {
  272. if( Device )
  273. {
  274. //
  275. // Update: Rotate the cube.
  276. //
  277.  
  278. D3DXMATRIX xRot;
  279. D3DXMatrixRotationX(&xRot, D3DX_PI * 0.2f);
  280.  
  281. static float y = 0.0f;
  282. D3DXMATRIX yRot;
  283. D3DXMatrixRotationY(&yRot, y);
  284. y += timeDelta;
  285.  
  286. if( y >= 6.28f )
  287. y = 0.0f;
  288.  
  289. D3DXMATRIX World = xRot * yRot;
  290.  
  291. Device->SetTransform(D3DTS_WORLD, &World);
  292.  
  293. //
  294. // Render
  295. //
  296.  
  297. Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
  298. Device->BeginScene();
  299.  
  300. for(int i = 0; i < NumSubsets; i++)
  301. {
  302. Device->SetTexture( 0, Textures[i] );
  303. Mesh->DrawSubset( i );
  304. }
  305.  
  306. Device->EndScene();
  307. Device->Present(0, 0, 0, 0);
  308. }
  309. return true;
  310. }

directX基础学习系列7 网格(自己创建)的更多相关文章

  1. DirectX基础学习系列8 渐进网格以及外接体

    1 IUnknown--> ID3DXBUFFER D3D泛型接口: GetBufferPointer Retrieves a pointer to the data in the buffer ...

  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基础学习系列4 颜色和光照

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

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

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

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

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

  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. hdu 3336 kmp+next数组应用

    分析转自:http://972169909-qq-com.iteye.com/blog/1114968 十分易懂 题意:求字串中[前缀+跟前缀相同的子串]的个数? Sample Input 1 4 a ...

  2. CC2540开发板学习笔记(九)—— BLE协议简介

    一.BLE协议简介 1.协议是什么? 协议是一系列的通信标准,双方需要共同按照这进行正常数据 协议是一系列的通信标准,双方需要共同按照这进行正常数据发射和 接收.协议栈是的具体实现形式,通俗点来理解就 ...

  3. Android中动画

    两种动画 view动画 属性动画  (也可以使用xml描述动画) view 4动画 补间动画 渐变 AlphaAnimation 缩放 ScaleAnimation 平移 TranslateAnima ...

  4. sql2005-数据库备份方案 (转载)

    sql2005数据库备份一般情况分为二种:一是手工备份.二是自动备份.以下是二种方法的步骤: 一.手工备份 打开数据库,选择要备份数据库,右键选择[任务]->[备份],打开备份数据库页面,在[源 ...

  5. Codeforces Round #294 (Div. 2)

    水 A. A and B and Chess /* 水题 */ #include <cstdio> #include <algorithm> #include <iost ...

  6. html5 header和group

    <html> <head> <meta charset="UTF-8" /> <meta name="generator&quo ...

  7. BZOJ4298 : [ONTAK2015]Bajtocja

    设f[i][j]为第i张图中j点所在连通块的编号,加边时可以通过启发式合并在$O(dn\log n)$的时间内维护出来. 对于每个点,设h[i]为f[j][i]的hash值,若两个点hash值相等,则 ...

  8. BZOJ3808 : Neerc2012 Labyrinth of the Minotaur

    左上角和右下角不四连通等价于左下角和右上角八连通 枚举正方形的左上角,先二分出最大的边长,使得里面不含障碍物 然后再二分出最小的边长,使得两部分连通,用前缀和判断 这题WA了好久…一直对拍都没问题…于 ...

  9. 哈希表工作原理 (并不特指Java中的HashTable)

    1. 引言         哈希表(Hash Table)的应用近两年才在NOI中出现,作为一种高效的数据结构,它正在竞赛中发挥着越来越重要的作用.  哈希表最大的优点,就是把数据的存储和查找消耗的时 ...

  10. Java多线程编程详解

    转自:http://programming.iteye.com/blog/158568 线程的同步 由于同一进程的多个线程共享同一片存储空间,在带来方便的同时,也带来了访问冲突这个严重的问题.Ja ...