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

Author Topic: [SOLVED] Error when drawing with Shader and BlendMode to RenderTexture  (Read 4151 times)

0 Members and 1 Guest are viewing this topic.

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
Hi, I'm currently working on my lighting system. I use a simple shader to draw a light gradient, draw those light sources via sf::BlendAdd to a render texture and finally fetch the texture and draw it to the actual screen using sf::BlendMultiply. This works great - if I don't resize the view by zooming. When zoomed, the render texture is recreated in order to fill the entire screen.

If resizing, the program crashs with an assertion error... But first, here's a minimal example which causes the problem on my machine.
(click to show/hide)

Here's the report from gdb
(click to show/hide)

In my opinion I need to draw to the render texture in order to guarantee those different blending modes. Drawing without the render texture isn't working correctly (from the result's point of view).

What I've tested:
  • Drawing with a shader to a render texture without resizing the view works fine.
  • Drawing with a shader to the render window with resizing the view works fine, as well.
  • Drawing without a shader to a render texture or render window with resizing the view works fine.

So finally, it seems to be a problem with zoom + shader + render texture, but I don't know what's wrong with my code!
Just in case: The machine is a netbook with Linux Ubuntu (32 bit) with a no-name Intel graphics chip. I'm using clang and C++11 for building the application.

Kind regards
Glocke
« Last Edit: February 21, 2015, 10:08:09 am by Glocke »
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Error when drawing with Shader and BlendMode to RenderTexture
« Reply #1 on: February 20, 2015, 01:22:29 pm »
Quote
When zoomed, the render texture is recreated in order to fill the entire screen.
Why? As long as the size of the window doesn't change, you don't have to resize the render-texture. You just have to use the same view on both the window and the render-texture, and draw the render-texture on the window with the default view.

About the crash... I have no idea. Make sure that your graphics drivers are up-to-date.
Laurent Gomila - SFML developer

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
Re: Error when drawing with Shader and BlendMode to RenderTexture
« Reply #2 on: February 20, 2015, 03:28:56 pm »
Make sure that your graphics drivers are up-to-date.
I'll check this, thanks!

You just have to use the same view on both the window and the render-texture, and draw the render-texture on the window with the default view.

Ok I tried but failed. (Here's my code)
(click to show/hide)
When moving or zooming, the render texture does not move or scale :S Example screenshots are attached.

(light.frag Shader source)
(click to show/hide)

Kind regards
Glocke
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Error when drawing with Shader and BlendMode to RenderTexture
« Reply #3 on: February 20, 2015, 04:19:46 pm »
Quote
When moving or zooming, the render texture does not move or scale :S Example screenshots are attached.
It always covers the entire window, which exactly what it is supposed to do. Now the problem is that your lit area (the shape) is always 640x480, so even if you move or scale the view, nothing will be lit outside this shape.
Laurent Gomila - SFML developer

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
Re: Error when drawing with Shader and BlendMode to RenderTexture
« Reply #4 on: February 20, 2015, 04:49:03 pm »
It always covers the entire window, which exactly what it is supposed to do. Now the problem is that your lit area (the shape) is always 640x480, so even if you move or scale the view, nothing will be lit outside this shape.

Well, I was trying to do so: I calculated each light source's position inside the window (using the view's center and size within a calculation) and finally rendered the light texture to the window using the view. But zooming the view broke the process referring to "fill the screen"-ability

The process looked like this and was perfectly working (when not zooming^^)
                // [...]
                // create light texture
                buffer.clear({25, 25, 25});
                sf::RenderStates states;
                states.blendMode = sf::BlendAdd;
                states.shader = &shader;
                shader.setParameter("intensity", 255u);
                shader.setParameter("center", shape.getPosition() - view.getCenter() + view.getSize() / 2.f);
                shader.setParameter("radius", 500.f);
                buffer.draw(array, states);
                buffer.display();
                sf::Sprite sprite{buffer.getTexture()};
               
                // draw scene
                window.clear(sf::Color::White);
                window.setView(view);
                window.draw(shape);
                sprite.setPosition(view.getCenter());
                sprite.setOrigin(view.getSize() / 2.f);
                window.draw(sprite, sf::BlendMultiply);
                window.display();

                // [...]
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Error when drawing with Shader and BlendMode to RenderTexture
« Reply #5 on: February 20, 2015, 07:01:42 pm »
With this strategy, you need to adjust "array" so that it fills the whole render-texture. To do so, it must cover the same rectangle as the view.
Laurent Gomila - SFML developer

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
Re: Error when drawing with Shader and BlendMode to RenderTexture
« Reply #6 on: February 20, 2015, 08:11:46 pm »
Meanwhile I moved rotation/movement handling out of the event-loop - now using sf::Keyboard directly. So the example is a bit smoother.

With this strategy, you need to adjust "array" so that it fills the whole render-texture. To do so, it must cover the same rectangle as the view.
Do you mean like this?
                // handle zoom and moving
                bool adjust{false};
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) { view.zoom(1.025f); adjust = true; }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::E)) { view.zoom(0.975f); adjust = true; }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { view.move(0.f, -5.f); }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { view.move(-5.f, 0.f); }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { view.move(0.f, 5.f); }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { view.move(5.f, 0.f); }
               
                if (adjust) {
                        auto size = sf::Vector2f(view.getSize());
                        array[0].position = array[0].texCoords = {0.f, 0.f};
                        array[1].position = array[1].texCoords = {size.x, 0.f};
                        array[2].position = array[2].texCoords = {size.x, size.y};
                        array[3].position = array[3].texCoords = {0.f, size.y};
                }
Unfortunately, it doesn't affect the zooming issue. With and without this adjustment, the result looks (when zooming "out") like the attached screenshot.

I guess there's a small thing missing :S It seems like the render texture isn't scaled right when drawing to screen.

(btw full code)
(click to show/hide)
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Error when drawing with Shader and BlendMode to RenderTexture
« Reply #7 on: February 20, 2015, 08:48:11 pm »
This last code doesn't use the default view for drawing the render-texture.

Well, I'm just trying to help you solve your problems one after the other, but maybe you should take a while to think about the whole system, figure out a robust solution, and implement it, rather than trying to hack randomly in the existing code until it works.
Laurent Gomila - SFML developer

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
Re: Error when drawing with Shader and BlendMode to RenderTexture
« Reply #8 on: February 21, 2015, 09:42:41 am »
(click to show/hide)

/EDIT: NOOOOW :D
I finally realized what you meant with "adjust the array" after I saved some zoomed render textures to file. So I decided to adjust the array after each move or zoom operation:
                if (adjust) {
                        auto size = view.getSize();
                        auto topleft = view.getCenter() - size / 2.f;
                       
                        array[0].position = array[0].texCoords = topleft;
                        array[1].position = array[1].texCoords = {topleft.x + size.x, topleft.y};
                        array[2].position = array[2].texCoords = topleft + size;
                        array[3].position = array[3].texCoords = {topleft.x, topleft.y + size.y};
                }
Now it's working, because (as you predicted) the array now fits the entire render texture 8)

Thanks a lot!! And sorry for my "hacky behavior" :-\

Btw I put everything else in this post inside a spoiler. And here's another one with the current (working) code - maybe useful for everyone else suffering this "problem":

(click to show/hide)
(click to show/hide)

Kind regards
Glocke
« Last Edit: February 21, 2015, 10:07:49 am by Glocke »
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [SOLVED] Error when drawing with Shader and BlendMode to RenderTexture
« Reply #9 on: February 21, 2015, 10:47:05 am »
I'm glad you solved your problem :)
Laurent Gomila - SFML developer