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

Author Topic: Colour interpolation during scaling  (Read 2009 times)

0 Members and 1 Guest are viewing this topic.

dassie

  • Newbie
  • *
  • Posts: 13
    • View Profile
Colour interpolation during scaling
« on: September 02, 2017, 09:13:04 am »
Apologies if this comes off as spamming. I asked my question in this thread, but the title is unrelated so it would be missed.
https://en.sfml-dev.org/forums/index.php?topic=22453.0

The issue I'm having is that I use a scaling transform when drawing a vertex array (of points) to a window. Like so:

   window->clear();
   window->draw(*varr, *transform);
   window->display();

The clear() fills the window with black, then the draw() call "stretches" my vertex array without interpolating the colours in between. So what's supposed to be a white background ends up looking grey (i.e. spaced apart white pixels overtop of a black background).

Screenshot:
http://i.imgur.com/J7z9k1r.png

Is there a way to tell the transform to interpolate the colors in between? In some examples related to sf::Transform, the scaling does interpolate colors. Is it due to the fact that I'm using PrimitiveType::Points and this is why it can't do the interpolation?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Colour interpolation during scaling
« Reply #1 on: September 02, 2017, 10:23:41 am »
If you get pixel information, I'd probably go with filling a sf::Image (or std::vector<sf::Uint8>) with the pixel information, the call update on a texture and finally rendrr that to the screen. This way, your image data will be stretched accordingly to the view.

As for your, it's not really possible that way. Vertex points are really points, so they don't scale based on a transform, instead one would, as suggested in the other thread, simply draw everything 1:1 and then scale the view.
« Last Edit: September 02, 2017, 11:05:15 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

dassie

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Colour interpolation during scaling
« Reply #2 on: September 02, 2017, 10:10:00 pm »
Cool, so I went with that first thing you mentioned. It works :)

However, it's a bit round-about to put all my data into the image, then update the texture from the image. I did a CPU profile and a significant amount of my "UpdateWindow" function was being spent on the Image::setPixel() function.

I tried a different approach to get a little more speed out of it. I maintain a Uint8*, update it with my emulator's buffer, then call texture::update before drawing it to the screen

 
        for (int i = 0; i < width; i++)
        {
                for (int j = 0; j < height; j++)
                {    
                        // sf_frame_buffer is the Uint8*
                        int index = i * width + j * 4;
                        sf_frame_buffer[index + 0] = em_frame_buffer[i][j].Red;
                        sf_frame_buffer[index + 1] = em_frame_buffer[i][j].Green;
                        sf_frame_buffer[index + 2] = em_frame_buffer[i][j].Blue;
                        sf_frame_buffer[index + 3] = 0xFF;
                }
        }

        window->clear();
        texture->update(sf_frame_buffer);
        window->draw(*rectangle, *transform);
        window->display();

Hope that makes sense, what I'm trying to do.

The result ends up looking really wonky. The image looks like its squeezed vertically.
http://i.imgur.com/PJUWUdU.png

I don't see anything wrong with the code. Here's the part where I initialize all my objects:
Code: [Select]
VideoMode vm(width * integer_scale, height * integer_scale);
window = new RenderWindow(vm, title);
window->clear(Color::White);
window->display();

transform = new Transform();
transform->scale(scale_factor, scale_factor);

rectangle = new RectangleShape(Vector2f(width, height));
texture = new Texture();
texture->create(width, height);
rectangle->setTexture(texture);

// Each pixel has an 8bit R, G, B and A value. Hence 4 times the number of pixels.
sf_frame_buffer = new Uint8[4 * width * height];

Any idea what I'm doing wrong?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Colour interpolation during scaling
« Reply #3 on: September 02, 2017, 10:25:47 pm »
Quote
int index = i * width + j * 4;
Should be
int index = (i * width + j) * 4;
Laurent Gomila - SFML developer

dassie

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Colour interpolation during scaling
« Reply #4 on: September 03, 2017, 02:28:08 am »
Of course! Thank you very much, it works now :)

 

anything