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

Author Topic: [SOLVED] Possible Bug with RenderTexture coordinates?  (Read 2912 times)

0 Members and 1 Guest are viewing this topic.

sNIk

  • Newbie
  • *
  • Posts: 10
    • View Profile
[SOLVED] Possible Bug with RenderTexture coordinates?
« on: January 17, 2012, 10:26:32 am »
Hello there!

i think, i have found a bug today. first of all, the code snippet:

Code: [Select]

...

sf::VertexArray primitives( sf::Lines, 4 );

// x-axis
primitives[0].Position = sf::Vector2f( 0, 0 );
primitives[1].Position = sf::Vector2f( width, 0 );

// y-axis
primitives[2].Position = sf::Vector2f( 0, 0 );
primitives[3].Position = sf::Vector2f( 0, height );

...

// draw to texture first
texture.Draw( primitives );

...

// then draw it to the window
window.Draw( spriteWithMyTexture );

...



i expected to see nothing, but the x-axis primitive is shifted exactly 1 pixel down. if i draw the primitives directly to the window everything is ok. this is how it looks like:

OS: Gentoo Linux
Kernel: 2.6.39

GPU: Radeon HD 3650
CPU: Intel(R) Core(TM)2 Duo CPU     T9400  @ 2.53GHz

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED] Possible Bug with RenderTexture coordinates?
« Reply #1 on: January 17, 2012, 10:30:11 am »
Can you please provide a complete and minimal source code that reproduces the problem, so that I can do some tests?
Laurent Gomila - SFML developer

sNIk

  • Newbie
  • *
  • Posts: 10
    • View Profile
[SOLVED] Possible Bug with RenderTexture coordinates?
« Reply #2 on: January 17, 2012, 10:53:26 am »
yes, sir. ;)

Code: [Select]

/* INCLUDES
***********************************************************************************************************************/
#include <SFML/Graphics.hpp>


/* MAIN
***********************************************************************************************************************/
int main()
{
    // create window
    sf::RenderWindow window;
    sf::VideoMode videoMode;
    window.Create( videoMode.GetFullscreenModes()[0], "Test", sf::Style::Fullscreen );

    // create texture target
    sf::RenderTexture textureTarget;
    textureTarget.Create( window.GetWidth(), window.GetHeight() );

    // create primitives
    sf::VertexArray lines( sf::Lines, 4 );

    lines[0].Position = sf::Vector2f( 0, 0 );
    lines[1].Position = sf::Vector2f( window.GetWidth(), 0 );

    lines[2].Position = sf::Vector2f( 0, 0 );
    lines[3].Position = sf::Vector2f( 0, window.GetHeight() );

    // draw primitives once
    textureTarget.Clear( sf::Color(57,57,57) );
    textureTarget.Draw( lines );
    textureTarget.Display();

    // create sprite
    sf::Sprite sprite;
    sprite.SetTexture( textureTarget.GetTexture() );

    // main loop
    while( window.IsOpen() )
    {
        // process input
        sf::Event event;
        while(window.PollEvent(event))
        {
            if ( event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Keyboard::Escape) {
                window.Close();
            }
        }

        // clear first
        window.Clear(sf::Color::Blue);

        // draw the sprite
        window.Draw( sprite );

        // display
        window.Display();
    }

    return EXIT_SUCCESS;
}
OS: Gentoo Linux
Kernel: 2.6.39

GPU: Radeon HD 3650
CPU: Intel(R) Core(TM)2 Duo CPU     T9400  @ 2.53GHz

sNIk

  • Newbie
  • *
  • Posts: 10
    • View Profile
[SOLVED] Possible Bug with RenderTexture coordinates?
« Reply #3 on: January 17, 2012, 11:15:32 am »
by the way, i built this code on a x86_64 windows 7 machine at work. i did not test it on my gentoo box, yet.
OS: Gentoo Linux
Kernel: 2.6.39

GPU: Radeon HD 3650
CPU: Intel(R) Core(TM)2 Duo CPU     T9400  @ 2.53GHz

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED] Possible Bug with RenderTexture coordinates?
« Reply #4 on: January 17, 2012, 11:38:00 am »
Ok thanks :)

