尝试从文件加载 SFML 着色器时出错

问题描述 投票:0回答:0

我试图在我的 sfml 应用程序中实现着色器,但没有成功。在 ubuntu 上使用 g++ 编译时出现以下错误:

 Failed to compile vertex shader:
0:1(1): error: duplicate storage qualifier 

这是我要编译的代码,在 main.cpp 中:

#include <SFML/Graphics.hpp>
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);
    sf::Shader shader;
    shader.loadFromFile("vertex_shader.vert", "fragment_shader.frag");

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(shape, &shader);
        window.display();
    }

    return 0;
}

我没有自己写那些着色器,我看到一个人在 youtube 视频上使用它们。

这是一个测试程序,我尝试让着色器工作,因为它的代码比我的主项目少很多,而且可以更清楚地看到事情。我正试图找到一种方法来摆脱错误。

“vertex_shader.vert”和“fragment_shader.frag”与 main.cpp 位于同一文件夹中

此外,这里是 vertex_shader.vert:

varying out vec4 vert_pos;

void main()
{
    // transform the vertex position
    vert_pos = gl_ModelViewProjectionMatrix * gl_Vertex;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; 

    // transform the texture coordinates
    gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;

    // forward the vertex color
    gl_FrontColor = gl_Color;
}

和 fragment_shader.frag:

varying in vec4 vert_pos;

uniform sampler2D texture;
uniform bool hasTexture;
uniform vec2 lightPos;

void main()
{
    //Ambient light
    vec4 ambient = vec4(0.02, 0.02, 0.5, 1.0);
    
    //Convert light to view coords
    lightPos = (gl_ModelViewProjectionMatrix * vec4(lightPos, 0, 1)).xy;
    
    //Calculate the vector from light to pixel (Make circular)
    vec2 lightToFrag = lightPos - vert_pos.xy;
    lightToFrag.y = lightToFrag.y / 1.7;

    //Length of the vector (distance)
    float vecLength = clamp(length(lightToFrag) * 2, 0, 1);

    // lookup the pixel in the texture
    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);

    // multiply it by the color and lighting
    if(hasTexture == true)
    {
        gl_FragColor = gl_Color * pixel * (clamp(ambient + vec4(1-vecLength, 1-vecLength, 1-vecLength, 1), 0, 1));
    }
    else
    {
        gl_FragColor = gl_Color;
    }
}
`
c++ shader sfml
© www.soinside.com 2019 - 2024. All rights reserved.