Skip to content

Commit

Permalink
Added shader program helper interface to GLRender
Browse files Browse the repository at this point in the history
  • Loading branch information
LessComplexity committed Mar 8, 2020
1 parent e1c3006 commit 5d8cd1e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 28 deletions.
35 changes: 35 additions & 0 deletions glshift/GLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,42 @@
//

#include "GLRenderer.h"
#include <iostream>

void GLShift::GLRenderer::setWindow(GLFWwindow * context) {
this->window = context;
}

void GLShift::GLRenderer::createProgram(const std::string& name) {
if(this->glPrograms.count(name) > 0) {
std::cerr << "Program already exists." << std::endl;
return;
}

this->glPrograms[name] = glCreateProgram();
}

void GLShift::GLRenderer::addShader(const std::string& programName, const char *shaderSource, GLint shader_type) {
GLuint vShader = glCreateShader(shader_type);
glShaderSource(vShader, 1, &shaderSource, nullptr);
glCompileShader(vShader);
glAttachShader(this->glPrograms[programName], vShader);
}

void GLShift::GLRenderer::linkProgram(const std::string& name) {
if(this->glPrograms.count(name) == 0) {
std::cerr << "Program doesn't exist!" << std::endl;
return;
}

glLinkProgram(this->glPrograms[name]);
}

void GLShift::GLRenderer::useProgram(const std::string &name) {
if(this->glPrograms.count(name) == 0) {
std::cerr << "Program doesn't exist!" << std::endl;
return;
}

glUseProgram(this->glPrograms[name]);
}
34 changes: 33 additions & 1 deletion glshift/GLRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define YOURAPPNAME_GLRENDERER_H

#include "definitions.h"
#include <unordered_map>

class GLShift::GLRenderer {
public:
Expand All @@ -17,8 +18,39 @@ class GLShift::GLRenderer {
* Override to provide code for the initialization loop
*/
virtual void init() = 0;
/**
* Set the window that the render applies to
* @param context
*/
void setWindow(GLFWwindow * context);
private:
/**
* Create and store an empty program in the renderer
* @param name Name to reference to the program
*/
void createProgram(const std::string& name);
/**
* Link the shaders given to the programs to make it complete
* @param name Name of program to link
*/
void linkProgram(const std::string& name);
/**
* Add a shader to a program
* @param programName Program name to add to
* @param shaderSource Source code of the shader
* @param shader_type Shader type
*/
void addShader(const std::string& programName, const char* shaderSource, GLint shader_type = GL_VERTEX_SHADER);
/**
* A safe function to call programs.
* Newbies might use the command line `glUseProgram(this->glPrograms["name"])`.
* The main problem is that if the program with the key "name" doesn't exists,
* then it will be created, thus you will get en error. This functions checks
* if the key exists before proceeding to use the program to avoid errors
* @param name Name of the program to load
*/
void useProgram(const std::string& name);
protected:
std::unordered_map<std::string, GLuint> glPrograms;
GLFWwindow * window;
};

Expand Down
49 changes: 22 additions & 27 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
#include "glshift/GLShift.h"
#include <vector>

#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
*/
Expand All @@ -34,23 +11,41 @@ class MyRenderer : public GLShift::GLRenderer {
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);

glUseProgram(this->renderingProgram);
// Rather use this function instead of glUseProgram
// Since it checks for availability of the program
this->useProgram("main");
glDrawArrays(GL_POINTS, 0, 1);
}

void init() override {
this->renderingProgram = createShaderProgram();
// Example for fixed shader
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); }";

// Example for creating and linking a shader pipeline
this->createProgram("main");
this->addShader("main", vShaderSource, GL_VERTEX_SHADER);
this->addShader("main", fShaderSource, GL_FRAGMENT_SHADER);
this->linkProgram("main");

// Just a point
glGenVertexArrays(numVAOs, this->vao);
glBindVertexArray(vao[0]);
}

private:
GLuint renderingProgram;
GLuint vao[numVAOs];
};

int main() {
/**
/*
* Example for a GLManager implementation
*/
GLShift::GLManager glManager = GLShift::GLManager(4, 3);
Expand Down

0 comments on commit 5d8cd1e

Please sign in to comment.