mirror of
https://github.com/brunoherbelin/vimix.git
synced 2025-12-05 23:40:02 +01:00
Preliminary implementation of Shader editor
Connect TextEditor with ImageFilter from current Clone Source. GLSL Compilation seems to work....
This commit is contained in:
@@ -28,13 +28,12 @@
|
||||
#include "Visitor.h"
|
||||
#include "FrameBuffer.h"
|
||||
#include "Decorations.h"
|
||||
#include "ImageFilter.h"
|
||||
|
||||
#include "CloneSource.h"
|
||||
|
||||
|
||||
CloneSource::CloneSource(Source *origin, uint64_t id) : Source(id), origin_(origin), cloningsurface_(nullptr),
|
||||
garbage_image_(nullptr), timer_reset_(false), delay_(0.0), paused_(false), filter_(nullptr)
|
||||
garbage_image_(nullptr), timer_reset_(false), delay_(0.0), paused_(false), filter_render_(nullptr)
|
||||
{
|
||||
// initial name copies the origin name: diplucates are namanged in session
|
||||
name_ = origin->name();
|
||||
@@ -65,8 +64,8 @@ CloneSource::~CloneSource()
|
||||
images_.pop();
|
||||
}
|
||||
|
||||
if (filter_)
|
||||
delete filter_;
|
||||
if (filter_render_)
|
||||
delete filter_render_;
|
||||
|
||||
if (cloningsurface_)
|
||||
delete cloningsurface_;
|
||||
@@ -88,13 +87,14 @@ void CloneSource::init()
|
||||
// ask to reset elapsed-timer
|
||||
timer_reset_ = true;
|
||||
|
||||
// create filter
|
||||
filter_ = new ImageFilter( res, true );
|
||||
filter_->setInputTexture( images_.front()->texture() );
|
||||
// create filter for this resolution (with alpha channel)
|
||||
filter_render_ = new ImageFilterRenderer( res );
|
||||
|
||||
// set initial texture surface
|
||||
texturesurface_->setTextureIndex( filter_->getOutputTexture() );
|
||||
// texturesurface_->setTextureIndex( images_.front()->texture() );
|
||||
// provide initial texture to filter
|
||||
filter_render_->setInputTexture( images_.front()->texture() );
|
||||
|
||||
// set texture surface to draw the result of the filter
|
||||
texturesurface_->setTextureIndex( filter_render_->getOutputTexture() );
|
||||
|
||||
// create render Frame buffer matching size of images
|
||||
FrameBuffer *renderbuffer = new FrameBuffer( res, true );
|
||||
@@ -119,8 +119,8 @@ void CloneSource::render()
|
||||
init();
|
||||
else {
|
||||
// render filtered image
|
||||
filter_->draw();
|
||||
texturesurface_->setTextureIndex( filter_->getOutputTexture() );
|
||||
filter_render_->draw();
|
||||
texturesurface_->setTextureIndex( filter_render_->getOutputTexture() );
|
||||
|
||||
// render textured surface into frame buffer
|
||||
renderbuffer_->begin();
|
||||
@@ -206,12 +206,12 @@ void CloneSource::update(float dt)
|
||||
|
||||
// make sure the queue is not empty (in case of failure above)
|
||||
if ( !images_.empty() ) {
|
||||
// blit rendered framebuffer in the newest image (back)
|
||||
// blit origin framebuffer in the newest image (back)
|
||||
origin_->frame()->blit( images_.back() );
|
||||
|
||||
// update the source surface to be rendered with the oldest image (front)
|
||||
// update the surface to be rendered with the oldest image (front)
|
||||
// texturesurface_->setTextureIndex( images_.front()->texture() );
|
||||
filter_->setInputTexture( images_.front()->texture() );
|
||||
filter_render_->setInputTexture( images_.front()->texture() );
|
||||
|
||||
}
|
||||
}
|
||||
@@ -228,6 +228,17 @@ void CloneSource::setDelay(double second)
|
||||
delay_ = CLAMP(second, 0.0, 2.0);
|
||||
}
|
||||
|
||||
|
||||
void CloneSource::setFilter(const ImageFilter &filter)
|
||||
{
|
||||
filter_render_->setFilter(filter);
|
||||
}
|
||||
|
||||
ImageFilter CloneSource::filter() const
|
||||
{
|
||||
return filter_render_->filter();
|
||||
}
|
||||
|
||||
void CloneSource::play (bool on)
|
||||
{
|
||||
// if a different state is asked
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
#include <queue>
|
||||
|
||||
#include "Source.h"
|
||||
#include "ImageFilter.h"
|
||||
|
||||
class ImageFilterRenderer;
|
||||
|
||||
class CloneSource : public Source
|
||||
{
|
||||
@@ -31,7 +34,10 @@ public:
|
||||
|
||||
// Clone properties
|
||||
void setDelay(double second);
|
||||
double delay() const { return delay_; }
|
||||
inline double delay() const { return delay_; }
|
||||
|
||||
void setFilter(const ImageFilter &filter);
|
||||
ImageFilter filter() const;
|
||||
|
||||
glm::ivec2 icon() const override;
|
||||
std::string info() const override;
|
||||
@@ -59,7 +65,7 @@ protected:
|
||||
bool paused_;
|
||||
|
||||
// filter
|
||||
class ImageFilter *filter_;
|
||||
ImageFilterRenderer *filter_render_;
|
||||
|
||||
// connecting line
|
||||
class DotLine *connection_;
|
||||
|
||||
317
ImageFilter.cpp
317
ImageFilter.cpp
@@ -16,12 +16,14 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
**/
|
||||
#include <ctime>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_access.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
#include "defines.h"
|
||||
#include "Shader.h"
|
||||
#include "Visitor.h"
|
||||
#include "FrameBuffer.h"
|
||||
#include "Primitives.h"
|
||||
@@ -29,93 +31,76 @@
|
||||
|
||||
#include "ImageFilter.h"
|
||||
|
||||
std::string fragmentDeclaration = "#version 330 core\n"
|
||||
"out vec4 FragColor;\n"
|
||||
"in vec4 vertexColor;\n"
|
||||
"in vec2 vertexUV;\n"
|
||||
"vec3 iChannelResolution[2];\n"
|
||||
"uniform mat4 iTransform;\n"
|
||||
"uniform vec4 color;\n"
|
||||
"uniform float stipple;\n";
|
||||
std::string ShaderToyHeaderHelp = "vec3 iResolution; // viewport resolution (in pixels)\n"
|
||||
"float iTime; // shader playback time (in seconds)\n"
|
||||
"float iTimeDelta; // render time (in seconds)\n"
|
||||
"int iFrame; // shader playback frame\n"
|
||||
"vec3 iChannelResolution[2]; // input channel resolution (in pixels)\n"
|
||||
"vec4 iDate; // (year, month, day, time in seconds)\n"
|
||||
"sampler2D iChannel0; // input channel (texture).\n"
|
||||
"sampler2D iChannel1; // input channel (mask).\n"
|
||||
"vec4 iDate; // (year, month, day, time in seconds)\n";
|
||||
|
||||
std::string filterHeader = "uniform vec3 iResolution; // viewport resolution (in pixels)\n"
|
||||
"uniform sampler2D iChannel0; // input channel (texture)\n"
|
||||
"uniform sampler2D iChannel1; // input channel (mask)\n"
|
||||
"uniform float iTime; // shader playback time (in seconds)\n"
|
||||
"uniform float iTimeDelta; // render time (in seconds)\n"
|
||||
"uniform int iFrame; // shader playback frame\n"
|
||||
"uniform vec4 iDate; // (year, month, day, time in seconds)\n" ;
|
||||
std::string fragmentHeader = "#version 330 core\n"
|
||||
"out vec4 FragColor;\n"
|
||||
"in vec4 vertexColor;\n"
|
||||
"in vec2 vertexUV;\n"
|
||||
"vec3 iChannelResolution[2];\n"
|
||||
"uniform mat4 iTransform;\n"
|
||||
"uniform vec4 color;\n"
|
||||
"uniform float stipple;\n"
|
||||
"uniform vec3 iResolution;\n"
|
||||
"uniform sampler2D iChannel0;\n"
|
||||
"uniform sampler2D iChannel1;\n"
|
||||
"uniform float iTime;\n"
|
||||
"uniform float iTimeDelta;\n"
|
||||
"uniform int iFrame;\n"
|
||||
"uniform vec4 iDate;\n";
|
||||
|
||||
std::string filterDefaultCode = "void mainImage( out vec4 fragColor, in vec2 fragCoord )\n"
|
||||
"{\n"
|
||||
" vec2 uv = fragCoord.xy / iResolution.xy;\n"
|
||||
" fragColor = texture(iChannel0, uv);\n"
|
||||
"}\n";
|
||||
// Filter code starts at line 16 :
|
||||
std::string filterDefault = "void mainImage( out vec4 fragColor, in vec2 fragCoord )\n"
|
||||
"{\n"
|
||||
" vec2 uv = fragCoord.xy / iResolution.xy;\n"
|
||||
" fragColor = texture(iChannel0, uv);\n"
|
||||
"}\n";
|
||||
|
||||
std::string fragmentMainCode = "\nvoid main() {\n"
|
||||
" iChannelResolution[0] = vec3(textureSize(iChannel0, 0), 0.f);\n"
|
||||
" iChannelResolution[1] = vec3(textureSize(iChannel1, 0), 0.f);\n"
|
||||
" vec4 texcoord = iTransform * vec4(vertexUV.x, vertexUV.y, 0.0, 1.0);\n"
|
||||
" mainImage( FragColor, texcoord.xy * iChannelResolution[0].xy );\n"
|
||||
"}\n";
|
||||
std::string fragmentFooter = "\nvoid main() {\n"
|
||||
" iChannelResolution[0] = vec3(textureSize(iChannel0, 0), 0.f);\n"
|
||||
" iChannelResolution[1] = vec3(textureSize(iChannel1, 0), 0.f);\n"
|
||||
" vec4 texcoord = iTransform * vec4(vertexUV.x, vertexUV.y, 0.0, 1.0);\n"
|
||||
" mainImage( FragColor, texcoord.xy * iChannelResolution[0].xy );\n"
|
||||
"}\n";
|
||||
|
||||
|
||||
std::string filterBloomCode = "float Threshold = 0.1;"
|
||||
"float Intensity = 1.0;"
|
||||
"float BlurSize = 6.0;"
|
||||
"vec4 BlurColor (in vec2 Coord, in sampler2D Tex, in float MipBias)"
|
||||
"{"
|
||||
" vec2 TexelSize = MipBias/iChannelResolution[0].xy;"
|
||||
" vec4 Color = texture(Tex, Coord, MipBias);"
|
||||
" Color += texture(Tex, Coord + vec2(TexelSize.x,0.0), MipBias);"
|
||||
" Color += texture(Tex, Coord + vec2(-TexelSize.x,0.0), MipBias);"
|
||||
" Color += texture(Tex, Coord + vec2(0.0,TexelSize.y), MipBias);"
|
||||
" Color += texture(Tex, Coord + vec2(0.0,-TexelSize.y), MipBias);"
|
||||
" Color += texture(Tex, Coord + vec2(TexelSize.x,TexelSize.y), MipBias);"
|
||||
" Color += texture(Tex, Coord + vec2(-TexelSize.x,TexelSize.y), MipBias);"
|
||||
" Color += texture(Tex, Coord + vec2(TexelSize.x,-TexelSize.y), MipBias);"
|
||||
" Color += texture(Tex, Coord + vec2(-TexelSize.x,-TexelSize.y), MipBias);"
|
||||
" return Color/9.0;"
|
||||
"}"
|
||||
"void mainImage( out vec4 fragColor, in vec2 fragCoord )"
|
||||
"{"
|
||||
" vec2 uv = (fragCoord.xy/iResolution.xy);"
|
||||
" vec4 Color = texture(iChannel0, uv);"
|
||||
" vec4 Highlight = clamp(BlurColor(uv, iChannel0, BlurSize)-Threshold,0.0,1.0)*1.0/(1.0-Threshold);"
|
||||
" fragColor = 1.0-(1.0-Color)*(1.0-Highlight*Intensity);"
|
||||
"}";
|
||||
class ImageFilteringShader : public Shader
|
||||
{
|
||||
ShadingProgram custom_shading_;
|
||||
|
||||
// fragment shader GLSL code
|
||||
std::string code_;
|
||||
|
||||
std::string montecarloCode = "#version 330 core\n"
|
||||
"#define ITER 32\n"
|
||||
"#define SIZE 100.0\n"
|
||||
"out vec4 FragColor;\n"
|
||||
"in vec4 vertexColor;\n"
|
||||
"in vec2 vertexUV;\n"
|
||||
"uniform mat4 iTransform;\n"
|
||||
"uniform vec4 color;\n"
|
||||
"uniform sampler2D iChannel0;\n"
|
||||
"uniform sampler2D iChannel1;\n"
|
||||
"uniform float size;\n"
|
||||
"uniform float factor;\n"
|
||||
"void srand(vec2 a, out float r) {r=sin(dot(a,vec2(1233.224,1743.335)));}\n"
|
||||
"float rand(inout float r) { r=fract(3712.65*r+0.61432); return (r-0.5)*2.0;}\n"
|
||||
"void main() {"
|
||||
"vec4 texcoord = iTransform * vec4(vertexUV.x, vertexUV.y, 0.0, 1.0);\n"
|
||||
"float p = (SIZE * size + 1.0)/textureSize(iChannel0, 0).y * factor;"
|
||||
"vec4 c=vec4(0.0);"
|
||||
"float r;"
|
||||
"srand(vec2(texcoord), r);"
|
||||
"vec2 rv;"
|
||||
"for(int i=0;i<ITER;i++) {"
|
||||
"rv.x=rand(r);"
|
||||
"rv.y=rand(r);"
|
||||
"c+=texture(iChannel0,vec2(texcoord)+rv*p)/float(ITER);"
|
||||
"}"
|
||||
"FragColor = c;\n"
|
||||
"}";
|
||||
// list of uniform vars in GLSL
|
||||
// with associated pair (current_value, default_values) in range [0.f 1.f]
|
||||
std::map<std::string, glm::vec2> uniforms_;
|
||||
|
||||
std::map< std::string, float > montecarloParam = { { "factor", 0.9 }, {"size", 0.1} };
|
||||
// for iTime & iFrame
|
||||
GTimer *timer_;
|
||||
double iTime_;
|
||||
int iFrame_;
|
||||
|
||||
public:
|
||||
|
||||
ImageFilteringShader();
|
||||
~ImageFilteringShader();
|
||||
|
||||
void use() override;
|
||||
void reset() override;
|
||||
void accept(Visitor& v) override;
|
||||
void copy(ImageFilteringShader const& S);
|
||||
|
||||
// set fragment shader code and uniforms (with default values)
|
||||
void setFilterCode(const std::string &code, std::map<std::string, float> parameters);
|
||||
};
|
||||
|
||||
|
||||
ImageFilteringShader::ImageFilteringShader(): Shader()
|
||||
@@ -123,6 +108,8 @@ ImageFilteringShader::ImageFilteringShader(): Shader()
|
||||
program_ = &custom_shading_;
|
||||
custom_shading_.setShaders("shaders/image.vs", "shaders/image.fs");
|
||||
|
||||
timer_ = g_timer_new ();
|
||||
|
||||
Shader::reset();
|
||||
}
|
||||
|
||||
@@ -135,15 +122,31 @@ void ImageFilteringShader::use()
|
||||
{
|
||||
Shader::use();
|
||||
|
||||
//
|
||||
// Shadertoy uniforms
|
||||
//
|
||||
// Calculate iTime and iTimeDelta
|
||||
double elapsed = g_timer_elapsed (timer_, NULL);
|
||||
program_->setUniform("iTimeDelta", float(elapsed - iTime_) );
|
||||
iTime_ = elapsed;
|
||||
if (iTime_ > FLT_MAX) {
|
||||
g_timer_start(timer_);
|
||||
iTime_ = 0.f;
|
||||
}
|
||||
program_->setUniform("iTime", float(iTime_) );
|
||||
// calculate iFrame
|
||||
program_->setUniform("iFrame", ++iFrame_);
|
||||
if (iFrame_ > INT_MAX -2)
|
||||
iFrame_ = 0;
|
||||
// calculate iDate
|
||||
std::time_t now = std::time(0);
|
||||
std::tm *local = std::localtime(&now);
|
||||
glm::vec4 iDate(local->tm_year, local->tm_mon, local->tm_mday, local->tm_hour*3600.0+local->tm_min*60.0+local->tm_sec);
|
||||
program_->setUniform("iDate", iDate);
|
||||
|
||||
program_->setUniform("iTime", 0.f );
|
||||
program_->setUniform("iTimeDelta", 0.f );
|
||||
|
||||
static int f = 0;
|
||||
program_->setUniform("iFrame", ++f);
|
||||
program_->setUniform("iDate", glm::vec4(0.f, 0.f, 0.f, 0.f) );
|
||||
|
||||
//
|
||||
// loop over all uniforms
|
||||
//
|
||||
for (auto u = uniforms_.begin(); u != uniforms_.end(); ++u)
|
||||
// set uniform to current value
|
||||
program_->setUniform( u->first, u->second.x );
|
||||
@@ -153,17 +156,22 @@ void ImageFilteringShader::reset()
|
||||
{
|
||||
Shader::reset();
|
||||
|
||||
// reset times
|
||||
g_timer_start(timer_);
|
||||
iFrame_ = 0;
|
||||
|
||||
// loop over all uniforms
|
||||
for (auto u = uniforms_.begin(); u != uniforms_.end(); ++u)
|
||||
// reset current value to detault value
|
||||
u->second.x = u->second.y;
|
||||
}
|
||||
|
||||
void ImageFilteringShader::setFragmentCode(const std::string &code, std::map<std::string, float> parameters)
|
||||
void ImageFilteringShader::setFilterCode(const std::string &code, std::map<std::string, float> parameters)
|
||||
{
|
||||
// change the shading code for fragment
|
||||
code_ = code;
|
||||
code_ = fragmentHeader + code + fragmentFooter;
|
||||
custom_shading_.setShaders("shaders/image.vs", code_);
|
||||
// g_print("Filter code:\n%s", code_.c_str());
|
||||
|
||||
// set the list of uniforms
|
||||
uniforms_.clear();
|
||||
@@ -192,23 +200,102 @@ void ImageFilteringShader::accept(Visitor& v)
|
||||
}
|
||||
|
||||
|
||||
ImageFilter::ImageFilter(glm::vec3 resolution, bool useAlpha)
|
||||
|
||||
|
||||
|
||||
|
||||
std::string filterBloomCode = "float Threshold = 0.2;"
|
||||
"float Intensity = 1.0;"
|
||||
"float BlurSize = 4.0;"
|
||||
"vec4 BlurColor (in vec2 Coord, in sampler2D Tex, in float MipBias)"
|
||||
"{"
|
||||
" vec2 TexelSize = MipBias/iChannelResolution[0].xy;"
|
||||
" vec4 Color = texture(Tex, Coord, MipBias);"
|
||||
" Color += texture(Tex, Coord + vec2(TexelSize.x,0.0), MipBias);"
|
||||
" Color += texture(Tex, Coord + vec2(-TexelSize.x,0.0), MipBias);"
|
||||
" Color += texture(Tex, Coord + vec2(0.0,TexelSize.y), MipBias);"
|
||||
" Color += texture(Tex, Coord + vec2(0.0,-TexelSize.y), MipBias);"
|
||||
" Color += texture(Tex, Coord + vec2(TexelSize.x,TexelSize.y), MipBias);"
|
||||
" Color += texture(Tex, Coord + vec2(-TexelSize.x,TexelSize.y), MipBias);"
|
||||
" Color += texture(Tex, Coord + vec2(TexelSize.x,-TexelSize.y), MipBias);"
|
||||
" Color += texture(Tex, Coord + vec2(-TexelSize.x,-TexelSize.y), MipBias);"
|
||||
" return Color/9.0;"
|
||||
"}"
|
||||
"void mainImage( out vec4 fragColor, in vec2 fragCoord )"
|
||||
"{"
|
||||
" vec2 uv = (fragCoord.xy/iResolution.xy);"
|
||||
" vec4 Color = texture(iChannel0, uv);"
|
||||
" vec4 Highlight = clamp(BlurColor(uv, iChannel0, BlurSize)-Threshold,0.0,1.0)*1.0/(1.0-Threshold);"
|
||||
" fragColor = 1.0-(1.0-Color)*(1.0-Highlight*Intensity);"
|
||||
"}";
|
||||
|
||||
|
||||
std::string montecarloCode = "#define ITER 32\n"
|
||||
"#define SIZE 100\n"
|
||||
"void srand(vec2 a, out float r) {r=sin(dot(a,vec2(1233.224,1743.335)));}\n"
|
||||
"float rand(inout float r) { r=fract(3712.65*r+0.61432); return (r-0.5)*2.0;}\n"
|
||||
"void mainImage( out vec4 fragColor, in vec2 fragCoord ) {\n"
|
||||
"vec2 texcoord = fragCoord.xy / iResolution.xy;\n"
|
||||
"float p = (SIZE * 0.6 + 1.0)/textureSize(iChannel0, 0).y * 0.98;"
|
||||
"vec4 c=vec4(0.0);"
|
||||
"float r;"
|
||||
"srand(vec2(texcoord), r);"
|
||||
"vec2 rv;"
|
||||
"for(int i=0;i<ITER;i++) {"
|
||||
"rv.x=rand(r);"
|
||||
"rv.y=rand(r);"
|
||||
"c+=texture(iChannel0,texcoord+rv*p)/float(ITER);"
|
||||
"}"
|
||||
"fragColor = c;\n"
|
||||
"}";
|
||||
|
||||
std::map< std::string, float > montecarloParam = { { "factor", 0.9 }, {"size", 0.1} };
|
||||
|
||||
|
||||
ImageFilter::ImageFilter() : code_(filterDefault)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ImageFilter::ImageFilter(const ImageFilter &other) : code_(other.code_)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ImageFilter& ImageFilter::operator = (const ImageFilter& other)
|
||||
{
|
||||
if (this != &other) {
|
||||
this->code_ = other.code_;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool ImageFilter::operator != (const ImageFilter& other) const
|
||||
{
|
||||
if (this != &other) {
|
||||
|
||||
if (this->code_ != other.code_)
|
||||
return true;
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// set the code of the filter
|
||||
void setCode(const std::string &code);
|
||||
|
||||
ImageFilterRenderer::ImageFilterRenderer(glm::vec3 resolution): enabled_(true)
|
||||
{
|
||||
shader_ = new ImageFilteringShader;
|
||||
surface_ = new Surface(shader_);
|
||||
buffer_ = new FrameBuffer( resolution, useAlpha );
|
||||
|
||||
std::string codefilter = fragmentDeclaration + filterHeader + filterDefaultCode + fragmentMainCode;
|
||||
// std::string codefilter = fragmentDeclaration + filterHeader + filterBloomCode + fragmentMainCode;
|
||||
|
||||
g_print("Filter code:\n%s", codefilter.c_str());
|
||||
buffer_ = new FrameBuffer( resolution, true );
|
||||
|
||||
std::map< std::string, float > paramfilter;
|
||||
|
||||
shader_->setFragmentCode(codefilter, paramfilter);
|
||||
shader_->setFilterCode( filter_.code(), paramfilter);
|
||||
}
|
||||
|
||||
ImageFilter::~ImageFilter()
|
||||
ImageFilterRenderer::~ImageFilterRenderer()
|
||||
{
|
||||
if (buffer_)
|
||||
delete buffer_;
|
||||
@@ -217,22 +304,40 @@ ImageFilter::~ImageFilter()
|
||||
delete surface_;
|
||||
}
|
||||
|
||||
void ImageFilter::setInputTexture(uint t)
|
||||
void ImageFilterRenderer::setInputTexture(uint t)
|
||||
{
|
||||
surface_->setTextureIndex( t );
|
||||
}
|
||||
|
||||
uint ImageFilter::getOutputTexture() const
|
||||
uint ImageFilterRenderer::getOutputTexture() const
|
||||
{
|
||||
return buffer_->texture();
|
||||
if (enabled_)
|
||||
return buffer_->texture();
|
||||
else
|
||||
return surface_->textureIndex();
|
||||
}
|
||||
|
||||
void ImageFilter::draw()
|
||||
void ImageFilterRenderer::draw()
|
||||
{
|
||||
// render filtered surface into frame buffer
|
||||
buffer_->begin();
|
||||
surface_->draw(glm::identity<glm::mat4>(), buffer_->projection());
|
||||
buffer_->end();
|
||||
if (enabled_)
|
||||
{
|
||||
// render filtered surface into frame buffer
|
||||
buffer_->begin();
|
||||
surface_->draw(glm::identity<glm::mat4>(), buffer_->projection());
|
||||
buffer_->end();
|
||||
}
|
||||
}
|
||||
|
||||
void ImageFilterRenderer::setFilter(const ImageFilter &f)
|
||||
{
|
||||
if (f != filter_) {
|
||||
// set to a different filter : apply change to shader
|
||||
std::map< std::string, float > paramfilter;
|
||||
shader_->setFilterCode( f.code(), paramfilter );
|
||||
}
|
||||
|
||||
// keep new filter
|
||||
filter_ = f;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2,67 +2,68 @@
|
||||
#define IMAGEFILTER_H
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Surface;
|
||||
class FrameBuffer;
|
||||
|
||||
#include "Shader.h"
|
||||
|
||||
class ImageFilteringShader : public Shader
|
||||
{
|
||||
ShadingProgram custom_shading_;
|
||||
|
||||
// fragment shader GLSL code
|
||||
std::string code_;
|
||||
|
||||
// list of uniform vars in GLSL
|
||||
// with associated pair (current_value, default_values) in range [0.f 1.f]
|
||||
std::map<std::string, glm::vec2> uniforms_;
|
||||
|
||||
public:
|
||||
|
||||
ImageFilteringShader();
|
||||
~ImageFilteringShader();
|
||||
|
||||
void use() override;
|
||||
void reset() override;
|
||||
void accept(Visitor& v) override;
|
||||
void copy(ImageFilteringShader const& S);
|
||||
|
||||
// set fragment shader code and uniforms (with default values)
|
||||
void setFragmentCode(const std::string &code, std::map<std::string, float> parameters);
|
||||
};
|
||||
class ImageFilteringShader;
|
||||
|
||||
|
||||
class ImageFilter
|
||||
{
|
||||
std::string code_;
|
||||
|
||||
public:
|
||||
|
||||
ImageFilter();
|
||||
ImageFilter(const ImageFilter &other);
|
||||
|
||||
ImageFilter& operator = (const ImageFilter& other);
|
||||
bool operator !=(const ImageFilter& other) const;
|
||||
|
||||
// set the code of the filter
|
||||
inline void setCode(const std::string &code) { code_ = code; }
|
||||
|
||||
// get the code of the filter
|
||||
inline std::string code() const { return code_; }
|
||||
};
|
||||
|
||||
|
||||
class ImageFilterRenderer
|
||||
{
|
||||
Surface *surface_;
|
||||
FrameBuffer *buffer_;
|
||||
ImageFilteringShader *shader_;
|
||||
uint type_;
|
||||
bool enabled_;
|
||||
|
||||
ImageFilter filter_;
|
||||
|
||||
public:
|
||||
|
||||
// instanciate an image filter at given resolution, with alpha channel
|
||||
ImageFilter(glm::vec3 resolution, bool useAlpha = false);
|
||||
~ImageFilter();
|
||||
ImageFilterRenderer(glm::vec3 resolution);
|
||||
~ImageFilterRenderer();
|
||||
|
||||
inline void setEnabled (bool on) { enabled_ = on; }
|
||||
inline bool enabled () const { return enabled_; }
|
||||
|
||||
// set the texture to draw into the framebuffer
|
||||
void setInputTexture(uint t);
|
||||
|
||||
// typedef enum {
|
||||
|
||||
// } ;
|
||||
|
||||
|
||||
void setFilter(uint filter);
|
||||
|
||||
// draw the input texture with filter on the framebuffer
|
||||
void draw();
|
||||
|
||||
// get the texture id of the rendered framebuffer
|
||||
uint getOutputTexture() const;
|
||||
|
||||
// set the code of the filter
|
||||
void setFilter(const ImageFilter &f);
|
||||
|
||||
// get the code of the filter
|
||||
inline ImageFilter filter() const { return filter_; }
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -77,7 +77,7 @@ bool MultiFileSequence::valid() const
|
||||
return !( location.empty() || codec.empty() || width < 1 || height < 1 || max == min);
|
||||
}
|
||||
|
||||
inline MultiFileSequence& MultiFileSequence::operator = (const MultiFileSequence& b)
|
||||
MultiFileSequence& MultiFileSequence::operator = (const MultiFileSequence& b)
|
||||
{
|
||||
if (this != &b) {
|
||||
this->width = b.width;
|
||||
|
||||
17
Shader.cpp
17
Shader.cpp
@@ -39,6 +39,10 @@
|
||||
|
||||
#include "Shader.h"
|
||||
|
||||
#ifndef NDEBUG
|
||||
#define SHADER_DEBUG
|
||||
#endif
|
||||
|
||||
// Globals
|
||||
ShadingProgram *ShadingProgram::currentProgram_ = nullptr;
|
||||
ShadingProgram simpleShadingProgram("shaders/simple.vs", "shaders/simple.fs");
|
||||
@@ -123,9 +127,10 @@ bool ShadingProgram::compile()
|
||||
else {
|
||||
// LINK PROGRAM
|
||||
|
||||
// create new GL Program only if not already done
|
||||
if (id_ == 0)
|
||||
id_ = glCreateProgram();
|
||||
// create new GL Program
|
||||
if (id_ != 0)
|
||||
glDeleteProgram(id_);
|
||||
id_ = glCreateProgram();
|
||||
|
||||
// attach shaders and link
|
||||
glAttachShader(id_, vertex_id_);
|
||||
@@ -144,6 +149,9 @@ bool ShadingProgram::compile()
|
||||
glUseProgram(id_);
|
||||
glUniform1i(glGetUniformLocation(id_, "iChannel0"), 0);
|
||||
glUniform1i(glGetUniformLocation(id_, "iChannel1"), 1);
|
||||
#ifdef SHADER_DEBUG
|
||||
g_printerr("New GLSL Program %d \n", id_);
|
||||
#endif
|
||||
}
|
||||
|
||||
// done (no more need for shaders)
|
||||
@@ -183,6 +191,9 @@ void ShadingProgram::enduse()
|
||||
void ShadingProgram::reset()
|
||||
{
|
||||
if (id_ != 0) {
|
||||
#ifdef SHADER_DEBUG
|
||||
g_printerr("Delete GLSL Program %d \n", id_);
|
||||
#endif
|
||||
glDeleteProgram(id_);
|
||||
id_ = 0;
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <array>
|
||||
#include <map>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
@@ -88,6 +89,7 @@ using namespace std;
|
||||
#include "SrtReceiverSource.h"
|
||||
#include "StreamSource.h"
|
||||
#include "PickingVisitor.h"
|
||||
#include "ImageFilter.h"
|
||||
#include "ImageShader.h"
|
||||
#include "ImageProcessingShader.h"
|
||||
#include "Metronome.h"
|
||||
@@ -5189,7 +5191,7 @@ void InputMappingInterface::Render()
|
||||
/// SHADER EDITOR
|
||||
///
|
||||
///
|
||||
ShaderEditor::ShaderEditor() : WorkspaceWindow("Shader")
|
||||
ShaderEditor::ShaderEditor() : WorkspaceWindow("Shader"), current_(nullptr), current_changed_(true)
|
||||
{
|
||||
auto lang = TextEditor::LanguageDefinition::GLSL();
|
||||
|
||||
@@ -5223,6 +5225,7 @@ ShaderEditor::ShaderEditor() : WorkspaceWindow("Shader")
|
||||
// init editor
|
||||
_editor.SetLanguageDefinition(lang);
|
||||
_editor.SetHandleKeyboardInputs(true);
|
||||
_editor.SetText("");
|
||||
}
|
||||
|
||||
void ShaderEditor::setVisible(bool on)
|
||||
@@ -5305,10 +5308,60 @@ void ShaderEditor::Render()
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
if (ImGui::MenuItem( " Compile ")) {
|
||||
if (current_ != nullptr)
|
||||
filters_[current_].setCode(_editor.GetText());
|
||||
current_->setFilter( filters_[current_] );
|
||||
}
|
||||
|
||||
ImGui::EndMenuBar();
|
||||
}
|
||||
|
||||
// garbage collection of code_
|
||||
for (auto it = filters_.begin(); it != filters_.end(); ) {
|
||||
// keep only if the source exists in the session
|
||||
if ( Mixer::manager().session()->find( it->first ) != Mixer::manager().session()->end() )
|
||||
++it;
|
||||
else
|
||||
it = filters_.erase(it);
|
||||
}
|
||||
|
||||
// get current clone source
|
||||
CloneSource *c = nullptr;
|
||||
Source *s = Mixer::manager().currentSource();
|
||||
if (s != nullptr) {
|
||||
// there is a current source
|
||||
c = dynamic_cast<CloneSource *>(s);
|
||||
if ( c != nullptr ) {
|
||||
// the current source is a clone
|
||||
if ( filters_.find(c) == filters_.end() )
|
||||
// the current clone was not registered
|
||||
filters_[c] = c->filter();
|
||||
}
|
||||
}
|
||||
|
||||
// change editor text only if current changed
|
||||
if ( current_ != c) {
|
||||
// switch to another clone
|
||||
if ( c != nullptr ) {
|
||||
_editor.SetText(filters_[c].code());
|
||||
_editor.SetReadOnly(false);
|
||||
}
|
||||
// cancel edit clone
|
||||
else {
|
||||
// get the editor text and remove trailing '\n'
|
||||
std::string code = _editor.GetText();
|
||||
code.back() = '\0';
|
||||
// remember this code as buffered for the filter of this source
|
||||
filters_[current_].setCode( code );
|
||||
// cancel editor
|
||||
_editor.SetText("");
|
||||
_editor.SetReadOnly(true);
|
||||
}
|
||||
// current changed
|
||||
current_ = c;
|
||||
}
|
||||
|
||||
// render the editor
|
||||
ImGuiToolkit::PushFont(ImGuiToolkit::FONT_MONO);
|
||||
_editor.Render("Shader Editor");
|
||||
|
||||
@@ -99,6 +99,7 @@
|
||||
#include "InfoVisitor.h"
|
||||
#include "DialogToolkit.h"
|
||||
#include "SessionParser.h"
|
||||
#include "ImageFilter.h"
|
||||
|
||||
|
||||
struct ImVec2;
|
||||
@@ -389,7 +390,9 @@ public:
|
||||
|
||||
class ShaderEditor : public WorkspaceWindow
|
||||
{
|
||||
std::string current_text_edited_;
|
||||
CloneSource *current_;
|
||||
bool current_changed_;
|
||||
std::map<CloneSource *, ImageFilter> filters_;
|
||||
|
||||
public:
|
||||
ShaderEditor();
|
||||
|
||||
Reference in New Issue
Block a user