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

Author Topic: Moving camera causes graphic glitch. My investigation.  (Read 3955 times)

0 Members and 1 Guest are viewing this topic.

Carlitox

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Moving camera causes graphic glitch. My investigation.
« on: February 18, 2016, 01:35:28 am »
I use SFML-2.3.2-windows-vc14-32-bit in Visual Studio 15. It happens in my PC and in my laptop. Look to the brick joints, when I move the character the joints changes the widths. Look the picture, different sizes.



And video in movement:



 

This not happens when I resize the window manually. That could be a clue. Only making the window bigger in the resize.



Look the tile map in Tiled. It not happens only in the edges of the tile because. The joints are where it's more visible but there areanother  pixels that changes width.










It seems like a graphics glitch but maibe it's the way I set the textures and the positions. ¿Precision problem? Here you have my code where I set the Quads of each tile.

std::vector<int> map = all_map[layer_id];
        LayerObject layer;
        layer.texture.loadFromFile(spritesheet);
        layer.entity_vertex.setPrimitiveType(sf::Quads);
        layer.entity_vertex.resize(width * height * 4); // Each tile have 4 vertex
        layer.layer_id = layer_id;
        //sf::Vector2u TS(tmx_info.tile_size.x, tmx_info.tile_size.y); //TILE_SIZE
       
        sf::Vector2u TS(16, 16); //TILE_SIZE

        for (unsigned int i = 0; i < height; ++i)
        {
                for (unsigned int j = 0; j < width; ++j)
                {
                        unsigned int tile_pos = j + i*width;
                        unsigned int tile_texture_num = 0;

                        //TR = Tile Row // TC =Tile column
                        unsigned int TR = map[tile_pos] / (layer.texture.getSize().x / TS.x); //TILE_ROW
                        unsigned int TC = ((map[tile_pos]) % (static_cast<int>(layer.texture.getSize().x / TS.x))); //TILE_COLUMN

                        sf::Vertex* quad = &layer.entity_vertex[tile_pos * 4];


                        quad[0].position = sf::Vector2f( j * TS.x ,      i * TS.y);
                        quad[1].position = sf::Vector2f((j + 1) * TS.x,  i * TS.y);
                        quad[2].position = sf::Vector2f((j + 1) * TS.x, (i + 1) * TS.y);
                        quad[3].position = sf::Vector2f( j * TS.x ,     (i + 1) * TS.y);
                       
                       
                        quad[0].texCoords = sf::Vector2f( TC * TS.x ,       TR * TS.y );
                        quad[1].texCoords = sf::Vector2f((TC + 1) * TS.x ,  TR * TS.y );
                        quad[2].texCoords = sf::Vector2f((TC + 1) * TS.x , (TR + 1) * TS.y);
                        quad[3].texCoords = sf::Vector2f( TC * TS.x ,      (TR + 1) * TS.y);
                       
                }
        }

       
        map_layers.push_back(layer);

« Last Edit: February 18, 2016, 02:05:41 am by Carlitox »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Moving camera causes graphic glitch. My investigation.
« Reply #1 on: February 18, 2016, 02:31:35 am »
It looks like you're scaling those textures to a non-power of two. If you're scaling (up or down), if the scale factor is not doubling or halving, the pixels are distributing unevenly.
Consider setting your scaling to a power of two, trying turning on smooth for the texture, or render to a render texture and then scale that (possibly with smooth).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Carlitox

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Moving camera causes graphic glitch. My investigation.
« Reply #2 on: February 18, 2016, 03:02:45 am »
Thank you. That was the solution, I was scaling the map by 2.6 and I changed to 2.

I imagined that I was calculing the scale badly, just a 20 seconds thought. Probably I couldn't find the solution by myself.

Carlitox

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Moving camera causes graphic glitch. My investigation.
« Reply #3 on: February 18, 2016, 03:53:17 pm »
It looks like you're scaling those textures to a non-power of two. If you're scaling (up or down), if the scale factor is not doubling or halving, the pixels are distributing unevenly.
Consider setting your scaling to a power of two, trying turning on smooth for the texture, or render to a render texture and then scale that (possibly with smooth).

I found also this info: http://gamedev.stackexchange.com/questions/58195/sfml-fail-to-load-image-as-texture

Quote
OpenGL does not like loading in textures that are not to the power of 2, some loaders add padding to make up to these numbers, but you cannot account for all libraries you use to achieve this. Try scaling the image to 512x512 as opposed to 510x510 and change 125 to 128, usually the texture will load in this instance in graphics libraries and you will experience tearing and distortion across the surface of the model, as it moves in relation to the view port.


Does it mean that when I load textures I must use images always with power of 2? In my case the tileset texture have this size 144x80, if the above info it's true I must change the texture size to 256x128. Isn't it?

Does SFML solve the OpenGl problem?
« Last Edit: February 18, 2016, 03:58:35 pm by Carlitox »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Moving camera causes graphic glitch. My investigation.
« Reply #4 on: February 19, 2016, 12:39:00 am »
I'm not sure. I think that OpenGL may specify that textures should be powers of two but many implementation (the drivers) may allow more flexible sizes. It could be that older OpenGL only support texture sizes of powers of 2 and later ones are more flexible. It should be simple enough to search this if you're interested enough  ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Carlitox

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Moving camera causes graphic glitch. My investigation.
« Reply #5 on: February 19, 2016, 01:57:34 am »
Since Opengl 2.0 it's not necessary to use power of 2 textures.

 

anything