From 677992d93c9cf30c79330d4bb434eebac1ae5a8e Mon Sep 17 00:00:00 2001
From: Quentin Bazin <quent42340@gmail.com>
Date: Fri, 11 Jun 2021 05:25:25 +0200
Subject: [PATCH] [external/gamekit] Vertex attr layout is now handled by
 VertexBuffer.

---
 external/gamekit                           |  2 +-
 source/client/core/ClientApplication.cpp   |  7 -------
 source/client/core/Vertex.hpp              | 14 --------------
 source/client/graphics/CelestialObject.cpp |  6 ++++--
 source/client/graphics/CelestialObject.hpp |  2 +-
 source/client/graphics/ChunkRenderer.cpp   |  7 +++----
 source/client/graphics/Framebuffer.cpp     | 22 +++++++++++-----------
 source/client/graphics/PlayerBox.cpp       |  8 +++++++-
 source/client/gui/InventoryCube.cpp        | 11 ++++++++++-
 source/client/gui/Text.cpp                 |  3 ++-
 source/client/hud/BlockCursor.cpp          | 11 +++++++++--
 source/client/hud/Minimap.cpp              |  3 ++-
 source/client/world/ClientChunk.cpp        | 13 +++++++++++++
 source/client/world/ClientChunk.hpp        |  3 +--
 source/client/world/ClientWorld.cpp        |  3 ---
 15 files changed, 64 insertions(+), 51 deletions(-)

diff --git a/external/gamekit b/external/gamekit
index 388f8d9d..a047a6b0 160000
--- a/external/gamekit
+++ b/external/gamekit
@@ -1 +1 @@
-Subproject commit 388f8d9de2868b3bd6ad3657093fdf00420e5dcf
+Subproject commit a047a6b003234e53b7251793f97236b36151a8cb
diff --git a/source/client/core/ClientApplication.cpp b/source/client/core/ClientApplication.cpp
index d71fcf65..23ac18cc 100644
--- a/source/client/core/ClientApplication.cpp
+++ b/source/client/core/ClientApplication.cpp
@@ -64,13 +64,6 @@ bool ClientApplication::init() {
 
 	gk::CoreApplication::init();
 
-	m_window.addVertexAttribute(VertexAttribute::Coord3d, 0, "coord3d", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, coord3d)));
-	m_window.addVertexAttribute(VertexAttribute::TexCoord, 1, "texCoord", 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, texCoord)));
-	m_window.addVertexAttribute(VertexAttribute::Color, 2, "color", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, color)));
-	m_window.addVertexAttribute(VertexAttribute::Normal, 3, "normal", 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, normal)));
-	m_window.addVertexAttribute(VertexAttribute::LightValue, 4, "lightValue", 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, lightValue)));
-	m_window.addVertexAttribute(VertexAttribute::AmbientOcclusion, 5, "ambientOcclusion", 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, ambientOcclusion)));
-
 	if (m_argumentParser.getArgument("help").isFound)
 		return true;
 
diff --git a/source/client/core/Vertex.hpp b/source/client/core/Vertex.hpp
index 2dc52758..f57dd050 100644
--- a/source/client/core/Vertex.hpp
+++ b/source/client/core/Vertex.hpp
@@ -38,18 +38,4 @@ struct Vertex {
 	GLfloat ambientOcclusion = 5;
 };
 
-namespace VertexAttribute {
-	enum {
-		Coord3d          = 8,
-		TexCoord         = 16,
-		Color            = 32,
-		Normal           = 64,
-		LightValue       = 128,
-		AmbientOcclusion = 256,
-
-		Basic            = Coord3d | TexCoord | Color,
-		All              = Basic | Normal | LightValue | AmbientOcclusion
-	};
-}
-
 #endif // VERTEX_HPP_
diff --git a/source/client/graphics/CelestialObject.cpp b/source/client/graphics/CelestialObject.cpp
index 8599e102..e2c24172 100644
--- a/source/client/graphics/CelestialObject.cpp
+++ b/source/client/graphics/CelestialObject.cpp
@@ -34,6 +34,10 @@
 #include "GameTime.hpp"
 #include "Vertex.hpp"
 
