Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. VertexType* vertices;
  2. unsigned long* indices;
  3. int index, i, j;
  4. float positionX, positionZ, u, v, increment;
  5. D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
  6. D3D11_SUBRESOURCE_DATA vertexData, indexData;
  7.  
  8. // Calculate the number of vertices in the terrain mesh.
  9. vertexCount = (resolution - 1) * (resolution - 1) * 6;
  10.  
  11.  
  12. indexCount = vertexCount;
  13. vertices = new VertexType[vertexCount];
  14. indices = new unsigned long[indexCount];
  15.  
  16.  
  17. index = 0;
  18. // UV coords.
  19. u = 0;
  20. v = 0;
  21. increment = 1.0f / (resolution - 1);
  22.  
  23. for (j = 0; j < (resolution - 1); j++)
  24. {
  25. for (i = 0; i < (resolution - 1); i++)
  26. {
  27.  
  28. // Upper right.
  29. positionX = (float)(i + 1);
  30. positionZ = (float)(j + 1);
  31.  
  32. vertices[index].position = XMFLOAT3(positionX, 0.0f, positionZ);
  33. vertices[index].texture = XMFLOAT2(u + increment, v + increment);
  34. vertices[index].normal = XMFLOAT3(0.0, 1.0, 0.0);
  35. indices[index] = index;
  36. index++;
  37.  
  38.  
  39.  
  40. // lower right
  41. positionX = (float)(i);
  42. positionZ = (float)(j + 1);
  43.  
  44.  
  45. vertices[index].position = XMFLOAT3(positionX, 0.0f, positionZ);
  46. vertices[index].texture = XMFLOAT2(u, v + increment);
  47. vertices[index].normal = XMFLOAT3(0.0, 1.0, 0.0);
  48. indices[index] = index;
  49. index++;
  50.  
  51. // Bottom left
  52. positionX = (float)(i);
  53. positionZ = (float)(j);
  54.  
  55. vertices[index].position = XMFLOAT3(positionX, 0.0f, positionZ);
  56. vertices[index].texture = XMFLOAT2(u, v);
  57. vertices[index].normal = XMFLOAT3(0.0, 1.0, 0.0);
  58. indices[index] = index;
  59. index++;
  60.  
  61. // Upper left.
  62. positionX = (float)(i + 1);
  63. positionZ = (float)(j);
  64.  
  65. vertices[index].position = XMFLOAT3(positionX, 0.0f, positionZ);
  66. vertices[index].texture = XMFLOAT2(u + increment, v);
  67. vertices[index].normal = XMFLOAT3(0.0, 1.0, 0.0);
  68. indices[index] = index;
  69. index++;
  70.  
  71.  
  72. u += increment;
  73.  
  74. }
  75.  
  76. u = 0;
  77. v += increment;
  78. }
  79.  
  80. // Set up the description of the static vertex buffer.
  81. vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  82. vertexBufferDesc.ByteWidth = sizeof(VertexType) * vertexCount;
  83. vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  84. vertexBufferDesc.CPUAccessFlags = 0;
  85. vertexBufferDesc.MiscFlags = 0;
  86. vertexBufferDesc.StructureByteStride = 0;
  87. // Give the subresource structure a pointer to the vertex data.
  88. vertexData.pSysMem = vertices;
  89. vertexData.SysMemPitch = 0;
  90. vertexData.SysMemSlicePitch = 0;
  91. // Now create the vertex buffer.
  92. device->CreateBuffer(&vertexBufferDesc, &vertexData, &vertexBuffer);
  93.  
  94. // Set up the description of the static index buffer.
  95. indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  96. indexBufferDesc.ByteWidth = sizeof(unsigned long) * indexCount;
  97. indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
  98. indexBufferDesc.CPUAccessFlags = 0;
  99. indexBufferDesc.MiscFlags = 0;
  100. indexBufferDesc.StructureByteStride = 0;
  101. // Give the subresource structure a pointer to the index data.
  102. indexData.pSysMem = indices;
  103. indexData.SysMemPitch = 0;
  104. indexData.SysMemSlicePitch = 0;
  105. // Create the index buffer.
  106. device->CreateBuffer(&indexBufferDesc, &indexData, &indexBuffer);
  107.  
  108. // Release the arrays now that the buffers have been created and loaded.
  109. delete[] vertices;
  110. vertices = 0;
  111. delete[] indices;
  112. indices = 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement