Added Renderer class

This commit is contained in:
Vitor Fernandes 2020-06-21 22:39:55 +01:00
parent a5c84e31d2
commit d4827b1366
No known key found for this signature in database
GPG key ID: EBFB4EE09F348A26
11 changed files with 67 additions and 45 deletions

View file

@ -18,6 +18,7 @@ add_executable(
IndexBuffer.cpp
VertexArray.cpp
glew.c
utils.cpp
)

View file

@ -1,26 +1,8 @@
#include "Renderer.h"
#include <iostream>
//Discard error flags from openGL state machine
void GLClearError()
{
while (glGetError() != GL_NO_ERROR)
;
void Renderer::draw(const VertexArray& va,const IndexBuffer& ib, const Shader& shader) const {
shader.Bind();
va.Bind();
ib.Bind();
GLCall(glDrawElements(GL_TRIANGLES,ib.GetCount(),GL_UNSIGNED_INT,nullptr));
}
bool GLLogCall(const char *function, const char *file, int line)
{
while (GLenum error = glGetError())
{
std::cout << "[OpenGL error] ("
<< error << "): " << function << " "
<< file << ":" << line << std::endl;
return true;
}
return false;
}
void ErrorGLCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam)
{
std::cout << "Source: " << source << "Type: " << type << "Id: " << id << "Severity: " << severity << "Length: " << length << message << std::endl;
}

View file

@ -7,19 +7,12 @@
#include <GLFW/glfw3.h>
#include <csignal>
#include "VertexArray.h"
#include "IndexBuffer.h"
#include "Shader.h"
//This should be review to integrate with debugger
#define ASSERT(x) \
if (!(x)) \
std::raise(SIGTRAP)
#define GLCall(x) \
GLClearError(); \
x; \
ASSERT(GLLogCall(#x, __FILE__, __LINE__))
//Discard error flags from openGL state machine
void GLClearError();
bool GLLogCall(const char *function, const char *file, int line);
//This is just for OpenGL4.3 onwards
void ErrorGLCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam);
class Renderer
{
public:
void draw(const VertexArray &va, const IndexBuffer &ib, const Shader &shader) const;
};

View file

@ -19,12 +19,12 @@ Shader::~Shader()
{
}
void Shader::Bind()
void Shader::Bind() const
{
GLCall(glUseProgram(m_RendererID));
}
void Shader::Unbind()
void Shader::Unbind() const
{
GLCall(glUseProgram(0));
}

View file

@ -27,8 +27,8 @@ public:
Shader(const std::string &filepath);
~Shader();
void Bind();
void Unbind();
void Bind() const;
void Unbind() const;
//Set uniforms
void SetUniform4f(const std::string &name, float v0, float v1, float v2, float v3);

View file

@ -1,5 +1,5 @@
#include "VertexArray.h"
#include "Renderer.h"
#include "utils.h"
VertexArray::VertexArray()
{

View file

@ -1,5 +1,5 @@
#include "VertexBuffer.h"
#include "Renderer.h"
#include "utils.h"
VertexBuffer::VertexBuffer(const void *data, unsigned int size)
{

View file

@ -2,7 +2,7 @@
#include <vector>
#include "glew.h"
#include "Renderer.h"
#include "utils.h"
struct VertexBufferElement
{

View file

@ -79,6 +79,8 @@ int main(void)
ib.Unbind();
shader.Unbind();
Renderer renderer;
float red = 0.0f;
float increment = 0.05f;
/* Loop until the user closes the window */
@ -110,6 +112,8 @@ int main(void)
shader.Bind();
shader.SetUniform4f("u_Color",red, 0.3f, 0.8f, 1.0f);
renderer.draw(va,ib,shader);
if (red > 1.0f)
increment = -0.05f;
else if (red < 0.0f)

24
src/opengl/utils.cpp Normal file
View file

@ -0,0 +1,24 @@
#include "utils.h"
//Discard error flags from openGL state machine
void GLClearError()
{
while (glGetError() != GL_NO_ERROR)
;
}
bool GLLogCall(const char *function, const char *file, int line)
{
while (GLenum error = glGetError())
{
std::cout << "[OpenGL error] ("
<< error << "): " << function << " "
<< file << ":" << line << std::endl;
return true;
}
return false;
}
void ErrorGLCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam)
{
std::cout << "Source: " << source << "Type: " << type << "Id: " << id << "Severity: " << severity << "Length: " << length << message << std::endl;
}

View file

@ -1,8 +1,26 @@
#pragma once
#include <iostream>
#include "glew.h"
#include <csignal>
#if IS_DEBUG==1
#define LOG(x) std::cout << x << std::endl
#else
#define LOG(x)
#endif
//This should be review to integrate with debugger
#define ASSERT(x) \
if (!(x)) \
std::raise(SIGTRAP)
#define GLCall(x) \
GLClearError(); \
x; \
ASSERT(GLLogCall(#x, __FILE__, __LINE__))
//Discard error flags from openGL state machine
void GLClearError();
bool GLLogCall(const char *function, const char *file, int line);
//This is just for OpenGL4.3 onwards
void ErrorGLCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam);