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
#define IMAGEPROCESSINGSHADER_H
#include <glm/glm.hpp>
#include "ImageShader.h"
#include "Shader.h"
class ImageProcessingShader : public Shader
{

View File

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

View File

@@ -12,7 +12,9 @@ class FrameBuffer;
class ShadingProgram
{
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();
bool initialized();
void use();
@@ -23,9 +25,9 @@ public:
static void enduse();
private:
void checkCompileErr();
bool checkCompileErr();
void checkLinkingErr();
void compile();
bool compile();
void link();
unsigned int vertex_id_, fragment_id_, id_;
std::string vertex_code_;