2017-06-28 21:21:42 +00:00
|
|
|
/*
|
|
|
|
* Modern effects for a modern Streamer
|
|
|
|
* Copyright (C) 2017 Michael Fabian Dirks
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2019-01-14 10:23:21 +00:00
|
|
|
#include "filter-displacement.hpp"
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include "strings.hpp"
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2018-01-16 10:47:24 +00:00
|
|
|
// Initializer & Finalizer
|
2019-01-24 20:05:38 +00:00
|
|
|
static filter::DisplacementAddon* filterDisplacementInstance;
|
2018-11-07 14:24:25 +00:00
|
|
|
INITIALIZER(FilterDisplacementInit)
|
|
|
|
{
|
2019-01-24 20:05:38 +00:00
|
|
|
initializerFunctions.push_back([] { filterDisplacementInstance = new filter::DisplacementAddon(); });
|
2018-11-07 14:24:25 +00:00
|
|
|
finalizerFunctions.push_back([] { delete filterDisplacementInstance; });
|
2018-01-16 10:47:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
filter::DisplacementAddon::DisplacementAddon()
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2017-06-28 21:21:42 +00:00
|
|
|
memset(&sourceInfo, 0, sizeof(obs_source_info));
|
2018-11-07 14:24:25 +00:00
|
|
|
sourceInfo.id = "obs-stream-effects-filter-displacement";
|
|
|
|
sourceInfo.type = OBS_SOURCE_TYPE_FILTER;
|
|
|
|
sourceInfo.output_flags = OBS_SOURCE_VIDEO;
|
|
|
|
sourceInfo.get_name = get_name;
|
|
|
|
sourceInfo.get_defaults = get_defaults;
|
2017-06-28 21:21:42 +00:00
|
|
|
sourceInfo.get_properties = get_properties;
|
|
|
|
|
2018-11-07 14:24:25 +00:00
|
|
|
sourceInfo.create = create;
|
|
|
|
sourceInfo.destroy = destroy;
|
|
|
|
sourceInfo.update = update;
|
|
|
|
sourceInfo.activate = activate;
|
|
|
|
sourceInfo.deactivate = deactivate;
|
|
|
|
sourceInfo.show = show;
|
|
|
|
sourceInfo.hide = hide;
|
|
|
|
sourceInfo.video_tick = video_tick;
|
2017-06-28 21:21:42 +00:00
|
|
|
sourceInfo.video_render = video_render;
|
|
|
|
|
|
|
|
obs_register_source(&sourceInfo);
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
filter::DisplacementAddon::~DisplacementAddon() {}
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
const char* filter::DisplacementAddon::get_name(void*)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2017-08-19 21:56:05 +00:00
|
|
|
return P_TRANSLATE(S_FILTER_DISPLACEMENT);
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void* filter::DisplacementAddon::create(obs_data_t* data, obs_source_t* source)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-01-24 20:05:38 +00:00
|
|
|
return new Displacement(data, source);
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void filter::DisplacementAddon::destroy(void* ptr)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-01-24 20:05:38 +00:00
|
|
|
delete reinterpret_cast<Displacement*>(ptr);
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
uint32_t filter::DisplacementAddon::get_width(void* ptr)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-01-24 20:05:38 +00:00
|
|
|
return reinterpret_cast<Displacement*>(ptr)->get_width();
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
uint32_t filter::DisplacementAddon::get_height(void* ptr)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-01-24 20:05:38 +00:00
|
|
|
return reinterpret_cast<Displacement*>(ptr)->get_height();
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void filter::DisplacementAddon::get_defaults(obs_data_t* data)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2017-06-29 05:35:41 +00:00
|
|
|
char* disp = obs_module_file("filter-displacement/neutral.png");
|
2017-08-19 21:56:05 +00:00
|
|
|
obs_data_set_default_string(data, S_FILTER_DISPLACEMENT_FILE, disp);
|
|
|
|
obs_data_set_default_double(data, S_FILTER_DISPLACEMENT_RATIO, 0);
|
|
|
|
obs_data_set_default_double(data, S_FILTER_DISPLACEMENT_SCALE, 0);
|
2017-06-28 21:21:42 +00:00
|
|
|
bfree(disp);
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
obs_properties_t* filter::DisplacementAddon::get_properties(void* ptr)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
|
|
|
obs_properties_t* pr = obs_properties_create();
|
2017-06-28 21:21:42 +00:00
|
|
|
|
|
|
|
std::string path = "";
|
|
|
|
if (ptr)
|
2019-01-24 20:05:38 +00:00
|
|
|
path = reinterpret_cast<Displacement*>(ptr)->get_file();
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2018-11-07 14:24:25 +00:00
|
|
|
obs_properties_add_path(pr, S_FILTER_DISPLACEMENT_FILE, P_TRANSLATE(S_FILTER_DISPLACEMENT_FILE),
|
|
|
|
obs_path_type::OBS_PATH_FILE, P_TRANSLATE(S_FILTER_DISPLACEMENT_FILE_TYPES), path.c_str());
|
|
|
|
obs_properties_add_float_slider(pr, S_FILTER_DISPLACEMENT_RATIO, P_TRANSLATE(S_FILTER_DISPLACEMENT_RATIO), 0, 1,
|
|
|
|
0.01);
|
|
|
|
obs_properties_add_float_slider(pr, S_FILTER_DISPLACEMENT_SCALE, P_TRANSLATE(S_FILTER_DISPLACEMENT_SCALE), -1000,
|
|
|
|
1000, 0.01);
|
2017-06-28 21:21:42 +00:00
|
|
|
return pr;
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void filter::DisplacementAddon::update(void* ptr, obs_data_t* data)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-01-24 20:05:38 +00:00
|
|
|
reinterpret_cast<Displacement*>(ptr)->update(data);
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void filter::DisplacementAddon::activate(void* ptr)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-01-24 20:05:38 +00:00
|
|
|
reinterpret_cast<Displacement*>(ptr)->activate();
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void filter::DisplacementAddon::deactivate(void* ptr)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-01-24 20:05:38 +00:00
|
|
|
reinterpret_cast<Displacement*>(ptr)->deactivate();
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void filter::DisplacementAddon::show(void* ptr)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-01-24 20:05:38 +00:00
|
|
|
reinterpret_cast<Displacement*>(ptr)->show();
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void filter::DisplacementAddon::hide(void* ptr)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-01-24 20:05:38 +00:00
|
|
|
reinterpret_cast<Displacement*>(ptr)->hide();
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void filter::DisplacementAddon::video_tick(void* ptr, float time)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-01-24 20:05:38 +00:00
|
|
|
reinterpret_cast<Displacement*>(ptr)->video_tick(time);
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void filter::DisplacementAddon::video_render(void* ptr, gs_effect_t* effect)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-01-24 20:05:38 +00:00
|
|
|
reinterpret_cast<Displacement*>(ptr)->video_render(effect);
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
filter::Displacement::Displacement(obs_data_t* data, obs_source_t* context)
|
2019-01-24 20:42:26 +00:00
|
|
|
: m_self(context), m_active(true), m_effect(nullptr), m_distance(0), m_timer(0)
|
2019-01-24 22:34:43 +00:00
|
|
|
{
|
2019-01-24 20:42:26 +00:00
|
|
|
this->m_displacement_map.texture = nullptr;
|
|
|
|
this->m_displacement_map.createTime = 0;
|
|
|
|
this->m_displacement_map.modifiedTime = 0;
|
|
|
|
this->m_displacement_map.size = 0;
|
|
|
|
|
2019-01-24 22:34:43 +00:00
|
|
|
char* effectFile = obs_module_file("effects/displace.effect");
|
|
|
|
try {
|
|
|
|
m_effect = std::make_shared<gs::effect>(effectFile);
|
|
|
|
} catch (...) {
|
|
|
|
P_LOG_ERROR("<Displacement Filter:%s> Failed to load displacement effect.", obs_source_get_name(m_self));
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
2019-01-24 22:34:43 +00:00
|
|
|
bfree(effectFile);
|
2017-06-28 21:21:42 +00:00
|
|
|
|
|
|
|
update(data);
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
filter::Displacement::~Displacement()
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-01-24 22:34:43 +00:00
|
|
|
m_effect.reset();
|
|
|
|
|
2017-06-28 21:21:42 +00:00
|
|
|
obs_enter_graphics();
|
2019-01-24 20:42:26 +00:00
|
|
|
gs_texture_destroy(m_displacement_map.texture);
|
2017-06-28 21:21:42 +00:00
|
|
|
obs_leave_graphics();
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void filter::Displacement::update(obs_data_t* data)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
|
|
|
updateDisplacementMap(obs_data_get_string(data, S_FILTER_DISPLACEMENT_FILE));
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2019-01-24 20:42:26 +00:00
|
|
|
m_distance = float_t(obs_data_get_double(data, S_FILTER_DISPLACEMENT_RATIO));
|
|
|
|
vec2_set(&m_displacement_scale, float_t(obs_data_get_double(data, S_FILTER_DISPLACEMENT_SCALE)),
|
2018-11-07 14:24:25 +00:00
|
|
|
float_t(obs_data_get_double(data, S_FILTER_DISPLACEMENT_SCALE)));
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
uint32_t filter::Displacement::get_width()
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2017-06-28 21:21:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
uint32_t filter::Displacement::get_height()
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2017-06-28 21:21:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void filter::Displacement::activate() {}
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void filter::Displacement::deactivate() {}
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void filter::Displacement::show() {}
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void filter::Displacement::hide() {}
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void filter::Displacement::video_tick(float time)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-01-24 20:42:26 +00:00
|
|
|
m_timer += time;
|
|
|
|
if (m_timer >= 1.0) {
|
|
|
|
m_timer -= 1.0;
|
|
|
|
updateDisplacementMap(m_displacement_map.file);
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 14:24:25 +00:00
|
|
|
float interp(float a, float b, float v)
|
|
|
|
{
|
2017-06-28 21:21:42 +00:00
|
|
|
return (a * (1.0f - v)) + (b * v);
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void filter::Displacement::video_render(gs_effect_t*)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-01-24 20:42:26 +00:00
|
|
|
obs_source_t* parent = obs_filter_get_parent(m_self);
|
|
|
|
obs_source_t* target = obs_filter_get_target(m_self);
|
2018-11-07 14:24:25 +00:00
|
|
|
uint32_t baseW = obs_source_get_base_width(target), baseH = obs_source_get_base_height(target);
|
2017-06-28 21:21:42 +00:00
|
|
|
|
|
|
|
// Skip rendering if our target, parent or context is not valid.
|
2019-01-24 22:34:43 +00:00
|
|
|
if (!parent || !target || !baseW || !baseH || !m_displacement_map.texture) {
|
2019-01-24 20:42:26 +00:00
|
|
|
obs_source_skip_video_filter(m_self);
|
2017-06-28 21:21:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:42:26 +00:00
|
|
|
if (!obs_source_process_filter_begin(m_self, GS_RGBA, OBS_ALLOW_DIRECT_RENDERING))
|
2017-06-28 21:21:42 +00:00
|
|
|
return;
|
|
|
|
|
2018-11-07 14:24:25 +00:00
|
|
|
gs_eparam_t* param;
|
2017-06-28 21:21:42 +00:00
|
|
|
|
|
|
|
vec2 texelScale;
|
2019-01-24 20:42:26 +00:00
|
|
|
vec2_set(&texelScale, interp((1.0f / baseW), 1.0f, m_distance), interp((1.0f / baseH), 1.0f, m_distance));
|
2019-01-24 22:34:43 +00:00
|
|
|
|
|
|
|
if (m_effect->has_parameter("texelScale")) {
|
|
|
|
m_effect->get_parameter("texelScale").set_float2(texelScale);
|
|
|
|
}
|
|
|
|
if (m_effect->has_parameter("displacementScale")) {
|
|
|
|
m_effect->get_parameter("displacmenetScale").set_float2(m_displacement_scale);
|
|
|
|
}
|
|
|
|
if (m_effect->has_parameter("displacementMap")) {
|
|
|
|
m_effect->get_parameter("displacementMap").set_texture(m_displacement_map.texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_source_process_filter_end(m_self, m_effect->get_object(), baseW, baseH);
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
std::string filter::Displacement::get_file()
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-01-24 20:42:26 +00:00
|
|
|
return m_displacement_map.file;
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:05:38 +00:00
|
|
|
void filter::Displacement::updateDisplacementMap(std::string file)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2017-06-28 21:21:42 +00:00
|
|
|
bool shouldUpdateTexture = false;
|
|
|
|
|
|
|
|
// Different File
|
2019-01-24 20:42:26 +00:00
|
|
|
if (file != m_displacement_map.file) {
|
|
|
|
m_displacement_map.file = file;
|
|
|
|
shouldUpdateTexture = true;
|
2017-06-28 21:21:42 +00:00
|
|
|
} else { // Different Timestamps
|
|
|
|
struct stat stats;
|
2019-01-24 20:42:26 +00:00
|
|
|
if (os_stat(m_displacement_map.file.c_str(), &stats) != 0) {
|
|
|
|
shouldUpdateTexture = shouldUpdateTexture || (m_displacement_map.createTime != stats.st_ctime)
|
|
|
|
|| (m_displacement_map.modifiedTime != stats.st_mtime);
|
|
|
|
m_displacement_map.createTime = stats.st_ctime;
|
|
|
|
m_displacement_map.modifiedTime = stats.st_mtime;
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldUpdateTexture) {
|
|
|
|
obs_enter_graphics();
|
2019-01-24 20:42:26 +00:00
|
|
|
if (m_displacement_map.texture) {
|
|
|
|
gs_texture_destroy(m_displacement_map.texture);
|
|
|
|
m_displacement_map.texture = nullptr;
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
if (os_file_exists(file.c_str()))
|
2019-01-24 20:42:26 +00:00
|
|
|
m_displacement_map.texture = gs_texture_create_from_file(m_displacement_map.file.c_str());
|
2017-06-28 21:21:42 +00:00
|
|
|
obs_leave_graphics();
|
|
|
|
}
|
|
|
|
}
|