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

Author Topic: Using geometry shaders on sf::Drawables  (Read 3434 times)

0 Members and 2 Guests are viewing this topic.

jorne22

  • Newbie
  • *
  • Posts: 4
    • View Profile
Using geometry shaders on sf::Drawables
« on: March 17, 2019, 08:45:14 pm »
Hi, I've been having some trouble using geometry shaders on drawables such as sf::Circles and sf::ConvexShapes. When attempting to draw the object with a loaded geometry shader, nothing is drawn whatsoever. I can only get the geometry shader to successfully execute on an array of sf::Vertex (sf::Vertex[]). I would have expected the geometry shader naturally to operate on each vertex of an sf::Drawable and at least display something, though I'm not well versed in what goes on under the hood of window.draw() so I'm probably missing something.

FWIW I'm on SFML-2.4.2 with MinGW 6.1.0. Here's my code:

Code: [Select]
//main.cpp
#include <iostream>
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window;
    sf::VideoMode mode(800, 800);
    window.create(mode, "test");

    sf::Shader shader;
    shader.loadFromFile("./test.vert", "./test.geom", "./test.frag");

    sf::Vertex shape[] = {sf::Vertex({400.0f, 400.0f})};

    sf::CircleShape shape2(5.0f);
    shape2.setPosition(400, 100);
    sf::Drawable* draw = &shape2;

    while(window.isOpen())
    {
        sf::Event currEvent;

        while(window.pollEvent(currEvent))
        {
            switch(currEvent.type)
            {
                case(sf::Event::Closed):
                    window.close();
                    break;
                default:
                    break;
            }
        }

        window.clear(sf::Color::Black);

        window.draw(shape, 1, sf::Points, &shader);
        window.draw(shape2, &shader);

        window.display();
    }
}

Code: [Select]
//test.vert
void main()
{
    vec4 vertex = gl_ModelViewMatrix * gl_Vertex;

    gl_Position = gl_ProjectionMatrix * vertex;

    gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
    gl_FrontColor = gl_Color;
}

Code: [Select]
//test.geom
layout (points) in;

layout (triangle_strip, max_vertices = 3) out;


// Main entry point
void main()
{
    vec4 offset = vec4(0.1, vec3(0.0));
    vec4 offset2 = vec4(0.0, 0.1, 0.0, 0.0);

    gl_Position = gl_in[0].gl_Position + offset;
    EmitVertex();

    gl_Position = gl_in[0].gl_Position - offset;
    EmitVertex();

    gl_Position = gl_in[0].gl_Position + offset2;
    EmitVertex();

    EndPrimitive();
}

Code: [Select]
//test.frag
void main()
{
    gl_FragColor = vec4(1.0,0.0,1.0,1.0);
}

The window displays a pink triangle at the centre of the window but there is no circleshape or other geometry to be seen.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Using geometry shaders on sf::Drawables
« Reply #1 on: March 18, 2019, 08:41:41 am »
I've never used geometry shaders, but since:
- you draw everything with that shader
- it outputs a pink triangle
- you actually see a pink triangle

... then what's wrong? Do you expect to see the original SFML shape as well? I'm not sure geometry shaders work like that, the initial shape is the geometry input, and what is displayed is only the output (ie. the vertices that you emit from the shader).
Laurent Gomila - SFML developer

jorne22

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Using geometry shaders on sf::Drawables
« Reply #2 on: March 18, 2019, 10:16:38 am »
In my code above I'm attempting to apply my shader to two different objects: an sf::circleshape as well as an sf::vertex.

As you can see, the window displays the desired output when operating on the sf::vertex I.e. the pink triangle.

However, the line
Code: [Select]
window.draw(shape2, &shader);, where I'm attempting to draw the circle shape, in fact doesn't draw anything, and I'm not sure why. I would have expected there to be a circular pattern of triangles in place of the original vertices of the circle.

Even if you replace the geometry shader with one that passes vertices through unmodified, the circleshape is nowhere to be seen.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Using geometry shaders on sf::Drawables
« Reply #3 on: March 18, 2019, 01:09:42 pm »
Quote
As you can see, [...]
No I can't. So you have to be accurate when you describe your problem; the output was not clear, from your previous post.

Now it's clearer, but unfortunately I'm not an expert in geometry shaders. Did you have a look at the example provided in the SFML SDK? Does it work for you?
Laurent Gomila - SFML developer

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Using geometry shaders on sf::Drawables
« Reply #4 on: March 18, 2019, 01:23:31 pm »
SFML drawables all send primitive information to OpenGL in the form of triangles. You have to make sure your geometry shader also accepts triangles as the input primitive instead of points as is the case in your example.

For more information, refer to the available documentation, e.g. https://www.khronos.org/opengl/wiki/Geometry_Shader
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

jorne22

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Using geometry shaders on sf::Drawables
« Reply #5 on: March 18, 2019, 02:52:50 pm »
Thank you binary, that turned out to be the cause of the issue.

For any future readers, I was able to input sf::Drawable data into the geometry shader by replacing

Code: [Select]
layout (points) in;
with

Code: [Select]
layout (triangles) in;

 

anything