Game of Ur 0.3.3
This is a computer adaptation of Game of Ur, written in C++ mainly using SDL and OpenGL.
Loading...
Searching...
No Matches
instance.hpp
Go to the documentation of this file.
1
10
11#ifndef FOOLSENGINE_INSTANCE
12#define FOOLSENGINE_INSTANCE
13
14#include <vector>
15#include <string>
16
17#include <glm/glm.hpp>
18#include <GL/glew.h>
19
20namespace ToyMaker {
27 FIXED_MATRIXMODEL=7,
28 RUNTIME=-8,
29 };
30
45 InstanceAttributeDescriptor(const std::string& name, const GLint layoutLocation, GLuint nComponents, GLenum type=GL_FLOAT)
46 : mName {name}, mLayoutLocation { layoutLocation },
47 mNComponents {nComponents}, mType {type},
48 mSize{GetGLTypeSize(type) * nComponents}
49 {}
50
55 const std::string mName;
56
61 const GLint mLayoutLocation;
62
67 const GLuint mNComponents;
68
73 const GLenum mType;
74
79 const std::size_t mSize;
80
92 bool operator==(const InstanceAttributeDescriptor& other) const {
93 if(
94 other.mName == mName
96 && other.mNComponents == mNComponents
97 && other.mType == mType
98 && other.mSize == mSize
99 ) return true;
100 return false;
101 }
102
103 private:
110 static std::size_t GetGLTypeSize(GLenum type){
111 std::size_t componentSize;
112 switch(type) {
113 case GL_FLOAT:
114 // fall through
115 case GL_INT:
116 // fall through
117 case GL_UNSIGNED_INT:
118 componentSize = sizeof(GLfloat);
119 break;
120
121 default:
122 assert(false && "Unrecognized or unsupported type");
123 }
124 return componentSize;
125 }
126 };
127
136 public:
142 InstanceLayout(const std::vector<InstanceAttributeDescriptor>& attributeList) : mAttributeList{attributeList} {}
143
149 std::vector<InstanceAttributeDescriptor> getAttributeList() const {
150 return mAttributeList;
151 }
152
158 std::size_t computeStride() const {
159 std::size_t stride {0};
160 for(const auto& attribute: mAttributeList) {
161 stride += attribute.mSize;
162 }
163 return stride;
164 }
165
172 std::size_t computeRelativeOffset(std::size_t attributeIndex) const {
173 assert(attributeIndex < mAttributeList.size());
174 std::size_t baseOffset {0};
175 for(std::size_t i{0} ; i < attributeIndex; ++i) {
176 baseOffset += mAttributeList[i].mSize;
177 }
178 return baseOffset;
179 }
180
188 bool isSubsetOf(const InstanceLayout& other) const {
189 if(mAttributeList.size() > other.mAttributeList.size()) return false;
190
191 std::size_t myAttributeIndex {0};
192 for(std::size_t i{0}; i < other.mAttributeList.size() && myAttributeIndex < mAttributeList.size(); ++i) {
193 if(other.mAttributeList[i] == mAttributeList[myAttributeIndex]) {
194 ++ myAttributeIndex;
195 if(myAttributeIndex == mAttributeList.size()) return true;
196 }
197 }
198
199 return false;
200 }
201
202 private:
207 std::vector<InstanceAttributeDescriptor> mAttributeList;
208 };
209
218 public:
225 : mInstanceLayout(instanceLayout)
226 {}
227
229 BaseInstanceAllocator(const BaseInstanceAllocator& other) = delete;
230
231 BaseInstanceAllocator& operator=(BaseInstanceAllocator&& other) = delete;
232 BaseInstanceAllocator& operator=(const BaseInstanceAllocator& other) = delete;
233
238 virtual ~BaseInstanceAllocator();
239
246
252 void bind(const InstanceLayout& shaderInstanceLayout);
253
258 void unbind();
259
266 bool isUploaded() { return mUploaded; }
267
268 protected:
273 virtual void upload() = 0;
274
280
281 private:
286 void unload();
287
294 void _upload();
295
302 void setAttributePointers(const InstanceLayout& shaderInstanceLayout, std::size_t startingOffset = 0);
303
309
314 bool mUploaded {false};
315 };
316
323 {
324 {"modelMatrixCol0", FIXED_MATRIXMODEL, 4, GL_FLOAT},
325 {"modelMatrixCol1", FIXED_MATRIXMODEL+1, 4, GL_FLOAT},
326 {"modelMatrixCol2", FIXED_MATRIXMODEL+2, 4, GL_FLOAT},
327 {"modelMatrixCol3", FIXED_MATRIXMODEL+3, 4, GL_FLOAT},
328 }
329 };
330
340 class BuiltinModelMatrixAllocator : public BaseInstanceAllocator {
341 public:
342 BuiltinModelMatrixAllocator(std::vector<glm::mat4> modelMatrices)
343 :
345 mModelMatrices{modelMatrices}
346 {}
347
348 protected:
349 virtual void upload() override;
350
351 private:
352 std::vector<glm::mat4> mModelMatrices;
353 };
354}
355
356#endif
Class that is responsible for taking an instance layout and correctly uploading it to the GPU.
Definition instance.hpp:217
BaseInstanceAllocator(const InstanceLayout &instanceLayout)
Construct a new base instance allocator object.
Definition instance.hpp:224
bool isUploaded()
Tests whether the attribute data associated with this allocator has been uploaded to memory.
Definition instance.hpp:266
GLuint mVertexBufferIndex
The OpenGL handle associated with the buffer that this object's instance data has been storerd on.
Definition instance.hpp:279
virtual ~BaseInstanceAllocator()
Destroys the allocator object.
Definition instance.cpp:7
void unload()
Deallocates instance data from GPU, deletes the associated vertex buffer.
Definition instance.cpp:24
void _upload()
Method which calls the override for upload() defined by a subclass.
Definition instance.cpp:16
void bind(const InstanceLayout &shaderInstanceLayout)
Binds a (subset of) this object's instance attributes to the currently active shader.
Definition instance.cpp:101
InstanceLayout mInstanceLayout
The layout associated with this allocator.
Definition instance.hpp:308
virtual void upload()=0
Uploads this object's attribute data to GPU memory.
void setAttributePointers(const InstanceLayout &shaderInstanceLayout, std::size_t startingOffset=0)
Sets attribute pointers per the data contained in the instance layout.
Definition instance.cpp:33
InstanceLayout getInstanceLayout() const
Gets the instance attribute layout for this object.
Definition instance.cpp:12
bool mUploaded
Whether or not the instance data associated with this allocator has been uploaded.
Definition instance.hpp:314
void unbind()
Unbinds this object's instance attributes.
Definition instance.cpp:117
virtual void upload() override
Uploads this object's attribute data to GPU memory.
Definition instance.cpp:121
DefaultInstanceAttributeLocations
Attribute locations, per existing shaders.
Definition instance.hpp:26
static InstanceLayout BuiltinModelMatrixLayout
The layout of the in-built model matrix instance attribute, present on pretty much every engine-defin...
Definition instance.hpp:322
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition camera_system.hpp:20
const GLenum mType
The underlying OpenGL type of the attribute.
Definition instance.hpp:73
InstanceAttributeDescriptor(const std::string &name, const GLint layoutLocation, GLuint nComponents, GLenum type=GL_FLOAT)
Constructs a new instance attribute descriptor.
Definition instance.hpp:45
const GLuint mNComponents
The number or components or dimensions that represent this attribute.
Definition instance.hpp:67
const GLint mLayoutLocation
The OpenGL shader attribute location of this attribute.
Definition instance.hpp:61
const std::string mName
Name of the attribute.
Definition instance.hpp:55
const std::size_t mSize
The size of the attribute, computed as size of mType * mNComponents.
Definition instance.hpp:79
bool operator==(const InstanceAttributeDescriptor &other) const
Compares two vertex attributes for equality.
Definition instance.hpp:92
static std::size_t GetGLTypeSize(GLenum type)
Returns the size of a few known OpenGL component types, throws an error otherwise.
Definition instance.hpp:110
Object representing the layout of one set of related attributes representing (presumably) one object ...
Definition instance.hpp:135
std::vector< InstanceAttributeDescriptor > mAttributeList
The list of attribute descriptors that make up this InstanceLayout.
Definition instance.hpp:207
bool isSubsetOf(const InstanceLayout &other) const
Tests whether another InstanceLayout has the same attributes in the same order as this one,...
Definition instance.hpp:188
std::vector< InstanceAttributeDescriptor > getAttributeList() const
Returns this layout's attribute list.
Definition instance.hpp:149
std::size_t computeRelativeOffset(std::size_t attributeIndex) const
Computes the offset of a specific attribute from the start of the instance, in bytes.
Definition instance.hpp:172
std::size_t computeStride() const
Computes the stride for this layout, i.e., the number of bytes separating the start of one instance's...
Definition instance.hpp:158
InstanceLayout(const std::vector< InstanceAttributeDescriptor > &attributeList)
Constructs a new instance layout object.
Definition instance.hpp:142