Added Renderer class
This commit is contained in:
parent
a5c84e31d2
commit
d4827b1366
11 changed files with 67 additions and 45 deletions
|
@ -18,6 +18,7 @@ add_executable(
|
||||||
IndexBuffer.cpp
|
IndexBuffer.cpp
|
||||||
VertexArray.cpp
|
VertexArray.cpp
|
||||||
glew.c
|
glew.c
|
||||||
|
utils.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,26 +1,8 @@
|
||||||
#include "Renderer.h"
|
#include "Renderer.h"
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
//Discard error flags from openGL state machine
|
void Renderer::draw(const VertexArray& va,const IndexBuffer& ib, const Shader& shader) const {
|
||||||
void GLClearError()
|
shader.Bind();
|
||||||
{
|
va.Bind();
|
||||||
while (glGetError() != GL_NO_ERROR)
|
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;
|
|
||||||
}
|
|
|
@ -7,19 +7,12 @@
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
|
|
||||||
|
#include "VertexArray.h"
|
||||||
|
#include "IndexBuffer.h"
|
||||||
|
#include "Shader.h"
|
||||||
|
|
||||||
//This should be review to integrate with debugger
|
class Renderer
|
||||||
#define ASSERT(x) \
|
{
|
||||||
if (!(x)) \
|
public:
|
||||||
std::raise(SIGTRAP)
|
void draw(const VertexArray &va, const IndexBuffer &ib, const Shader &shader) const;
|
||||||
|
};
|
||||||
#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);
|
|
|
@ -19,12 +19,12 @@ Shader::~Shader()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::Bind()
|
void Shader::Bind() const
|
||||||
{
|
{
|
||||||
GLCall(glUseProgram(m_RendererID));
|
GLCall(glUseProgram(m_RendererID));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::Unbind()
|
void Shader::Unbind() const
|
||||||
{
|
{
|
||||||
GLCall(glUseProgram(0));
|
GLCall(glUseProgram(0));
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,8 +27,8 @@ public:
|
||||||
Shader(const std::string &filepath);
|
Shader(const std::string &filepath);
|
||||||
~Shader();
|
~Shader();
|
||||||
|
|
||||||
void Bind();
|
void Bind() const;
|
||||||
void Unbind();
|
void Unbind() const;
|
||||||
|
|
||||||
//Set uniforms
|
//Set uniforms
|
||||||
void SetUniform4f(const std::string &name, float v0, float v1, float v2, float v3);
|
void SetUniform4f(const std::string &name, float v0, float v1, float v2, float v3);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#include "VertexArray.h"
|
#include "VertexArray.h"
|
||||||
#include "Renderer.h"
|
#include "utils.h"
|
||||||
|
|
||||||
VertexArray::VertexArray()
|
VertexArray::VertexArray()
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#include "VertexBuffer.h"
|
#include "VertexBuffer.h"
|
||||||
#include "Renderer.h"
|
#include "utils.h"
|
||||||
|
|
||||||
VertexBuffer::VertexBuffer(const void *data, unsigned int size)
|
VertexBuffer::VertexBuffer(const void *data, unsigned int size)
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "glew.h"
|
#include "glew.h"
|
||||||
#include "Renderer.h"
|
#include "utils.h"
|
||||||
|
|
||||||
struct VertexBufferElement
|
struct VertexBufferElement
|
||||||
{
|
{
|
||||||
|
|
|
@ -79,6 +79,8 @@ int main(void)
|
||||||
ib.Unbind();
|
ib.Unbind();
|
||||||
shader.Unbind();
|
shader.Unbind();
|
||||||
|
|
||||||
|
Renderer renderer;
|
||||||
|
|
||||||
float red = 0.0f;
|
float red = 0.0f;
|
||||||
float increment = 0.05f;
|
float increment = 0.05f;
|
||||||
/* Loop until the user closes the window */
|
/* Loop until the user closes the window */
|
||||||
|
@ -110,6 +112,8 @@ int main(void)
|
||||||
shader.Bind();
|
shader.Bind();
|
||||||
shader.SetUniform4f("u_Color",red, 0.3f, 0.8f, 1.0f);
|
shader.SetUniform4f("u_Color",red, 0.3f, 0.8f, 1.0f);
|
||||||
|
|
||||||
|
renderer.draw(va,ib,shader);
|
||||||
|
|
||||||
if (red > 1.0f)
|
if (red > 1.0f)
|
||||||
increment = -0.05f;
|
increment = -0.05f;
|
||||||
else if (red < 0.0f)
|
else if (red < 0.0f)
|
||||||
|
|
24
src/opengl/utils.cpp
Normal file
24
src/opengl/utils.cpp
Normal 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;
|
||||||
|
}
|
|
@ -1,8 +1,26 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include "glew.h"
|
||||||
|
#include <csignal>
|
||||||
|
|
||||||
#if IS_DEBUG==1
|
#if IS_DEBUG==1
|
||||||
#define LOG(x) std::cout << x << std::endl
|
#define LOG(x) std::cout << x << std::endl
|
||||||
#else
|
#else
|
||||||
#define LOG(x)
|
#define LOG(x)
|
||||||
#endif
|
#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);
|
Loading…
Reference in a new issue