+CelestialObject::CelestialObject() {
+	m_vbo.layout().setupDefaultLayout();
+}
+
 void CelestialObject::setTexture(const std::string &textureName) {
 	if (textureName.empty()) return;
 
@@ -161,8 +165,6 @@ void CelestialObject::draw(gk::RenderTarget &target, gk::RenderStates states) co
 	states.transform.rotateY(-GameTime::getCurrentTime(m_rotationOffset, m_rotationSpeed) * 360.f);
 	states.transform *= getTransform();
 
-	states.vertexAttributes = gk::VertexAttribute::All;
-
 	if (m_texture)
 		states.texture = m_texture;
 
diff --git a/source/client/graphics/CelestialObject.hpp b/source/client/graphics/CelestialObject.hpp
index 2865939c..8e4de60d 100644
--- a/source/client/graphics/CelestialObject.hpp
+++ b/source/client/graphics/CelestialObject.hpp
@@ -34,7 +34,7 @@
 
 class CelestialObject : public gk::Drawable, public gk::Transformable  {
 	public:
-		CelestialObject() = default;
+		CelestialObject();
 
 		float width() const { return m_width; }
 		float height() const { return m_height; }
diff --git a/source/client/graphics/ChunkRenderer.cpp b/source/client/graphics/ChunkRenderer.cpp
index 4a34d827..0baec6f2 100644
--- a/source/client/graphics/ChunkRenderer.cpp
+++ b/source/client/graphics/ChunkRenderer.cpp
@@ -126,7 +126,8 @@ void ChunkRenderer::drawChunks(gk::RenderTarget &target, gk::RenderStates states
 	glCheck(glEnable(GL_DEPTH_TEST));
 
 	states.texture = &m_textureAtlas.texture();
-	target.beginSceneDraw(states);
+
+	target.beginDrawing(states);
 
 	states.shader->setUniform("u_renderDistance", Config::renderDistance * CHUNK_WIDTH);
 
@@ -149,14 +150,12 @@ void ChunkRenderer::drawChunks(gk::RenderTarget &target, gk::RenderStates states
 
 			states.shader->setUniform("u_modelMatrix", it.second);
 
-			target.drawVertexBuffer(it.first->getVBO(layer), GL_TRIANGLES, 0, (GLsizei)verticesCount, states);
+			target.drawVertexBuffer(it.first->getVBO(layer), GL_TRIANGLES, 0, (GLsizei)verticesCount);
 
 			it.first->setHasBeenDrawn(true);
 		}
 	}
 
-	target.endSceneDraw(states);
-
 	if(Config::isWireframeModeEnabled) glCheck(glPolygonMode(GL_FRONT_AND_BACK, GL_FILL));
 }
 
diff --git a/source/client/graphics/Framebuffer.cpp b/source/client/graphics/Framebuffer.cpp
index 9988a3f6..287f6968 100644
--- a/source/client/graphics/Framebuffer.cpp
+++ b/source/client/graphics/Framebuffer.cpp
@@ -130,8 +130,8 @@ void Framebuffer::loadShader(const std::string &name) {
 
 void Framebuffer::begin() const {
 	bind(this);
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-	glEnable(GL_DEPTH_TEST);
+	glCheck(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
+	glCheck(glEnable(GL_DEPTH_TEST));
 }
 
 void Framebuffer::end() const {
@@ -157,23 +157,23 @@ void Framebuffer::end() const {
 
 	gk::VertexBuffer::bind(&m_vbo);
 
-	glEnableVertexAttribArray(0);
-	glEnableVertexAttribArray(1);
+	glCheck(glEnableVertexAttribArray(0));
+	glCheck(glEnableVertexAttribArray(1));
 
-	m_vbo.setAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(0 * sizeof(float)));
-	m_vbo.setAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
+	glCheck(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(0 * sizeof(float))));
+	glCheck(glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float))));
 
-	glDrawArrays(GL_TRIANGLES, 0, 6);
+	glCheck(glDrawArrays(GL_TRIANGLES, 0, 6));
 
-	glDisableVertexAttribArray(1);
-	glDisableVertexAttribArray(0);
+	glCheck(glDisableVertexAttribArray(1));
+	glCheck(glDisableVertexAttribArray(0));
 
 	gk::VertexBuffer::bind(nullptr);
 	gk::Shader::bind(nullptr);
 
-	glActiveTexture(GL_TEXTURE0);
+	glCheck(glActiveTexture(GL_TEXTURE0));
 
-	glEnable(GL_DEPTH_TEST);
+	glCheck(glEnable(GL_DEPTH_TEST));
 }
 
 void Framebuffer::bind(const Framebuffer *framebuffer) {
diff --git a/source/client/graphics/PlayerBox.cpp b/source/client/graphics/PlayerBox.cpp
index 6b174b4f..e1b63317 100644
--- a/source/client/graphics/PlayerBox.cpp
+++ b/source/client/graphics/PlayerBox.cpp
@@ -250,6 +250,13 @@ PlayerBox::PlayerBox(const gk::Camera &camera)
 	: m_camera(camera),
 	  m_texture(gk::ResourceHandler::getInstance().get<gk::Texture>("texture-player"))
 {
+	m_vbo.layout().addAttribute(0, "coord3d", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, coord3d)));
+	m_vbo.layout().addAttribute(1, "texCoord", 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, texCoord)));
+	m_vbo.layout().addAttribute(2, "color", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, color)));
+	m_vbo.layout().addAttribute(3, "normal", 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, normal)));
+	m_vbo.layout().addAttribute(4, "lightValue", 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, lightValue)));
+	m_vbo.layout().addAttribute(5, "ambientOcclusion", 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, ambientOcclusion)));
+
 	updateVertexBuffer();
 }
 
