[IDrawable|RenderStates|RenderTarget] Added. Trying a new rendering system based on the SFML one.

This commit is contained in:
Quentin Bazin 2018-06-14 21:16:56 +02:00
parent b9087294c2
commit 7201b31b55
14 changed files with 193 additions and 53 deletions

View File

@ -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_

29
include/gl/IDrawable.hpp Normal file
View File

@ -0,0 +1,29 @@
/*
* =====================================================================================
*
* Filename: IDrawable.hpp
*
* Description:
*
* Created: 14/06/2018 19:53:57
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#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_

View File

@ -0,0 +1,33 @@
/*
* =====================================================================================
*
* Filename: RenderStates.hpp
*
* Description:
*
* Created: 14/06/2018 19:52:27
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#ifndef RENDERSTATES_HPP_
#define RENDERSTATES_HPP_
#include <glm/matrix.hpp>
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_

View File

@ -0,0 +1,28 @@
/*
* =====================================================================================
*
* Filename: RenderTarget.hpp
*
* Description:
*
* Created: 14/06/2018 19:54:56
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#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_

View File

@ -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);

View File

@ -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_

View File

@ -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_

View File

@ -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_

View File

@ -88,7 +88,7 @@ class Chunk : public NonCopyable {
std::map<u32, u32> m_verticesID;
std::map<u32, u32> m_extendedFaces;
VertexBuffer m_vbo;
VertexBuffer m_vbo{GL_QUADS, 0, 0};
Chunk *m_surroundingChunks[4];

View File

@ -0,0 +1,46 @@
/*
* =====================================================================================
*
* Filename: RenderTarget.cpp
*
* Description:
*
* Created: 14/06/2018 19:55:28
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#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);
}

View File

@ -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));
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);