1
DotNet / Re: Applying shaders to draw gives black screen
« on: May 08, 2017, 05:09:41 pm »
Ok, so I got it working by copying the code from the SFML 2.2 shader example and stripping it down to what I needed:
After that I call it by calling by a normal draw:
Honestly I have no clue how this works, but it does.
abstract class Effect : SFML.Graphics.Drawable
{
public void Update(Texture texture)
{
if (Shader.IsAvailable)
OnUpdate(texture);
}
public void Draw(RenderTarget target, RenderStates states)
{
if (Shader.IsAvailable)
{
OnDraw(target, states);
}
}
protected abstract void OnUpdate(Texture texture);
protected abstract void OnDraw(RenderTarget target, RenderStates states);
}
{
public void Update(Texture texture)
{
if (Shader.IsAvailable)
OnUpdate(texture);
}
public void Draw(RenderTarget target, RenderStates states)
{
if (Shader.IsAvailable)
{
OnDraw(target, states);
}
}
protected abstract void OnUpdate(Texture texture);
protected abstract void OnDraw(RenderTarget target, RenderStates states);
}
class Pixelate : Effect
{
public Pixelate()
{
// Load the texture and initialize the sprite
mySprite = new Sprite();
// Load the shader
myShader = new Shader(null, "Assets/Shaders/pixelate.frag");
myShader.SetParameter("texture", Shader.CurrentTexture);
}
protected override void OnUpdate(Texture texture)
{
mySprite.Texture = texture;
}
protected override void OnDraw(RenderTarget target, RenderStates states)
{
states = new RenderStates(states);
states.Shader = myShader;
target.Draw(mySprite, states);
}
private Texture myTexture = null;
private Sprite mySprite = null;
private Shader myShader = null;
}
{
public Pixelate()
{
// Load the texture and initialize the sprite
mySprite = new Sprite();
// Load the shader
myShader = new Shader(null, "Assets/Shaders/pixelate.frag");
myShader.SetParameter("texture", Shader.CurrentTexture);
}
protected override void OnUpdate(Texture texture)
{
mySprite.Texture = texture;
}
protected override void OnDraw(RenderTarget target, RenderStates states)
{
states = new RenderStates(states);
states.Shader = myShader;
target.Draw(mySprite, states);
}
private Texture myTexture = null;
private Sprite mySprite = null;
private Shader myShader = null;
}
After that I call it by calling by a normal draw:
pixelate.Update(renderTexture.Texture);
window.Draw(pixelate);
window.Draw(pixelate);
Honestly I have no clue how this works, but it does.