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

Author Topic: SFML updateTexture error  (Read 1520 times)

0 Members and 1 Guest are viewing this topic.

MenosGrandes

  • Newbie
  • *
  • Posts: 7
    • View Profile
SFML updateTexture error
« on: November 02, 2015, 07:11:07 pm »
Im on Windows 8.1 with MinGw32 4.8.1-4 using SFML 2.1. Because all precompiled versions don't work, but it's not the point.

I have
    m_pixels = new Colour[width*height];
 
where Colour have     
 unsigned char r,g,b,a;
Now I'm trying to rewrite all pixels from m_pixels to   
 sf::Uint8 * pixels = new sf::Uint8[img_size.x*img_size.y*4];
 
using
void RenderTarget::rewritePixelForTexture(d_type::Bubyte* pixels)
{
    for (d_type::Bint i=0; i<m_size.x*m_size.y ; i++)
    {
        d_type::Bint k=i*4;
        pixels[k+3]=m_pixels[i].a;
        pixels[k+2]=m_pixels[i].r;
        pixels[k+1]=m_pixels[i].g;
        pixels[k]  =m_pixels[i].b;

    }

}

Whole main.cpp :
sf::RenderWindow window(sf::VideoMode(img_size.x, img_size.y), "RENDERER!");

    sf::Texture texture;
    if (!texture.create(img_size.x, img_size.y))
    {
        std::cout<<"ERROR WITH TEXTURE\n";
    }

    sf::Sprite sprite(texture);

    sf::Uint8 * pixels = new sf::Uint8[img_size.x*img_size.y*4];
    sf::Clock FPSClock;
    std::stringstream ss;
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }


        file->rewritePixelForTexture(pixels);
        texture.update(pixels);

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

        window.display();

        ss<<getFPS(FPSClock.restart());

       window.setTitle(ss.str());
       ss.str("");


    }
And now to the core. This program supose to be softwere renderer, so NOT USING OPENGL\DIRECTX to render objects. I'm using the SFML only for window. And I have class to draw to file, simple TGA


void RenderTarget::drawToFile(std::string m_filename)
{
    //Error checking
    if (m_size.x <= 0 || m_size.y <= 0)
    {
        std::cout << "Image size is not set properly\n";
        return;
    }

    std::ofstream o(m_filename.c_str(), std::ios::out | std::ios::binary);

    //Write the header
    o.put(0);
    o.put(0);
    o.put(2);                         /* uncompressed RGB */
    o.put(0);
    o.put(0);
    o.put(0);
    o.put(0);
    o.put(0);
    o.put(0);
    o.put(0);           /* X origin */
    o.put(0);
    o.put(0);           /* y origin */
    o.put((m_size.x  & 0x00FF));
    o.put((m_size.x & 0xFF00) / 256);
    o.put((m_size.y  & 0x00FF));
    o.put((m_size.y  & 0xFF00) / 256);
    o.put(32);                        /* 24 bit bitmap */
    o.put(0);

    //Write the pixel data
    for (d_type::Bint i=0; i<getSizePixels() ; i++)
    {
        o.put(m_pixels[i].b);
        o.put(m_pixels[i].g);
        o.put(m_pixels[i].r);
        o.put(m_pixels[i].a);
    }

    //close the file
    o.close();
}
And this is the resault. The triangle is in position:
Vector3Bf(0,0,0),Vector3Bf(0.5f,0.5f,0),Vector3Bf(0.5f,0,0)
But the file and the Texture/Sprite are different.

And my question is simple.. WHY?....
https://imgur.com/a/lb8L5

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML updateTexture error
« Reply #1 on: November 02, 2015, 07:59:35 pm »
Obviously, the triangle is upside-down and R/B color channels are swapped. So either change something in the file header, or swap pixels lines and R/B components when you write them.
Laurent Gomila - SFML developer

MenosGrandes

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: SFML updateTexture error
« Reply #2 on: November 02, 2015, 08:07:56 pm »
Dispise the colors, why the triangle is upside down?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML updateTexture error
« Reply #3 on: November 02, 2015, 08:38:26 pm »
I don't know, it's your job to find out this kind of stuff ;) It's either your own code that inverts rows, or the TGA format that doesn't use the same convention than SFML. Check by yourself.
Laurent Gomila - SFML developer

MenosGrandes

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: SFML updateTexture error
« Reply #4 on: November 02, 2015, 08:42:07 pm »
Point (0,0,0) is in the center of the view space. The Triangle in TGA is correctly drawn, but when I'm rewriting the data to the SFML the texture flip out..