[nostalgia/core/studio] Cleanup TileSheetEditor with new GlUtils helpers
This commit is contained in:
parent
9948346ce4
commit
1cf09433e8
@ -9,6 +9,37 @@
|
|||||||
|
|
||||||
namespace nostalgia::core {
|
namespace nostalgia::core {
|
||||||
|
|
||||||
|
const glutils::ProgramSource TileSheetPixels::s_programSrc = {
|
||||||
|
.shaderParams = {
|
||||||
|
{
|
||||||
|
.len = 2,
|
||||||
|
.name = ox::String("vPosition"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.len = 3,
|
||||||
|
.name = ox::String("vColor"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
.vertShader = ox::sfmt(R"(
|
||||||
|
{}
|
||||||
|
in vec2 vPosition;
|
||||||
|
in vec3 vColor;
|
||||||
|
out vec3 fColor;
|
||||||
|
uniform vec2 vScroll;
|
||||||
|
void main() {
|
||||||
|
gl_Position = vec4(vPosition + vScroll, 0.0, 1.0);
|
||||||
|
fColor = vColor;
|
||||||
|
})", core::gl::GlslVersion),
|
||||||
|
.fragShader = ox::sfmt(R"(
|
||||||
|
{}
|
||||||
|
in vec3 fColor;
|
||||||
|
out vec4 outColor;
|
||||||
|
void main() {
|
||||||
|
//outColor = vec4(0.0, 0.7, 1.0, 1.0);
|
||||||
|
outColor = vec4(fColor, 1.0);
|
||||||
|
})", core::gl::GlslVersion),
|
||||||
|
};
|
||||||
|
|
||||||
TileSheetPixels::TileSheetPixels(TileSheetEditorModel &model) noexcept: m_model(model) {
|
TileSheetPixels::TileSheetPixels(TileSheetEditorModel &model) noexcept: m_model(model) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -17,9 +48,7 @@ void TileSheetPixels::setPixelSizeMod(float sm) noexcept {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ox::Error TileSheetPixels::buildShader() noexcept {
|
ox::Error TileSheetPixels::buildShader() noexcept {
|
||||||
auto const Vshad = ox::sfmt(VShad, gl::GlslVersion);
|
return glutils::buildShaderProgram(s_programSrc).moveTo(m_shader);
|
||||||
auto const Fshad = ox::sfmt(FShad, gl::GlslVersion);
|
|
||||||
return glutils::buildShaderProgram(Vshad, Fshad).moveTo(m_shader);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TileSheetPixels::draw(bool update, ox::Vec2 const&scroll) noexcept {
|
void TileSheetPixels::draw(bool update, ox::Vec2 const&scroll) noexcept {
|
||||||
@ -40,14 +69,7 @@ void TileSheetPixels::initBufferSet(ox::Vec2 const&paneSize) noexcept {
|
|||||||
m_bufferSet.vbo = glutils::generateBuffer();
|
m_bufferSet.vbo = glutils::generateBuffer();
|
||||||
m_bufferSet.ebo = glutils::generateBuffer();
|
m_bufferSet.ebo = glutils::generateBuffer();
|
||||||
update(paneSize);
|
update(paneSize);
|
||||||
// vbo layout
|
glutils::setupShaderParams(m_shader, s_programSrc.shaderParams);
|
||||||
auto const posAttr = static_cast<GLuint>(glGetAttribLocation(m_shader, "vPosition"));
|
|
||||||
glEnableVertexAttribArray(posAttr);
|
|
||||||
glVertexAttribPointer(posAttr, 2, GL_FLOAT, GL_FALSE, VertexVboRowLength * sizeof(float), nullptr);
|
|
||||||
auto const colorAttr = static_cast<GLuint>(glGetAttribLocation(m_shader, "vColor"));
|
|
||||||
glEnableVertexAttribArray(colorAttr);
|
|
||||||
glVertexAttribPointer(colorAttr, 3, GL_FLOAT, GL_FALSE, VertexVboRowLength * sizeof(float),
|
|
||||||
std::bit_cast<void*>(uintptr_t{2 * sizeof(float)}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TileSheetPixels::update(ox::Vec2 const&paneSize) noexcept {
|
void TileSheetPixels::update(ox::Vec2 const&paneSize) noexcept {
|
||||||
@ -78,14 +100,14 @@ void TileSheetPixels::setPixelBufferObject(
|
|||||||
y += 1.0f - ymod;
|
y += 1.0f - ymod;
|
||||||
auto const r = redf(color), g = greenf(color), b = bluef(color);
|
auto const r = redf(color), g = greenf(color), b = bluef(color);
|
||||||
// don't worry, these memcpys gets optimized to something much more ideal
|
// don't worry, these memcpys gets optimized to something much more ideal
|
||||||
ox::Array<float, VertexVboLength> const vertices = {
|
std::array const vertices = {
|
||||||
x, y, r, g, b, // bottom left
|
x, y, r, g, b, // bottom left
|
||||||
x + xmod, y, r, g, b, // bottom right
|
x + xmod, y, r, g, b, // bottom right
|
||||||
x + xmod, y + ymod, r, g, b, // top right
|
x + xmod, y + ymod, r, g, b, // top right
|
||||||
x, y + ymod, r, g, b, // top left
|
x, y + ymod, r, g, b, // top left
|
||||||
};
|
};
|
||||||
memcpy(vbo, vertices.data(), sizeof(vertices));
|
memcpy(vbo, vertices.data(), sizeof(vertices));
|
||||||
ox::Array<GLuint, VertexEboLength> const elms = {
|
std::array const elms = {
|
||||||
vertexRow + 0, vertexRow + 1, vertexRow + 2,
|
vertexRow + 0, vertexRow + 1, vertexRow + 2,
|
||||||
vertexRow + 2, vertexRow + 3, vertexRow + 0,
|
vertexRow + 2, vertexRow + 3, vertexRow + 0,
|
||||||
};
|
};
|
||||||
@ -99,7 +121,8 @@ void TileSheetPixels::setBufferObjects(ox::Vec2 const&paneSize) noexcept {
|
|||||||
auto const width = subSheet.columns * TileWidth;
|
auto const width = subSheet.columns * TileWidth;
|
||||||
auto const height = subSheet.rows * TileHeight;
|
auto const height = subSheet.rows * TileHeight;
|
||||||
auto const pixels = static_cast<size_t>(width) * static_cast<size_t>(height);
|
auto const pixels = static_cast<size_t>(width) * static_cast<size_t>(height);
|
||||||
m_bufferSet.vertices.resize(pixels * VertexVboLength);
|
auto const vboLen = static_cast<size_t>(s_programSrc.vboLen);
|
||||||
|
m_bufferSet.vertices.resize(pixels * vboLen);
|
||||||
m_bufferSet.elements.resize(pixels * VertexEboLength);
|
m_bufferSet.elements.resize(pixels * VertexEboLength);
|
||||||
// set pixels
|
// set pixels
|
||||||
walkPixels(subSheet, m_model.img().bpp, [&](std::size_t i, uint8_t p) {
|
walkPixels(subSheet, m_model.img().bpp, [&](std::size_t i, uint8_t p) {
|
||||||
@ -107,9 +130,9 @@ void TileSheetPixels::setBufferObjects(ox::Vec2 const&paneSize) noexcept {
|
|||||||
auto const pt = idxToPt(static_cast<int>(i), subSheet.columns);
|
auto const pt = idxToPt(static_cast<int>(i), subSheet.columns);
|
||||||
auto const fx = static_cast<float>(pt.x);
|
auto const fx = static_cast<float>(pt.x);
|
||||||
auto const fy = static_cast<float>(pt.y);
|
auto const fy = static_cast<float>(pt.y);
|
||||||
auto const vbo = &m_bufferSet.vertices[i * VertexVboLength];
|
auto const vbo = &m_bufferSet.vertices[i * vboLen];
|
||||||
auto const ebo = &m_bufferSet.elements[i * VertexEboLength];
|
auto const ebo = &m_bufferSet.elements[i * VertexEboLength];
|
||||||
if (i * VertexVboLength + VertexVboLength > m_bufferSet.vertices.size()) {
|
if (i * vboLen + vboLen > m_bufferSet.vertices.size()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (i * VertexEboLength + VertexEboLength > m_bufferSet.elements.size()) {
|
if (i * VertexEboLength + VertexEboLength > m_bufferSet.elements.size()) {
|
||||||
|
@ -18,28 +18,9 @@ class TileSheetPixels {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr auto VertexVboRows = 4;
|
static constexpr auto VertexVboRows = 4;
|
||||||
static constexpr auto VertexVboRowLength = 5;
|
|
||||||
static constexpr auto VertexVboLength = VertexVboRows * VertexVboRowLength;
|
|
||||||
static constexpr auto VertexEboLength = 6;
|
static constexpr auto VertexEboLength = 6;
|
||||||
static constexpr auto VShad = R"(
|
|
||||||
{}
|
|
||||||
in vec2 vPosition;
|
|
||||||
in vec3 vColor;
|
|
||||||
out vec3 fColor;
|
|
||||||
uniform vec2 vScroll;
|
|
||||||
void main() {
|
|
||||||
gl_Position = vec4(vPosition + vScroll, 0.0, 1.0);
|
|
||||||
fColor = vColor;
|
|
||||||
})";
|
|
||||||
static constexpr auto FShad = R"(
|
|
||||||
{}
|
|
||||||
in vec3 fColor;
|
|
||||||
out vec4 outColor;
|
|
||||||
void main() {
|
|
||||||
//outColor = vec4(0.0, 0.7, 1.0, 1.0);
|
|
||||||
outColor = vec4(fColor, 1.0);
|
|
||||||
})";
|
|
||||||
|
|
||||||
|
static const glutils::ProgramSource s_programSrc;
|
||||||
float m_pixelSizeMod = 1;
|
float m_pixelSizeMod = 1;
|
||||||
glutils::GLProgram m_shader;
|
glutils::GLProgram m_shader;
|
||||||
glutils::BufferSet m_bufferSet;
|
glutils::BufferSet m_bufferSet;
|
||||||
|
Loading…
Reference in New Issue
Block a user