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

Author Topic: Shader Filling Screen With White?  (Read 919 times)

0 Members and 1 Guest are viewing this topic.

tetra

  • Newbie
  • *
  • Posts: 16
    • View Profile
Shader Filling Screen With White?
« on: May 17, 2015, 09:48:13 pm »
This is in JSFML, but the syntax is quite similar and I know the chances of getting a response here are better.

I'm trying to apply a simple greyscale filter to my screen:

public class Window {

    private final RenderWindow rw;
    private Shader greyscale;
    private RenderTexture rtex;
    private Sprite shaderSprite;

    public Window(String title) {
        rw = new RenderWindow(new VideoMode(WindowConfig.WINDOW_WIDTH, WindowConfig.WINDOW_HEIGHT), title, WindowStyle.DEFAULT, new ContextSettings(2));
       
        State.setCurrentScreen(ScreenType.MAIN_MENU);
       
        if (!Shader.isAvailable()) {
            System.err.println("Hardware does not support shaders!");
            return;
        }
       
        greyscale = new Shader();
        try {
            greyscale.loadFromFile(Paths.get("shaders/screen.frag"), Shader.Type.FRAGMENT);
        } catch (IOException | ShaderSourceException ex) {
            ex.printStackTrace();
            return;
        }
       
        rtex = new RenderTexture();
        try {
            rtex.create(rw.getSize().x, rw.getSize().y);
        } catch (TextureCreationException ex) {
            ex.printStackTrace();
            return;
        }
       
        shaderSprite = new Sprite(rtex.getTexture());
    }

   
    public void display() {
        while (rw.isOpen()) {
            Screen s = State.getCurrentScreen();
            s.update();
           
            rtex.clear();
            rtex.draw(s);
            rtex.display();
           
            rw.clear();
            rw.draw(shaderSprite, new RenderStates(greyscale));
            rw.display();
           
            InputHandler.handleEvents(rw);
        }
    }

However, my shaderSprite is just a white image. I know this, because if I change the color of the sprite to black, the screen displays a black image. What am I doing wrong?

Here's my fragment shader:

Code: [Select]
void main()
{
    //Compute level of gray
    float gray = dot(gl_Color.rgb, vec3(0.299, 0.587, 0.114));

    //Write destination color
    gl_FragColor = vec4(gray, gray, gray, gl_Color.a);
}
« Last Edit: May 17, 2015, 09:50:25 pm by tetra »

G.

  • Hero Member
  • *****
  • Posts: 1401
    • View Profile
Re: Shader Filling Screen With White?
« Reply #1 on: May 17, 2015, 10:12:29 pm »
gl_color is the diffuse color of your sprite. (what's defined when you call setColor(...))
If you want the color of the texture on your sprite, use texture2D(texture, gl_TexCoord[0].xy)

Take a look at the fragment shader in the tutorial.
« Last Edit: May 17, 2015, 10:16:58 pm by G. »

tetra

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Shader Filling Screen With White?
« Reply #2 on: May 17, 2015, 10:48:51 pm »
The color isn't the problem, the problem is that the sprite isn't setting the correct texture (or the shader is wrong). Or I'm doing something terribly wrong.

G.

  • Hero Member
  • *****
  • Posts: 1401
    • View Profile
Re: Shader Filling Screen With White?
« Reply #3 on: May 17, 2015, 11:45:22 pm »
You don't use any texture in your fragment shader... The tutorial I uselessly linked shows how...

tetra

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Shader Filling Screen With White?
« Reply #4 on: May 17, 2015, 11:47:47 pm »
Sorry, I don't quite understand what to do with the shader.

Here's my changed code:

        greyscale.setParameter("texture", rtex.getTexture());

However, what do I change in the fragment shader to change the pixels to grey?

What do I need to add to this?

Code: [Select]
uniform sampler2D texture;

void main()
{
    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);

    //Compute level of gray
    float gray = dot(gl_Color.rgb, vec3(0.299, 0.587, 0.114));

    //Write destination color
    gl_FragColor = vec4(gray, gray, gray, gl_Color.a);
}

Edit:

Solved by changing the pixel line to this:

Code: [Select]
float gray = dot(pixel.rgb, vec3(0.299, 0.587, 0.114));
« Last Edit: May 18, 2015, 06:09:30 am by tetra »