Skip to content

Commit

Permalink
Merge pull request #170 from uPiscium/develop
Browse files Browse the repository at this point in the history
Change buffer interface to use location map instead of auto atribute generating.
  • Loading branch information
uPiscium authored Dec 20, 2024
2 parents 15c5251 + c637cda commit 5b376fd
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 28 deletions.
22 changes: 12 additions & 10 deletions impls/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,22 @@ void Buffer::SetAttributeDivisor(AttributeData const &attribute,
this->Unbind();
}

void Buffer::LoadData(Shader &shader, Vec<Float> const &raw,
void Buffer::LoadData(Vec<Float> const &raw,
Map<Str, AttributeData> const &attrs,
Map<Str, Uint> const &locations,
BufferUsage const &usage) {
Ulong size = raw.size() * sizeof(Float);
shader.Use();
this->Bind();
GLObject buffer = GLObject();
glGenBuffers(1, buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, size, raw.data(), (GLenum)usage);

for (auto &[name, attr] : attrs) {
Uint index = shader.GetAttribute(name);
if (locations.find(name) == locations.end()) {
throw Exceptions::BufferError("Attribute location not found.");
}
Uint index = locations.at(name);
glEnableVertexAttribArray(index);
glVertexAttribPointer(index, attr.size, GL_FLOAT, GL_FALSE, attr.stride,
reinterpret_cast<void const *>(attr.offset));
Expand All @@ -107,26 +110,27 @@ void Buffer::LoadData(Shader &shader, Vec<Float> const &raw,
}
this->Unbind();
glBindBuffer(GL_ARRAY_BUFFER, 0);
shader.Unuse();
shader.Link();
mBuffers.push_back(buffer);
}

void Buffer::LoadData(Shader &shader, BufferDataConstructor const &bdc,
void Buffer::LoadData(BufferDataConstructor const &bdc,
Map<Str, Uint> const &locations,
BufferUsage const &usage) {
Map<Str, AttributeData> const &attributes = bdc.GetAttributes();
Vec<Float> data = bdc.GetVertexData();
Ulong size = data.size() * sizeof(Float);
shader.Use();
this->Bind();
GLObject buffer = GLObject();
glGenBuffers(1, buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, size, data.data(), (GLenum)usage);

for (auto &name : bdc.GetAttributeNames()) {
if (locations.find(name) == locations.end()) {
throw Exceptions::BufferError("Attribute location not found.");
}
AttributeData const &attr = attributes.at(name);
Uint index = shader.GetAttribute(name);
Uint index = locations.at(name);
glEnableVertexAttribArray(index);
glVertexAttribPointer(index, attr.size, GL_FLOAT, GL_FALSE, attr.stride,
reinterpret_cast<void const *>(attr.offset));
Expand All @@ -135,8 +139,6 @@ void Buffer::LoadData(Shader &shader, BufferDataConstructor const &bdc,
}
this->Unbind();
glBindBuffer(GL_ARRAY_BUFFER, 0);
shader.Unuse();
shader.Link();
mBuffers.push_back(buffer);
}

Expand Down
2 changes: 1 addition & 1 deletion impls/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void Text::LoadText() {
px += chr.advance >> 6;
}

mBuffer.LoadData(mShader, mBuffer.Flatten(vertices), mAttributes);
mBuffer.LoadData(mBuffer.Flatten(vertices), mAttributes, mLocations);
mBuffer.LoadIndices(indices);
mLastText = mText;
}
Expand Down
15 changes: 8 additions & 7 deletions includes/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,23 @@ class Buffer : public TerreateObjectBase {
}

/*
* @brief: Load data into the new buffer
* @param: shader: Shader to use to draw the buffer
* @brief: Load data into the buffer
* @param: raw: Raw data to load
* @param: attrs: Attributes to load
* @param: locations: Locations to load
* @param: usage: Buffer usage
*/
void LoadData(Shader &shader, Vec<Float> const &raw,
Map<Str, AttributeData> const &attrs,
void LoadData(Vec<Float> const &raw, Map<Str, AttributeData> const &attrs,
Map<Str, Uint> const &locations,
BufferUsage const &usage = BufferUsage::STATIC_DRAW);
/*
* @brief: Load data into the new buffer
* @param: shader: Shader to use to draw the buffer
* @brief: Load data into the buffer
* @param: bdc: Buffer data constructor object
* @param: locations: Locations to load
* @param: usage: Buffer usage
*/
void LoadData(Shader &shader, BufferDataConstructor const &bdc,
void LoadData(BufferDataConstructor const &bdc,
Map<Str, Uint> const &locations,
BufferUsage const &usage = BufferUsage::STATIC_DRAW);
/*
* @brief: Reload data into the buffer
Expand Down
1 change: 1 addition & 0 deletions includes/text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Text : public TerreateObjectBase {
Map<Str, AttributeData> mAttributes = {
{"iPosition", {0, 0, 3, 6 * sizeof(Float), 0}},
{"iUV", {0, 1, 3, 6 * sizeof(Float), 3 * sizeof(Float)}}};
Map<Str, Uint> mLocations = {{"iPosition", 0}, {"iUV", 1}};
Font *mFont = nullptr;
Core::Shader mShader;
vec3 mColor = vec3(1.0f, 1.0f, 1.0f);
Expand Down
7 changes: 4 additions & 3 deletions tests/TGTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class TestApp {
mScreenShader.Compile();
mScreenShader.Link();

Map<Str, Uint> attrs = {{"iPosition", 0}, {"iUV", 1}, {"iColor", 2}};
BufferDataConstructor bdc;

bdc.AddVertexComponent("iPosition", {{-600.0f, -600.0f, 600.0f},
Expand All @@ -203,7 +204,7 @@ class TestApp {
{3, 0}, {2, 1}, {1, 2}, {0, 3}, {4, 0}, {5, 1}, {6, 2}, {7, 3},
});
bdc.Construct();
mBuffer.LoadData(mShader, bdc);
mBuffer.LoadData(bdc, attrs);

BufferDataConstructor screenBDC;
screenBDC.AddVertexComponent("iPosition", {{-0.8f, -0.8f, 0.2f},
Expand All @@ -214,14 +215,14 @@ class TestApp {
"iUV", {{0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}});
screenBDC.SetVertexIndices({{0, 0}, {1, 1}, {2, 2}, {3, 3}});
screenBDC.Construct();
mScreenBuffer.LoadData(mScreenShader, screenBDC);
mScreenBuffer.LoadData(screenBDC, attrs);

mColorDataConstructor.AddVertexComponent("iColor", {{1, 0, 0}});
mColorDataConstructor.SetVertexIndices(
{{0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0},
{0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}});
mColorDataConstructor.Construct();
mBuffer.LoadData(mShader, mColorDataConstructor);
mBuffer.LoadData(mColorDataConstructor, attrs);

mBuffer.LoadIndices({{0, 1, 2, 2, 3, 0},
{4, 5, 6, 6, 7, 4},
Expand Down
6 changes: 3 additions & 3 deletions tests/resources/shaders/mainVert.glsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#version 430 core
in vec3 iPosition;
in vec2 iUV;
in vec3 iColor;
layout(location=0) in vec3 iPosition;
layout(location=1) in vec2 iUV;
layout(location=2) in vec3 iColor;

out vec3 vNormal;
out vec2 vUV;
Expand Down
4 changes: 2 additions & 2 deletions tests/resources/shaders/screenVert.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#version 430 core
in vec3 iPosition;
in vec2 iUV;
layout(location=0) in vec3 iPosition;
layout(location=1) in vec2 iUV;

out vec2 vUV;

Expand Down
4 changes: 2 additions & 2 deletions tests/resources/shaders/textVert.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#version 430 core
in vec3 iPosition;
in vec3 iUV;
layout(location=0) in vec3 iPosition;
layout(location=1) in vec3 iUV;

out vec3 vUV;

Expand Down

0 comments on commit 5b376fd

Please sign in to comment.