ToyMaker Game Engine 0.0.2
ToyMaker is a game engine developed and maintained by Zoheb Shujauddin.
Loading...
Searching...
No Matches
vertex.hpp
Go to the documentation of this file.
1
10
11#ifndef TOYMAKERENGINE_VERTEX_H
12#define TOYMAKERENGINE_VERTEX_H
13
14#include <SDL3/SDL_opengl.h>
15
16#include <string>
17#include <glm/glm.hpp>
18#include <nlohmann/json.hpp>
19
20namespace ToyMaker {
21
28 LOCATION_POSITION=0, //< Location usually mapped to a vertex shader's "attrPosition" vec4 attribute.
29 LOCATION_NORMAL=1, //< Location usually mapped to a vertex shader's "attrNormal" vec4 attribute.
30 LOCATION_TANGENT=2, //< Location usually mapped to a vertex shader's "attrTangent" vec4 attribute.
31 LOCATION_COLOR=3, //< Location usually mapped to a vertex shader's "attrColor" vec4 attribute.
32 LOCATION_UV1=4, //< Location usually mapped to a vertex shader's "attrUV1" vec2 attribute.
33 LOCATION_UV2=5, //< Location usually mapped to a vertex shader's "attrUV2" vec2 attribute.
34 LOCATION_UV3=6, //< Location usually mapped to a vertex shader's "attrUV3" vec2 attribute.
35 };
36
51 VertexAttributeDescriptor(const std::string& name, GLint layoutLocation, GLuint nComponents=1, GLenum type=GL_FLOAT)
52 :
53 mName{name}, mLayoutLocation{layoutLocation}, mNComponents{nComponents},
54 mType{type}, mSize { GetGLTypeSize(type) * nComponents}
55 {
56 assert(mNComponents >= 1 && mNComponents <= 4);
57 assert(mType == GL_FLOAT || mType == GL_UNSIGNED_INT || mType == GL_INT);
58 }
59
64 std::string mName;
65
71
77
82 GLenum mType;
83
90 std::size_t mSize;
91
101 bool operator==(const VertexAttributeDescriptor& other) const {
102 if(
103 other.mName == mName
105 && other.mNComponents == mNComponents
106 && other.mType == mType
107 && other.mSize == mSize
108 ) return true;
109 return false;
110 }
111
112 private:
119 static std::size_t GetGLTypeSize(GLenum type){
120 std::size_t componentSize;
121 switch(type) {
122 case GL_FLOAT:
123 // fall through
124 case GL_INT:
125 // fall through
126 case GL_UNSIGNED_INT:
127 componentSize = sizeof(GLfloat);
128 break;
129 default:
130 assert(false && "Unsupported or invalid attribute component type specified");
131 }
132 return componentSize;
133 }
134 };
135
149 VertexLayout(std::vector<VertexAttributeDescriptor> attributeList) : mAttributeList{attributeList} {}
150
156 std::vector<VertexAttributeDescriptor> getAttributeList() const {
157 return mAttributeList;
158 }
159
166 std::size_t computeStride() const {
167 std::size_t stride {0};
168 for(const auto& attribute : mAttributeList) {
169 stride += attribute.mSize;
170 }
171 return stride;
172 }
173
180 std::size_t computeRelativeOffset(std::size_t attributeIndex) const {
181 assert(attributeIndex < mAttributeList.size());
182 std::size_t baseOffset {0};
183 for(std::size_t i {0}; i < attributeIndex; ++i) {
184 baseOffset += mAttributeList[i].mSize;
185 }
186 return baseOffset;
187 }
188
200 bool isSubsetOf(const VertexLayout& other) const {
201 if(mAttributeList.size() > other.mAttributeList.size()) return false;
202
203 std::size_t myAttributeIndex {0};
204 for(std::size_t i{0}; i < other.mAttributeList.size() && myAttributeIndex < mAttributeList.size(); ++i) {
205 if(other.mAttributeList[i] == mAttributeList[myAttributeIndex]){
206 ++myAttributeIndex;
207 if(myAttributeIndex == mAttributeList.size()) return true;
208 }
209 }
210
211 return false;
212 }
213
214 private:
219 std::vector<VertexAttributeDescriptor> mAttributeList {};
220 };
221
232 glm::vec4 mPosition;
233
238 glm::vec4 mNormal;
239
244 glm::vec4 mTangent;
245
250 glm::vec4 mColor {1.f}; // by default, white
251
256 glm::vec2 mUV1;
257
264 glm::vec2 mUV2;
265
272 glm::vec2 mUV3;
273 };
274
276 inline void from_json(const nlohmann::json& json, BuiltinVertexData& builtinVertexData) {
277 json.at("position").at(0).get_to(builtinVertexData.mPosition.x);
278 json.at("position").at(1).get_to(builtinVertexData.mPosition.y);
279 json.at("position").at(2).get_to(builtinVertexData.mPosition.z);
280 json.at("position").at(3).get_to(builtinVertexData.mPosition.w);
281 json.at("normal").at(0).get_to(builtinVertexData.mNormal.x);
282 json.at("normal").at(1).get_to(builtinVertexData.mNormal.y);
283 json.at("normal").at(2).get_to(builtinVertexData.mNormal.z);
284 json.at("normal").at(3).get_to(builtinVertexData.mNormal.w);
285 json.at("tangent").at(0).get_to(builtinVertexData.mTangent.x);
286 json.at("tangent").at(1).get_to(builtinVertexData.mTangent.y);
287 json.at("tangent").at(2).get_to(builtinVertexData.mTangent.z);
288 json.at("tangent").at(3).get_to(builtinVertexData.mTangent.w);
289 json.at("color").at(0).get_to(builtinVertexData.mColor.r);
290 json.at("color").at(1).get_to(builtinVertexData.mColor.g);
291 json.at("color").at(2).get_to(builtinVertexData.mColor.b);
292 json.at("color").at(3).get_to(builtinVertexData.mColor.a);
293 json.at("uv1").at(0).get_to(builtinVertexData.mUV1.s);
294 json.at("uv1").at(1).get_to(builtinVertexData.mUV1.t);
295 json.at("uv2").at(0).get_to(builtinVertexData.mUV2.s);
296 json.at("uv2").at(1).get_to(builtinVertexData.mUV2.t);
297 json.at("uv3").at(0).get_to(builtinVertexData.mUV3.s);
298 json.at("uv3").at(1).get_to(builtinVertexData.mUV3.t);
299 }
300
302 inline void to_json(nlohmann::json& json, const BuiltinVertexData& builtinVertexData) {
303 json = {
304 {"position",
305 {
306 builtinVertexData.mPosition.x,
307 builtinVertexData.mPosition.y,
308 builtinVertexData.mPosition.z,
309 builtinVertexData.mPosition.w
310 }
311 },
312 {"normal",
313 {
314 builtinVertexData.mNormal.x,
315 builtinVertexData.mNormal.y,
316 builtinVertexData.mNormal.z,
317 builtinVertexData.mNormal.w,
318 }
319 },
320 {"tangent",
321 {
322 builtinVertexData.mTangent.x,
323 builtinVertexData.mTangent.y,
324 builtinVertexData.mTangent.z,
325 builtinVertexData.mTangent.w,
326 }
327 },
328 {"color",
329 {
330 builtinVertexData.mColor.r,
331 builtinVertexData.mColor.g,
332 builtinVertexData.mColor.b,
333 builtinVertexData.mColor.a,
334 }
335 },
336 {"uv1",
337 {
338 builtinVertexData.mUV1.s,
339 builtinVertexData.mUV1.t,
340 }
341 },
342 {"uv2",
343 {
344 builtinVertexData.mUV2.s,
345 builtinVertexData.mUV2.t,
346 }
347 },
348 {"uv3",
349 {
350 builtinVertexData.mUV3.s,
351 builtinVertexData.mUV3.t,
352 }
353 }
354 };
355 }
356
363 {
364 {"position", LOCATION_POSITION, 4, GL_FLOAT},
365 {"normal", LOCATION_NORMAL, 4, GL_FLOAT},
366 {"tangent", LOCATION_TANGENT, 4, GL_FLOAT},
367 {"color", LOCATION_COLOR, 4, GL_FLOAT},
368 {"UV1", LOCATION_UV1, 2, GL_FLOAT},
369 {"UV2", LOCATION_UV2, 2, GL_FLOAT},
370 {"UV3", LOCATION_UV3, 2, GL_FLOAT}
371 }
372 };
373
374}
375
376#endif
DefaultAttributeLocation
Values for different attribute locations used by the engine's built-in shader programs.
Definition vertex.hpp:27
VertexLayout BuiltinVertexLayout
The vertex layout used by most shader programs built into the engine.
Definition vertex.hpp:362
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition application.hpp:25
The vertex data used by all this engine's in-built shader program's vertex shaders.
Definition vertex.hpp:227
glm::vec4 mNormal
The normal vector to the vertex (with w set to 0.f).
Definition vertex.hpp:238
glm::vec4 mColor
This vertex's color, where each color component is a value between 0.f and 1.f.
Definition vertex.hpp:250
glm::vec4 mPosition
The position of the vertex (normally with w set to 1.f).
Definition vertex.hpp:232
glm::vec2 mUV1
The UV coordinates corresponding to this vertex, with coordinates pointing into the first texture set...
Definition vertex.hpp:256
glm::vec2 mUV2
(currently unused) Coordinates pointing into the second texture set used by this vertex's owning mode...
Definition vertex.hpp:264
glm::vec4 mTangent
The tangent vector to the vertex (with w set to 0.f).
Definition vertex.hpp:244
glm::vec2 mUV3
(currently unused) Coordinates pointing into the third texture set used by this vertex's owning model...
Definition vertex.hpp:272
bool operator==(const VertexAttributeDescriptor &other) const
Compares two vertex attribute descriptors for equality.
Definition vertex.hpp:101
GLint mLayoutLocation
The layout location ID of the attribute, as specified by the shader.
Definition vertex.hpp:70
GLuint mNComponents
The number of components making up the attribute.
Definition vertex.hpp:76
static std::size_t GetGLTypeSize(GLenum type)
Gets the size of the type of a single component of an attribute.
Definition vertex.hpp:119
std::string mName
The name of the attribute, per the shader.
Definition vertex.hpp:64
VertexAttributeDescriptor(const std::string &name, GLint layoutLocation, GLuint nComponents=1, GLenum type=GL_FLOAT)
Creates a single vertex attribute descriptor.
Definition vertex.hpp:51
GLenum mType
The type of component used by the attribute, such as GL_FLOAT or GL_UNSIGNED_BYTE.
Definition vertex.hpp:82
std::size_t mSize
The computed size of the attribute, in bytes.
Definition vertex.hpp:90
A list of attribute descriptors that together define the layout and size of the vertex they make up i...
Definition vertex.hpp:143
bool isSubsetOf(const VertexLayout &other) const
Tests whether this vertex layout is the subset of another.
Definition vertex.hpp:200
std::size_t computeStride() const
Computes the total space occupied by a single vertex including all its attributes in memory.
Definition vertex.hpp:166
std::vector< VertexAttributeDescriptor > getAttributeList() const
Gets the list of attribute descriptors making up this layout.
Definition vertex.hpp:156
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:180
std::vector< VertexAttributeDescriptor > mAttributeList
The list of attribute descriptors that define this layout.
Definition vertex.hpp:219
VertexLayout(std::vector< VertexAttributeDescriptor > attributeList)
Create a vertex layout from a list of attribute descriptors.
Definition vertex.hpp:149