Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Applying shaders to draw gives black screen  (Read 4018 times)

0 Members and 2 Guests are viewing this topic.

Malp

  • Newbie
  • *
  • Posts: 2
    • View Profile
Applying shaders to draw gives black screen
« on: May 08, 2017, 01:47:15 am »
As the title suggests, whenever I try using a shader with the window draw, I get a black screen.

This is the draw function I'm using:
private void Draw()
{
        window.Clear();
        renderTexture.Clear();
        renderTexture.Draw(borderSprite);
        EntityManager.Draw(renderTexture);
        renderTexture.Display();
        renderTextureSprite.Texture = renderTexture.Texture;
        RenderStates states = new RenderStates();
        states.Shader = myShader;
        window.Draw(renderTextureSprite, states);                      
        window.Display();
}

Normal, if I were to just draw the texture without any states, it works exactly like intended:
window.Draw(renderTextureSprite);  

I tried setting up an example shader from the SFML.NET 2.2 examples. I chose the pixelate shader. Here's how I set up the shader:
myShader = new Shader(null, "Assets/Shaders/pixelate.frag");
myShader.SetParameter("texture", Shader.CurrentTexture);
myShader.SetParameter("pixel_threshold", 200);

When I run the EXE of the example (shader.exe), the shader works as intended, but when I try to use it on my game: black screen. I am pretty sure I copied all the shader code exactly like the example code.

My end goal is to create a bloom effect like in this thread(https://en.sfml-dev.org/forums/index.php?topic=7827.0) to make it look like Geometry Wars ()

I am using SFML.NET 2.2 with VS2013.

Malp

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Applying shaders to draw gives black screen
« Reply #1 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:
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);
       
    }

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;
    }

After that I call it by calling by a normal draw:

pixelate.Update(renderTexture.Texture);
window.Draw(pixelate);

Honestly I have no clue how this works, but it does.