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

Author Topic: binding vertex array with both texture and shaders  (Read 1763 times)

0 Members and 1 Guest are viewing this topic.

happy11

  • Newbie
  • *
  • Posts: 7
    • View Profile
binding vertex array with both texture and shaders
« on: August 15, 2023, 04:38:54 am »
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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: binding vertex array with both texture and shaders
« Reply #1 on: August 15, 2023, 07:52:41 am »
Could it also be that your shader interacts with the texture?
What shader are you trying to use?

Depending on the shader, you could also use it as a post-processing on a render texture onto which you previously rendered the vertex array.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

happy11

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: binding vertex array with both texture and shaders
« Reply #2 on: August 15, 2023, 12:16:13 pm »
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), 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);  
    }
}
« Last Edit: August 15, 2023, 12:26:56 pm by happy11 »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: binding vertex array with both texture and shaders
« Reply #3 on: August 15, 2023, 08:42:24 pm »
The fragment shader determines the final colour for the pixel.

If it doesn't know about the texture, the texture cannot be included.

You need to pass the texture to the shader and then use the texture colour (for that fragment) and combine it with your fragment colour.

Also note that OpenGL use an opposite y direction from SFML (OpenGL fragments have 0,0 at bottom-left and SFML has 0,0 at top-left - by default) so you need to invert the y co-ordinate; this is why the radial gradient shader requires the window height.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

happy11

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: binding vertex array with both texture and shaders
« Reply #4 on: August 15, 2023, 09:09:12 pm »
The fragment shader determines the final colour for the pixel.

If it doesn't know about the texture, the texture cannot be included.

You need to pass the texture to the shader and then use the texture colour (for that fragment) and combine it with your fragment colour.

Also note that OpenGL use an opposite y direction from SFML (OpenGL fragments have 0,0 at bottom-left and SFML has 0,0 at top-left - by default) so you need to invert the y co-ordinate; this is why the radial gradient shader requires the window height.

and how do I pass the texture to the shader? to which one I should pass it to, the vertex or the fragment?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: binding vertex array with both texture and shaders
« Reply #5 on: August 15, 2023, 10:22:29 pm »
Both.

If you look at the radial gradient shader, I'd left in the texture in the vertex shader (even though I don't use it in the fragment shader). It's the line missing from your vertex shader.

To use the texture in the fragment shader, have a look at this tiny fragment shader. It takes the shader and multiplies it with a colour (basically what SFML default fragment shader does if you don't load one specifically):
https://github.com/Hapaxia/Lens/blob/master/Lens/default.frag



EDIT:
There is an official tutorial showing how to use shaders with SFML and the minimal shaders there show the same thing:
https://www.sfml-dev.org/tutorials/2.6/graphics-shader.php#minimal-shaders
« Last Edit: August 16, 2023, 09:05:14 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*