Adapted to dynamic attributes commit in GameKit.

This commit is contained in:
Quentin Bazin 2020-04-03 04:04:19 +02:00
parent 94b959cf03
commit 15218e2214
12 changed files with 92 additions and 23 deletions

View File

@ -37,6 +37,7 @@
#include "Font.hpp" #include "Font.hpp"
#include "TextureAtlas.hpp" #include "TextureAtlas.hpp"
#include "TextureLoader.hpp" #include "TextureLoader.hpp"
#include "Vertex.hpp"
#include "TitleScreenState.hpp" #include "TitleScreenState.hpp"
@ -56,6 +57,13 @@ void ClientApplication::init() {
gk::CoreApplication::init(); gk::CoreApplication::init();
m_window.addVertexAttribute(VertexAttribute::Coord3d, "coord3d", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, coord3d)));
m_window.addVertexAttribute(VertexAttribute::Color, "color", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, color)));
m_window.addVertexAttribute(VertexAttribute::TexCoord, "texCoord", 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, texCoord)));
m_window.addVertexAttribute(VertexAttribute::Normal, "normal", 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, normal)));
m_window.addVertexAttribute(VertexAttribute::LightValue, "lightValue", 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, lightValue)));
m_window.addVertexAttribute(VertexAttribute::AmbientOcclusion, "ambientOcclusion", 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid *>(offsetof(Vertex, ambientOcclusion)));
if (m_argumentParser.getArgument("help").isFound) if (m_argumentParser.getArgument("help").isFound)
return; return;

View File

@ -0,0 +1,55 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
*
* This file is part of OpenMiner.
*
* OpenMiner is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* OpenMiner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#ifndef VERTEX_HPP_
#define VERTEX_HPP_
#include <gk/gl/OpenGL.hpp>
struct Vertex {
GLfloat coord3d[4] = {0, 0, 0, 1};
GLfloat texCoord[2] = {-1, -1};
GLfloat color[4] = {0, 0, 0, 1};
GLfloat normal[3] = {0, 0, 0};
GLfloat lightValue[2] = {-1, -1};
GLfloat ambientOcclusion = 5;
};
namespace VertexAttribute {
enum {
Coord3d = 8,
TexCoord = 16,
Color = 32,
Normal = 64,
LightValue = 128,
AmbientOcclusion = 256,
Basic = Coord3d | TexCoord | Color,
All = 0xffff
};
}
#endif // VERTEX_HPP_

View File

