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

Author Topic: Using RenderTexture correctly  (Read 1271 times)

0 Members and 1 Guest are viewing this topic.

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Using RenderTexture correctly
« on: August 30, 2019, 06:28:04 am »
Hello,

I am having a difficult time understanding how to do this properly. I am following a tutorial on Line Of Sight (not SFML) and he uses a light source in photoshop as a .png and gathers the pixels based on the line of sight to give it a nice glow.

In his example he uses (a little SDL Engine) 
        Sprite buffLightRay;
        Sprite buffLightTex;
        Sprite sprLightCast;

He initializes buffLightRay to the light_cast.png and creates buffLightTex and sprLightCast to the size of the window.

When he gets to his draw method, he does SetDrawTarget(buffLightTex) and clears the buffer to black. and draws sprLightCast to the position of the cursor.

He then SetsDrawTarget(buffLightRay) and clears the buffer to blank. After some shape drawings towards the bottom, he has a loop like this

// Wherever rays exist in ray sprite, copy over radial light sprite pixels
                        SetDrawTarget(nullptr);
                        for (int x = 0; x < ScreenWidth(); x++)
                                for (int y = 0; y < ScreenHeight(); y++)
                                        if (buffLightRay->GetPixel(x, y).r > 0)
                                                Draw(x, y, buffLightTex->GetPixel(x, y));


Now I've had a lot of trouble trying to translate this into SFML. I made an sf::RenderTexture renderLight and sf::RenderTexture renderRay. I made the light_cast.png an sf::Image (so I can get the pixels).

I started off creating everything the screen as he does. When it comes to the first "SetDrawTarget" I call
renderLight.clear()
sprLightCast.setPosition(sourceX, sourceY)  (already set the origin)
renderLight.draw(sprLightCast)
renderLight.display();

renderRay.clear()
...draw triangle fans loops with renderRay
renderRay.display()

I then use

renderSprite.setTexture(rt.getTexture());
m_window->draw(renderSprite);

renderSprite.setTexture(renderTriangle.getTexture());
m_window->draw(renderSprite);

 

From here I do the loop involving getting the pixels
for (int x = 0; x < WINDOW_SIZE.x; x++) {
                        for (int y = 0; y < WINDOW_SIZE.y; y++)
                                if (buffLightTex.getPixel(x, y).r > 0) {
                                        auto c = buffLightTex.getPixel(x, y);
                                        buffLightRay.setPixel(x, y, c);
                                        texLightCast.update(buffLightTex);
                                        sf::Sprite sprite{texLightCast};
                                        m_window->draw(sprite);
                                }
                }
 
I have a feeling this is where the problem is. But it moves around a 500x500 (assuming) white image when I right click (which is the "light") and doesn't refresh correctly after letting go of right click.

Here is the github to the project I am learning from. https://github.com/OneLoneCoder/olcPixelGameEngine/blob/master/OneLoneCoder_PGE_ShadowCasting2D.cpp

This is all under the draw method. I just need to know how to properly do this correctly if possible.
Thanks, sorry for the confusion.

If you need more information to help, please let me know.