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

Author Topic: Using a shader with an array of sf:Vertex  (Read 3363 times)

0 Members and 1 Guest are viewing this topic.

For4Que

  • Newbie
  • *
  • Posts: 6
    • View Profile
Using a shader with an array of sf:Vertex
« on: June 16, 2013, 11:30:21 am »
sf::RenderWindow window(sf::VideoMode(800, 600), "Test");

sf::Vertex vertices[] =
{
        sf::Vertex(sf::Vector2f( 0, 0), sf::Color::Blue),
        sf::Vertex(sf::Vector2f(200, 0), sf::Color::Red),
        sf::Vertex(sf::Vector2f(200, 200), sf::Color::Red),
        sf::Vertex(sf::Vector2f( 0, 200), sf::Color::Yellow),
};

sf::Shader shader;

//the example shader from the SDK
shader.loadFromFile("pixelate.frag", sf::Shader::Fragment);
shader.setParameter("pixel_threshold", .01f);

window.draw(vertices, 4, sf::Quads, &shader);

 
I'm probably not understanding something which is why I'm asking this question. Why doesn't this work?
The only thing that happens to the Quad is that it gets turned all black. I'm thinking it has something to do with textures since:
sf::Texture texture;
texture.create(800,600);
window.draw(vertices, 4, sf::Quads);
texture.update(window);
sf::Sprite sprite(texture);
window.draw(sprite, &shader);
 
this workaround works, but is probably bad since it uses the whole window. texture.update is overloaded, but I have no idea how to change a vertex array to an array of pixels or sf::Image (If it's even the correct way to do this).

This is the shader code just in case:
uniform sampler2D texture;
uniform float pixel_threshold;

void main()
{
    float factor = 1.0 / (pixel_threshold + 0.001);
        vec2 pos = floor(gl_TexCoord[0].xy * factor + 0.5) / factor;
        gl_FragColor = texture2D(texture, pos) * gl_Color;     
}
 
« Last Edit: June 16, 2013, 11:32:47 am by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Using a shader with an array of sf:Vertex
« Reply #1 on: June 16, 2013, 11:39:33 am »
...
shader.setCurrentTexture("texture");

sf::RenderStates states;
states.shader = &shader;
states.texture = &texture;
window.draw(vertices, 4, sf::Quads, states);
Laurent Gomila - SFML developer

For4Que

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Using a shader with an array of sf:Vertex
« Reply #2 on: June 16, 2013, 10:56:57 pm »
shader.setCurrentTexture("texture");
I'm not sure I understand this. sf::Shader doesn't seem to have that function.
« Last Edit: June 16, 2013, 10:59:34 pm by For4Que »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Using a shader with an array of sf:Vertex
« Reply #3 on: June 16, 2013, 11:12:25 pm »
Quote
shader.setCurrentTexture("texture");
That was random..
shader.setParameter("texture",sf::Shader::CurrentTexture);
Back to C++ gamedev with SFML in May 2023

For4Que

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Using a shader with an array of sf:Vertex
« Reply #4 on: June 17, 2013, 12:11:46 am »
That was random..
shader.setParameter("texture",sf::Shader::CurrentTexture);

That's what I thought he meant, but it doesn't solve my problem either.
Basically, I'm wondering if I can apply the shader to whatever the vertices are drawing - in this case, the quad with interpolated colors. I think this doesn't work because the colors itself doesn't count as a texture which is why I had to draw it to the screen first, then store that whole image as a texture, create a sprite with that texture, then draw that sprite with the shader.

What I'm wondering is if I can apply a shader directly to the vertices without having to use textures. And if not, is there a way to not use the whole screen and just use the vertices as a texture.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Using a shader with an array of sf:Vertex
« Reply #5 on: June 17, 2013, 07:54:49 am »
Sorry for the syntax, that was the old one ;D

Quote
What I'm wondering is if I can apply a shader directly to the vertices without having to use textures.
You should have said that in your first post... since your shader uses a texture, I assumed that you really meant to use a texture.

In this case, half of your shader is useless. Only gl_Color will be useful.
Laurent Gomila - SFML developer

For4Que

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Using a shader with an array of sf:Vertex
« Reply #6 on: June 17, 2013, 11:34:01 am »
Sorry about that. I'm just starting to learn glsl and I mentioned earlier that the shader was directly from the sfml examples. The shader used a texture because that was how the example had it and I'm still trying to wrap my head around how each the vertex and fragment shaders get their inputs. At this point, it seems this is a glsl question, and if you don't mind(since it looks somewhat simple), how would this look like in the shader code?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Using a shader with an array of sf:Vertex
« Reply #7 on: June 17, 2013, 12:11:34 pm »
You should first tell us what you want it to do :P

If you just want to output the color, without additional processing, then it's trivial:

gl_FragColor = gl_Color;
Laurent Gomila - SFML developer

For4Que

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Using a shader with an array of sf:Vertex
« Reply #8 on: June 17, 2013, 04:25:10 pm »
And I thought this was my biggest clue!
shader.loadFromFile("pixelate.frag", sf::Shader::Fragment);
Well in any case, I'm trying to apply the pixelate effect (from the example) to the vertices without using textures.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Using a shader with an array of sf:Vertex
« Reply #9 on: June 17, 2013, 04:52:00 pm »
The pixelate effect works because you can sample neighbour texels, by changing the texture coordinates. But you can't sample neighbour colors, you can only access the current pixel's color. So you can't directly apply this kind of fragment shader without a texture.
Laurent Gomila - SFML developer

For4Que

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Using a shader with an array of sf:Vertex
« Reply #10 on: June 18, 2013, 04:39:40 am »
Guess I'm out of luck then. Hopefully the workaround won't cause too much of a performance drop - not that I should be worried about it at this point, and by then maybe I would have a clearer idea of what is it that I'm trying to do design wise. Thanks for the help.  :)