But I don't understand what's wrong. Why do you expect to see nothing, and why do you say that it's shifted?
Laurent Gomila - SFML developer

sNIk

  • Newbie
  • *
  • Posts: 10
    • View Profile
[SOLVED] Possible Bug with RenderTexture coordinates?
« Reply #5 on: January 17, 2012, 12:26:05 pm »
on the one hand, if you draw a simple line (x-axis) at P1(0, 0), P2(width, 0) there should be no line at all but the line appears at P1(0, 1), P2(width, 1). on the other hand, the line (y-axis) at P1(0,0), P2(0, height) is not shown, as supposed. if you move the line along the x-axis by 1 pixel, it gets visible.


another example:

x-axis line: P1(1,1), P2(width,1)
y-axis line: P1(1,1), P2(1,height)

you would expect both lines to be drawn in the first row (x-axis) and in the first column (y-axis) of the target. but the x-axis is drawn in the 2. row, thats the problem. :)

this happens only with RenderTexture. if you draw the same vertices directly to the window, everything is OK.
OS: Gentoo Linux
Kernel: 2.6.39

GPU: Radeon HD 3650
CPU: Intel(R) Core(TM)2 Duo CPU     T9400  @ 2.53GHz

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED] Possible Bug with RenderTexture coordinates?
« Reply #6 on: January 17, 2012, 12:38:25 pm »
The first row is the number 0, why would you expect coordinate 0 to be out of the target?

So the problem is that your Y-axis primitive is not shown, actually.

You should avoid integer coordinates when you draw lines. If you want a 1-pixel wide line that fills the first row or column of pixels, ie. the range [0, 1], it's coordinate must probably be 0.5 since it represents the "center" of the line.

Try this and let me know if it solves your problem.
Laurent Gomila - SFML developer

sNIk

  • Newbie
  • *
  • Posts: 10
    • View Profile
[SOLVED] Possible Bug with RenderTexture coordinates?
« Reply #7 on: January 17, 2012, 01:35:09 pm »
Quote from: "Laurent"
The first row is the number 0, why would you expect coordinate 0 to be out of the target?

So the problem is that your Y-axis primitive is not shown, actually.

You should avoid integer coordinates when you draw lines. If you want a 1-pixel wide line that fills the first row or column of pixels, ie. the range [0, 1], it's coordinate must probably be 0.5 since it represents the "center" of the line.

Try this and let me know if it solves your problem.


that's not the problem. i don't want to draw the lines along the axes right at the border at all. i just discovered this strange behavior while playing around and wanted to report it. :)

just try to draw a horizontal line at x(0.f) and a vertical line at y(0.f). you'll see the horizontal line but not the vertical one if you draw them to a texture. there must be something wrong.

i hope you understood my intention now. ;)
OS: Gentoo Linux
Kernel: 2.6.39

GPU: Radeon HD 3650
CPU: Intel(R) Core(TM)2 Duo CPU     T9400  @ 2.53GHz

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED] Possible Bug with RenderTexture coordinates?
« Reply #8 on: January 17, 2012, 01:58:15 pm »
Although it may seem inconsistent, I'm pretty sure it's not a "bug" but rather the effect of the OpenGL rasterization rules.

When you define a line to be at an integer coordinate, it is exactly between two rows or columns of pixels. Therefore there's no preferred choice, either side can be equally chosen.

I suspect that the behaviour is consistent, and that X and Y don't behave the same because SFML reverts the Y axis compared to the OpenGL defaults. So with the default coordinate system it would probably be consistent.
Laurent Gomila - SFML developer

sNIk

  • Newbie
  • *
  • Posts: 10
    • View Profile
[SOLVED] Possible Bug with RenderTexture coordinates?
« Reply #9 on: January 17, 2012, 02:06:27 pm »
OK, that makes sense. I hope this thread will help somebody in the future. :)
OS: Gentoo Linux
Kernel: 2.6.39

GPU: Radeon HD 3650
CPU: Intel(R) Core(TM)2 Duo CPU     T9400  @ 2.53GHz

 

anything