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"
|
2019-09-04 00:59:04 +00:00
|
|
|
#include <stdexcept>
|
2019-01-14 10:23:21 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include "strings.hpp"
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
#define ST "Filter.Displacement"
|
|
|
|
#define ST_FILE "Filter.Displacement.File"
|
|
|
|
#define ST_SCALE "Filter.Displacement.Scale"
|
2019-11-17 13:09:59 +00:00
|
|
|
#define ST_SCALE_TYPE "Filter.Displacment.Scale.Type"
|
2019-08-04 14:20:26 +00:00
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
filter::displacement::displacement_instance::displacement_instance(obs_data_t* data, obs_source_t* context)
|
|
|
|
: obs::source_instance(data, context)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-11-17 13:09:59 +00:00
|
|
|
std::string effect = "";
|
|
|
|
{
|
|
|
|
char* buf = obs_module_file("effects/displace.effect");
|
|
|
|
effect = buf;
|
|
|
|
bfree(buf);
|
|
|
|
}
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
_effect = gs::effect::create(effect);
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
filter::displacement::displacement_instance::~displacement_instance()
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-11-17 13:09:59 +00:00
|
|
|
_effect.reset();
|
|
|
|
_texture.reset();
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
void filter::displacement::displacement_instance::load(obs_data_t* settings)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-11-17 13:09:59 +00:00
|
|
|
update(settings);
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
inline void migrate_settings(obs_data_t* settings)
|
2019-01-24 22:34:43 +00:00
|
|
|
{
|
2019-11-17 13:09:59 +00:00
|
|
|
uint64_t version = static_cast<uint64_t>(obs_data_get_int(settings, S_VERSION));
|
|
|
|
|
|
|
|
switch (version & STREAMEFFECTS_MASK_COMPAT) {
|
|
|
|
case 0:
|
|
|
|
obs_data_set_double(settings, ST_SCALE, obs_data_get_double(settings, "Filter.Displacement.Scale") * 0.5);
|
|
|
|
obs_data_set_double(settings, ST_SCALE_TYPE,
|
|
|
|
obs_data_get_double(settings, "Filter.Displacement.Ratio") * 100.0);
|
|
|
|
obs_data_unset_user_value(settings, "Filter.Displacement.Ratio");
|
|
|
|
case STREAMEFFECTS_MAKE_VERSION(0, 8, 0, 0):
|
|
|
|
break;
|
2019-04-23 15:00:18 +00:00
|
|
|
}
|
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
obs_data_set_int(settings, S_VERSION, STREAMEFFECTS_VERSION);
|
|
|
|
}
|
2019-01-24 23:27:46 +00:00
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
void filter::displacement::displacement_instance::update(obs_data_t* settings)
|
|
|
|
{
|
|
|
|
migrate_settings(settings);
|
2019-01-24 23:27:46 +00:00
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
_scale[0] = _scale[1] = static_cast<float_t>(obs_data_get_double(settings, ST_SCALE));
|
|
|
|
_scale_type = static_cast<float_t>(obs_data_get_double(settings, ST_SCALE_TYPE) / 100.0);
|
2019-01-24 20:42:26 +00:00
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
std::string new_file = obs_data_get_string(settings, ST_FILE);
|
|
|
|
if (new_file != _texture_file) {
|
2019-08-04 21:14:43 +00:00
|
|
|
try {
|
2019-11-17 13:09:59 +00:00
|
|
|
_texture = std::make_shared<gs::texture>(new_file);
|
|
|
|
_texture_file = new_file;
|
2019-04-23 15:20:07 +00:00
|
|
|
} catch (...) {
|
2019-11-17 13:09:59 +00:00
|
|
|
_texture.reset();
|
2019-04-23 15:20:07 +00:00
|
|
|
}
|
2019-01-24 23:27:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
void filter::displacement::displacement_instance::video_tick(float_t)
|
2019-01-24 23:27:46 +00:00
|
|
|
{
|
2019-11-17 13:09:59 +00:00
|
|
|
_width = obs_source_get_base_width(_self);
|
|
|
|
_height = obs_source_get_base_height(_self);
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
void filter::displacement::displacement_instance::video_render(gs_effect_t*)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-11-17 13:09:59 +00:00
|
|
|
if (!_texture) { // No displacement map, so just skip us for now.
|
|
|
|
obs_source_skip_video_filter(_self);
|
|
|
|
return;
|
|
|
|
}
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
if (!obs_source_process_filter_begin(_self, GS_RGBA, OBS_ALLOW_DIRECT_RENDERING)) {
|
|
|
|
obs_source_skip_video_filter(_self);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_effect->get_parameter("image_size")->set_float2(static_cast<float_t>(_width), static_cast<float_t>(_height));
|
|
|
|
_effect->get_parameter("image_inverse_size")
|
|
|
|
->set_float2(static_cast<float_t>(1.0 / _width), static_cast<float_t>(1.0 / _height));
|
|
|
|
_effect->get_parameter("normal")->set_texture(_texture->get_object());
|
|
|
|
_effect->get_parameter("scale")->set_float2(_scale[0], _scale[1]);
|
|
|
|
_effect->get_parameter("scale_type")->set_float(_scale_type);
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
obs_source_process_filter_end(_self, _effect->get_object(), _width, _height);
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
std::string filter::displacement::displacement_instance::get_file()
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-11-17 13:09:59 +00:00
|
|
|
return _texture_file;
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
std::shared_ptr<filter::displacement::displacement_factory>
|
|
|
|
filter::displacement::displacement_factory::factory_instance = nullptr;
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
filter::displacement::displacement_factory::displacement_factory()
|
|
|
|
{
|
|
|
|
_info.id = "obs-stream-effects-filter-displacement";
|
|
|
|
_info.type = OBS_SOURCE_TYPE_FILTER;
|
|
|
|
_info.output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW;
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
_info.get_width = _info.get_height = nullptr;
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
obs_register_source(&_info);
|
|
|
|
}
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
filter::displacement::displacement_factory::~displacement_factory() {}
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
const char* filter::displacement::displacement_factory::get_name()
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-11-17 13:09:59 +00:00
|
|
|
return D_TRANSLATE(ST);
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
void filter::displacement::displacement_factory::get_defaults2(obs_data_t* data)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-11-17 13:09:59 +00:00
|
|
|
{
|
|
|
|
char* disp = obs_module_file("examples/normal-maps/neutral.png");
|
|
|
|
obs_data_set_default_string(data, ST_FILE, disp);
|
|
|
|
bfree(disp);
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_data_set_default_double(data, ST_SCALE, 0.0);
|
|
|
|
obs_data_set_default_double(data, ST_SCALE_TYPE, 0.0);
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
obs_properties_t*
|
|
|
|
filter::displacement::displacement_factory::get_properties2(filter::displacement::displacement_instance* data)
|
2018-11-07 14:24:25 +00:00
|
|
|
{
|
2019-11-17 13:09:59 +00:00
|
|
|
obs_properties_t* pr = obs_properties_create();
|
2017-06-28 21:21:42 +00:00
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
std::string path = "";
|
|
|
|
if (data) {
|
|
|
|
path = data->get_file();
|
|
|
|
} else {
|
|
|
|
char* buf = obs_module_file("examples/normal-maps/neutral.png");
|
|
|
|
path = buf;
|
|
|
|
bfree(buf);
|
2019-01-24 23:27:46 +00:00
|
|
|
}
|
2019-01-24 22:34:43 +00:00
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
{
|
|
|
|
auto p = obs_properties_add_path(pr, ST_FILE, D_TRANSLATE(ST_FILE), obs_path_type::OBS_PATH_FILE,
|
|
|
|
D_TRANSLATE(S_FILEFILTERS_TEXTURE), path.c_str());
|
|
|
|
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_FILE)));
|
2019-01-24 22:34:43 +00:00
|
|
|
}
|
2019-11-17 13:09:59 +00:00
|
|
|
{
|
|
|
|
auto p = obs_properties_add_float(pr, ST_SCALE, D_TRANSLATE(ST_SCALE), -10000000.0, 10000000.0, 0.01);
|
|
|
|
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_SCALE)));
|
2019-01-24 22:34:43 +00:00
|
|
|
}
|
2019-11-17 13:09:59 +00:00
|
|
|
{
|
|
|
|
auto p = obs_properties_add_float_slider(pr, ST_SCALE_TYPE, D_TRANSLATE(ST_SCALE_TYPE), 0.0, 100.0, 0.01);
|
|
|
|
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_SCALE_TYPE)));
|
2019-01-24 22:34:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-17 13:09:59 +00:00
|
|
|
return pr;
|
2017-06-28 21:21:42 +00:00
|
|
|
}
|