Page 1 of 1

Stereographic Projection(tiny planet) Shader problem

Posted: Mon May 25, 2015 11:50 am
by StevenM
I have no idea how to get rid of this render artifact. The example I seen
used gl_Position = orthoProjectionMatrix*vertex but not knowing how to do that, I just used the canvas. Don't know if that makes a difference.

This project doesn't run outside of FL STudio - but here is the glsl .

If anyone can help, I'd appreciate it.

Vertex Shader:

Code: Select all

void main()
{
  vec4 vertex = gl_Vertex;
  vertex.xy *= 2.0;
  gl_Position = vertex;
  gl_TexCoord[0] = gl_MultiTexCoord0;
}
Frag Shader:

Code: Select all

uniform sampler2D tex1;
uniform float scale,px,py,Alpha;
uniform vec3 rot;
uniform float width,height;

#define PI 3.141592653589793

mat3 getrot(vec3 r)
{
   float cx = cos(radians(r.x));
   float sx = sin(radians(r.x));
   float cy = cos(radians(r.y));
   float sy = sin(radians(r.y));
   float cz = cos(radians(r.z));
   float sz = sin(radians(r.z));

   float m1,m2,m3,m4,m5,m6,m7,m8,m9;

   m1=cy*cz;
   m2=cx*sz+sx*sy*cz;
   m3=sx*sz-cx*sy*cz;
   m4=-cy*sz;
   m5=cx*cz-sx*sy*sz;
   m6=sx*cz+cx*sy*sz;
   m7=sy;
   m8=-sx*cy;
   m9=cx*cy;

   return mat3(m1,m2,m3,m4,m5,m6,m7,m8,m9);
}


void main(){
  mat3 t=getrot(rot);
  vec2 rads = vec2(PI*2.0 , PI);
  vec2 offset=vec2(px,py);
  vec2 pnt = (gl_TexCoord[0] - .5-offset).xy*vec2(scale, scale*height/width);
  // Project to Sphere
  float x2y2 = pnt.x * pnt.x + pnt.y * pnt.y;
  vec3 sphere_pnt = vec3(2. * pnt, x2y2 - 1.0) / (x2y2 + 1.0);
  sphere_pnt *= t;

  // Convert to Spherical Coordinates
  float r = length(sphere_pnt);
  float lon = atan(sphere_pnt.y, sphere_pnt.x);
  float lat = acos(sphere_pnt.z / r);

  gl_FragColor = texture2D(tex1, vec2(lon, lat) / rads);
  gl_FragColor *=Alpha;
}

Re: Stereographic Projection(tiny planet) Shader problem

Posted: Mon May 25, 2015 12:25 pm
by Kjell
Hi Steven,
StevenM wrote:I have no idea how to get rid of this render artifact.
Before i help you .. do you really need / want to do everything in a fragment shader? For instance, calculating the exact same rotation matrix for each pixel is a terrible waste of cycles.

I suspect you've taken inspiration from a ShaderToy ( or similar ) project? Keep in mind that those "environments" impose arbitrary limitations .. which is part of the fun & challenge, but it often forces users to use crazy / "terrible" workarounds .. while ZGE obviously doesn't impose any of these limitations.

Think of it like "toothpick art". Sure it's mighty impressive that someone manages to create a model boat out of mere toothpicks ( and glue ), but when you can use anything you want ( and you're not going for the particular aesthetic that you get from using just toothpicks ), it's not exactly the most logical choice of material.

Image

K

Posted: Mon May 25, 2015 8:32 pm
by StevenM
Before i help you .. do you really need / want to do everything in a fragment shader?
In this case, it might be necessary(I don't know)- it's a clear effect for the visualizer?

Otherwise, I don't mind a vertex alternative.

The rotation, I'm sure there is a better way, this was the easiest solution for me.

I'm using a sphere with rotation velocity to animate the shader. The vec3 rotation of the sphere is the uniform vec3 in the shader that is converted to mat3, and applied to the projection.

I use this sphere as visual helper object that can be turned off or on.
Sort of like an animated sphere version of this here, but front view:

Image

Posted: Thu May 28, 2015 7:44 pm
by Kjell
Hi StevenM,
StevenM wrote:In this case, it might be necessary?
No, doing a relatively complicated calculation 2 million times ( 1920x1080 = 2073600 ) while the result is the exact same every time is never necessary :wink:
StevenM wrote:Otherwise, I don't mind a vertex alternative.
The spherical coordinate sampling you can keep per fragment ( to prevent texture coordinate warping ). Not exactly sure what type of effect you're after though.

Anyway, i tried to replicate the rendering artifact ( on several systems ) but i can't get it to appear :? I suspect you're on a system with a AMD GPU?

K

I see the problem

Posted: Thu May 28, 2015 11:25 pm
by StevenM
Not exactly sure what type of effect you're after though.


same as this video below, time the effect starts is cued(you might need to refresh the page to set time):
https://www.youtube.com/watch?v=aPx4KPeF7xg#t=597
--------------------------------------------------------------------------
I suspect you're on a system with a AMD GPU
Yes, and discovered that it might be caused by the mipmap filter. Thanks for your help.

Is this an amd issue?

Embedded bitmaps with linear filter work fine, but if I use the flstudio visualizer canvas feedback or an image import,I get the artifact.

Does the visualizer default to mipmap?

Is there a way I can fix this?

Re: I see the problem

Posted: Fri May 29, 2015 11:09 am
by Kjell
Hi Steven,
StevenM wrote:Yes, and discovered that it might be caused by the mipmap filter.
Ah, i didn't try using a mip-mapped texture .. that explains it. What happens is that you "smear" out the texture over a range of 360 degrees ( kind of like a hand fan ). But that also happens at the opposite end, so you end up with pixels that sample from a a super dense texture coordinate space, which makes it looks like a glitched line.

One possible solution would be to split the spherical sampling in two halves so you don't end up with such area.

K

Posted: Fri May 29, 2015 3:13 pm
by StevenM
Ok, this is good for now, thanks again. The speed is fine for me capped at 60 fps, even with the inefficient code.

The shadertoy code is easy to get working in ZGE, but I would like to learn beyond shadertoy examples though. Shadertoy is a great resource for anyone, like myself, that does not program in c++. I can't get far beyond the basics with c++ examples because of my inexperience with the c++ language and compiler. Different versions of opengl complicates things too.