aboutsummaryrefslogtreecommitdiffstats
path: root/src/graphics
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-03-23 08:45:12 +0000
committerJoursoir <chat@joursoir.net>2021-03-23 08:45:12 +0000
commit533abb2b643a0f8e1d41e0d570e08e3df5339f80 (patch)
tree18edf5a2a36f23f5b10ddf1ad5cd028253386ea8 /src/graphics
parent6eec70ec2ad54168442eca4f15030e7e6294cfb7 (diff)
downloadspace-simulator-533abb2b643a0f8e1d41e0d570e08e3df5339f80.tar.gz
space-simulator-533abb2b643a0f8e1d41e0d570e08e3df5339f80.tar.bz2
space-simulator-533abb2b643a0f8e1d41e0d570e08e3df5339f80.zip
add shader subsystem
Diffstat (limited to 'src/graphics')
-rw-r--r--src/graphics/Shader.cpp60
-rw-r--r--src/graphics/Shader.hpp31
2 files changed, 91 insertions, 0 deletions
diff --git a/src/graphics/Shader.cpp b/src/graphics/Shader.cpp
new file mode 100644
index 0000000..35a451d
--- /dev/null
+++ b/src/graphics/Shader.cpp
@@ -0,0 +1,60 @@
+#include <GL/glew.h>
+
+#include "Shader.hpp"
+
+Shader::~Shader()
+{
+ if(shader_id)
+ glDeleteShader(shader_id);
+ if(error_log)
+ delete[] error_log;
+}
+
+int Shader::Compile(const char *source, GLenum shaderType)
+{
+ GLint success;
+
+ shader_id = glCreateShader(shaderType);
+ glShaderSource(shader_id, 1, &source, NULL);
+ glCompileShader(shader_id);
+ glGetShaderiv(shader_id, GL_COMPILE_STATUS, &success);
+ if(success == 0) {
+ error_log = new char[GL_INFO_LOG_LENGTH];
+ glGetShaderInfoLog(shader_id, GL_INFO_LOG_LENGTH, NULL, error_log);
+ return 1;
+ }
+ return 0;
+}
+
+////////////////////////////////////////////////////////////////////
+
+ShaderManager::~ShaderManager()
+{
+ if(program_id)
+ glDeleteProgram(program_id);
+ if(error_log)
+ delete[] error_log;
+}
+
+int ShaderManager::Link(Shader *arr, int nsize)
+{
+ GLint success;
+ int i;
+
+ program_id = glCreateProgram();
+ for(i = 0; i < nsize; i++)
+ glAttachShader(program_id, arr[i].GetID());
+ glLinkProgram(program_id);
+ glGetProgramiv(program_id, GL_LINK_STATUS, &success);
+ if(success == 0) {
+ error_log = new char[GL_INFO_LOG_LENGTH];
+ glGetProgramInfoLog(program_id, GL_INFO_LOG_LENGTH, NULL, error_log);
+ return 1;
+ }
+ return 0;
+}
+
+void ShaderManager::Use()
+{
+ glUseProgram(program_id);
+}
diff --git a/src/graphics/Shader.hpp b/src/graphics/Shader.hpp
new file mode 100644
index 0000000..81841ad
--- /dev/null
+++ b/src/graphics/Shader.hpp
@@ -0,0 +1,31 @@
+#ifndef ENGINE_SHADER_H
+#define ENGINE_SHADER_H
+
+class Shader {
+ GLuint shader_id;
+ GLchar *error_log;
+
+public:
+ Shader() : shader_id(0), error_log(0) { }
+ ~Shader();
+
+ int Compile(const char *source, GLenum shaderType);
+ char *GetError() { return error_log; }
+ GLuint GetID() { return shader_id; }
+};
+
+class ShaderManager {
+ GLuint program_id;
+ GLchar *error_log;
+
+public:
+ ShaderManager() : program_id(0), error_log(0) { }
+ ~ShaderManager();
+
+ int Link(Shader *arr, int nsize);
+ char *GetError() { return error_log; }
+ GLuint GetID() { return program_id; }
+ void Use();
+};
+
+#endif /* ENGINE_SHADER_H */