@ -24,18 +24,17 @@
* *
* ===================================================================================== * =====================================================================================
*/ */
#include <gk/gl/Vertex.hpp>
#include <gk/gl/GLCheck.hpp> #include <gk/gl/GLCheck.hpp>
#include <gk/resource/ResourceHandler.hpp> #include <gk/resource/ResourceHandler.hpp>
#include "PlayerBox.hpp" #include "PlayerBox.hpp"
#include "Vertex.hpp"
constexpr int NUM_QUADS = 34; constexpr int NUM_QUADS = 34;
constexpr int NUM_VERTICES_PER_QUAD = 4; constexpr int NUM_VERTICES_PER_QUAD = 4;
constexpr int NUM_VERTEX_ELEMENTS = 5; constexpr int NUM_VERTEX_ELEMENTS = 5;
static constexpr float modelCoords[NUM_QUADS * NUM_VERTICES_PER_QUAD][NUM_VERTEX_ELEMENTS] = { static constexpr float modelCoords[NUM_QUADS * NUM_VERTICES_PER_QUAD][NUM_VERTEX_ELEMENTS] = {
// RIGHT LEG // RIGHT LEG
// West // West
{-0.15f, -0.15f - 0.18f, 0.f, 16.f/64.f, 0.f/32.f}, {-0.15f, -0.15f - 0.18f, 0.f, 16.f/64.f, 0.f/32.f},
@ -255,7 +254,7 @@ PlayerBox::PlayerBox(const gk::Camera &camera)
} }
void PlayerBox::updateVertexBuffer() { void PlayerBox::updateVertexBuffer() {
gk::Vertex vertices[NUM_QUADS * NUM_VERTICES_PER_QUAD]; Vertex vertices[NUM_QUADS * NUM_VERTICES_PER_QUAD];
for (u8 i = 0 ; i < NUM_QUADS * NUM_VERTICES_PER_QUAD ; ++i) { for (u8 i = 0 ; i < NUM_QUADS * NUM_VERTICES_PER_QUAD ; ++i) {
vertices[i].coord3d[0] = modelCoords[i][0]; vertices[i].coord3d[0] = modelCoords[i][0];
vertices[i].coord3d[1] = modelCoords[i][1]; vertices[i].coord3d[1] = modelCoords[i][1];
@ -278,6 +277,7 @@ void PlayerBox::draw(gk::RenderTarget &target, gk::RenderStates states) const {
states.transform *= getTransform(); states.transform *= getTransform();
states.texture = &m_texture; states.texture = &m_texture;
states.vertexAttributes = VertexAttribute::Basic;
glCheck(glEnable(GL_CULL_FACE)); glCheck(glEnable(GL_CULL_FACE));

View File

@ -28,7 +28,6 @@
#include <gk/graphics/Color.hpp> #include <gk/graphics/Color.hpp>
#include <gk/gl/GLCheck.hpp> #include <gk/gl/GLCheck.hpp>
#include <gk/gl/Vertex.hpp>
#include <gk/math/Math.hpp> #include <gk/math/Math.hpp>
#include <gk/resource/ResourceHandler.hpp> #include <gk/resource/ResourceHandler.hpp>
@ -38,6 +37,7 @@
#include "EngineConfig.hpp" #include "EngineConfig.hpp"
#include "InventoryCube.hpp" #include "InventoryCube.hpp"
#include "TextureAtlas.hpp" #include "TextureAtlas.hpp"
#include "Vertex.hpp"
InventoryCube::InventoryCube(float size) : m_textureAtlas(gk::ResourceHandler::getInstance().get<TextureAtlas>("atlas-blocks")) { InventoryCube::InventoryCube(float size) : m_textureAtlas(gk::ResourceHandler::getInstance().get<TextureAtlas>("atlas-blocks")) {
m_size = size; m_size = size;
@ -56,7 +56,7 @@ using namespace BlockGeometry;
void InventoryCube::updateVertexBuffer(const Block &block) { void InventoryCube::updateVertexBuffer(const Block &block) {
if (!block.id()) return; if (!block.id()) return;
gk::Vertex vertices[nFaces][nVertsPerFace]; Vertex vertices[nFaces][nVertsPerFace];
glm::vec3 vertexPos[nVertsPerCube] { glm::vec3 vertexPos[nVertsPerCube] {
// Order is important. It matches the bit order defined in BlockGeometry::cubeVerts. // Order is important. It matches the bit order defined in BlockGeometry::cubeVerts.
@ -145,7 +145,7 @@ 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.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.texture = &m_textureAtlas.texture();
states.vertexAttributes = gk::VertexAttribute::Only2d; states.vertexAttributes = VertexAttribute::Basic;
glCheck(glEnable(GL_CULL_FACE)); glCheck(glEnable(GL_CULL_FACE));
glCheck(glEnable(GL_DEPTH_TEST)); glCheck(glEnable(GL_DEPTH_TEST));

View File

@ -78,7 +78,7 @@ void Text::draw(gk::RenderTarget &target, gk::RenderStates states) const {
states.transform.translate(m_padding.x, m_padding.y); states.transform.translate(m_padding.x, m_padding.y);
states.texture = &m_font.texture(); states.texture = &m_font.texture();
states.vertexAttributes = gk::VertexAttribute::Only2d; states.vertexAttributes = VertexAttribute::Basic;
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
@ -89,7 +89,7 @@ void Text::draw(gk::RenderTarget &target, gk::RenderStates states) const {
void Text::updateVertexBuffer() const { void Text::updateVertexBuffer() const {
if (!m_isUpdateNeeded) return; if (!m_isUpdateNeeded) return;
std::vector<gk::Vertex> vertices; std::vector<Vertex> vertices;
u32 x = 0; u32 x = 0;
u32 y = 0; u32 y = 0;
@ -129,7 +129,7 @@ void Text::updateVertexBuffer() const {
m_verticesCount = vertices.size(); m_verticesCount = vertices.size();
gk::VertexBuffer::bind(&m_vbo); gk::VertexBuffer::bind(&m_vbo);
m_vbo.setData(sizeof(gk::Vertex) * m_verticesCount, vertices.data(), GL_DYNAMIC_DRAW); m_vbo.setData(sizeof(Vertex) * m_verticesCount, vertices.data(), GL_DYNAMIC_DRAW);
gk::VertexBuffer::bind(nullptr); gk::VertexBuffer::bind(nullptr);
m_size.x = std::max(x, maxX); m_size.x = std::max(x, maxX);
@ -141,7 +141,7 @@ void Text::updateVertexBuffer() const {
m_background.setSize(backgroundX, backgroundY); m_background.setSize(backgroundX, backgroundY);
} }
void Text::addCharacter(u32 x, u32 y, const gk::Color &color, u8 c, std::vector<gk::Vertex> &vertices) const { void Text::addCharacter(u32 x, u32 y, const gk::Color &color, u8 c, std::vector<Vertex> &vertices) const {
static const u8 coords[6][2] = { static const u8 coords[6][2] = {
{1, 0}, {1, 0},
{0, 0}, {0, 0},
@ -154,7 +154,7 @@ void Text::addCharacter(u32 x, u32 y, const gk::Color &color, u8 c, std::vector<
for (int i = 0 ; i < 6 ; ++i) { for (int i = 0 ; i < 6 ; ++i) {
vertices.emplace_back(); vertices.emplace_back();
gk::Vertex &vertex = vertices.back(); Vertex &vertex = vertices.back();
vertex.coord3d[0] = x + coords[i][0] * m_font.getTileSize().x; vertex.coord3d[0] = x + coords[i][0] * m_font.getTileSize().x;
vertex.coord3d[1] = y + coords[i][1] * m_font.getTileSize().y; vertex.coord3d[1] = y + coords[i][1] * m_font.getTileSize().y;

View File

@ -30,9 +30,10 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <gk/gl/Vertex.hpp>
#include <gk/graphics/RectangleShape.hpp> #include <gk/graphics/RectangleShape.hpp>
#include "Vertex.hpp"
class Font; class Font;
class Text : public gk::Drawable, public gk::Transformable { class Text : public gk::Drawable, public gk::Transformable {
@ -61,7 +62,7 @@ class Text : public gk::Drawable, public gk::Transformable {
private: private:
void draw(gk::RenderTarget &target, gk::RenderStates states) const override; void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
void addCharacter(u32 x, u32 y, const gk::Color &color, u8 c, std::vector<gk::Vertex> &vertices) const; void addCharacter(u32 x, u32 y, const gk::Color &color, u8 c, std::vector<Vertex> &vertices) const;
std::string m_text; std::string m_text;

View File

@ -174,7 +174,7 @@ void BlockCursor::update(const Hotbar &hotbar) {
using namespace BlockGeometry; using namespace BlockGeometry;
void BlockCursor::updateVBOCoords(gk::Vertex vertices[nFaces][nVertsPerFace], const Block &block, void BlockCursor::updateVBOCoords(Vertex vertices[nFaces][nVertsPerFace], const Block &block,
float face, u8f orientation) float face, u8f orientation)
{ {
glm::vec3 bottomLeft{block.boundingBox().x, block.boundingBox().y, block.boundingBox().z}; glm::vec3 bottomLeft{block.boundingBox().x, block.boundingBox().y, block.boundingBox().z};
@ -214,7 +214,7 @@ void BlockCursor::updateVBOCoords(gk::Vertex vertices[nFaces][nVertsPerFace], co
} }
void BlockCursor::updateVertexBuffer(const Block &block, u8f orientation) { void BlockCursor::updateVertexBuffer(const Block &block, u8f orientation) {
gk::Vertex vertices[nFaces][nVertsPerFace]; Vertex vertices[nFaces][nVertsPerFace];
updateVBOCoords(vertices, block, -1, orientation); updateVBOCoords(vertices, block, -1, orientation);
gk::VertexBuffer::bind(&m_vbo); gk::VertexBuffer::bind(&m_vbo);
@ -223,7 +223,7 @@ void BlockCursor::updateVertexBuffer(const Block &block, u8f orientation) {
} }
void BlockCursor::updateAnimationVertexBuffer(const Block &block, u8f orientation, int animationPos) { void BlockCursor::updateAnimationVertexBuffer(const Block &block, u8f orientation, int animationPos) {
gk::Vertex vertices[nFaces][nVertsPerFace]; Vertex vertices[nFaces][nVertsPerFace];
updateVBOCoords(vertices, block, -2, orientation); updateVBOCoords(vertices, block, -2, orientation);
GLfloat color[4] = {1, 1, 1, 0.5}; GLfloat color[4] = {1, 1, 1, 0.5};
@ -256,6 +256,8 @@ void BlockCursor::updateAnimationVertexBuffer(const Block &block, u8f orientatio
void BlockCursor::draw(gk::RenderTarget &target, gk::RenderStates states) const { void BlockCursor::draw(gk::RenderTarget &target, gk::RenderStates states) const {
if (m_selectedBlock.w == -1) return; if (m_selectedBlock.w == -1) return;
states.vertexAttributes = VertexAttribute::All;
glCheck(glDisable(GL_POLYGON_OFFSET_FILL)); glCheck(glDisable(GL_POLYGON_OFFSET_FILL));
glCheck(glDisable(GL_CULL_FACE)); glCheck(glDisable(GL_CULL_FACE));

View File

@ -50,7 +50,7 @@ class BlockCursor : public gk::Drawable {
private: private:
void updateVertexBuffer(const Block &block, const u8f orientation); void updateVertexBuffer(const Block &block, const u8f orientation);
void updateAnimationVertexBuffer(const Block &block, const u8f orientation, int animationPos = -1); void updateAnimationVertexBuffer(const Block &block, const u8f orientation, int animationPos = -1);
void updateVBOCoords(gk::Vertex vertices[BlockGeometry::nFaces][BlockGeometry::nVertsPerFace], void updateVBOCoords(Vertex vertices[BlockGeometry::nFaces][BlockGeometry::nVertsPerFace],
const Block &block, float face, u8f orientation); const Block &block, float face, u8f orientation);
void draw(gk::RenderTarget &target, gk::RenderStates states) const override; void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

View File

@ -107,7 +107,7 @@ void HUD::draw(gk::RenderTarget &target, gk::RenderStates states) const {
states.shader = &m_shader; states.shader = &m_shader;
states.projectionMatrix = m_orthoMatrix; states.projectionMatrix = m_orthoMatrix;
states.viewMatrix = gk::Transform::Identity; states.viewMatrix = gk::Transform::Identity;
states.vertexAttributes = gk::VertexAttribute::Only2d; states.vertexAttributes = VertexAttribute::Basic;
states.transform *= getTransform(); states.transform *= getTransform();

View File

@ -145,7 +145,7 @@ std::array<std::size_t, ChunkBuilder::layers> ChunkBuilder::buildChunk(const Cli
m_vertices[i].shrink_to_fit(); m_vertices[i].shrink_to_fit();
gk::VertexBuffer::bind(&vbo[i]); gk::VertexBuffer::bind(&vbo[i]);
vbo[i].setData(m_vertices[i].size() * sizeof(gk::Vertex), m_vertices[i].data(), GL_DYNAMIC_DRAW); vbo[i].setData(m_vertices[i].size() * sizeof(Vertex), m_vertices[i].data(), GL_DYNAMIC_DRAW);
gk::VertexBuffer::bind(nullptr); gk::VertexBuffer::bind(nullptr);
verticesCount[i] = m_vertices[i].size(); verticesCount[i] = m_vertices[i].size();
@ -200,7 +200,7 @@ inline void ChunkBuilder::addFace(s8f x, s8f y, s8f z, s8f f, const ClientChunk
} }
// Prepare vertex information for VBO // Prepare vertex information for VBO
gk::Vertex vertices[nVertsPerFace]; Vertex vertices[nVertsPerFace];
for (s8f v = 0; v < nVertsPerFace; ++v) { for (s8f v = 0; v < nVertsPerFace; ++v) {
if (block.drawType() == BlockDrawType::Cactus) { if (block.drawType() == BlockDrawType::Cactus) {
vertices[v].coord3d[0] = x + vertexPos[v]->x - boundingBox.x * normal.x; vertices[v].coord3d[0] = x + vertexPos[v]->x - boundingBox.x * normal.x;
@ -295,7 +295,7 @@ inline void ChunkBuilder::addCross(s8f x, s8f y, s8f z, const ClientChunk &chunk
}; };
for (int f = 0; f < nCrossFaces ; ++f) { for (int f = 0; f < nCrossFaces ; ++f) {
gk::Vertex vertices[nVertsPerFace]; Vertex vertices[nVertsPerFace];
for (int v = 0 ; v < nVertsPerFace ; ++v) { for (int v = 0 ; v < nVertsPerFace ; ++v) {
vertices[v].coord3d[0] = x + vertexPos[f][v]->x; vertices[v].coord3d[0] = x + vertexPos[f][v]->x;
vertices[v].coord3d[1] = y + vertexPos[f][v]->y; vertices[v].coord3d[1] = y + vertexPos[f][v]->y;

View File

@ -33,9 +33,10 @@
#include <glm/matrix.hpp> #include <glm/matrix.hpp>
#include <gk/core/Vector3.hpp> #include <gk/core/Vector3.hpp>
#include <gk/gl/Vertex.hpp>
#include <gk/gl/VertexBuffer.hpp> #include <gk/gl/VertexBuffer.hpp>
#include "Vertex.hpp"
class Block; class Block;
class ClientChunk; class ClientChunk;
class TextureAtlas; class TextureAtlas;
@ -75,7 +76,7 @@ class ChunkBuilder {
u8 getLightForVertex(Light light, s8f x, s8f y, s8f z, const gk::Vector3i &offset, u8 getLightForVertex(Light light, s8f x, s8f y, s8f z, const gk::Vector3i &offset,
const gk::Vector3i &normal, const ClientChunk &chunk); const gk::Vector3i &normal, const ClientChunk &chunk);
std::array<std::vector<gk::Vertex>, layers> m_vertices; std::array<std::vector<Vertex>, layers> m_vertices;
TextureAtlas &m_textureAtlas; TextureAtlas &m_textureAtlas;
}; };

View File

@ -219,6 +219,8 @@ void ClientWorld::draw(gk::RenderTarget &target, gk::RenderStates states) const
return; return;
} }
states.vertexAttributes = VertexAttribute::All;
gk::Shader::bind(states.shader); gk::Shader::bind(states.shader);
states.shader->setUniform("u_renderDistance", Config::renderDistance * CHUNK_WIDTH); states.shader->setUniform("u_renderDistance", Config::renderDistance * CHUNK_WIDTH);