Advertisement
Guest User

Untitled

a guest
Jan 10th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. void SetupTestVertices(ID3D11Device *pd3dDevice) {
  2. SimpleVertex *ptrV;
  3. int i;
  4. float *ptrToPoints;
  5. float scaleFactor = 1.f / (MAX_RAND * 0.5f); // the test points are from 0 to MAX_RAND, so scale to 0 to 1.0
  6. int xyOffset = (MAX_RAND / 2);
  7. float brightness; // to make the triangles visually different from one another
  8. std::vector<R3> pts, pts2;
  9. // to make the triangles visually different from one another
  10.  
  11.  
  12. // create a random set of points to test the triangulation fuction
  13. if (g_numTestPoints > MAX_TEST_POINTS) g_numTestPoints = MAX_TEST_POINTS;
  14. if (g_numTestPoints < 4) g_numTestPoints = 4;
  15. ptrToPoints = testPointsXY;
  16. for (i = (g_numTestPoints * NUM_DIMENSIONS); i> 0; i--) {
  17. *ptrToPoints++ = ((rand() % MAX_RAND) - xyOffset) * scaleFactor;
  18. }
  19.  
  20. for (int i = 0; i < g_numTestPoints* NUM_DIMENSIONS; i++)
  21. {
  22. R3 pt;
  23. pt.c = testPointsXY[i];
  24. pt.r = testPointsXY[(i*3)+1];
  25. pt.z = testPointsXY[(i*3)+2];
  26.  
  27. pts.push_back(pt);
  28. }
  29.  
  30. std::vector<Tri> tris;
  31. //
  32. int ts = NewtonApple_Delaunay(pts, tris);
  33.  
  34. // put those random points into a buffer for display
  35. ptrV = testvertexes;
  36. ptrToPoints = testPointsXY;
  37.  
  38. for (i = 0; i < 30; i++) {
  39. // scale to between -1 and +1 since that all that this tutorial program was designed to show
  40. ptrV->Pos.x = pts[i].r;
  41. ptrV->Pos.y = pts[i].c;
  42. if (NUM_DIMENSIONS == 3) ptrV->Pos.z = pts[i].z;
  43. else ptrV->Pos.z = 0;
  44. // give the nodes different colors to make it easier to distinguish between the triangles, especially when they are filled
  45. brightness = (float)i / (float)30;
  46. ptrV->Color.x = brightness;
  47. ptrV->Color.y = brightness * 0.2f;
  48. ptrV->Color.z = brightness * 0.6f;
  49.  
  50. ptrV->Color.w = 1.f;
  51. ptrV++;
  52. }
  53.  
  54. std::vector<WORD> triangeList;
  55.  
  56. triangeList.resize(tris.size() * 3);
  57.  
  58.  
  59. for (int i = 0; i < tris.size(); i++)
  60. {
  61. triangeList[i*3] = tris[i].a;
  62. triangeList[(i*3)+1] = tris[i].b;
  63. triangeList[(i*3)+2] = tris[i].c;
  64.  
  65. }
  66. std::vector<WORD> lineIndex;
  67.  
  68. lineIndex.resize(tris.size()*3*sizeof(WORD));
  69. int l, t;
  70. for (l = 0, t = 0; t<tris.size()*3; t += 3) {
  71. // Each triangle has 3 lines, so D3D_PRIMITIVE_TOPOLOGY_LINELIST needs 6 vertices
  72. // Each vertex has to be listed twice
  73. lineIndex[l] = triangeList[t]; l++;
  74. lineIndex[l] = triangeList[t + 1]; l++;
  75. lineIndex[l] = triangeList[t + 1]; l++;
  76. lineIndex[l] = triangeList[t + 2]; l++;
  77. lineIndex[l] = triangeList[t + 2]; l++;
  78. lineIndex[l] = triangeList[t]; l++;
  79. }
  80. ReleaseD3D_Buffers(); // from the previous cycle
  81.  
  82. // Fill in a buffer description for the triangle vertices
  83. D3D11_BUFFER_DESC bd;
  84. ZeroMemory(&bd, sizeof(bd));
  85. bd.Usage = D3D11_USAGE_DEFAULT;
  86. bd.ByteWidth = sizeof(SimpleVertex) * g_numTestPoints;
  87. bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  88. // Fill in the subresource data
  89. D3D11_SUBRESOURCE_DATA InitData;
  90. ZeroMemory(&InitData, sizeof(InitData));
  91. InitData.pSysMem = testvertexes;
  92. pd3dDevice->CreateBuffer(&bd, &InitData, &g_pTestVector);
  93.  
  94. // Fill in a buffer description for filled triangles
  95. ZeroMemory(&bd, sizeof(bd));
  96. bd.Usage = D3D11_USAGE_DEFAULT;
  97. bd.ByteWidth = tris.size()*sizeof(WORD); // <---- numTriangleVertices comes from the function
  98. bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
  99. bd.CPUAccessFlags = 0;
  100. ZeroMemory(&InitData, sizeof(InitData));
  101. InitData.pSysMem = static_cast<void *>(triangeList.data()); // <---- triangleIndexList is from the function also
  102. pd3dDevice->CreateBuffer(&bd, &InitData, &g_pTestVectorIndexBuffer);
  103.  
  104. // Fill in a buffer description for drawing only the triangle outlines
  105. ZeroMemory(&bd, sizeof(bd));
  106. bd.Usage = D3D11_USAGE_DEFAULT;
  107. bd.ByteWidth = lineIndex.size(); // <---- from the function
  108. bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
  109. bd.CPUAccessFlags = 0;
  110. ZeroMemory(&InitData, sizeof(InitData));
  111. InitData.pSysMem = static_cast<void *>(lineIndex.data());
  112. pd3dDevice->CreateBuffer(&bd, &InitData, &g_pTriOutlineIndexBuffer);
  113.  
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement