我尝试在我的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;
}
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.片段:
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;
}
}
1条答案
按热度按时间mbjcgjjk1#
in
和out
不适用于#version 110
(as implied by the lack of a#version
directive)GLSL中的varying
:删除它们: