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

Author Topic: RenderTexture inverts pixels?  (Read 1153 times)

0 Members and 1 Guest are viewing this topic.

gremio

  • Newbie
  • *
  • Posts: 9
    • View Profile
RenderTexture inverts pixels?
« on: March 02, 2014, 07:15:09 pm »
Hi,

I'm trying to make a mask (2d vector of boolean) from a rotated sprite. When I print the mask to the console as 1 for true, and 0 for false, the 1s are never where I would expect them to be. Here is one exemple of a case that I dont understand.

void maskByAngle::buildMasks(bar aBar)
{
        if(aBar.isLeft()){
                int angle = -30;
                Texture wtvTexture;
                Sprite tempSprite;
               
                wtvTexture.loadFromFile("Ressources/bar_left.png");
                tempSprite.setTexture(wtvTexture);
                tempSprite.setOrigin(0,tempSprite.getLocalBounds().height/2);
                while(angle <= 30){
                       
                        RenderTexture newRT;
                        newRT.create(tempSprite.getGlobalBounds().width, tempSprite.getGlobalBounds().height);
                        tempSprite.setPosition(0,0);
                        newRT.draw(tempSprite);
                        Texture myTexture = newRT.getTexture();
                        Image myImage = myTexture.copyToImage();
                        newRT.clear(Color::Black);
                        std::cout << "X = " << myImage.getSize().x << "  Y = " << myImage.getSize().y << endl;
                        vector<vector<bool>> vBBTemp;
                        for(int y = 0; y < myImage.getSize().y; ++y){
                       
                                vector<bool> vBTemp;
                                for(int x = 0; x < myImage.getSize().x; ++x){
                                        if(myImage.getPixel(x,y).a > 200){
                                                vBTemp.push_back(true);
                                                std::cout << 1;
                                        }else{
                                                vBTemp.push_back(false);
                                                std::cout << 0;
                                        }
                                }
                                std::cout << endl;
                                vBBTemp.push_back(vBTemp);
                        }
                        myMasksLeft.push_back(vBBTemp);
                        ++angle;
                }
        }else if(!aBar.isLeft()){
 

In the case I just printed, I get this in the console for the case where the angle = 0.




Even if you see a cout mentionning compensation, I am not applying it. Nevertheless, what I see in the console is not far from what I would expect. I expected the image to be a inverted version of this one. The 1s should be top, not at the bottom of the console output. Since my origin is at half-height, I know I'm suppose to "lose" half of the pixels, but why do I lose the bottom ones? Furthermore, I tried to draw the Sprite to my renderwindow before building my image, and It shows exactly what I would expect to find in my console :



Furthermore, I tried something while writting this post and I found that if I print the image like this :
Texture aTexture;
aTexture.loadFromImage(myImage);
Sprite aSprite(aTexture);
myWin.draw(aSprite);
 

I actually get the same result I got by printing in the console. But why!? Why are the pixels inverted in the image?

Hope I was clear.



******************EDIT*********************
Sorry about the language, I didnt notice it was the french section of the forum (I am french canadian....)

Anyways, I tested something using renderTexture and I now know that render texture inverts the pixels. Here is the code I used to test (I tried to make it testable for others but you'll need your own renderwindow) :


        Sprite utilitySprite;
        Texture utilityTexture;

        utilityTexture.loadFromFile("Ressources/meteor.png");
        utilitySprite.setTexture(utilityTexture);

        utilitySprite.setPosition(200,0);
        MyWin.draw(utilitySprite);
       
        RenderTexture newRT;
        Image newImage;
        Texture newTexture;
        utilitySprite.setPosition(0,0);
        newRT.create(utilitySprite.getLocalBounds().width, utilitySprite.getLocalBounds().height);
        newRT.clear(Color::Yellow);
        newRT.draw(utilitySprite);
        newTexture = newRT.getTexture();
        newImage = newTexture.copyToImage();
        Texture aTexture;
        aTexture.loadFromImage(newImage);
        //Sprite newSprite(aTexture);
        Sprite newSprite(newRT.getTexture());
        newSprite.setPosition(400,0);
        MyWin.draw(newSprite);
       

 

here is what was rendered :



Am I doing something wrong ? or should I flip the texture myself?
« Last Edit: March 02, 2014, 08:05:35 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: RenderTexture inverts pixels?
« Reply #1 on: March 02, 2014, 08:06:24 pm »
Please read the doc and tutorial. It's explained there.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10914
    • View Profile
    • development blog
    • Email
AW: RenderTexture inverts pixels?
« Reply #2 on: March 02, 2014, 09:01:41 pm »
aka rt.display()
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/