SFML community forums
Help => Graphics => Topic started by: sNIk on January 17, 2012, 10:26:32 am
-
Hello there!
i think, i have found a bug today. first of all, the code snippet:
...
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:
(http://img585.imageshack.us/img585/7653/screenshotvl.th.png) (http://imageshack.us/photo/my-images/585/screenshotvl.png/)
-
Can you please provide a complete and minimal source code that reproduces the problem, so that I can do some tests?
-
yes, sir. ;)
/* 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;
}
-
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.
-
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?
-
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.
-
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.
-
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. ;)
-
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.
-
OK, that makes sense. I hope this thread will help somebody in the future. :)