Add possibility to define ShadingProgram with GLSL code

This commit is contained in:
Bruno Herbelin
2022-04-18 14:02:51 +02:00
parent 1c8575e40c
commit c25427cf4a
3 changed files with 32 additions and 17 deletions

View File

@@ -1,10 +1,7 @@
#ifndef IMAGEPROCESSINGSHADER_H #ifndef IMAGEPROCESSINGSHADER_H
#define IMAGEPROCESSINGSHADER_H #define IMAGEPROCESSINGSHADER_H
#include <glm/glm.hpp> #include "Shader.h"
#include "ImageShader.h"
class ImageProcessingShader : public Shader class ImageProcessingShader : public Shader
{ {

View File

@@ -72,16 +72,29 @@ GLenum blending_destination_function[9] = {GL_ONE_MINUS_SRC_ALPHA,// normal
GL_ONE, // lighten only GL_ONE, // lighten only
GL_ZERO}; GL_ZERO};
ShadingProgram::ShadingProgram(const std::string& vertex_file, const std::string& fragment_file) : ShadingProgram::ShadingProgram(const std::string& vertex, const std::string& fragment) :
vertex_id_(0), fragment_id_(0), id_(0), vertex_file_(vertex_file), fragment_file_(fragment_file) vertex_id_(0), fragment_id_(0), id_(0)
{ {
if (Resource::hasPath(vertex))
vertex_file_ = vertex;
else
vertex_code_ = vertex;
if (Resource::hasPath(fragment))
fragment_file_ = fragment;
else
fragment_code_ = fragment;
} }
void ShadingProgram::init() void ShadingProgram::init()
{ {
if (vertex_code_.empty())
vertex_code_ = Resource::getText(vertex_file_); vertex_code_ = Resource::getText(vertex_file_);
if (fragment_code_.empty())
fragment_code_ = Resource::getText(fragment_file_); fragment_code_ = Resource::getText(fragment_file_);
compile();
if ( compile() )
link(); link();
} }
@@ -90,7 +103,7 @@ bool ShadingProgram::initialized()
return (id_ != 0); return (id_ != 0);
} }
void ShadingProgram::compile() bool ShadingProgram::compile()
{ {
const char* vcode = vertex_code_.c_str(); const char* vcode = vertex_code_.c_str();
vertex_id_ = glCreateShader(GL_VERTEX_SHADER); vertex_id_ = glCreateShader(GL_VERTEX_SHADER);
@@ -102,7 +115,7 @@ void ShadingProgram::compile()
glShaderSource(fragment_id_, 1, &fcode, NULL); glShaderSource(fragment_id_, 1, &fcode, NULL);
glCompileShader(fragment_id_); glCompileShader(fragment_id_);
checkCompileErr(); return checkCompileErr();
} }
void ShadingProgram::link() void ShadingProgram::link()
@@ -117,7 +130,9 @@ void ShadingProgram::link()
glUniform1i(glGetUniformLocation(id_, "iChannel1"), 1); glUniform1i(glGetUniformLocation(id_, "iChannel1"), 1);
glUseProgram(0); glUseProgram(0);
glDeleteShader(vertex_id_); glDeleteShader(vertex_id_);
vertex_id_ = 0;
glDeleteShader(fragment_id_); glDeleteShader(fragment_id_);
fragment_id_ = 0;
} }
void ShadingProgram::use() void ShadingProgram::use()
@@ -125,7 +140,7 @@ void ShadingProgram::use()
if (currentProgram_ == nullptr || currentProgram_ != this) if (currentProgram_ == nullptr || currentProgram_ != this)
{ {
currentProgram_ = this; currentProgram_ = this;
glUseProgram(id_); glUseProgram(id_); // NB: if not linked, use 0 as default
} }
} }
@@ -190,7 +205,7 @@ void ShadingProgram::setUniform<glm::mat4>(const std::string& name, glm::mat4 va
// glUniformMatrix4fv(glGetUniformLocation(id_, name.c_str()), 1, GL_FALSE, val); // glUniformMatrix4fv(glGetUniformLocation(id_, name.c_str()), 1, GL_FALSE, val);
// } // }
void ShadingProgram::checkCompileErr() bool ShadingProgram::checkCompileErr()
{ {
int success; int success;
char infoLog[1024]; char infoLog[1024];
@@ -204,6 +219,7 @@ void ShadingProgram::checkCompileErr()
glGetShaderInfoLog(fragment_id_, 1024, NULL, infoLog); glGetShaderInfoLog(fragment_id_, 1024, NULL, infoLog);
Log::Warning("Error compiling Fragment ShadingProgram:\n%s", infoLog); Log::Warning("Error compiling Fragment ShadingProgram:\n%s", infoLog);
} }
return success;
} }
void ShadingProgram::checkLinkingErr() void ShadingProgram::checkLinkingErr()

View File

@@ -12,7 +12,9 @@ class FrameBuffer;
class ShadingProgram class ShadingProgram
{ {
public: public:
ShadingProgram(const std::string& vertex_file, const std::string& fragment_file); // create GLSL Program from resource file (if exist) or code of vertex and fragment shaders
ShadingProgram(const std::string& vertex, const std::string& fragment);
void init(); void init();
bool initialized(); bool initialized();
void use(); void use();
@@ -23,9 +25,9 @@ public:
static void enduse(); static void enduse();
private: private:
void checkCompileErr(); bool checkCompileErr();
void checkLinkingErr(); void checkLinkingErr();
void compile(); bool compile();
void link(); void link();
unsigned int vertex_id_, fragment_id_, id_; unsigned int vertex_id_, fragment_id_, id_;
std::string vertex_code_; std::string vertex_code_;