11#ifndef TOYMAKERENGINE_VERTEX_H
12#define TOYMAKERENGINE_VERTEX_H
14#include <SDL3/SDL_opengl.h>
18#include <nlohmann/json.hpp>
57 assert(
mType == GL_FLOAT ||
mType == GL_UNSIGNED_INT ||
mType == GL_INT);
120 std::size_t componentSize;
126 case GL_UNSIGNED_INT:
127 componentSize =
sizeof(GLfloat);
130 assert(
false &&
"Unsupported or invalid attribute component type specified");
132 return componentSize;
167 std::size_t stride {0};
169 stride += attribute.mSize;
182 std::size_t baseOffset {0};
183 for(std::size_t i {0}; i < attributeIndex; ++i) {
203 std::size_t myAttributeIndex {0};
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);
302 inline void to_json(nlohmann::json& json,
const BuiltinVertexData& builtinVertexData) {
306 builtinVertexData.mPosition.x,
307 builtinVertexData.mPosition.y,
308 builtinVertexData.mPosition.z,
309 builtinVertexData.mPosition.w
314 builtinVertexData.mNormal.x,
315 builtinVertexData.mNormal.y,
316 builtinVertexData.mNormal.z,
317 builtinVertexData.mNormal.w,
322 builtinVertexData.mTangent.x,
323 builtinVertexData.mTangent.y,
324 builtinVertexData.mTangent.z,
325 builtinVertexData.mTangent.w,
330 builtinVertexData.mColor.r,
331 builtinVertexData.mColor.g,
332 builtinVertexData.mColor.b,
333 builtinVertexData.mColor.a,
338 builtinVertexData.mUV1.s,
339 builtinVertexData.mUV1.t,
344 builtinVertexData.mUV2.s,
345 builtinVertexData.mUV2.t,
350 builtinVertexData.mUV3.s,
351 builtinVertexData.mUV3.t,
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}
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