From 712d40f3514e8598b616898943679d7cc108461f Mon Sep 17 00:00:00 2001 From: Maurice Mohlek Date: Wed, 2 Aug 2017 21:53:57 +0200 Subject: [PATCH] fix shader --- CMakeLists.txt | 1 + src/engine/shader/Shader.cpp | 4 ++++ src/engine/shader/ShaderProgram.cpp | 1 + src/main.cpp | 12 +++++++++--- src/shader/main.frag | 7 +++++-- src/shader/main.vert | 7 +++++++ 6 files changed, 27 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ead21fe..f9fdf7b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ set(SOURCE set(SHADERS src/shader/main.frag + src/shader/main.vert ) ######################################################### diff --git a/src/engine/shader/Shader.cpp b/src/engine/shader/Shader.cpp index 5afd669..d55dfb2 100644 --- a/src/engine/shader/Shader.cpp +++ b/src/engine/shader/Shader.cpp @@ -7,6 +7,10 @@ Shader::~Shader() { } void Shader::compile() { + if (shaderId) { + return; + } + this->shaderId = glCreateShader(this->shaderType); const char* s = this->source.c_str(); glShaderSource(this->shaderId, 1, &s, 0); diff --git a/src/engine/shader/ShaderProgram.cpp b/src/engine/shader/ShaderProgram.cpp index 2633a2e..57b0fdc 100644 --- a/src/engine/shader/ShaderProgram.cpp +++ b/src/engine/shader/ShaderProgram.cpp @@ -33,6 +33,7 @@ void ShaderProgram::link() { this->programId = glCreateProgram(); for (auto& s : this->shaders) { + s->compile(); glAttachShader(this->programId, s->getShaderId()); } diff --git a/src/main.cpp b/src/main.cpp index e492a46..3e18d87 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,9 +15,15 @@ int main(int argc, char** argv) { win->create(); ShaderProgram p1; - ShaderLoader sl(GL_FRAGMENT_SHADER, "shader/main.frag"); - Shader s(sl); - p1.shaders.push_back(std::make_shared(s)); + + ShaderLoader loadMainVertex(GL_VERTEX_SHADER, "shader/main.vert"); + Shader mainVertex(loadMainVertex); + p1.shaders.push_back(std::make_shared(mainVertex)); + + ShaderLoader loadMainFrag(GL_FRAGMENT_SHADER, "shader/main.frag"); + Shader mainFrag(loadMainFrag); + p1.shaders.push_back(std::make_shared(mainFrag)); + p1.link(); while (win->loop()) { diff --git a/src/shader/main.frag b/src/shader/main.frag index c5fceca..c48aa27 100644 --- a/src/shader/main.frag +++ b/src/shader/main.frag @@ -1,4 +1,7 @@ -#version 450 +#version 450 core -void main { +out vec4 color; + +void main() { + color = vec4(0.5, 0.0, 0.0, 1.0); } diff --git a/src/shader/main.vert b/src/shader/main.vert index e69de29..ec618e9 100644 --- a/src/shader/main.vert +++ b/src/shader/main.vert @@ -0,0 +1,7 @@ +#version 450 core + +layout (location = 0) in vec3 aPos; + +void main() { + gl_Position = vec4(aPos, 1.0); +}