opengl 在几何体着色器中绘制两个相互重叠的形状?

x0fgdtte  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(188)

用这个小程序我想实现:
1.发布点
1.使用顶点着色器变换此点
1.使用几何着色器创建正方形和三角形
1.使用片段着色器填充三角形和正方形
不幸的是,我无法将两个形状叠加起来,我只能看到三角形或正方形,我尝试使用z,但没有任何改变。

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();
}

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();
}

主要

#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();
    }
}

顶点

#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;
}

几何形状

#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();
}

碎片

#version 150
in vec4 finalColour;
out vec4 out_Colour;

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

slhcrj9b1#

几何体着色器创建2个基本体,一个具有6个顶点,另一个具有3个顶点,总共需要9个顶点:
第一个月

layout (triangle_strip, max_vertices = 9) out;

相关问题