Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Rendering a sf::Sprite using a shader results in an inverted Y-axis?  (Read 2954 times)

0 Members and 2 Guests are viewing this topic.

SuperNova

  • Newbie
  • *
  • Posts: 3
    • View Profile
I've recently been toying around with Shaders and I found out that, unless I use a sf::RenderTexture, the displayed result is always Y-Inverted.

// General parameters
uniform sampler2D tex0;
uniform sampler2D tex1;
uniform float time;
uniform vec2 resolution;
 
const vec2 center = vec2(0.5, 0.5);

float quadraticInOut(float t) {
  float p = 2.0 * t * t;
  return t < 0.5 ? p : -p + (4.0 * t) - 1.0;
}

float rand(vec2 co) {
  return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

void main() {
  vec2 p = gl_FragCoord.xy / resolution.xy;
 
  if (time == 0.0) {
    gl_FragColor = texture2D(tex0, p);
  } else if (time == 1.0) {
    gl_FragColor = texture2D(tex1, p);
  } else {
    float x = time;
    float dist = distance(center, p);
    float r = x - min(rand(vec2(p.y, 0.0)), rand(vec2(0.0, p.x)));
    float m = dist <= r ? 1.0 : 0.0;
    gl_FragColor = mix(texture2D(tex0, p), texture2D(tex1, p), m);    
  }
 
}

A simple shader like this one, only a fragment shader might I add, is always upside down.

Is there something I'm missing?  ???
I'm quite new to shaders so keep that in mind.

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Rendering a sf::Sprite using a shader results in an inverted Y-axis?
« Reply #1 on: October 06, 2016, 12:00:36 am »
Hope this answer helps you a bit.

But if i remeber well you need to flip the Y axis.
I would like a spanish/latin community...
Problems building for Android? Look here

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Rendering a sf::Sprite using a shader results in an inverted Y-axis?
« Reply #2 on: October 06, 2016, 12:47:41 am »
The reason for this is that SFML uses positive y for down (top is zero) whereas OpenGL uses positive y for up (bottom is zero).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Rendering a sf::Sprite using a shader results in an inverted Y-axis?
« Reply #3 on: October 06, 2016, 03:30:22 am »
OpenGL's "bottom" is -1 actually, center is 0.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Rendering a sf::Sprite using a shader results in an inverted Y-axis?
« Reply #4 on: October 10, 2016, 08:24:50 am »
OpenGL's "bottom" is -1 actually, center is 0.
In the scene/view co-ordinate system, sure, but the fragment co-ordinates (the shader is a fragment shader) start from the bottom-left, not the centre.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Rendering a sf::Sprite using a shader results in an inverted Y-axis?
« Reply #5 on: October 11, 2016, 10:31:43 pm »
Oh that's right, my bad.

I tend to set pixel_center_integer so it's uniform across the API.

 

anything