CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
VertexLayout.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "VertexDeclaration.hpp"
11
14
16
17namespace CeresEngine {
18
24
26 size_t offset = 0;
27
30 size_t stride = 0;
31
36 [[nodiscard]] size_t operator[](const size_t index) const { return offset + stride * index; }
37
45 template<typename T> StridedMemoryView<const T> view(const MemoryView<const Byte> view, const size_t count = 0) const {
48 return StridedMemoryView<const T>(reinterpret_cast<const T*>(slice.data()), slice.size() / stride, stride);
49 }
50
58 template<typename T> StridedMemoryView<T> view(const MemoryView<Byte> view, const size_t count = 0) const {
60 const MemoryView<Byte>& slice = view.slice(offset, stride * count);
61 return StridedMemoryView<T>(reinterpret_cast<T*>(slice.data()), slice.size() / stride, stride);
62 }
63 };
64
66 struct VertexLayout {
68
75 void addElement(VertexElement element, const size_t offset, size_t stride = ~0ul) {
76 if(stride == ~0ul) {
77 stride = element.getSize();
78 }
79
81 .element = element,
82 .offset = offset,
83 .stride = stride,
84 });
85 }
86
93 const std::size_t stride = declaration.getSize();
94 std::size_t offset = 0;
95
96 VertexLayout layout;
97 for(const VertexElement& element : declaration.elements) {
98 layout.addElement(element, offset, stride);
99 offset += element.getSize();
100 }
101
102 return layout;
103 }
104
112 static VertexLayout separated(const VertexDeclaration& declaration, const std::size_t vertexCount) {
113 std::size_t offset = 0;
114
115 VertexLayout layout;
116 for(const VertexElement& element : declaration.elements) {
117 const std::size_t length = element.getSize() * vertexCount;
118 layout.addElement(element, offset, element.getSize());
119 offset += length;
120 }
121
122 return layout;
123 }
124 };
125
126} // namespace CeresEngine
#define CE_ASSERT(...)
Definition Macros.hpp:323
A memory view is a class which attaches to an chunk of memory and provides a view to it (optionally c...
Definition MemoryView.hpp:62
SizeType size() const noexcept
Gets the size of the view as number of elements.
Definition MemoryView.hpp:294
MemoryView< T > slice(SizeType offset, SizeType length=~0ul) const noexcept
Slices the memory view by taking the element that starts at offset and returns a new view with the gi...
Definition MemoryView.hpp:145
ConstPointer data() const noexcept
Gets a pointer to the first element in the view.
Definition MemoryView.hpp:227
A memory view is a class which attaches to an chunk of memory and provides a view to it (optionally c...
Definition MemoryView.hpp:439
Determines how a single element should be a vertex.
Definition VertexDeclaration.hpp:153
bool is() const noexcept
Checks if the vertex element type and component type matches the C++ type T.
Definition VertexDeclaration.hpp:480
std::size_t getSize() const noexcept
Returns the length, in bytes, of the vertex element.
Definition Application.hpp:19
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
constexpr CountAlgorithmFunctor count
Returns the number of elements matching an element.
Definition Count.hpp:82
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Determines how vertices are laid-out on a vertex buffer or a mesh data.
Definition VertexDeclaration.hpp:205
Represents the memory position of a single vertex element.
Definition VertexLayout.hpp:20
StridedMemoryView< const T > view(const MemoryView< const Byte > view, const size_t count=0) const
Returns a StridedMemoryView that allows iterating over every vertex of of this element in a vertex bu...
Definition VertexLayout.hpp:45
size_t operator[](const size_t index) const
Computes the absolute offset of the vertex at position index.
Definition VertexLayout.hpp:36
size_t stride
The stride, the amount of bytes to skip to reach the next vertex data.
Definition VertexLayout.hpp:30
VertexElement element
The vertex element description.
Definition VertexLayout.hpp:23
StridedMemoryView< T > view(const MemoryView< Byte > view, const size_t count=0) const
Returns a StridedMemoryView that allows iterating over every vertex of of this element in a vertex bu...
Definition VertexLayout.hpp:58
size_t offset
The offset to where this vertex element starts.
Definition VertexLayout.hpp:26
Contains the definition of a single vertex buffer layout.
Definition VertexLayout.hpp:66
static VertexLayout interleaved(const VertexDeclaration &declaration)
Computes a layout in which every vertex element in declaration is interleaved, that is,...
Definition VertexLayout.hpp:92
void addElement(VertexElement element, const size_t offset, size_t stride=~0ul)
Adds a new element to the vertex layout.
Definition VertexLayout.hpp:75
Vector< VertexLayoutElement > layoutElements
Definition VertexLayout.hpp:67
static VertexLayout separated(const VertexDeclaration &declaration, const std::size_t vertexCount)
Computes a layout in which every vertex element in declaration is separated, that is,...
Definition VertexLayout.hpp:112