openglglslgeometry-shader

Draw two shapes on top of each other in geometry shader?


With this small program I want to achieve:

  1. Publish a point
  2. Transform this point with vertex shader
  3. Create a square and a triangle with geometry shader
  4. Fill both the triangle and the square with fragment shader

Unfortunately, I cannot manage to superpose both shapes. I see either only the triangle or only the square. I tried to play with z but it doesn't change anything.

void main(void) {
    finalColour = vec4(1.0, 0.5, 0.5, 1.0);
    square(1.0, 0.0);

    finalColour = vec4(0.0, 1.0, 1.0, 0.0);
    triangle(vec3(0.0, 0.0, 0.0), angle[0], 1.0);
    EndPrimitive();
}

enter image description here

void main(void) {
    // finalColour = vec4(1.0, 0.5, 0.5, 1.0);
    // square(1.0, 0.0);

    finalColour = vec4(0.0, 1.0, 1.0, 0.0);
    triangle(vec3(0.0, 0.0, 0.0), angle[0], 1.0);
    EndPrimitive();
}

enter image description here

Main

#include <SFML/Graphics.hpp>
#include <GL/glew.h>
#include <vector>

#define WIDTH 800
int main() {
    sf::RenderWindow window(sf::VideoMode(WIDTH, WIDTH), "Test");

    sf::Shader shader;
    shader.loadFromFile("shader.vert", "shader.geom", "shader.frag");
    sf::Transform matrix = sf::Transform::Identity;
    matrix.scale(1.0 / WIDTH, 1.0 / WIDTH);
    sf::Glsl::Mat4 projectionViewMatrix = matrix;

    shader.setUniform("projectionViewMatrix", projectionViewMatrix);

    std::vector<GLfloat> vertices;

    vertices.push_back(0.0); vertices.push_back(0.0); vertices.push_back(0.0);

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

        window.clear(sf::Color::Black);
        glEnable(GL_BLEND);
        glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
        glVertexPointer(3, GL_FLOAT, 0, vertices.data());
        glEnableClientState(GL_VERTEX_ARRAY);
        glDrawArrays(GL_POINTS, 0, vertices.size() / 3);
        glDisableClientState(GL_VERTEX_ARRAY);

        sf::Shader::bind(&shader);

        window.display();
    }
}

Vertex

#version 150
in vec3 position;

out float angle;

uniform mat4 projectionViewMatrix;

void main(void){
    gl_Position = projectionViewMatrix * vec4(position.xy, 0.0 ,1.0);
    angle = position.z;
}

Geometry

#version 150

layout (points) in;
layout (triangle_strip, max_vertices = 6) out;

in float angle[];

out vec4 finalColour;

uniform mat4 projectionViewMatrix;

void createVertex(vec3 offset) {
    vec4 actualOffset = vec4(offset, 1.0);
    vec4 worldPosition = gl_in[0].gl_Position + actualOffset;
    gl_Position = worldPosition;
    EmitVertex();
}

void createAngularVertex(vec3 offset, float angle, float radius) {
    vec4 actualOffset = vec4(offset, 1.0);
    vec4 worldPosition = gl_in[0].gl_Position + actualOffset;
    gl_Position = worldPosition;
    gl_Position.x += cos(angle) * radius;
    gl_Position.y += sin(angle) * radius;
    EmitVertex();
}

void square(float size, float z) {
    createVertex(vec3(-size, -size, z));
    createVertex(vec3(size, -size, z));
    createVertex(vec3(-size, size, z));
    createVertex(vec3(size, size, z));
    createVertex(vec3(size, -size, z));
    createVertex(vec3(-size, size, z));
    EndPrimitive();
}

void triangle(vec3 offset, float angle, float radius) {
    float pi = 3.1415;
    float angleOffset = 0.5;
    createAngularVertex(offset, angle, radius);
    createAngularVertex(offset, angle + pi + angleOffset, radius);
    createAngularVertex(offset, angle + pi - angleOffset, radius);
    EndPrimitive();
}

void main(void) {
    finalColour = vec4(1.0, 0.5, 0.5, 1.0);
    square(1.0, 0.0);

    finalColour = vec4(0.0, 1.0, 1.0, 0.0);
    triangle(vec3(0.0, 0.0, 0.0), angle[0], 0.1);
    EndPrimitive();
}

Fragment

#version 150
in vec4 finalColour;
out vec4 out_Colour;

void main(void){
    out_Colour = vec4(finalColour.rgb, 1.0);
}

Solution

  • The geometry shader creates 2 primitives, one with 6 vertices and one with 3 vertices, requiring a total of 9 vertices:

    layout (triangle_strip, max_vertices = 6) out;

    layout (triangle_strip, max_vertices = 9) out;