Skip to content

Commit 5d8cd1e

Browse files
Added shader program helper interface to GLRender
1 parent e1c3006 commit 5d8cd1e

File tree

3 files changed

+90
-28
lines changed

3 files changed

+90
-28
lines changed

glshift/GLRenderer.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,42 @@
33
//
44

55
#include "GLRenderer.h"
6+
#include <iostream>
67

78
void GLShift::GLRenderer::setWindow(GLFWwindow * context) {
89
this->window = context;
910
}
11+
12+
void GLShift::GLRenderer::createProgram(const std::string& name) {
13+
if(this->glPrograms.count(name) > 0) {
14+
std::cerr << "Program already exists." << std::endl;
15+
return;
16+
}
17+
18+
this->glPrograms[name] = glCreateProgram();
19+
}
20+
21+
void GLShift::GLRenderer::addShader(const std::string& programName, const char *shaderSource, GLint shader_type) {
22+
GLuint vShader = glCreateShader(shader_type);
23+
glShaderSource(vShader, 1, &shaderSource, nullptr);
24+
glCompileShader(vShader);
25+
glAttachShader(this->glPrograms[programName], vShader);
26+
}
27+
28+
void GLShift::GLRenderer::linkProgram(const std::string& name) {
29+
if(this->glPrograms.count(name) == 0) {
30+
std::cerr << "Program doesn't exist!" << std::endl;
31+
return;
32+
}
33+
34+
glLinkProgram(this->glPrograms[name]);
35+
}
36+
37+
void GLShift::GLRenderer::useProgram(const std::string &name) {
38+
if(this->glPrograms.count(name) == 0) {
39+
std::cerr << "Program doesn't exist!" << std::endl;
40+
return;
41+
}
42+
43+
glUseProgram(this->glPrograms[name]);
44+
}

glshift/GLRenderer.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define YOURAPPNAME_GLRENDERER_H
77

88
#include "definitions.h"
9+
#include <unordered_map>
910

1011
class GLShift::GLRenderer {
1112
public:
@@ -17,8 +18,39 @@ class GLShift::GLRenderer {
1718
* Override to provide code for the initialization loop
1819
*/
1920
virtual void init() = 0;
21+
/**
22+
* Set the window that the render applies to
23+
* @param context
24+
*/
2025
void setWindow(GLFWwindow * context);
21-
private:
26+
/**
27+
* Create and store an empty program in the renderer
28+
* @param name Name to reference to the program
29+
*/
30+
void createProgram(const std::string& name);
31+
/**
32+
* Link the shaders given to the programs to make it complete
33+
* @param name Name of program to link
34+
*/
35+
void linkProgram(const std::string& name);
36+
/**
37+
* Add a shader to a program
38+
* @param programName Program name to add to
39+
* @param shaderSource Source code of the shader
40+
* @param shader_type Shader type
41+
*/
42+
void addShader(const std::string& programName, const char* shaderSource, GLint shader_type = GL_VERTEX_SHADER);
43+
/**
44+
* A safe function to call programs.
45+
* Newbies might use the command line `glUseProgram(this->glPrograms["name"])`.
46+
* The main problem is that if the program with the key "name" doesn't exists,
47+
* then it will be created, thus you will get en error. This functions checks
48+
* if the key exists before proceeding to use the program to avoid errors
49+
* @param name Name of the program to load
50+
*/
51+
void useProgram(const std::string& name);
52+
protected:
53+
std::unordered_map<std::string, GLuint> glPrograms;
2254
GLFWwindow * window;
2355
};
2456

main.cpp

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,7 @@
11
#include "glshift/GLShift.h"
2-
#include <vector>
32

43
#define numVAOs 1
54

6-
GLuint createShaderProgram() {
7-
const char* vShaderSource =
8-
"#version 430 \n"
9-
"void main(void) \n"
10-
"{ gl_Position = vec4(0.0, 0.0, 0.0, 1.0); }";
11-
const char* fShaderSource =
12-
"#version 430 \n"
13-
"out vec4 color; \n"
14-
"void main(void) \n"
15-
"{ color = vec4(0.0, 1.0, 1.0, 1.0); }";
16-
17-
GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
18-
GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);
19-
glShaderSource(vShader, 1, &vShaderSource, NULL); glCompileShader(vShader);
20-
glShaderSource(fShader, 1, &fShaderSource, NULL); glCompileShader(fShader);
21-
22-
GLuint vfProgram = glCreateProgram();
23-
glAttachShader(vfProgram, vShader); glAttachShader(vfProgram, fShader);
24-
glLinkProgram(vfProgram);
25-
return vfProgram;
26-
}
27-
285
/**
296
* Example for a renderer implementation
307
*/
@@ -34,23 +11,41 @@ class MyRenderer : public GLShift::GLRenderer {
3411
glClearColor(1.0, 0.0, 0.0, 1.0);
3512
glClear(GL_COLOR_BUFFER_BIT);
3613

37-
glUseProgram(this->renderingProgram);
14+
// Rather use this function instead of glUseProgram
15+
// Since it checks for availability of the program
16+
this->useProgram("main");
3817
glDrawArrays(GL_POINTS, 0, 1);
3918
}
4019

4120
void init() override {
42-
this->renderingProgram = createShaderProgram();
21+
// Example for fixed shader
22+
const char* vShaderSource =
23+
"#version 430 \n"
24+
"void main(void) \n"
25+
"{ gl_Position = vec4(0.0, 0.0, 0.0, 1.0); }";
26+
const char* fShaderSource =
27+
"#version 430 \n"
28+
"out vec4 color; \n"
29+
"void main(void) \n"
30+
"{ color = vec4(0.0, 1.0, 1.0, 1.0); }";
31+
32+
// Example for creating and linking a shader pipeline
33+
this->createProgram("main");
34+
this->addShader("main", vShaderSource, GL_VERTEX_SHADER);
35+
this->addShader("main", fShaderSource, GL_FRAGMENT_SHADER);
36+
this->linkProgram("main");
37+
38+
// Just a point
4339
glGenVertexArrays(numVAOs, this->vao);
4440
glBindVertexArray(vao[0]);
4541
}
4642

4743
private:
48-
GLuint renderingProgram;
4944
GLuint vao[numVAOs];
5045
};
5146

5247
int main() {
53-
/**
48+
/*
5449
* Example for a GLManager implementation
5550
*/
5651
GLShift::GLManager glManager = GLShift::GLManager(4, 3);

0 commit comments

Comments
 (0)