Advertisement
Guest User

Simple Vertex and Index Buffer Handling

a guest
Jul 5th, 2012
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. // --------------------------------------------------------------------------------------------- //
  2.  
  3. /// <summary>Vertex type used to render a cube</summary>
  4. struct CubeVertex {
  5.  
  6. /// <summary>Describes the elements each vertex contains</summary>
  7. /// <returns>An array containing a description for each element in a vertex</returns>
  8. public: static const std::array<Nuclex::Graphics::VertexElement, 2> &Describe() {
  9. using namespace Nuclex::Graphics;
  10.  
  11. static const std::array<VertexElement, 2> result = {
  12. VertexElement("Position", VertexElementType::Float3),
  13. VertexElement("TextureCoordinates", VertexElementType::Float2)
  14. };
  15.  
  16. return result;
  17. }
  18.  
  19. /// <summary>Initializes a new cube vertex with the given coordinates</summary>
  20. /// <param name="x">X coordinate the vertex will be initialized with</param>
  21. /// <param name="y">Y coordinate the vertex will be initialized with</param>
  22. /// <param name="z">Z coordinate the vertex will be initialized with</param>
  23. /// <param name="u">U texture coordinate the vertex will be initialized with</param>
  24. /// <param name="v">V texture coordinate the vertex will be initialized with</param>
  25. public: CubeVertex(float x, float y, float z, float u, float v) :
  26. Position(x, y, z), TextureCoordinates(u, v) {}
  27.  
  28. /// <summary>Position of the vertex in 3D space</summary>
  29. public: Position3 Position;
  30. /// <summary>Texture coordinates for the polygon at the location of the vertex</summary>
  31. public: Position2 TextureCoordinates;
  32.  
  33. };
  34.  
  35. // --------------------------------------------------------------------------------------------- //
  36.  
  37. /// <summary>A vertex buffer that stores the vertices for a cube</summary>
  38. typedef Nuclex::Graphics::Rasterization::VertexBuffer<CubeVertex> CubeVertexBuffer;
  39.  
  40. /// <summary>Constructs a cube in a vertex buffer</summary>
  41. /// <returns>A vertex buffer containing the vertices for a cube</summary>
  42. inline std::shared_ptr<CubeVertexBuffer> BuildCubeVertexBuffer() {
  43. const std::size_t vertexCount = 8; // A cube has 8 corners :o)
  44. std::shared_ptr<CubeVertexBuffer> vertexBuffer(
  45. new CubeVertexBuffer(vertexCount)
  46. );
  47.  
  48. // Filling a vertex buffer is very simple. If the vertex buffer is already being
  49. // observed, you can call SuppressNotifications() followed by ResumeNotifications()
  50. // to avoid the overhead of many short-time locks or fill the vertex buffer in one
  51. // go by using the Write(bufferStartIndex, vertex *, vertexCount) method, of course.
  52. vertexBuffer->Append(CubeVertex(-0.5f, -0.5f, -0.5f, 0.0f, 0.0f));
  53. vertexBuffer->Append(CubeVertex(-0.5f, -0.5f, 0.5f, 0.0f, 0.0f));
  54. vertexBuffer->Append(CubeVertex(-0.5f, 0.5f, -0.5f, 0.0f, 0.0f));
  55. vertexBuffer->Append(CubeVertex(-0.5f, 0.5f, 0.5f, 0.0f, 0.0f));
  56. vertexBuffer->Append(CubeVertex( 0.5f, -0.5f, -0.5f, 0.0f, 0.0f));
  57. vertexBuffer->Append(CubeVertex( 0.5f, -0.5f, 0.5f, 0.0f, 0.0f));
  58. vertexBuffer->Append(CubeVertex( 0.5f, 0.5f, -0.5f, 0.0f, 0.0f));
  59. vertexBuffer->Append(CubeVertex( 0.5f, 0.5f, 0.5f, 0.0f, 0.0f));
  60.  
  61. return vertexBuffer;
  62. }
  63.  
  64. // --------------------------------------------------------------------------------------------- //
  65.  
  66. /// <summary>An index buffer that defines how to connect the cube's vertices</summary>
  67. typedef Nuclex::Graphics::Rasterization::IndexBuffer<std::uint16_t> CubeIndexBuffer;
  68.  
  69. /// <summary>Constructs a cube in a vertex buffer</summary>
  70. /// <returns>A vertex buffer containing the vertices for a cube</summary>
  71. inline std::shared_ptr<CubeIndexBuffer> BuildCubeIndexBuffer() {
  72. const std::size_t indexCount = 6 * 6; // 6 sides with 2 triangles (6 vertices) each
  73. std::shared_ptr<CubeIndexBuffer> indexBuffer(
  74. new CubeIndexBuffer(indexCount)
  75. );
  76.  
  77. // The process of filling an index buffer works exactly the same as it does
  78. // for a vertex buffer. You should call SuppressNotifications() followed by
  79. // ResumeNotifications() if you modify live index buffers or use the
  80. // Write(bufferStartIndex, index *, indexCount) method.
  81. indexBuffer->Append(0).Append(2).Append(1); // -x
  82. indexBuffer->Append(1).Append(2).Append(3);
  83. indexBuffer->Append(4).Append(5).Append(6); // +x
  84. indexBuffer->Append(5).Append(7).Append(6);
  85. indexBuffer->Append(0).Append(1).Append(5); // -y
  86. indexBuffer->Append(0).Append(5).Append(4);
  87. indexBuffer->Append(2).Append(6).Append(7); // +y
  88. indexBuffer->Append(2).Append(7).Append(3);
  89. indexBuffer->Append(0).Append(4).Append(6); // -z
  90. indexBuffer->Append(0).Append(6).Append(2);
  91. indexBuffer->Append(1).Append(3).Append(7); // +z
  92. indexBuffer->Append(1).Append(7).Append(5);
  93.  
  94. return indexBuffer;
  95. }
  96.  
  97. // --------------------------------------------------------------------------------------------- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement