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

Author Topic: RenderTexture won't clear properly  (Read 39386 times)

0 Members and 2 Guests are viewing this topic.

Zapfyr

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
RenderTexture won't clear properly
« on: October 08, 2012, 03:50:39 pm »
Hello,

I'm using SFML 2.0 (compiled from the source files) on windows and I am trying to use the rendertexture. Which works fine as long as I do not move anything around, when I animated a sprite to move I got the following effect:


The code for this is a bit complex, however the core of it is the following:
renderTexture->clear();

/// Draw everything!
RenderingManager::RenderObjects(renderTexture);

renderTexture->display();

// Clear screen
window->clear();

window->draw(*renderSprite);

// Update the window
window->display();

RenderingManager::ClearRenderList();
 

Which is built around the example code for rendertexture usage:
 // Create a new render-window
 sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

 // Create a new render-texture
 sf::RenderTexture texture;
 if (!texture.create(500, 500))
     return -1

 // The main loop
 while (window.isOpen())
 {
    // Event processing
    // ...

    // Clear the whole texture with red color
    texture.clear(sf::Color::Red);

    // Draw stuff to the texture
    texture.draw(sprite);  // sprite is a sf::Sprite
    texture.draw(shape);   // shape is a sf::Shape
    texture.draw(text);    // text is a sf::Text

    // We're done drawing to the texture
    texture.display();

    // Now we start rendering to the window, clear it first
    window.clear();

    // Draw the texture
    sf::Sprite sprite(texture.getTexture());
    window.draw(sprite);

    // End the current frame and display its contents on screen
    window.display();
 }
 

I am running this on a AMD HD7870 Eyefinity card on 2 monitors with the latest drivers installed.

// Zap

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: RenderTexture won't clear properly
« Reply #1 on: October 08, 2012, 03:54:44 pm »
This shouldn't and actually can't happen, thus I suspect that your clear() call doesn't get executed, but without the full code it's impossible to say. But since you provide an example, does the example also not clear the RenderTexture?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Zapfyr

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Re: RenderTexture won't clear properly
« Reply #2 on: October 08, 2012, 04:02:29 pm »
I fairly sure that the rendertextures clear is called. Every visual object in my program have Z-value, when update is done all objects are pushed to a list in a sorted order (kinda like painters algorithm) when they shall be drawn the program iterates over the objects and draws them in order.  Here is a bit more code:
void Player::Run(const sf::Time &delta)
{
        isRunning = window->isOpen();

        if (isRebuilding)
                Rebuild();

        HandleEvents(delta);
        Update(delta);
        Draw(delta);

        UpdateLayout();

        frameRate = 1.0f / delta.asSeconds();
}

void Player::CleanUp()
{
        isRunning = false;

        if (renderSprite != NULL)
        {
                delete renderSprite;
                renderSprite = NULL;
        }

        if (renderTexture != NULL)
        {
                delete renderTexture;
                renderTexture = NULL;
        }

        if (window != NULL && window->isOpen())
        {
                window->close();
                delete window;
                window = NULL;
        }

        LogMessage("Shuting down and cleaning up player module.", utils::LogManager::P2, utils::LogManager::PLAYER);
}

void Player::HandleEvents(const sf::Time &delta)
{
        sf::Event event;
        while (window->pollEvent(event))
        {
                // Close window : exit
                if (event.type == sf::Event::Closed)
                        isRunning = false;

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                        isRunning = false;
        }
}

void Player::Update(const sf::Time &delta)
{
        if (root != NULL)
                root->Update(delta);
}

void Player::Draw(const sf::Time &delta)
{
        if (window        != NULL &&
                renderTexture != NULL &&
                renderSprite  != NULL)
        {
                renderTexture->clear();

                /// Draw everything!
                RenderingManager::RenderObjects(renderTexture);

                renderTexture->display();

                // Clear screen
                window->clear();

                //window->draw(*sprite);
                renderSprite->setPosition(Utils::Vec2ToSFVec(utils::GraphicsManager::GetOrigin()));
                renderSprite->setScale(Utils::Vec2ToSFVec(utils::GraphicsManager::GetAdaptedScale()));

                window->draw(*renderSprite);
                //RenderingManager::RenderObjects(window);

                // Update the window
                window->display();

                RenderingManager::ClearRenderList();
        }
}

