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:
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);
}
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?
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:
float gray = dot(pixel.rgb, vec3(0.299, 0.587, 0.114));