问题描述
我试图在我的按钮被点击时为我的按钮创建一个轮廓效果,但我只是在用着色器测试东西......每当我用我的着色器绘制东西时,它都会使形状完全变黑
#include int main(){sf::RenderWindow window(sf::VideoMode(500, 500), "hello world", sf::Style::Close);SF::RectangleShape 矩形;rect.setSize(sf::Vector2f(100, 100));rect.setFillColor(sf::Color::Red);SF::着色器模糊;if (!blur.loadFromFile("blur.frag", sf::Shader::Fragment)){返回 1;}如果 (!blur.isAvailable()){返回 1;}blur.setUniform("texture", sf::Shader::CurrentTexture);blur.setUniform("blur_radius", 0.002f);而 (window.isOpen()){SF::事件事件;而 (window.pollEvent(evnt)){if (evnt.type == sf::Event::Closed)window.close();}window.clear(sf::Color(0, 100, 100));sf::RenderStates 状态;state.shader = &blur;window.draw(rect, state);屏幕显示();}返回0;}
和我的着色器代码.
统一的sampler2D纹理;均匀浮动模糊半径;无效主(){vec2 offx = vec2(blur_radius, 0.0);vec2 offy = vec2(0.0, blur_radius);vec4 像素 = 纹理 2D(纹理,gl_TexCoord[0].xy)* 4.0 +纹理2D(纹理,gl_TexCoord[0].xy - offx)* 2.0 +纹理2D(纹理,gl_TexCoord[0].xy + offx)* 2.0 +纹理2D(纹理,gl_TexCoord[0].xy - offy)* 2.0 +纹理2D(纹理,gl_TexCoord[0].xy + offy)* 2.0 +纹理2D(纹理,gl_TexCoord[0].xy - offx - offy)* 1.0 +纹理2D(纹理,gl_TexCoord[0].xy - offx + offy)* 1.0 +纹理2D(纹理,gl_TexCoord[0].xy + offx - offy)* 1.0 +纹理2D(纹理,gl_TexCoord[0].xy + offx + offy)* 1.0;gl_FragColor = gl_Color * (pixel/16.0);}
我希望左上角会出现一个模糊的红色矩形,但会出现一个黑色实心矩形.
因为 rect
没有贴图,blur.frag 中使用的所有像素都是无效的,我猜在这个如果它使用黑色像素,因此使用黑色矩形.
您应该
I'm trying to create a outline effect to my buttons when they are clicked but atm I'm just testing stuff with shaders... whenever I draw something with my shader tho it renders the shape completely black
#include <SFML\Graphics.hpp>
int main(){
sf::RenderWindow window(sf::VideoMode(500, 500), "hello world", sf::Style::Close);
sf::RectangleShape rect;
rect.setSize(sf::Vector2f(100, 100));
rect.setFillColor(sf::Color::Red);
sf::Shader blur;
if (!blur.loadFromFile("blur.frag", sf::Shader::Fragment)){
return 1;
}
if (!blur.isAvailable()){
return 1;
}
blur.setUniform("texture", sf::Shader::CurrentTexture);
blur.setUniform("blur_radius", 0.002f);
while (window.isOpen()){
sf::Event evnt;
while (window.pollEvent(evnt)){
if (evnt.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color(0, 100, 100));
sf::RenderStates state;
state.shader = &blur;
window.draw(rect, state);
window.display();
}
return 0;
}
and my shader code.
uniform sampler2D texture;
uniform float blur_radius;
void main()
{
vec2 offx = vec2(blur_radius, 0.0);
vec2 offy = vec2(0.0, blur_radius);
vec4 pixel = texture2D(texture, gl_TexCoord[0].xy) * 4.0 +
texture2D(texture, gl_TexCoord[0].xy - offx) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy + offx) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy - offy) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy + offy) * 2.0 +
texture2D(texture, gl_TexCoord[0].xy - offx - offy) * 1.0 +
texture2D(texture, gl_TexCoord[0].xy - offx + offy) * 1.0 +
texture2D(texture, gl_TexCoord[0].xy + offx - offy) * 1.0 +
texture2D(texture, gl_TexCoord[0].xy + offx + offy) * 1.0;
gl_FragColor = gl_Color * (pixel / 16.0);
}
I would expect a blurred red rectangle to appear in the top left cornor but instead theres a black solid rectangle.
Because rect
does not have a texture attach to it, all the pixels used in blur.frag are invalid, I guess in this case it use black pixels, hence the black rectangle.
You should create a texture (use sf::RenderTexture
), draw whatever you want on it, then create a sf::Sprite
on top of it and draw this sprite. Alternatively you may load a texture from an image.
#include <SFML/Graphics.hpp>
int main(){
sf::RenderWindow window(sf::VideoMode(200, 200), "hello world", sf::Style::Close);
window.setFramerateLimit(60);
sf::RectangleShape rectBlue;
rectBlue.setSize(sf::Vector2f(100, 50));
rectBlue.setFillColor(sf::Color::Blue);
sf::RectangleShape rectYellow;
rectYellow.setSize(sf::Vector2f(100, 50));
rectYellow.setFillColor(sf::Color::Yellow);
rectYellow.setPosition(0, 50);
sf::RenderTexture renderTexture;
if (!renderTexture.create(100, 100)){ //create a render texture
return 1;
}
renderTexture.clear();
renderTexture.draw(rectBlue); //draw blue rect on the render texture
renderTexture.draw(rectYellow); //draw yellow rect
renderTexture.display();
sf::Sprite sprite(renderTexture.getTexture()); //create a sprite from the created texture
sf::Shader blur;
if (!blur.loadFromFile("blur.frag", sf::Shader::Fragment)){
return 1;
}
if (!blur.isAvailable()){
return 1;
}
blur.setUniform("texture", sf::Shader::CurrentTexture);
blur.setUniform("blur_radius", 0.05f);
while (window.isOpen()){
sf::Event evnt;
while (window.pollEvent(evnt)){
if (evnt.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color(0, 100, 100));
window.draw(sprite, &blur); //draw the sprite with blur shader
window.display();
}
return 0;
}
Result:
这篇关于SFML 着色器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!