diff options
author | Andrei Karas <akaras@inbox.ru> | 2014-06-08 15:40:35 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2014-06-08 15:40:35 +0300 |
commit | 548c2b03f8184497a5306f4b4412e3120eaef211 (patch) | |
tree | a3813d15d21c320be53650e1e195b68842f12197 /src/render/shaders/shadersmanager.cpp | |
parent | 0f500fab826a57f8aae8a3e2062a90ee9e80a199 (diff) | |
download | mv-548c2b03f8184497a5306f4b4412e3120eaef211.tar.gz mv-548c2b03f8184497a5306f4b4412e3120eaef211.tar.bz2 mv-548c2b03f8184497a5306f4b4412e3120eaef211.tar.xz mv-548c2b03f8184497a5306f4b4412e3120eaef211.zip |
Add shaderprogram class.
Diffstat (limited to 'src/render/shaders/shadersmanager.cpp')
-rw-r--r-- | src/render/shaders/shadersmanager.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/render/shaders/shadersmanager.cpp b/src/render/shaders/shadersmanager.cpp index 85c5bf64e..e457ba473 100644 --- a/src/render/shaders/shadersmanager.cpp +++ b/src/render/shaders/shadersmanager.cpp @@ -28,6 +28,9 @@ #include "render/mgl.h" #include "render/shaders/shader.h" +#include "render/shaders/shaderprogram.h" + +#include "resources/resourcemanager.h" #include "utils/files.h" @@ -59,4 +62,52 @@ Shader *ShadersManager::createShader(const unsigned int type, return nullptr; } +ShaderProgram *ShadersManager::createProgram(const std::string &vertex, + const std::string &fragment) +{ + ResourceManager *const resman = ResourceManager::getInstance(); + Shader *const vertexShader = static_cast<Shader*>( + resman->getShader(GL_VERTEX_SHADER, vertex)); + if (!vertexShader) + return nullptr; + + Shader *const fragmentShader = static_cast<Shader*>( + resman->getShader(GL_FRAGMENT_SHADER, fragment)); + + if (!fragmentShader) + { + vertexShader->decRef(); + return nullptr; + } + + GLuint programId = mglCreateProgram(); + if (!programId) + { + vertexShader->decRef(); + fragmentShader->decRef(); + return nullptr; + } + + mglAttachShader(programId, vertexShader->getShaderId()); + mglAttachShader(programId, fragmentShader->getShaderId()); + mglLinkProgram(programId); + GLint isLinked = 0; + mglGetProgramiv(programId, GL_LINK_STATUS, &isLinked); + if (isLinked == GL_TRUE) + return new ShaderProgram(programId, vertexShader, fragmentShader); + + GLint len = 0; + mglGetProgramiv(programId, GL_INFO_LOG_LENGTH, &len); + char *buf = new char[len + 1]; + mglGetProgramInfoLog(programId, len, &len, buf); + buf[len] = 0; + logger->log("Program '%s, %s' compilation error: %s", + vertexShader->getIdPath().c_str(), + fragmentShader->getIdPath().c_str(), + buf); + delete [] buf; + mglDeleteProgram(programId); + return nullptr; +} + #endif |