Milan Irigoyen Milan Irigoyen - 1 month ago 11x
C++ Question

Text and shaders

Using SFML 2.4, I want to draw a text on the screen and apply effects on it with a fragment shader. But when I try it, the text is drawn with squares instead of characters

Fragment shader :

void main() { gl_FragColor= vec4(1,0,0,1); }

SFML text code :

sf::Text t;
t.setString("this is a test");

//myWindow.draw(t); // This displays a white "this is a test"
//myWindow.draw(t, myShader); // This displays red squares instead of characters

I expected my shader to display that text in red. But instead I get red squares. Each square is the size of the bounding box of the character that's supposed to be there.
Am I missing something obvious here? Or is sf::Text just not intended to be used that way?

qxz qxz

I'm not familiar with how SFML text rendering works, but I highly suspect it puts the text into a texture. In that case, your fragment shader has to sample the texture to determine the color of a given fragment. Just setting the fragment color to red will do just that: set every rendered fragment to red. Try this:

uniform sampler2D texture;

void main() {
    gl_FragColor = texture2D(texture, gl_TexCoord[0].st) * vec4(1,0,0,1);    

This will sample the currently bound texture at the texture coordinates passed in from the vertex shader, and tint it red.