From 7201b31b5569d7d40cf9f121a4a8dca749f81526 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Thu, 14 Jun 2018 21:16:56 +0200 Subject: [PATCH] [IDrawable|RenderStates|RenderTarget] Added. Trying a new rendering system based on the SFML one. --- include/display/Skybox.hpp | 2 +- include/gl/IDrawable.hpp | 29 +++++++++++++++++++++++ include/gl/RenderStates.hpp | 33 ++++++++++++++++++++++++++ include/gl/RenderTarget.hpp | 28 ++++++++++++++++++++++ include/gl/Shader.hpp | 14 +++++------ include/gl/VertexBuffer.hpp | 14 ++++++++--- include/hud/Crosshair.hpp | 10 ++++---- include/states/GameState.hpp | 7 ++++-- include/world/Chunk.hpp | 2 +- source/gl/RenderTarget.cpp | 46 ++++++++++++++++++++++++++++++++++++ source/gl/Shader.cpp | 15 +++++------- source/gl/VertexBuffer.cpp | 10 +++++--- source/hud/Crosshair.cpp | 19 +++------------ source/states/GameState.cpp | 17 ++++++++----- 14 files changed, 193 insertions(+), 53 deletions(-) create mode 100644 include/gl/IDrawable.hpp create mode 100644 include/gl/RenderStates.hpp create mode 100644 include/gl/RenderTarget.hpp create mode 100644 source/gl/RenderTarget.cpp diff --git a/include/display/Skybox.hpp b/include/display/Skybox.hpp index 50484430..4106878d 100644 --- a/include/display/Skybox.hpp +++ b/include/display/Skybox.hpp @@ -25,7 +25,7 @@ class Skybox { void draw(Shader &shader); private: - VertexBuffer m_vbo; + VertexBuffer m_vbo{GL_QUADS, 0, 4 * 6}; }; #endif // SKYBOX_HPP_ diff --git a/include/gl/IDrawable.hpp b/include/gl/IDrawable.hpp new file mode 100644 index 00000000..7bb0a30a --- /dev/null +++ b/include/gl/IDrawable.hpp @@ -0,0 +1,29 @@ +/* + * ===================================================================================== + * + * Filename: IDrawable.hpp + * + * Description: + * + * Created: 14/06/2018 19:53:57 + * + * Author: Quentin Bazin, + * + * ===================================================================================== + */ +#ifndef IDRAWABLE_HPP_ +#define IDRAWABLE_HPP_ + +#include "RenderTarget.hpp" + +class IDrawable { + public: + virtual ~IDrawable() = default; + + protected: + friend class RenderTarget; + + virtual void draw(RenderTarget &target, RenderStates states) const = 0; +}; + +#endif // IDRAWABLE_HPP_ diff --git a/include/gl/RenderStates.hpp b/include/gl/RenderStates.hpp new file mode 100644 index 00000000..8e5796e0 --- /dev/null +++ b/include/gl/RenderStates.hpp @@ -0,0 +1,33 @@ +/* + * ===================================================================================== + * + * Filename: RenderStates.hpp + * + * Description: + * + * Created: 14/06/2018 19:52:27 + * + * Author: Quentin Bazin, + * + * ===================================================================================== + */ +#ifndef RENDERSTATES_HPP_ +#define RENDERSTATES_HPP_ + +#include + +class Texture; +class Shader; + +struct RenderStates { + glm::mat4 *projectionMatrix = nullptr; + glm::mat4 *viewMatrix = nullptr; + glm::mat4 *modelMatrix = nullptr; + + const Texture *texture = nullptr; + const Shader *shader = nullptr; + + static const RenderStates Default; +}; + +#endif // RENDERSTATES_HPP_ diff --git a/include/gl/RenderTarget.hpp b/include/gl/RenderTarget.hpp new file mode 100644 index 00000000..22158baa --- /dev/null +++ b/include/gl/RenderTarget.hpp @@ -0,0 +1,28 @@ +/* + * ===================================================================================== + * + * Filename: RenderTarget.hpp + * + * Description: + * + * Created: 14/06/2018 19:54:56 + * + * Author: Quentin Bazin, + * + * ===================================================================================== + */ +#ifndef RENDERTARGET_HPP_ +#define RENDERTARGET_HPP_ + +#include "RenderStates.hpp" + +class IDrawable; +class VertexBuffer; + +class RenderTarget { + public: + void draw(const IDrawable &drawable, const RenderStates &states = RenderStates::Default); + void draw(const VertexBuffer &vertexBuffer, const RenderStates &states = RenderStates::Default); +}; + +#endif // RENDERTARGET_HPP_ diff --git a/include/gl/Shader.hpp b/include/gl/Shader.hpp index 3bcc8c63..114b8252 100644 --- a/include/gl/Shader.hpp +++ b/include/gl/Shader.hpp @@ -23,7 +23,7 @@ class Shader { public: - Shader(); + Shader() = default; Shader(const std::string &vertexFilename, const std::string &fragementFilename); ~Shader(); @@ -34,14 +34,14 @@ class Shader { void addShader(GLenum type, const std::string &filename); - GLint attrib(const std::string &name); - GLint uniform(const std::string &name); + GLint attrib(const std::string &name) const; + GLint uniform(const std::string &name) const; - void enableVertexAttribArray(const std::string &name); - void disableVertexAttribArray(const std::string &name); + void enableVertexAttribArray(const std::string &name) const; + void disableVertexAttribArray(const std::string &name) const; - void setUniform(const std::string &name, int n); - void setUniform(const std::string &name, const glm::mat4 &matrix); + void setUniform(const std::string &name, int n) const; + void setUniform(const std::string &name, const glm::mat4 &matrix) const; static void bind(const Shader *shader); diff --git a/include/gl/VertexBuffer.hpp b/include/gl/VertexBuffer.hpp index 3ff59a0d..5e7cd9e9 100644 --- a/include/gl/VertexBuffer.hpp +++ b/include/gl/VertexBuffer.hpp @@ -18,16 +18,24 @@ class VertexBuffer { public: - VertexBuffer(); + VertexBuffer(GLenum mode, GLint first, GLsizei count); ~VertexBuffer(); - void setData(GLsizeiptr size, const GLvoid *data, GLenum usage); - void updateData(GLintptr offset, GLsizeiptr size, const GLvoid *data); + void setData(GLsizeiptr size, const GLvoid *data, GLenum usage) const; + void updateData(GLintptr offset, GLsizeiptr size, const GLvoid *data) const; + + GLenum mode() const { return m_mode; } + GLint first() const { return m_first; } + GLsizei count() const { return m_count; } static void bind(const VertexBuffer *vertexBuffer); private: GLuint m_id; + + GLenum m_mode; + GLint m_first; + GLsizei m_count; }; #endif // VERTEXBUFFER_HPP_ diff --git a/include/hud/Crosshair.hpp b/include/hud/Crosshair.hpp index 1847b939..ae82c677 100644 --- a/include/hud/Crosshair.hpp +++ b/include/hud/Crosshair.hpp @@ -14,17 +14,17 @@ #ifndef CROSSHAIR_HPP_ #define CROSSHAIR_HPP_ -#include "Shader.hpp" +#include "IDrawable.hpp" #include "VertexBuffer.hpp" -class Crosshair { +class Crosshair : public IDrawable { public: Crosshair(); - void draw(Shader &shader); - private: - VertexBuffer m_vbo; + void draw(RenderTarget &target, RenderStates states) const override; + + VertexBuffer m_vbo{GL_LINES, 0, 4}; }; #endif // CROSSHAIR_HPP_ diff --git a/include/states/GameState.hpp b/include/states/GameState.hpp index d3113c6c..4b6a4d4f 100644 --- a/include/states/GameState.hpp +++ b/include/states/GameState.hpp @@ -22,6 +22,8 @@ #include "Skybox.hpp" #include "World.hpp" +#include "RenderTarget.hpp" + class GameState : public ApplicationState { public: GameState(); @@ -49,9 +51,10 @@ class GameState : public ApplicationState { glm::vec4 m_selectedBlock{99999, 99999, 99999, -1}; - GLuint m_cursorVBO; - + VertexBuffer m_cursorVBO{GL_LINES, 0, 24}; Crosshair m_crosshair; + + RenderTarget m_renderTarget; }; #endif // GAMESTATE_HPP_ diff --git a/include/world/Chunk.hpp b/include/world/Chunk.hpp index 10e49511..9177bb22 100644 --- a/include/world/Chunk.hpp +++ b/include/world/Chunk.hpp @@ -88,7 +88,7 @@ class Chunk : public NonCopyable { std::map m_verticesID; std::map m_extendedFaces; - VertexBuffer m_vbo; + VertexBuffer m_vbo{GL_QUADS, 0, 0}; Chunk *m_surroundingChunks[4]; diff --git a/source/gl/RenderTarget.cpp b/source/gl/RenderTarget.cpp new file mode 100644 index 00000000..441c3527 --- /dev/null +++ b/source/gl/RenderTarget.cpp @@ -0,0 +1,46 @@ +/* + * ===================================================================================== + * + * Filename: RenderTarget.cpp + * + * Description: + * + * Created: 14/06/2018 19:55:28 + * + * Author: Quentin Bazin, + * + * ===================================================================================== + */ +#include "IDrawable.hpp" +#include "RenderTarget.hpp" +#include "Shader.hpp" +#include "VertexBuffer.hpp" + +void RenderTarget::draw(const IDrawable &drawable, const RenderStates &states) { + drawable.draw(*this, states); +} + +void RenderTarget::draw(const VertexBuffer &vertexBuffer, const RenderStates &states) { + if (!states.shader) return; + + Shader::bind(states.shader); + + VertexBuffer::bind(&vertexBuffer); + + states.shader->setUniform("u_projectionMatrix", (states.projectionMatrix) ? *states.projectionMatrix : glm::mat4{1}); + states.shader->setUniform("u_modelMatrix", (states.modelMatrix) ? *states.modelMatrix : glm::mat4{1}); + states.shader->setUniform("u_viewMatrix", (states.viewMatrix) ? *states.viewMatrix : glm::mat4{1}); + + states.shader->enableVertexAttribArray("coord3d"); + + glVertexAttribPointer(states.shader->attrib("coord3d"), 3, GL_FLOAT, GL_FALSE, 0, 0); + + glDrawArrays(vertexBuffer.mode(), vertexBuffer.first(), vertexBuffer.count()); + + states.shader->disableVertexAttribArray("coord3d"); + + VertexBuffer::bind(nullptr); + + Shader::bind(nullptr); +} + diff --git a/source/gl/Shader.cpp b/source/gl/Shader.cpp index 8c0823b2..e9fca4a8 100644 --- a/source/gl/Shader.cpp +++ b/source/gl/Shader.cpp @@ -20,9 +20,6 @@ #include "Exception.hpp" #include "Shader.hpp" -Shader::Shader() { -} - Shader::Shader(const std::string &vertexFilename, const std::string &fragmentFilename) { loadFromFile(vertexFilename, fragmentFilename); } @@ -127,7 +124,7 @@ void Shader::addShader(GLenum type, const std::string &filename) { glAttachShader(m_program, shader); } -GLint Shader::attrib(const std::string &name) { +GLint Shader::attrib(const std::string &name) const { GLint attrib = glGetAttribLocation(m_program, name.c_str()); if(attrib == -1) { @@ -137,7 +134,7 @@ GLint Shader::attrib(const std::string &name) { return attrib; } -GLint Shader::uniform(const std::string &name) { +GLint Shader::uniform(const std::string &name) const { GLint uniform = glGetUniformLocation(m_program, name.c_str()); if(uniform == -1) { @@ -147,19 +144,19 @@ GLint Shader::uniform(const std::string &name) { return uniform; } -void Shader::enableVertexAttribArray(const std::string &name) { +void Shader::enableVertexAttribArray(const std::string &name) const { glEnableVertexAttribArray(attrib(name)); } -void Shader::disableVertexAttribArray(const std::string &name) { +void Shader::disableVertexAttribArray(const std::string &name) const { glDisableVertexAttribArray(attrib(name)); } -void Shader::setUniform(const std::string &name, int n) { +void Shader::setUniform(const std::string &name, int n) const { glUniform1i(uniform(name), n); } -void Shader::setUniform(const std::string &name, const glm::mat4 &matrix) { +void Shader::setUniform(const std::string &name, const glm::mat4 &matrix) const { glUniformMatrix4fv(uniform(name), 1, GL_FALSE, glm::value_ptr(matrix)); } diff --git a/source/gl/VertexBuffer.cpp b/source/gl/VertexBuffer.cpp index 67a4a64b..e2fe3e63 100644 --- a/source/gl/VertexBuffer.cpp +++ b/source/gl/VertexBuffer.cpp @@ -13,19 +13,23 @@ */ #include "VertexBuffer.hpp" -VertexBuffer::VertexBuffer() { +VertexBuffer::VertexBuffer(GLenum mode, GLint first, GLsizei count) { glGenBuffers(1, &m_id); + + m_mode = mode; + m_first = first; + m_count = count; } VertexBuffer::~VertexBuffer() { glDeleteBuffers(1, &m_id); } -void VertexBuffer::setData(GLsizeiptr size, const GLvoid *data, GLenum usage) { +void VertexBuffer::setData(GLsizeiptr size, const GLvoid *data, GLenum usage) const { glBufferData(GL_ARRAY_BUFFER, size, data, usage); } -void VertexBuffer::updateData(GLintptr offset, GLsizeiptr size, const GLvoid *data) { +void VertexBuffer::updateData(GLintptr offset, GLsizeiptr size, const GLvoid *data) const { glBufferSubData(GL_ARRAY_BUFFER, offset, size, data); } diff --git a/source/hud/Crosshair.cpp b/source/hud/Crosshair.cpp index bbe7cc35..dfc2cec9 100644 --- a/source/hud/Crosshair.cpp +++ b/source/hud/Crosshair.cpp @@ -12,6 +12,7 @@ * ===================================================================================== */ #include "Crosshair.hpp" +#include "Shader.hpp" Crosshair::Crosshair() { float cross[4][3] = { @@ -26,24 +27,10 @@ Crosshair::Crosshair() { VertexBuffer::bind(nullptr); } -void Crosshair::draw(Shader &shader) { +void Crosshair::draw(RenderTarget &target, RenderStates states) const { glDisable(GL_DEPTH_TEST); - VertexBuffer::bind(&m_vbo); - - shader.setUniform("u_modelMatrix", glm::mat4(1)); - shader.setUniform("u_viewMatrix", glm::mat4(1)); - shader.setUniform("u_projectionMatrix", glm::mat4(1)); - - shader.enableVertexAttribArray("coord3d"); - - glVertexAttribPointer(shader.attrib("coord3d"), 3, GL_FLOAT, GL_FALSE, 0, 0); - - glDrawArrays(GL_LINES, 0, 4); - - shader.disableVertexAttribArray("coord3d"); - - VertexBuffer::bind(nullptr); + target.draw(m_vbo, states); glEnable(GL_DEPTH_TEST); } diff --git a/source/states/GameState.cpp b/source/states/GameState.cpp index 756dc4e7..a2cd0f16 100644 --- a/source/states/GameState.cpp +++ b/source/states/GameState.cpp @@ -41,8 +41,6 @@ GameState::GameState() : m_camera(Camera::getInstance()) { m_shader.setUniform("u_tex", 0); Shader::bind(nullptr); - - glGenBuffers(1, &m_cursorVBO); } void GameState::onEvent(const SDL_Event &event) { @@ -96,7 +94,10 @@ void GameState::draw() { m_world.draw(m_shader, m_projectionMatrix, m_viewMatrix); drawSelectedBlock(); - m_crosshair.draw(m_shader); + + RenderStates states; + states.shader = &m_shader; + m_renderTarget.draw(m_crosshair, states); Shader::bind(nullptr); } @@ -139,14 +140,18 @@ void GameState::drawSelectedBlock() { glDisable(GL_POLYGON_OFFSET_FILL); glDisable(GL_CULL_FACE); - glBindBuffer(GL_ARRAY_BUFFER, m_cursorVBO); + VertexBuffer::bind(&m_cursorVBO); + + m_cursorVBO.setData(sizeof(box), box, GL_DYNAMIC_DRAW); + m_shader.setUniform("u_modelMatrix", glm::mat4(1)); - glBufferData(GL_ARRAY_BUFFER, sizeof(box), box, GL_DYNAMIC_DRAW); + m_shader.enableVertexAttribArray("coord3d"); glVertexAttribPointer(m_shader.attrib("coord3d"), 3, GL_FLOAT, GL_FALSE, 0, 0); glDrawArrays(GL_LINES, 0, 24); m_shader.disableVertexAttribArray("coord3d"); - glBindBuffer(GL_ARRAY_BUFFER, 0); + + VertexBuffer::bind(nullptr); glEnable(GL_CULL_FACE); glEnable(GL_POLYGON_OFFSET_FILL);