void Player::Rebuild()
{
        isRebuilding = false;
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: RenderTexture won't clear properly
« Reply #3 on: October 08, 2012, 04:08:03 pm »
To whole run/cleanup/update thing doesn't really help since it's not connected to the drawing part.
What does RenderingManager::RenderObjects do?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Zapfyr

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Re: RenderTexture won't clear properly
« Reply #4 on: October 08, 2012, 04:16:47 pm »
void RenderingManager::RenderObjects(sf::RenderTarget *target)
{
    auto itr = renderList.begin();
    for (; itr != renderList.end(); ++itr)
    {
        Object *obj = *itr;
        obj->Draw(target);
    }
}
 
Where renderList is:
std::list<Object*> RenderingManager::renderList;
 
And Object is the base class for all visual objects, where Draw is a purely virtual function, e.g. ObjectImage's Draw looks like this:

void ObjectImage::Draw(sf::RenderTarget *target)
{
    if (sprite != NULL)
        target->draw(*sprite);
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: RenderTexture won't clear properly
« Reply #5 on: October 08, 2012, 04:28:02 pm »
std::list<Object*> RenderingManager::renderList;
 
And it contains only one object of the animation or does it hold all objects of your animation?

I mean if you want to move object x you move it, push it into the list and draw it.
But if you now keep doing this for every iteration the list will contain every object at any position and it's obvious that when drawing the whole list every object is drawn. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Zapfyr

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Re: RenderTexture won't clear properly
« Reply #6 on: October 08, 2012, 04:30:54 pm »
The list contains all object that should be drawn, and it is cleared every frame (I check that before posting ;) ). At the moment the screenshot was taken the list contained two objects (the bottle and the text).

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: RenderTexture won't clear properly
« Reply #7 on: October 08, 2012, 04:45:22 pm »
Then let's get back to the simple example...
How does the following code perform?

#include <SFML/Graphics.hpp>
#include <list>

int main()
{
    sf::RenderWindow window(sf::VideoMode(500, 500), "Test");
    window.setFramerateLimit(60);

    sf::RenderTexture rt;
    rt.create(500, 500);

    sf::RectangleShape rs;
    rs.setSize(sf::Vector2f(20, 20));
    rs.setPosition(0.f, 0.f);
    rs.setFillColor(sf::Color::Red);

    sf::Sprite sprite;
    sprite.setTexture(rt.getTexture(), true);

    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();
        }

        rs.setPosition(static_cast<sf::Vector2f>(sf::Mouse::getPosition(window)));

        rt.clear();
        rt.draw(rs);
        rt.display();

        window.clear();
        window.draw(sprite);
        window.display();
    }
}
 

If the render texture also doesn't get cleared then it's an issue with SFML and your graphics card (is the driver uptodate?).
If it gets cleared then it's something within your code and you should investigate further...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Zapfyr

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Re: RenderTexture won't clear properly
« Reply #8 on: October 08, 2012, 04:58:17 pm »
This is the results of running your code:


I am running the AMD Catalyst 12.8 drivers, which is the current version.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: RenderTexture won't clear properly
« Reply #9 on: October 08, 2012, 05:09:52 pm »
Well then there's something wrong with SFML/your setup.

Quote
SFML 2.0 (compiled from the source files)
Can you give the commit ID and state what compiler you use in which setup (release/debug, static/dynamic)?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: RenderTexture won't clear properly
« Reply #10 on: October 08, 2012, 05:26:14 pm »
eXpl0it3r's code works on my system. I am also using Catalyst 12.8 so it can't be a driver bug. I hardly doubt this has anything to do with your specific hardware either. OpenGL calls not getting executed is normally a result of software malfunction. Did you try to run it in debug mode? When NDEBUG isn't defined SFML_DEBUG should get defined and any OpenGL errors that occur will be printed to stdout.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Zapfyr

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Re: RenderTexture won't clear properly
« Reply #11 on: October 08, 2012, 07:45:00 pm »
Quote
Can you give the commit ID and state what compiler you use in which setup (release/debug, static/dynamic)?
I built and tested on both release and debug, dynamic. I grabbed the snapshot a week ago, so I don't think I know the commit ID, sorry. :( I will rebuild SFML from the current snapshot tomorrow and test again.

Quote
When NDEBUG isn't defined SFML_DEBUG should get defined and any OpenGL errors that occur will be printed to stdout.
I tested in both release and debug didn't see any OpenGL errors, didn't know about SFML_DEBUG tho, I am kinda new to SFML. Will look more carefully for errors, tomorrow.

Thanks for the fast replies, guys!

// Zap

Zapfyr

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Re: RenderTexture won't clear properly
« Reply #12 on: October 09, 2012, 08:01:17 am »
I have now built SFML from the latest snapshot, and the result is the same (running eXpl0it3r code), in both release and debug, using dynamically linking. Didn't see any OpenGL errors or similar. What should I look into next?

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: RenderTexture won't clear properly
« Reply #13 on: October 09, 2012, 05:19:43 pm »
Did you get the SFML examples to work? It could be that clearing the framebuffer doesn't work and it just looks like it is the RenderTexture that is broken. Not being able to clear the framebuffer is probably known best from when a program in Windows hangs so if that really is the case it probably has to do with the operating system.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Zapfyr

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Re: RenderTexture won't clear properly
« Reply #14 on: October 09, 2012, 07:35:04 pm »
Everything seems to work fine, except RenderTexture, the framebuffer clears nicely. :)