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
vertex.hpp
Go to the documentation of this file.
1
10
11#ifndef FOOLSENGINE_VERTEX_H
12#define FOOLSENGINE_VERTEX_H
13
14#include <string>
15#include <glm/glm.hpp>
16#include <nlohmann/json.hpp>
17
18namespace ToyMaker {
19
26 LOCATION_POSITION=0, //< Location usually mapped to a vertex shader's "attrPosition" vec4 attribute.
27 LOCATION_NORMAL=1, //< Location usually mapped to a vertex shader's "attrNormal" vec4 attribute.
28 LOCATION_TANGENT=2, //< Location usually mapped to a vertex shader's "attrTangent" vec4 attribute.
29 LOCATION_COLOR=3, //< Location usually mapped to a vertex shader's "attrColor" vec4 attribute.
30 LOCATION_UV1=4, //< Location usually mapped to a vertex shader's "attrUV1" vec2 attribute.
31 LOCATION_UV2=5, //< Location usually mapped to a vertex shader's "attrUV2" vec2 attribute.
32 LOCATION_UV3=6, //< Location usually mapped to a vertex shader's "attrUV3" vec2 attribute.
33 };
34
49 VertexAttributeDescriptor(const std::string& name, GLint layoutLocation, GLuint nComponents=1, GLenum type=GL_FLOAT)
50 :
51 mName{name}, mLayoutLocation{layoutLocation}, mNComponents{nComponents},
52 mType{type}, mSize { GetGLTypeSize(type) * nComponents}
53 {
54 assert(mNComponents >= 1 && mNComponents <= 4);
55 assert(mType == GL_FLOAT || mType == GL_UNSIGNED_INT || mType == GL_INT);
56 }
57
62 std::string mName;
63
69
75
80 GLenum mType;
81
88 std::size_t mSize;
89
99 bool operator==(const VertexAttributeDescriptor& other) const {
100 if(
101 other.mName == mName
103 && other.mNComponents == mNComponents
104 && other.mType == mType
105 && other.mSize == mSize
106 ) return true;
107 return false;
108 }
109
110 private:
117 static std::size_t GetGLTypeSize(GLenum type){
118 std::size_t componentSize;
119 switch(type) {
120 case GL_FLOAT:
121 // fall through
122 case GL_INT:
123 // fall through
124 case GL_UNSIGNED_INT:
125 componentSize = sizeof(GLfloat);
126 break;
127 default:
128 assert(false && "Unsupported or invalid attribute component type specified");
129 }
130 return componentSize;
131 }
132 };
133
147 VertexLayout(std::vector<VertexAttributeDescriptor> attributeList) : mAttributeList{attributeList} {}
148
154 std::vector<VertexAttributeDescriptor> getAttributeList() const {
155 return mAttributeList;
156 }
157
164 std::size_t computeStride() const {
165 std::size_t stride {0};
166 for(const auto& attribute : mAttributeList) {
167 stride += attribute.mSize;
168 }
169 return stride;
170 }
171
178 std::size_t computeRelativeOffset(std::size_t attributeIndex) const {
179 assert(attributeIndex < mAttributeList.size());
180 std::size_t baseOffset {0};
181 for(std::size_t i {0}; i < attributeIndex; ++i) {
182 baseOffset += mAttributeList[i].mSize;
183 }
184 return baseOffset;
185 }
186
198 bool isSubsetOf(const VertexLayout& other) const {
199 if(mAttributeList.size() > other.mAttributeList.size()) return false;
200
201 std::size_t myAttributeIndex {0};
202 for(std::size_t i{0}; i < other.mAttributeList.size() && myAttributeIndex < mAttributeList.size(); ++i) {
203 if(other.mAttributeList[i] == mAttributeList[myAttributeIndex]){
204 ++myAttributeIndex;
205 if(myAttributeIndex == mAttributeList.size()) return true;
206 }
207 }
208
209 return false;
210 }
211
212 private:
217 std::vector<VertexAttributeDescriptor> mAttributeList {};
218 };
219
230 glm::vec4 mPosition;
231
236 glm::vec4 mNormal;
237
242 glm::vec4 mTangent;
243
248 glm::vec4 mColor {1.f}; // by default, white
249
254 glm::vec2 mUV1;
255
262 glm::vec2 mUV2;
263
270 glm::vec2 mUV3;
271 };
272
274 inline void from_json(const nlohmann::json& json, BuiltinVertexData& builtinVertexData) {
275 json.at("position").at(0).get_to(builtinVertexData.mPosition.x);
276 json.at("position").at(1).get_to(builtinVertexData.mPosition.y);
277 json.at("position").at(2).get_to(builtinVertexData.mPosition.z);
278 json.at("position").at(3).get_to(builtinVertexData.mPosition.w);
279 json.at("normal").at(0).get_to(builtinVertexData.mNormal.x);
280 json.at("normal").at(1).get_to(builtinVertexData.mNormal.y);
281 json.at("normal").at(2).get_to(builtinVertexData.mNormal.z);
282 json.at("normal").at(3).get_to(builtinVertexData.mNormal.w);
283 json.at("tangent").at(0).get_to(builtinVertexData.mTangent.x);
284 json.at("tangent").at(1).get_to(builtinVertexData.mTangent.y);
285 json.at("tangent").at(2).get_to(builtinVertexData.mTangent.z);
286 json.at("tangent").at(3).get_to(builtinVertexData.mTangent.w);
287 json.at("color").at(0).get_to(builtinVertexData.mColor.r);
288 json.at("color").at(1).get_to(builtinVertexData.mColor.g);
289 json.at("color").at(2).get_to(builtinVertexData.mColor.b);
290 json.at("color").at(3).get_to(builtinVertexData.mColor.a);
291 json.at("uv1").at(0).get_to(builtinVertexData.mUV1.s);
292 json.at("uv1").at(1).get_to(builtinVertexData.mUV1.t);
293 json.at("uv2").at(0).get_to(builtinVertexData.mUV2.s);
294 json.at("uv2").at(1).get_to(builtinVertexData.mUV2.t);
295 json.at("uv3").at(0).get_to(builtinVertexData.mUV3.s);
296 json.at("uv3").at(1).get_to(builtinVertexData.mUV3.t);
297 }
298
300 inline void to_json(nlohmann::json& json, const BuiltinVertexData& builtinVertexData) {
301 json = {
302 {"position",
303 {
304 builtinVertexData.mPosition.x,
305 builtinVertexData.mPosition.y,
306 builtinVertexData.mPosition.z,
307 builtinVertexData.mPosition.w
308 }
309 },
310 {"normal",
311 {
312 builtinVertexData.mNormal.x,
313 builtinVertexData.mNormal.y,
314 builtinVertexData.mNormal.z,
315 builtinVertexData.mNormal.w,
316 }
317 },
318 {"tangent",
319 {
320 builtinVertexData.mTangent.x,
321 builtinVertexData.mTangent.y,
322 builtinVertexData.mTangent.z,
323 builtinVertexData.mTangent.w,
324 }
325 },
326 {"color",
327 {
328 builtinVertexData.mColor.r,
329 builtinVertexData.mColor.g,
330 builtinVertexData.mColor.b,
331 builtinVertexData.mColor.a,
332 }
333 },
334 {"uv1",
335 {
336 builtinVertexData.mUV1.s,
337 builtinVertexData.mUV1.t,
338 }
339 },
340 {"uv2",
341 {
342 builtinVertexData.mUV2.s,
343 builtinVertexData.mUV2.t,
344 }
345 },
346 {"uv3",
347 {
348 builtinVertexData.mUV3.s,
349 builtinVertexData.mUV3.t,
350 }
351 }
352 };
353 }
354
361 {
362 {"position", LOCATION_POSITION, 4, GL_FLOAT},
363 {"normal", LOCATION_NORMAL, 4, GL_FLOAT},
364 {"tangent", LOCATION_TANGENT, 4, GL_FLOAT},
365 {"color", LOCATION_COLOR, 4, GL_FLOAT},
366 {"UV1", LOCATION_UV1, 2, GL_FLOAT},
367 {"UV2", LOCATION_UV2, 2, GL_FLOAT},
368 {"UV3", LOCATION_UV3, 2, GL_FLOAT}
369 }
370 };
371
372}
373
374#endif
DefaultAttributeLocation
Values for different attribute locations used by the engine's built-in shader programs.
Definition vertex.hpp:25
VertexLayout BuiltinVertexLayout
The vertex layout used by most shader programs built into the engine.
Definition vertex.hpp:360
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition camera_system.hpp:20
The vertex data used by all this engine's in-built shader program's vertex shaders.
Definition vertex.hpp:225
glm::vec4 mNormal
The normal vector to the vertex (with w set to 0.f)
Definition vertex.hpp:236
glm::vec4 mColor
This vertex's color, where each color component is a value between 0.f and 1.f.
Definition vertex.hpp:248
glm::vec4 mPosition
The position of the vertex (normally with w set to 1.f).
Definition vertex.hpp:230
glm::vec2 mUV1
The UV coordinates corresponding to this vertex, with coordinates pointing into the first texture set...
Definition vertex.hpp:254
glm::vec2 mUV2
(currently unused) Coordinates pointing into the second texture set used by this vertex's owning mode...
Definition vertex.hpp:262
glm::vec4 mTangent
The tangent vector to the vertex (with w set to 0.f)
Definition vertex.hpp:242
glm::vec2 mUV3
(currently unused) Coordinates pointing into the third texture set used by this vertex's owning model...
Definition vertex.hpp:270
bool operator==(const VertexAttributeDescriptor &other) const
Compares two vertex attribute descriptors for equality.
Definition vertex.hpp:99
GLint mLayoutLocation
The layout location ID of the attribute, as specified by the shader.
Definition vertex.hpp:68
GLuint mNComponents
The number of components making up the attribute.
Definition vertex.hpp:74
static std::size_t GetGLTypeSize(GLenum type)
Gets the size of the type of a single component of an attribute.
Definition vertex.hpp:117
std::string mName
The name of the attribute, per the shader.
Definition vertex.hpp:62
VertexAttributeDescriptor(const std::string &name, GLint layoutLocation, GLuint nComponents=1, GLenum type=GL_FLOAT)
Creates a single vertex attribute descriptor.
Definition vertex.hpp:49
GLenum mType
The type of component used by the attribute, such as GL_FLOAT or GL_UNSIGNED_BYTE.
Definition vertex.hpp:80
std::size_t mSize
The computed size of the attribute, in bytes.
Definition vertex.hpp:88
A list of attribute descriptors that together define the layout and size of the vertex they make up i...
Definition vertex.hpp:141
bool isSubsetOf(const VertexLayout &other) const
Tests whether this vertex layout is the subset of another.
Definition vertex.hpp:198
std::size_t computeStride() const
Computes the total space occupied by a single vertex including all its attributes in memory.
Definition vertex.hpp:164
std::vector< VertexAttributeDescriptor > getAttributeList() const
Gets the list of attribute descriptors making up this layout.
Definition vertex.hpp:154
std::size_t computeRelativeOffset(std::size_t attributeIndex) const
Given the index of an attribute descriptor within the layout, computes the offset to that attribute f...
Definition vertex.hpp:178
std::vector< VertexAttributeDescriptor > mAttributeList
The list of attribute descriptors that define this layout.
Definition vertex.hpp:217
VertexLayout(std::vector< VertexAttributeDescriptor > attributeList)
Create a vertex layout from a list of attribute descriptors.
Definition vertex.hpp:147