|
|
@@ -18,6 +18,7 @@
|
|
|
#include "scope_guard/scope_guard.hpp"
|
|
|
#include "vector/vector.hpp"
|
|
|
|
|
|
+#include "game/graphics/shader.hpp"
|
|
|
#include "game/util/env.hpp"
|
|
|
#include "game/util/files.hpp"
|
|
|
|
|
|
@@ -190,8 +191,20 @@ namespace graphics { namespace shaders {
|
|
|
using std::runtime_error::runtime_error;
|
|
|
};
|
|
|
|
|
|
- unsigned int init(unsigned int type, std::string const & path) {
|
|
|
- unsigned int id;
|
|
|
+ struct linker_error : std::runtime_error {
|
|
|
+ using std::runtime_error::runtime_error;
|
|
|
+ };
|
|
|
+
|
|
|
+ static int gltype(type tp) {
|
|
|
+ switch (tp) {
|
|
|
+ case type::FRAGMENT:
|
|
|
+ return GL_FRAGMENT_SHADER;
|
|
|
+ case type::VERTEX:
|
|
|
+ return GL_VERTEX_SHADER;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ unsigned int init(type tp, std::string const & path) {
|
|
|
std::unique_ptr<char const[]> buffer;
|
|
|
|
|
|
// 1. Load the vertex shader code (text file) to a new memory buffer
|
|
|
@@ -201,7 +214,8 @@ namespace graphics { namespace shaders {
|
|
|
}
|
|
|
|
|
|
// 2. Create a new shader ID
|
|
|
- id = glCreateShader(type); // GL_VERTEX_SHADER or GL_FRAGMENT_SHADER
|
|
|
+ // GL_VERTEX_SHADER or GL_FRAGMENT_SHADER
|
|
|
+ unsigned int id = glCreateShader(gltype(tp));
|
|
|
|
|
|
// 3. Associate the shader code with the new shader ID
|
|
|
char const * buffer_ptr = buffer.get();
|
|
|
@@ -221,4 +235,34 @@ namespace graphics { namespace shaders {
|
|
|
}
|
|
|
return id;
|
|
|
}
|
|
|
+
|
|
|
+ unsigned int init_program(shader const & fragmentShader,
|
|
|
+ shader const & vertexShader) {
|
|
|
+ // 9. Create a new shader program ID
|
|
|
+ unsigned int id = glCreateProgram();
|
|
|
+
|
|
|
+ // 10. Attach the vertex and fragment shaders to the new shader program
|
|
|
+ glAttachShader(id, vertexShader.id);
|
|
|
+ glAttachShader(id, fragmentShader.id);
|
|
|
+
|
|
|
+ // 11. Do some other advanced stuff we'll do later on (like setting generic
|
|
|
+ // vertex attrib locations)
|
|
|
+ glBindAttribLocation(id, 0, "a_position");
|
|
|
+ glBindAttribLocation(id, 1, "a_color");
|
|
|
+ glBindAttribLocation(id, 2, "a_texCoords");
|
|
|
+ glBindAttribLocation(id, 3, "a_normal");
|
|
|
+
|
|
|
+ // 12. Link the program
|
|
|
+ glLinkProgram(id);
|
|
|
+
|
|
|
+ // 13. Check for link errors
|
|
|
+ int return_code;
|
|
|
+ glGetProgramiv(id, GL_LINK_STATUS, &return_code);
|
|
|
+
|
|
|
+ if (return_code != GL_TRUE) {
|
|
|
+ // LogShaderProgramError(*id, vertPath, fragPath);
|
|
|
+ throw linker_error("Could not link shader program");
|
|
|
+ }
|
|
|
+ return id;
|
|
|
+ }
|
|
|
}}
|