Skip to content

Instantly share code, notes, and snippets.

@headstash
Last active August 17, 2020 16:19
Show Gist options
  • Save headstash/9f8a2fb30fa29b181251dc31acaa70d9 to your computer and use it in GitHub Desktop.
Save headstash/9f8a2fb30fa29b181251dc31acaa70d9 to your computer and use it in GitHub Desktop.
Equirectangular.cpp (FFGL 2.1)
#include "Equirectangular.h"
using namespace ffglex;
using namespace ffglqs;
static CFFGLPluginInfo PluginInfo(
PluginFactory< Equirectangular >,// Create method
"SH06", // Plugin unique ID of maximum length 4.
"Equirectangular", // Plugin name
2, // API major version number
1, // API minor version number
1, // Plugin major version number
0, // Plugin minor version number
FF_EFFECT, // Plugin type
"", // Plugin description
"" // About
);
static const std::string fragmentShaderCode = R"(
#define PI 3.141592653589793
#define TWPI 6.283185307179586
vec2 lookXY = vec2(look_X, look_Y);
vec2 positionXY = vec2(position_X, position_Y);
vec2 _rotate(vec2 v, float angle) {return cos(angle)*v+sin(angle)*vec2(v.y,-v.x);}
void main()
{
vec2 _uv = (gl_FragCoord.xy / resolution.xy);
vec2 _uvc = (_uv.xy / resolution.y - vec2(resolution.x / resolution.y / 2.0, 0.5) * 4.0);
vec2 v = _uv + resolution.y;
v.y -= 0.5;
v.x -= 0.5;
float th = v.y * PI,
ph = v.x * TWPI;
vec3 sp = vec3( sin(ph) * cos(th), sin(th), cos(ph) * cos(th) );
vec3 pos = vec3( PI, perspective * PI, 0);
pos *= scale;
sp = mix(sp, normalize(vec3(_uvc, 1.0)), perspective);
sp.yz = _rotate(sp.yz, lookXY.y*PI);
sp.xy = _rotate(sp.xy, lookXY.x*PI);
fragColor = texture(inputTexture, vec2(dot(pos, sp.zxy), dot(pos.yzx, sp.zxy))+positionXY.xy);
}
)";
Equirectangular::Equirectangular()
{
// Input properties
SetMinInputs( 1 );
SetMaxInputs( 1 );
SetFragmentShader( fragmentShaderCode );
AddParam( ParamRange::Create( "scale", 0.15, { 0.01, 0.5 } ) );
AddParam( ParamRange::Create( "zoom", 1.0, { 0.01, 1.0 } ) );
AddParam( Param::Create( "perspective" ) );
AddParam( Param::Create( "rotate" ) );
AddParam( Param::Create( "look_X" ) );
AddParam( Param::Create( "look_Y" ) );
AddParam( Param::Create( "position_X", 0.5 ) );
AddParam( Param::Create( "position_Y", 0.5 ) );
}
Equirectangular::~Equirectangular()
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment