diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl new file mode 100644 index 0000000..7376f07 --- /dev/null +++ b/shaders/fragment.glsl @@ -0,0 +1,6 @@ +#version 330 core +out vec4 FragColor; + +void main() { + FragColor = vec4(1.0f, 1.0f, 1.0f, 0.0f); +} diff --git a/shaders/vert.shader b/shaders/vertex.glsl similarity index 87% rename from shaders/vert.shader rename to shaders/vertex.glsl index ea9cef1..2969ee2 100644 --- a/shaders/vert.shader +++ b/shaders/vertex.glsl @@ -1,6 +1,7 @@ #version 330 core layout (location = 0) in vec3 aPos; -void main() { +void main() +{ gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); } diff --git a/src/main.cpp b/src/main.cpp index f9d4b61..ef76cb1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,19 +8,8 @@ #define WINDOW_WIDTH 640 #define WINDOW_HEIGHT 480 -const char *vertexShaderSource = "#version 330 core\n" - "layout (location = 0) in vec3 aPos;\n" - "void main()\n" - "{\n" - " gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" - "}\0"; - -const char *fragShaderSource = "#version 330 core\n" - "out vec4 FragColor;\n" - "void main()\n" - "{\n" - " FragColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n" - "}\0"; +#define VERT_SHADER_SRC_FILE "shaders/vertex.glsl" +#define FRAG_SHADER_SRC_FILE "shaders/fragment.glsl" float verts[] = { 0.5f, 0.5f, 0.0f, @@ -53,8 +42,8 @@ struct RenderObj { RenderObj preRenderCallback() { // Compile a bunch of shaders etc - unsigned int vertShader = Shader::compile(GL_VERTEX_SHADER, vertexShaderSource); - unsigned int fragShader = Shader::compile(GL_FRAGMENT_SHADER, fragShaderSource); + unsigned int vertShader = Shader::compileFromFile(GL_VERTEX_SHADER, VERT_SHADER_SRC_FILE); + unsigned int fragShader = Shader::compileFromFile(GL_FRAGMENT_SHADER, FRAG_SHADER_SRC_FILE); unsigned int shaders[2] = {vertShader, fragShader}; // Create a shader program & link the shaders etc diff --git a/src/shaders.cpp b/src/shaders.cpp index c888f7f..3880d69 100644 --- a/src/shaders.cpp +++ b/src/shaders.cpp @@ -1,16 +1,32 @@ #include #include #include -#include +#include +#include namespace Shader { - char* loadSourceFromFile(const char* file) { - char* src; + std::string loadSourceFromFile(const char* file) { + std::string content; + std::ifstream fh(file, std::ios::in); - return src; + if(!fh.is_open()) { + printf("Unable to read file \"%s\"!", file); + return ""; + } + + std::string line = ""; + while(!fh.eof()) { + std::getline(fh, line); + content.append(line + "\n"); + } + + fh.close(); + return content.c_str(); } unsigned int compile(GLenum shadertype, const char* shaderSource) { + printf("--------------SRC-PREVIEW--------------\n%s\n---------------------------------------\n", shaderSource); + unsigned int shader; shader = glCreateShader(shadertype); glShaderSource(shader, 1, &shaderSource, NULL); @@ -27,6 +43,12 @@ namespace Shader { return shader; } + unsigned int compileFromFile(GLenum shadertype, const char* file) { + std::string src = loadSourceFromFile(file); + unsigned int shader = compile(shadertype, src.c_str()); + return shader; + } + unsigned int createProgram(std::vector shaders) { unsigned int shaderProg; shaderProg = glCreateProgram();