Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // --------------------------------------------------------------------------------------------- //
- /// <summary>Vertex type used to render a cube</summary>
- struct CubeVertex {
- /// <summary>Describes the elements each vertex contains</summary>
- /// <returns>An array containing a description for each element in a vertex</returns>
- public: static const std::array<Nuclex::Graphics::VertexElement, 2> &Describe() {
- using namespace Nuclex::Graphics;
- static const std::array<VertexElement, 2> result = {
- VertexElement("Position", VertexElementType::Float3),
- VertexElement("TextureCoordinates", VertexElementType::Float2)
- };
- return result;
- }
- /// <summary>Initializes a new cube vertex with the given coordinates</summary>
- /// <param name="x">X coordinate the vertex will be initialized with</param>
- /// <param name="y">Y coordinate the vertex will be initialized with</param>
- /// <param name="z">Z coordinate the vertex will be initialized with</param>
- /// <param name="u">U texture coordinate the vertex will be initialized with</param>
- /// <param name="v">V texture coordinate the vertex will be initialized with</param>
- public: CubeVertex(float x, float y, float z, float u, float v) :
- Position(x, y, z), TextureCoordinates(u, v) {}
- /// <summary>Position of the vertex in 3D space</summary>
- public: Position3 Position;
- /// <summary>Texture coordinates for the polygon at the location of the vertex</summary>
- public: Position2 TextureCoordinates;
- };
- // --------------------------------------------------------------------------------------------- //
- /// <summary>A vertex buffer that stores the vertices for a cube</summary>
- typedef Nuclex::Graphics::Rasterization::VertexBuffer<CubeVertex> CubeVertexBuffer;
- /// <summary>Constructs a cube in a vertex buffer</summary>
- /// <returns>A vertex buffer containing the vertices for a cube</summary>
- inline std::shared_ptr<CubeVertexBuffer> BuildCubeVertexBuffer() {
- const std::size_t vertexCount = 8; // A cube has 8 corners :o)
- std::shared_ptr<CubeVertexBuffer> vertexBuffer(
- new CubeVertexBuffer(vertexCount)
- );
- // Filling a vertex buffer is very simple. If the vertex buffer is already being
- // observed, you can call SuppressNotifications() followed by ResumeNotifications()
- // to avoid the overhead of many short-time locks or fill the vertex buffer in one
- // go by using the Write(bufferStartIndex, vertex *, vertexCount) method, of course.
- vertexBuffer->Append(CubeVertex(-0.5f, -0.5f, -0.5f, 0.0f, 0.0f));
- vertexBuffer->Append(CubeVertex(-0.5f, -0.5f, 0.5f, 0.0f, 0.0f));
- vertexBuffer->Append(CubeVertex(-0.5f, 0.5f, -0.5f, 0.0f, 0.0f));
- vertexBuffer->Append(CubeVertex(-0.5f, 0.5f, 0.5f, 0.0f, 0.0f));
- vertexBuffer->Append(CubeVertex( 0.5f, -0.5f, -0.5f, 0.0f, 0.0f));
- vertexBuffer->Append(CubeVertex( 0.5f, -0.5f, 0.5f, 0.0f, 0.0f));
- vertexBuffer->Append(CubeVertex( 0.5f, 0.5f, -0.5f, 0.0f, 0.0f));
- vertexBuffer->Append(CubeVertex( 0.5f, 0.5f, 0.5f, 0.0f, 0.0f));
- return vertexBuffer;
- }
- // --------------------------------------------------------------------------------------------- //
- /// <summary>An index buffer that defines how to connect the cube's vertices</summary>
- typedef Nuclex::Graphics::Rasterization::IndexBuffer<std::uint16_t> CubeIndexBuffer;
- /// <summary>Constructs a cube in a vertex buffer</summary>
- /// <returns>A vertex buffer containing the vertices for a cube</summary>
- inline std::shared_ptr<CubeIndexBuffer> BuildCubeIndexBuffer() {
- const std::size_t indexCount = 6 * 6; // 6 sides with 2 triangles (6 vertices) each
- std::shared_ptr<CubeIndexBuffer> indexBuffer(
- new CubeIndexBuffer(indexCount)
- );
- // The process of filling an index buffer works exactly the same as it does
- // for a vertex buffer. You should call SuppressNotifications() followed by
- // ResumeNotifications() if you modify live index buffers or use the
- // Write(bufferStartIndex, index *, indexCount) method.
- indexBuffer->Append(0).Append(2).Append(1); // -x
- indexBuffer->Append(1).Append(2).Append(3);
- indexBuffer->Append(4).Append(5).Append(6); // +x
- indexBuffer->Append(5).Append(7).Append(6);
- indexBuffer->Append(0).Append(1).Append(5); // -y
- indexBuffer->Append(0).Append(5).Append(4);
- indexBuffer->Append(2).Append(6).Append(7); // +y
- indexBuffer->Append(2).Append(7).Append(3);
- indexBuffer->Append(0).Append(4).Append(6); // -z
- indexBuffer->Append(0).Append(6).Append(2);
- indexBuffer->Append(1).Append(3).Append(7); // +z
- indexBuffer->Append(1).Append(7).Append(5);
- return indexBuffer;
- }
- // --------------------------------------------------------------------------------------------- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement