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

Author Topic: Fragment shader issue [possible bug]  (Read 2867 times)

0 Members and 1 Guest are viewing this topic.

Snail

  • Newbie
  • *
  • Posts: 6
    • View Profile
Fragment shader issue [possible bug]
« on: October 29, 2015, 03:16:00 am »
Hello, so I created a fragment shader which can be viewed here for the desired correct effect: http://glslsandbox.com/e#28532.0

Now in my game, the code is exactly the same except the stars are in wildly different positions. Here's the exact code that is being used in my game:
Code: [Select]
/*
 * Copyright (C) 2013-2015 Snailsoft <https://github.com/snailsoft/starborn/>
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

uniform float fade_time;
uniform float time;

uniform vec2 resolution;

bool outside(vec2 center, float radius)
{
    return (pow((center.x - gl_FragCoord.x), 2.0) + pow((center.y - gl_FragCoord.y), 2.0)) >= pow(radius, 2.0);
}

float random(vec2 value)
{
    return fract(sin(dot(value, vec2(12.9898, 78.233))) * 43758.5453);
}

void main()
{
    float rand = 0.0;

    vec2 position = (gl_FragCoord - (resolution / 2.0)) / resolution.y;
    vec2 position2 = (gl_FragCoord - (0.7 * resolution)) / (resolution.y / 24.0);

    position2.x = (gl_FragCoord.x - (0.47 * resolution.y)) / (resolution.y / 24.0);

    position /= cos(1.5 * length(position));
    position2 /= cos(1.5 * length(position2));

    vec3 star_color;

    if((random(gl_FragCoord / resolution) > 0.995) && outside(resolution / 2.0, resolution.y / 2.7))
    {
        rand = random(gl_FragCoord);
        star_color = vec3((rand < 0.85) ? vec3(1.0) : ((rand < 0.9) ? vec3(0.0, 1.0, 0.5) : vec3(0.0, 1.0, 1.0)));
    }

    float light_color = sqrt(pow(((((1.0 / ((64.0 * mod(-atan(position.y, position.x) + (((time / 3.0) + 43200.0) * (length(position) * length(position))), cos(-1.0) * 64.0)) + 0.2)) * (1.0 - step(0.42, length(position)))) + ((1.0 / ((0.125 * mod(-atan(position2.y, position2.x) + (((time / 0.025) + 43200.0) * (length(position2) * length(position2))), cos(-1.0) * 1.5)) + 0.2)) * (1.0 - step(0.42, length(position2))))) + (0.1 / distance(length(position2), 0.42))) + (1.0 / ((75.0 * distance(length(position), 0.42)) + 0.2)), 1.5));
    gl_FragColor = vec4((rand > 0.0) ? (star_color - light_color) : ((vec3(0.0, 1.0, 1.0) * 0.3) * light_color) * (1.6 - length(((gl_FragCoord / resolution) * 2.0) - 1.0)), (fade_time > 0.0) ? max(0.0, 1.0 - (time - fade_time)) : min(1.0, time));

    if(rand > 0.0)
        gl_FragColor.a *= rand * (0.225 * sin((time * (rand * 5.0)) + (720.0 * rand)) + 0.5);
}

And here is what it looks like in game: (ignore the bad gif quality, you get the idea of what's going on)


As you can see they are all being rendered in the lower left corner even though it is the same code and they both use GLSL so I'm really confused. Can anyone with shader experience have a look and see if anything immediate is going on here?

I'll highlight the actual code that is controlling where the stars are rendered here: if((random(gl_FragCoord / resolution) > 0.995) && outside(resolution / 2.0, resolution.y / 2.7)) // basically only render stars if the coord is outside of the "planet" which is coded as a radius around the center of the screen

Thanks in advance guys.

Jabberwocky

  • Full Member
  • ***
  • Posts: 157
    • View Profile
Re: Fragment shader issue [possible bug]
« Reply #1 on: October 29, 2015, 07:41:20 am »
I just had a very quick look.  But my first guess is that gl_FragCoord is in a different range in your project than it is for the glslsandbox example you linked to.

If you're using a sf::Sprite, gl_FragCoord x and y will be in the range of 0 to 1.

Your shader is comparing this to the "resolution" value.  I don't know what you've got that set to, but I'm guessing screen resolution, or the size of the sprite (or both).

If this is the case, you're comparing stuff in screen space (resolution) to stuff in texture space (gl_FragCoord), and that's kinda apples and oranges.  You'll need to convert both values to use the same coordinate space to have any meaningful comparison between them.

Snail

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Fragment shader issue [possible bug]
« Reply #2 on: October 29, 2015, 08:51:21 am »
I just had a very quick look.  But my first guess is that gl_FragCoord is in a different range in your project than it is for the glslsandbox example you linked to.

If you're using a sf::Sprite, gl_FragCoord x and y will be in the range of 0 to 1.

Your shader is comparing this to the "resolution" value.  I don't know what you've got that set to, but I'm guessing screen resolution, or the size of the sprite (or both).

If this is the case, you're comparing stuff in screen space (resolution) to stuff in texture space (gl_FragCoord), and that's kinda apples and oranges.  You'll need to convert both values to use the same coordinate space to have any meaningful comparison between them.

I see. The resolution vec2 is just the size of the texture, in this case I am rendering using a sf::RectangleShape that is set to the size of the window. Do you know if gl_FragCoord is also in a 0-1 range for sf::RectangleShape? Thanks for the reply!

Jabberwocky

  • Full Member
  • ***
  • Posts: 157
    • View Profile
Re: Fragment shader issue [possible bug]
« Reply #3 on: October 29, 2015, 09:51:24 am »
Do you know if gl_FragCoord is also in a 0-1 range for sf::RectangleShape? Thanks for the reply!

I think so.
Check out function Shape::updateTexCoords(), file Shape.cpp

Or you could tell pretty quick if you put a breakpoint in your code, and examine the m_vertices data member of your RectangleShape.

Snail

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Fragment shader issue [possible bug]
« Reply #4 on: October 29, 2015, 10:39:54 am »
Do you know if gl_FragCoord is also in a 0-1 range for sf::RectangleShape? Thanks for the reply!

I think so.
Check out function Shape::updateTexCoords(), file Shape.cpp

Or you could tell pretty quick if you put a breakpoint in your code, and examine the m_vertices data member of your RectangleShape.

Looks like it's using the actual window space values for the frag coord in that function and in the vertice data. I believe that's what the GLSL manual says is how it's suppose to behave so something else must be causing this.

fallahn

  • Sr. Member
  • ****
  • Posts: 499
  • Buns.
    • View Profile
    • Trederia
Re: Fragment shader issue [possible bug]
« Reply #5 on: October 29, 2015, 10:45:30 am »
gl_TexCoord is in the range 0-1, gl_FragCoord is in the range 0 - resolution. This is why you divide it by your resolution, to get a 0 - 1 scale.

Snail

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Fragment shader issue [possible bug]
« Reply #6 on: October 29, 2015, 11:08:38 am »
Turns out it was an error in the way that I was calculating for the bounds, ended up removing the outside() function and replacing the check line to look line this:
Code: [Select]
if((random(gl_FragCoord / resolution) > 0.995) && (length(gl_FragCoord - (resolution / 2.0)) > (resolution.y / 2.65)))
Works perfectly now.

Jabberwocky

  • Full Member
  • ***
  • Posts: 157
    • View Profile
Re: Fragment shader issue [possible bug]
« Reply #7 on: October 29, 2015, 04:45:59 pm »
gl_TexCoord is in the range 0-1, gl_FragCoord is in the range 0 - resolution. This is why you divide it by your resolution, to get a 0 - 1 scale.

Ah cool, thanks fallahn.
You're right I was getting confused between gl_TexCoord and gl_FragCoord.

Works perfectly now.
Nice!  Glad you got it working.