1
Graphics / Shader in SFML
« on: January 02, 2021, 06:11:16 am »
Hi everybody!
I learn shader from this website: https://thebookofshaders.com/05/
I complete to compiling my project and it's run not error, but not show line green in window.
I use cocos2dx then app run good.
file fragment shader:
file main:
Sorry for my English.
I learn shader from this website: https://thebookofshaders.com/05/
I complete to compiling my project and it's run not error, but not show line green in window.
I use cocos2dx then app run good.
file fragment shader:
Quote
uniform vec2 u_resolution;
uniform float u_time;
uniform sampler2D texture;
float plot(vec2 st, float pct){
return smoothstep( pct-0.02, pct, st.y) -
smoothstep( pct, pct+0.02, st.y);
}
void main() {
vec2 st = gl_FragCoord.xy/u_resolution;
// Smooth interpolation between 0.1 and 0.9
float y = smoothstep(0.1,0.9,st.x);
vec3 color = vec3(y);
float pct = plot(st,y);
color = (1.0-pct)*color+pct*vec3(0.0,1.0,0.0);
gl_FragColor = vec4(color,1.0);
}
file main:
Quote
sf::RectangleShape shape(sf::Vector2f(200.0f,200.0f));anyone help me this problem?. Thanks advance.
sf::Texture texture;
if (!texture.loadFromFile("container.jpg"))
{
// error...
}
shape.setTexture(&texture);
if (!sf::Shader::isAvailable())
{
std::cout<< "Not available" <<std::endl;
}
sf::Shader shader;
if (!shader.loadFromFile("cube.vs", "cube.fs"))
{
}
sf::Clock clock;
// run the program as long as the window is open
while (window.isOpen())
{
World.Step(1/60.f, 8, 3);
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
if(event.type == sf::Event::MouseButtonPressed){
if (event.mouseButton.button == sf::Mouse::Left)
{
int MouseX = event.mouseButton.x;
int MouseY = event.mouseButton.y;
}
}
}
window.clear(sf::Color::Black);
float sec = clock.getElapsedTime().asSeconds();
shader.setUniform("u_time",sec);
shader.setUniform("u_resolution",sf::Vector2f(200.0f, 200.0f));
shader.setUniform("texture", sf::Shader::CurrentTexture);
window.draw(shape,&shader);
World.DrawDebugData();
//at global scop
window.display();
}
Sorry for my English.