@@ -282,7 +289,6 @@ void PlayerBox::draw(gk::RenderTarget &target, gk::RenderStates states) const {
 
 	states.transform *= getTransform();
 	states.texture = &m_texture;
-	states.vertexAttributes = VertexAttribute::All;
 
 	glCheck(glEnable(GL_CULL_FACE));
 
diff --git a/source/client/gui/InventoryCube.cpp b/source/client/gui/InventoryCube.cpp
index 9b907353..220aa8e8 100644
--- a/source/client/gui/InventoryCube.cpp
+++ b/source/client/gui/InventoryCube.cpp
@@ -42,6 +42,16 @@
 InventoryCube::InventoryCube(float size, bool isEntity)
 	: m_textureAtlas(&gk::ResourceHandler::getInstance().get<TextureAtlas>("atlas-blocks"))
 {
+	m_vbo.layout().addAttribute(0, "coord3d", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, coord3d)));
+	m_vbo.layout().addAttribute(1, "texCoord", 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, texCoord)));
+	m_vbo.layout().addAttribute(2, "color", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, color)));
+
+	if (isEntity) {
+		m_vbo.layout().addAttribute(3, "normal", 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, normal)));
+		m_vbo.layout().addAttribute(4, "lightValue", 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, lightValue)));
+		m_vbo.layout().addAttribute(5, "ambientOcclusion", 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, ambientOcclusion)));
+	}
+
 	m_size = size;
 	m_isEntity = isEntity;
 
@@ -158,7 +168,6 @@ void InventoryCube::draw(gk::RenderTarget &target, gk::RenderStates states) cons
 	states.projectionMatrix = glm::ortho(0.0f, (float)Config::screenWidth, (float)Config::screenHeight, 0.0f, DIST_2D_FAR, DIST_2D_NEAR);
 
 	states.texture = &m_textureAtlas->texture();
-	states.vertexAttributes = VertexAttribute::All;
 
 	glCheck(glEnable(GL_CULL_FACE));
 	glCheck(glEnable(GL_DEPTH_TEST));
diff --git a/source/client/gui/Text.cpp b/source/client/gui/Text.cpp
index a309fc60..50b5bbc3 100644
--- a/source/client/gui/Text.cpp
+++ b/source/client/gui/Text.cpp
@@ -32,6 +32,8 @@
 #include "Text.hpp"
 
 Text::Text() : m_font(gk::ResourceHandler::getInstance().get<Font>("font-ascii")) {
+	m_vbo.layout().setupDefaultLayout();
+
 	m_background.setFillColor(gk::Color::Transparent);
 }
 
