I have managed to grasp a bit shaders and textures.
if I want to draw Vertex array and use a texture on its triangles I write
Texture my_texture;
// load my texture here
RenderState rs;
rs.texture = &my_texture;
VertexArray my_vertexArray;
// initialize my vertex value here
...
my_window.draw(my_vertexArray, rs);
It draws the triangles with the texture as intended.
if I want to draw Vertex array and use a shader effect on its triangles I write
Shader my_shader;
// load and compile my shader here
RenderState rs;
rs.shader = my_shader;
VertexArray my_vertexArray;
// initialize my vertex value here
...
my_window.draw(my_vertexArray, rs);
it also works as intended.
But when I combine the two and make the shader effect work on the texturized triangles -
...
RenderState rs;
rs.shader = my_shader;
rs.texture = &my_texture;
...
The program acts as I only used the shader and it doesnt seem to texture the triangles.
What am I doing wrong?
I tried-
1) to go throw the tutorial in learnOpenGl about shaders. it seems as they bind textures there differently then in SFML and I dont quite get it.
2) tweak the shader code, send texture to the shader (using Sampler2D), it didnt seem to work.
3) read the documentation about shaders and GLSL
No it does'nt. I tried to write a shader that only draws pixels around a certain point (https://en.sfml-dev.org/forums/index.php?topic=29148.0 (https://en.sfml-dev.org/forums/index.php?topic=29148.0)), and turns any pixel that is too far from that point to black.
Vertex shader -
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_FrontColor = gl_Color;
}
fragment shader -
uniform vec2 lightPos;
uniform float lightRadius;
void main()
{
if (lightPos[0] < gl_FragCoord.x + 150.0 && lightPos[0] > gl_FragCoord.x - 150.0 && lightPos[1] > gl_FragCoord.y - 150.0 && lightPos[1] < gl_FragCoord.y + 150.0)
{
float distanceFromSource = abs(gl_FragCoord.x - lightPos[0]) * abs(gl_FragCoord.x - lightPos[0]);
distanceFromSource += abs(gl_FragCoord.y - lightPos[1]) * abs(gl_FragCoord.y - lightPos[1]);
distanceFromSource = sqrt(distanceFromSource);
if (lightRadius > distanceFromSource)
{
gl_FragColor = gl_Color;
}
else
{
gl_FragColor = vec4(0, 0, 0, 1);
}
}
else
{
gl_FragColor = vec4(0, 0, 0, 1);
}
}