Skip to content

Commit

Permalink
[RenderTarget] Added cube index buffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Oct 29, 2023
1 parent c491ea0 commit b15af20
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 51 deletions.
26 changes: 26 additions & 0 deletions source/client/graphics/RenderTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ void RenderTarget::init() {
m_samplerUniform = bgfx::createUniform("u_tex", bgfx::UniformType::Sampler);

initDefaultIndexBuffer();
initCubeIndexBuffer();
}

void RenderTarget::free() {
m_cubeIndexBuffer.free();
m_defaultIndexBuffer.free();

bgfx::destroy(m_samplerUniform);
Expand Down Expand Up @@ -160,3 +162,27 @@ void RenderTarget::initDefaultIndexBuffer() {

m_defaultIndexBuffer.init(indices, 6 * 5 * sizeof(u16));
}

void RenderTarget::initCubeIndexBuffer() {
const u16 indices[6 * 6] = {
0, 1, 2,
2, 3, 0,

4, 5, 6,
6, 7, 4,

8, 9, 10,
10, 11, 8,

12, 13, 14,
14, 15, 12,

16, 17, 18,
18, 19, 16,

20, 21, 22,
22, 23, 20,
};

m_cubeIndexBuffer.init(indices, sizeof(indices));
}
3 changes: 3 additions & 0 deletions source/client/graphics/RenderTarget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ class RenderTarget {
void disableView() { m_view = nullptr; }

const IndexBuffer &defaultIndexBuffer() const { return m_defaultIndexBuffer; }
const IndexBuffer &cubeIndexBuffer() const { return m_cubeIndexBuffer; }

private:
void initDefaultIndexBuffer();
void initCubeIndexBuffer();

gk::IntRect getViewport(const View &view) const;

Expand All @@ -76,6 +78,7 @@ class RenderTarget {
bgfx::UniformHandle m_samplerUniform = BGFX_INVALID_HANDLE;

IndexBuffer m_defaultIndexBuffer;
IndexBuffer m_cubeIndexBuffer;
};

#endif // RENDERTARGET_HPP_
24 changes: 1 addition & 23 deletions source/client/gui/InventoryCube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,28 +140,6 @@ void InventoryCube::updateVertexBuffer(const Block &block, u8 state) {
}

m_vbo.init(vertices, sizeof(vertices), true);

const u16 indices[nFaces * (nVertsPerFace + 2)] = {
0, 1, 2,
2, 3, 0,

4, 5, 6,
6, 7, 4,

8, 9, 10,
10, 11, 8,

12, 13, 14,
14, 15, 12,

16, 17, 18,
18, 19, 16,

20, 21, 22,
22, 23, 20,
};

m_ibo.init(indices, sizeof(indices), true);
}

void InventoryCube::draw(RenderTarget &target, RenderStates states) const {
Expand All @@ -179,5 +157,5 @@ void InventoryCube::draw(RenderTarget &target, RenderStates states) const {

states.isDepthTestEnabled = false;

target.drawElements(m_vbo, m_ibo, 0, 0, states);
target.drawElements(m_vbo, target.cubeIndexBuffer(), 0, 0, states);
}
2 changes: 0 additions & 2 deletions source/client/gui/InventoryCube.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <gk/gl/Transformable.hpp>

#include "Drawable.hpp"
#include "IndexBuffer.hpp"
#include "VertexBuffer.hpp"
#include "View.hpp"

Expand All @@ -53,7 +52,6 @@ class InventoryCube : public Drawable, public gk::Transformable {
const TextureAtlas *m_textureAtlas;

VertexBuffer m_vbo;
IndexBuffer m_ibo;

gk::Transformable m_transform;

Expand Down
24 changes: 1 addition & 23 deletions source/client/hud/BlockCursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,6 @@ BlockCursor::BlockCursor(ClientPlayer &player, ClientWorld &world, ClientCommand
};

m_ibo.init(indices, sizeof(indices));

const u16 animationIndices[BlockGeometry::nFaces * (BlockGeometry::nVertsPerFace + 2)] = {
0, 1, 2,
2, 3, 0,

4, 5, 6,
6, 7, 4,

8, 9, 10,
10, 11, 8,

12, 13, 14,
14, 15, 12,

16, 17, 18,
18, 19, 16,

20, 21, 22,
22, 23, 20,
};

m_animationIBO.init(animationIndices, sizeof(animationIndices));
}

void BlockCursor::onEvent(const SDL_Event &event, const Hotbar &hotbar) {
Expand Down Expand Up @@ -359,7 +337,7 @@ void BlockCursor::draw(RenderTarget &target, RenderStates states) const {
states.view = 1; // Use chunk view for proper blending
states.primitiveType = 0; // Defaults to triangles

target.drawElements(m_animationVBO, m_animationIBO, 0, 0, states);
target.drawElements(m_animationVBO, target.cubeIndexBuffer(), 0, 0, states);
}
}

Expand Down
5 changes: 2 additions & 3 deletions source/client/hud/BlockCursor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ class BlockCursor : public Drawable {
ClientCommandHandler &m_client;

VertexBuffer m_vbo;
VertexBuffer m_animationVBO;

IndexBuffer m_ibo;
IndexBuffer m_animationIBO;

VertexBuffer m_animationVBO;

unsigned int m_animationStart = 0;
glm::ivec4 m_selectedBlock{0, 0, 0, -1};
Expand Down

0 comments on commit b15af20

Please sign in to comment.