diff --git a/src/opengl/CMakeLists.txt b/src/opengl/CMakeLists.txt index fbfcfc7..f6d4229 100644 --- a/src/opengl/CMakeLists.txt +++ b/src/opengl/CMakeLists.txt @@ -18,6 +18,7 @@ add_executable( IndexBuffer.cpp VertexArray.cpp glew.c + utils.cpp ) diff --git a/src/opengl/Renderer.cpp b/src/opengl/Renderer.cpp index 6913b1d..2ec8216 100644 --- a/src/opengl/Renderer.cpp +++ b/src/opengl/Renderer.cpp @@ -1,26 +1,8 @@ #include "Renderer.h" -#include -//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; -} \ No newline at end of file diff --git a/src/opengl/Renderer.h b/src/opengl/Renderer.h index 3ca3b10..e29ff37 100644 --- a/src/opengl/Renderer.h +++ b/src/opengl/Renderer.h @@ -7,19 +7,12 @@ #include #include +#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); \ No newline at end of file +class Renderer +{ +public: + void draw(const VertexArray &va, const IndexBuffer &ib, const Shader &shader) const; +}; \ No newline at end of file diff --git a/src/opengl/Shader.cpp b/src/opengl/Shader.cpp index 4ea007c..604257e 100644 --- a/src/opengl/Shader.cpp +++ b/src/opengl/Shader.cpp @@ -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)); } diff --git a/src/opengl/Shader.h b/src/opengl/Shader.h index ca2c34b..742ddb1 100644 --- a/src/opengl/Shader.h +++ b/src/opengl/Shader.h @@ -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); diff --git a/src/opengl/VertexArray.cpp b/src/opengl/VertexArray.cpp index b290398..57768c8 100644 --- a/src/opengl/VertexArray.cpp +++ b/src/opengl/VertexArray.cpp @@ -1,5 +1,5 @@ #include "VertexArray.h" -#include "Renderer.h" +#include "utils.h" VertexArray::VertexArray() { diff --git a/src/opengl/VertexBuffer.cpp b/src/opengl/VertexBuffer.cpp index 73ef97b..fc87967 100644 --- a/src/opengl/VertexBuffer.cpp +++ b/src/opengl/VertexBuffer.cpp @@ -1,5 +1,5 @@ #include "VertexBuffer.h" -#include "Renderer.h" +#include "utils.h" VertexBuffer::VertexBuffer(const void *data, unsigned int size) { diff --git a/src/opengl/VertexBufferLayout.h b/src/opengl/VertexBufferLayout.h index 7c38c43..ca4c44b 100644 --- a/src/opengl/VertexBufferLayout.h +++ b/src/opengl/VertexBufferLayout.h @@ -2,7 +2,7 @@ #include #include "glew.h" -#include "Renderer.h" +#include "utils.h" struct VertexBufferElement { diff --git a/src/opengl/gl1.cpp b/src/opengl/gl1.cpp index 664253f..8982074 100644 --- a/src/opengl/gl1.cpp +++ b/src/opengl/gl1.cpp @@ -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) diff --git a/src/opengl/utils.cpp b/src/opengl/utils.cpp new file mode 100644 index 0000000..d57b039 --- /dev/null +++ b/src/opengl/utils.cpp @@ -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; +} \ No newline at end of file diff --git a/src/opengl/utils.h b/src/opengl/utils.h index a3f0ed6..403433a 100644 --- a/src/opengl/utils.h +++ b/src/opengl/utils.h @@ -1,8 +1,26 @@ #pragma once #include +#include "glew.h" +#include #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); \ No newline at end of file