@@ -78,7 +80,6 @@ void Text::draw(gk::RenderTarget &target, gk::RenderStates states) const {
 
 	states.transform.translate((float)m_padding.x, (float)m_padding.y);
 	states.texture = &m_font.texture();
-	states.vertexAttributes = gk::VertexAttribute::All;
 
 	glDisable(GL_CULL_FACE);
 	glDisable(GL_DEPTH_TEST);
diff --git a/source/client/hud/BlockCursor.cpp b/source/client/hud/BlockCursor.cpp
index 69d82413..b84fca6b 100644
--- a/source/client/hud/BlockCursor.cpp
+++ b/source/client/hud/BlockCursor.cpp
@@ -45,6 +45,15 @@ BlockCursor::BlockCursor(ClientPlayer &player, ClientWorld &world, ClientCommand
 	: m_player(player), m_world(world), m_client(client)
 {
 	m_blockDestroyTexture = &gk::ResourceHandler::getInstance().get<gk::Texture>("texture-block_destroy");
+
+	m_vbo.layout().addAttribute(0, "coord3d", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, coord3d)));
+
+	m_animationVBO.layout().addAttribute(0, "coord3d", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, coord3d)));
+	m_animationVBO.layout().addAttribute(1, "texCoord", 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, texCoord)));
+	m_animationVBO.layout().addAttribute(2, "color", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, color)));
+	m_animationVBO.layout().addAttribute(3, "normal", 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, normal)));
+	m_animationVBO.layout().addAttribute(4, "lightValue", 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, lightValue)));
+	m_animationVBO.layout().addAttribute(5, "ambientOcclusion", 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, ambientOcclusion)));
 }
 
 void BlockCursor::onEvent(const SDL_Event &event, const Hotbar &hotbar) {
@@ -302,8 +311,6 @@ void BlockCursor::updateAnimationVertexBuffer(const BlockState &blockState, u8f
 void BlockCursor::draw(gk::RenderTarget &target, gk::RenderStates states) const {
 	if (m_selectedBlock.w == -1) return;
 
-	states.vertexAttributes = VertexAttribute::All;
-
 	glCheck(glDisable(GL_POLYGON_OFFSET_FILL));
 	glCheck(glDisable(GL_CULL_FACE));
 	glCheck(glEnable(GL_DEPTH_TEST));
diff --git a/source/client/hud/Minimap.cpp b/source/client/hud/Minimap.cpp
index 7a938dbb..6aeda19e 100644
--- a/source/client/hud/Minimap.cpp
+++ b/source/client/hud/Minimap.cpp
@@ -31,6 +31,8 @@
 #include "Minimap.hpp"
 
 Minimap::Minimap() {
+	m_vbo.layout().setupDefaultLayout();
+
 	m_border.setFillColor(gk::Color::Transparent);
 	m_border.setOutlineColor(gk::Color::fromRGBA32(224, 224, 224));
 	m_border.setOutlineThickness(1);
@@ -147,7 +149,6 @@ void Minimap::draw(gk::RenderTarget &target, gk::RenderStates states) const {
 
 	{
 		gk::RenderStates states2 = states;
-		states2.vertexAttributes = gk::VertexAttribute::All;
 		states2.transform.translate(minimapSize / 2 + chunkSize / 2, minimapSize / 2 + chunkSize / 2);
 		states2.transform *= m_playerFovRotationTransform;
 
diff --git a/source/client/world/ClientChunk.cpp b/source/client/world/ClientChunk.cpp
index 6e125289..c745e9ca 100644
--- a/source/client/world/ClientChunk.cpp
+++ b/source/client/world/ClientChunk.cpp
@@ -35,6 +35,19 @@ u32 ClientChunk::chunkUpdatesPerSec = 0;
 u32 ClientChunk::chunkUpdateCounter = 0;
 u64 ClientChunk::chunkUpdateTime = 0;
 
+ClientChunk::ClientChunk(s32 x, s32 y, s32 z, const Dimension &dimension, ClientWorld &world)
+	: Chunk(x, y, z, (World &)world), m_world(world), m_dimension(dimension)
+{
+	for (auto &vbo : m_vbo) {
+		vbo.layout().addAttribute(0, "coord3d", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, coord3d)));
+		vbo.layout().addAttribute(1, "texCoord", 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, texCoord)));
+		vbo.layout().addAttribute(2, "color", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, color)));
+		vbo.layout().addAttribute(3, "normal", 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, normal)));
+		vbo.layout().addAttribute(4, "lightValue", 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, lightValue)));
+		vbo.layout().addAttribute(5, "ambientOcclusion", 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, ambientOcclusion)));
+	}
+}
+
 void ClientChunk::update() {
 	m_lightmap.updateLights();
 
diff --git a/source/client/world/ClientChunk.hpp b/source/client/world/ClientChunk.hpp
index ab04f7f2..394135e1 100644
--- a/source/client/world/ClientChunk.hpp
+++ b/source/client/world/ClientChunk.hpp
@@ -39,8 +39,7 @@ class TextureAtlas;
 
 class ClientChunk : public Chunk {
 	public:
-		ClientChunk(s32 x, s32 y, s32 z, const Dimension &dimension, ClientWorld &world)
-			: Chunk(x, y, z, (World &)world), m_world(world), m_dimension(dimension) {}
+		ClientChunk(s32 x, s32 y, s32 z, const Dimension &dimension, ClientWorld &world);
 
 		void update() final;
 		void process() final;
diff --git a/source/client/world/ClientWorld.cpp b/source/client/world/ClientWorld.cpp
index 409f70a4..d5fb669a 100644
--- a/source/client/world/ClientWorld.cpp
+++ b/source/client/world/ClientWorld.cpp
@@ -266,11 +266,8 @@ void ClientWorld::draw(gk::RenderTarget &target, gk::RenderStates states) const
 
 	OM_PROFILE_START("ClientWorld::draw");
 
-	states.vertexAttributes = VertexAttribute::All;
-
 	m_chunkRenderer.draw(target, states, m_chunks, *m_camera, m_sky);
 
-	states.transform = gk::Transform::Identity;
 	target.draw(m_scene, states);
 
 	OM_PROFILE_END("ClientWorld::draw");