diff --git a/glshift/GLManager.cpp b/glshift/GLManager.cpp index 3807e07..0ea94f5 100644 --- a/glshift/GLManager.cpp +++ b/glshift/GLManager.cpp @@ -40,6 +40,7 @@ void GLShift::GLManager::openWindow(int width, int height, const char* title,boo void GLShift::GLManager::setRenderer(GLShift::GLRenderer *renderer) { this->renderer = renderer; + this->renderer->setWindow(this->window); // Call initialization function this->renderer->init(); } @@ -57,7 +58,6 @@ void GLShift::GLManager::run() { while(!glfwWindowShouldClose(this->window)) { this->renderer->render(); - glClear(GL_COLOR_BUFFER_BIT); glfwSwapBuffers(this->window); glfwPollEvents(); } diff --git a/glshift/GLRenderer.cpp b/glshift/GLRenderer.cpp index c7a54ea..68e1f36 100644 --- a/glshift/GLRenderer.cpp +++ b/glshift/GLRenderer.cpp @@ -2,4 +2,8 @@ // Created by lesscomplex on 08/03/2020. // -#include "GLRenderer.h" \ No newline at end of file +#include "GLRenderer.h" + +void GLShift::GLRenderer::setWindow(GLFWwindow * context) { + this->window = context; +} diff --git a/glshift/GLRenderer.h b/glshift/GLRenderer.h index 1d1419b..32909c0 100644 --- a/glshift/GLRenderer.h +++ b/glshift/GLRenderer.h @@ -17,6 +17,9 @@ class GLShift::GLRenderer { * Override to provide code for the initialization loop */ virtual void init() = 0; + void setWindow(GLFWwindow * context); +private: + GLFWwindow * window; }; #endif //YOURAPPNAME_GLRENDERER_H diff --git a/main.cpp b/main.cpp index 2f7580c..cc27e3d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,29 @@ #include "glshift/GLShift.h" +#include + +#define numVAOs 1 + +GLuint createShaderProgram() { + const char* vShaderSource = + "#version 430 \n" + "void main(void) \n" + "{ gl_Position = vec4(0.0, 0.0, 0.0, 1.0); }"; + const char* fShaderSource = + "#version 430 \n" + "out vec4 color; \n" + "void main(void) \n" + "{ color = vec4(0.0, 1.0, 1.0, 1.0); }"; + + GLuint vShader = glCreateShader(GL_VERTEX_SHADER); + GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(vShader, 1, &vShaderSource, NULL); glCompileShader(vShader); + glShaderSource(fShader, 1, &fShaderSource, NULL); glCompileShader(fShader); + + GLuint vfProgram = glCreateProgram(); + glAttachShader(vfProgram, vShader); glAttachShader(vfProgram, fShader); + glLinkProgram(vfProgram); + return vfProgram; +} /** * Example for a renderer implementation @@ -6,17 +31,29 @@ class MyRenderer : public GLShift::GLRenderer { public: void render() override { - glClearColor(1, 0, 0, 1); + glClearColor(1.0, 0.0, 0.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + + glUseProgram(this->renderingProgram); + glDrawArrays(GL_POINTS, 0, 1); + } + + void init() override { + this->renderingProgram = createShaderProgram(); + glGenVertexArrays(numVAOs, this->vao); + glBindVertexArray(vao[0]); } - void init() override {} +private: + GLuint renderingProgram; + GLuint vao[numVAOs]; }; int main() { /** * Example for a GLManager implementation */ - GLShift::GLManager glManager = GLShift::GLManager(); + GLShift::GLManager glManager = GLShift::GLManager(4, 3); glManager.openWindow(600, 300, "Yeahh!"); glManager.setRenderer(new MyRenderer()); glManager.run();