SFML community forums

Help => General => Topic started by: korczurekk on November 24, 2015, 07:53:05 pm

Title: Gravitational lensing shader
Post by: korczurekk on November 24, 2015, 07:53:05 pm
Hi, i've tried to use gravitational lensing shader from shadertoy.com (https://www.shadertoy.com/view/Md2XWc# (https://www.shadertoy.com/view/Md2XWc#)), but i have no idea, how to change shader on website so it can be used in my app. :/ It's that code:
// reference: http://www.photon.at/~werner/bh/gvsim.html

vec3 bh=vec3(0.0);
float bh_M=1.;

vec3 eye=vec3(0.,0.,10.);
vec3 up=vec3(0.,1.,0.);
mat3 cam_mat;// camera -> world
float tan_half_vfov=1.0;
vec2 iplane_size=2.*tan_half_vfov*vec2(iResolution.x/iResolution.y,1.);

void setupCamera()
{
        vec3 n=normalize(eye-bh);
        vec3 s=normalize(cross(up,n));
        vec3 t=cross(n,s);
        cam_mat[0]=s;
    cam_mat[1]=t;
    cam_mat[2]=n;
}

vec3 rot_y(vec3 v,float theta)
{
    float s=sin(theta),c=cos(theta);
    return vec3(c*v.x+s*v.z,v.y,c*v.z-s*v.x);    
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    setupCamera();
   
    vec2 ixy=(fragCoord.xy/iResolution.xy - 0.5)*iplane_size;
    vec3 ray_dir=cam_mat*normalize(vec3(ixy.x,ixy.y,-1.));  
   
        vec3 h2e=eye-bh; float l2_h2e=dot(h2e,h2e);
    float d=length(cross(ray_dir,h2e));
        if(d>3.*bh_M)
    {
        // closest point
        vec3 cp= eye+ray_dir*sqrt(l2_h2e-d*d);
                // bend
                float alpha=4.*bh_M/d;
        ray_dir=normalize(ray_dir+tan(alpha)*normalize(bh-cp));
       
        fragColor=textureCube(iChannel0,rot_y(ray_dir,-0.2*iGlobalTime));
    }
    else
        fragColor=vec4(0.);
}
 
Thanks!
Title: Re: Gravitational lensing shader
Post by: GraphicsWhale on November 24, 2015, 11:55:26 pm
It does not appear to be the same shading language as what SFML uses (which is whatever versions of GLSL OpenGL 2.0 and 2.1 use). You're going to have to re-write it in GLSL.
Title: Re: Gravitational lensing shader
Post by: Hapax on November 25, 2015, 03:34:05 am
You seem to have ignored the Shader Inputs on that link.
Title: Re: Gravitational lensing shader
Post by: korczurekk on November 25, 2015, 02:59:57 pm
It does not appear to be the same shading language as what SFML uses (which is whatever versions of GLSL OpenGL 2.0 and 2.1 use). You're going to have to re-write it in GLSL.
Thanks, i'll do it later and share results.

You seem to have ignored the Shader Inputs on that link.
I haven't, but i didn't know what "iChanel" etc. are. :/
Title: Re: Gravitational lensing shader
Post by: Hapax on November 27, 2015, 02:55:11 am
You seem to have ignored the Shader Inputs on that link.
I haven't, but i didn't know what "iChanel" etc. are. :/
On that link, click on "Shader Inputs". There is a list of uniforms that I would expect should be added to the shader.