1
General / Help with basic fragment-shadered full-screen sprite
« on: February 12, 2019, 02:13:29 pm »
Hello!
I'm trying desperately to run a simply 2D fragment shader, just a starfield, using SFML 2.5 (2018-2019)
It's a pretty minimalistic scenario:
A class that uses fragment shader as an in-game background, and the main() using that class.
The class code implementing this special shadered-backgrounds is this :
Now the main() and the only thing I get is a black drawing, assuming it's the sprite.
and the code on the fragment shader is like this, assuming it compiles ok, because the sf::Shader loadFromFile returns a true :
CampdEstels.glsl
So, I'm trying to report a bug in the loadFromFile at sf::Shader when the path contains slashes.
and also I'm trying to get help, because this code shows a 640x480 black sprite, although the both shaders are loading ok, and the uniforms are being updated regurarly.
Anyoen could find this an interesting phenomena. or help?...please?
I'm trying desperately to run a simply 2D fragment shader, just a starfield, using SFML 2.5 (2018-2019)
It's a pretty minimalistic scenario:
A class that uses fragment shader as an in-game background, and the main() using that class.
The class code implementing this special shadered-backgrounds is this :
#pragma once
#include <SFML/Graphics.hpp>
#include <list>
#include <iterator>
#include <string>
#include <fstream>
#include <streambuf>
#ifndef FONSFX_H
#define FONSFX_H
#include <SFML/Graphics.hpp>
namespace FonsFX
{
class FonsEfecte
{
private:
sf::Image img;
sf::Texture tex;
bool ShaderGeom_isOk = false;
bool ShaderFrag_isOk = false;
public:
sf::Sprite sprite;
sf::Shader sh;
FonsEfecte(unsigned int Ample, unsigned int Alt) {
img.create(Ample , Alt );
tex.loadFromImage(img);
sprite.setTexture(tex);
std::string CodiSharedQUADFixe = "void main(void) { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; }";
if (sh.loadFromMemory(CodiSharedQUADFixe, sf::Shader::Geometry))
ShaderGeom_isOk = true;
std::ifstream t("Data/gfx/CampDestels.glsl");
std::string CodiFragment((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());[/color]
if (sh.loadFromFile( CodiFragment, sf::Shader::Fragment))
ShaderFrag_isOk = true;
/*
[b][color=red]This might be a bug inside loadFromFile method, I assure that, because
as the std::ifstream code demonstrates, it's not a path/permission rights file access issue:
then the loadFromFile is not working properly when a // // path is given.
[/color][/b] */
}
void UpdateFXvar(std::string FXVarName, sf::Vector2f FXValue)
{
sf::Shader::bind(&sh);
sh.setUniform(FXVarName, FXValue);
}
void UpdateFXvar(std::string FXVarName,float FXValue)
{
sf::Shader::bind(&sh);
sh.setUniform(FXVarName, FXValue);
}
void Render(sf::RenderWindow *window)
{
sf::RenderStates r;
r.shader = &sh;
sf::Shader::bind(&sh);
window->draw(sprite, r);
}
~FonsEfecte() {
}
};
}
#endif
#include <SFML/Graphics.hpp>
#include <list>
#include <iterator>
#include <string>
#include <fstream>
#include <streambuf>
#ifndef FONSFX_H
#define FONSFX_H
#include <SFML/Graphics.hpp>
namespace FonsFX
{
class FonsEfecte
{
private:
sf::Image img;
sf::Texture tex;
bool ShaderGeom_isOk = false;
bool ShaderFrag_isOk = false;
public:
sf::Sprite sprite;
sf::Shader sh;
FonsEfecte(unsigned int Ample, unsigned int Alt) {
img.create(Ample , Alt );
tex.loadFromImage(img);
sprite.setTexture(tex);
std::string CodiSharedQUADFixe = "void main(void) { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; }";
if (sh.loadFromMemory(CodiSharedQUADFixe, sf::Shader::Geometry))
ShaderGeom_isOk = true;
std::ifstream t("Data/gfx/CampDestels.glsl");
std::string CodiFragment((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());[/color]
if (sh.loadFromFile( CodiFragment, sf::Shader::Fragment))
ShaderFrag_isOk = true;
/*
[b][color=red]This might be a bug inside loadFromFile method, I assure that, because
as the std::ifstream code demonstrates, it's not a path/permission rights file access issue:
then the loadFromFile is not working properly when a // // path is given.
[/color][/b] */
}
void UpdateFXvar(std::string FXVarName, sf::Vector2f FXValue)
{
sf::Shader::bind(&sh);
sh.setUniform(FXVarName, FXValue);
}
void UpdateFXvar(std::string FXVarName,float FXValue)
{
sf::Shader::bind(&sh);
sh.setUniform(FXVarName, FXValue);
}
void Render(sf::RenderWindow *window)
{
sf::RenderStates r;
r.shader = &sh;
sf::Shader::bind(&sh);
window->draw(sprite, r);
}
~FonsEfecte() {
}
};
}
#endif
Now the main() and the only thing I get is a black drawing, assuming it's the sprite.
int main()
{
sf::RenderWindow window;
FonsFX::FonsEfecte *ELFonsDestelsFx;
sf::Clock clock;
while (window.isOpen())
{
.
... other stuff,...events and so...
ELFonsDestelsFx->UpdateFXvar("time", clock.getElapsedTime().asSeconds());
ELFonsDestelsFx->UpdateFXvar("resolution", sf::Vector2f(window.getSize().x, window.getSize().y));
window.clear(sf::Color(180, 180, 180, 255));
ELFonsDestelsFx->Render(&window);
window.display();
}
return 0;
}
{
sf::RenderWindow window;
FonsFX::FonsEfecte *ELFonsDestelsFx;
sf::Clock clock;
while (window.isOpen())
{
.
... other stuff,...events and so...
ELFonsDestelsFx->UpdateFXvar("time", clock.getElapsedTime().asSeconds());
ELFonsDestelsFx->UpdateFXvar("resolution", sf::Vector2f(window.getSize().x, window.getSize().y));
window.clear(sf::Color(180, 180, 180, 255));
ELFonsDestelsFx->Render(&window);
window.display();
}
return 0;
}
and the code on the fragment shader is like this, assuming it compiles ok, because the sf::Shader loadFromFile returns a true :
CampdEstels.glsl
uniform float time;
uniform vec2 resolution;
float h(float i){
return fract(pow(3., sqrt(i/2.)));
}
void main(void){
vec2 p=gl_FragCoord.xy*2.-resolution;
float a=floor(degrees(4.+atan(p.y,p.x))*2.)/4.;
float d=pow(2.,-10.*fract(0.1*time*(h(a+.5)*-.1-.1)-h(a)*1000.));
if(abs(length(p)-d*length(resolution)) < d*35.){
gl_FragColor=vec4(d*(h(a+.5)*3.));
}else{
gl_FragColor=vec4(0.);
}
}
uniform vec2 resolution;
float h(float i){
return fract(pow(3., sqrt(i/2.)));
}
void main(void){
vec2 p=gl_FragCoord.xy*2.-resolution;
float a=floor(degrees(4.+atan(p.y,p.x))*2.)/4.;
float d=pow(2.,-10.*fract(0.1*time*(h(a+.5)*-.1-.1)-h(a)*1000.));
if(abs(length(p)-d*length(resolution)) < d*35.){
gl_FragColor=vec4(d*(h(a+.5)*3.));
}else{
gl_FragColor=vec4(0.);
}
}
So, I'm trying to report a bug in the loadFromFile at sf::Shader when the path contains slashes.
and also I'm trying to get help, because this code shows a 640x480 black sprite, although the both shaders are loading ok, and the uniforms are being updated regurarly.
Anyoen could find this an interesting phenomena. or help?...please?