SFML community forums

General => General discussions => Topic started by: Caribou on October 03, 2011, 08:03:46 pm

Title: Origin at bottom left (SFML2!?)
Post by: Caribou on October 03, 2011, 08:03:46 pm
Hello,

I know this isn't a feature of SFML2 but since there are some changes in the way sfml works i was wondering if there's an easy/not-ugly way to do it.
I've read most of the topics here and googling about that but nothing seems to work as i would, knowing that i'm really beginning with openGL...

A subject on Stack Overflow talks about that : http://stackoverflow.com/questions/6561604/mirroring-the-y-axis-in-sfml (note that i don't actually need mirroring, i just want to change the origin)

Here is the snippet:
Code: [Select]
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glScalef(1.0f, -1.0f, 1.0f);


And it doesn't seem to work here. Then Zagabar from the irc channel helped me with this code:

Code: [Select]
glMatrixMode(GL_PROJECTION);
glViewport(0, 0, w, h);
glLoadIdentity();
gluOrtho2D(0.0f, (float)w, 0.0f, (float)h); // set origin to bottom left corner
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


But that's mirroring everything, so i have to flip each of my sprites, that's ok since i'm subclassing sf::Sprite but then the text is mirrored as well and i can't find a way to reset the projection.

So basically my question is, how would you change the origin to the bottom left corner without having to break everything ?
Title: Origin at bottom left (SFML2!?)
Post by: Laurent on October 03, 2011, 08:09:14 pm
I don't think this can be done globally. You must handle it on each entity.

For SFML drawables it's easy: call SetOrigin.

For OpenGL primitives it should be fairly easy too, it's just a translation to apply before rendering them.
Title: Re: Origin at bottom left (SFML2!?)
Post by: gsaurus on October 04, 2011, 01:07:59 pm
Quote from: "Caribou"
But that's mirroring everything, so i have to flip each of my sprites, that's ok since i'm subclassing sf::Sprite but then the text is mirrored as well and i can't find a way to reset the projection.

Negative scaling on sf::Drawables isn't possible yet but I think it's about to change soon, so you'll be able to mirror all your drawable entities (including text) with SetScale or similar.

Speaking of that, currently sf::View doesn't allow mirrored display, there is rotation, center and position getters/setters, and even zoom, but any scale operations. Will sf::View be a Transformable Laurent? Mirrored views are sometimes useful.

If so, you should be able to achieve what you want by mirroring all entities and using a mirrored View, no openGL code needed. But for now I guess you have to do it manually :roll:
Title: Origin at bottom left (SFML2!?)
Post by: Laurent on October 04, 2011, 01:18:18 pm
I think it's wrong to talk about mirroring things. All he wants to do is to change the origin, so basically it's just a matter of adding a translation. Or, if he's building its own geometry himself, he just needs to define vertices toward the top/right of the origin instead of bottom/left. That's it.

Quote
Negative scaling on sf::Drawables isn't possible yet but I think it's about to change soon

Yep.

Quote
Will sf::View be a Transformable Laurent? Mirrored views are sometimes useful

I don't think so. What kind of use case can you find for mirrored views?
Title: Origin at bottom left (SFML2!?)
Post by: gsaurus on October 04, 2011, 01:58:37 pm
When I hear about origin at bottom left I associate with y axis growing from bottom to top, so inversed/mirrored. But if it's just a matter of Origin then yeah, it's a simple translation. Y will still "grow negatively" from bottom to top anyway.

Quote from: "Laurent"
What kind of use case can you find for mirrored views

Mirrors :lol: It's not that useful indeed, but why not? It would be more consistent/simple? Everything that can be rotated, moved etc is a Transformable. Views can be transformed in all ways a Transformable can, but scaled. So it's not a Transformable but the API looks almost the same. It is transformable but not a sf::Transformable because of one operation.
Although I understand it can cause problems because of sf::Rect
Title: Origin at bottom left (SFML2!?)
Post by: Laurent on October 04, 2011, 02:04:09 pm
Quote
Views can be transformed in all ways a Transformable can, but scaled. So it's not a Transformable but the API looks almost the same. It is transformable but not a sf::Transformable because of one operation.

I don't get it. Which operation?

Anyway, you're right, I could remove the whole sf::View class and use a sf::Transform instead (even more flexible than a sf::Transformable) -- and let the viewport live alone.

But it's still the same problem, to keep the S of SFML I need to provide a higher-level API (otherwise people can just use OpenGL). However it might be a good idea to have a closer look at the sf::View API. It can probably be improved/extended.
Title: Origin at bottom left (SFML2!?)
Post by: Caribou on October 04, 2011, 02:14:03 pm
Thanks for your answers guys.

I'm trying to translate again then, here is my piece of code :

Code: [Select]
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600, 32), "SFML origin");
    sf::Texture texture;
    texture.LoadFromFile("char.png");
    sf::Sprite myChar;
    myChar.SetTexture(texture);

    while (window.IsOpened())
    {
        window.Clear(sf::Color::White);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glScalef(1.0f, -1.0f, 1.0f);

        window.Draw(myChar);
        window.Display();
    }
}


and my character is not visible anymore.

That's the case in the sample project, but on my project i'm actually seeing some sort of zooming... but still, that's not working.

What did i miss ?
Title: Origin at bottom left (SFML2!?)
Post by: Laurent on October 04, 2011, 02:24:48 pm
Quote
Thanks for your answers guys

... which you clearly didn't understand :lol:

Forget about mirroring, you can't find a solution that can be applied globally. You must adjust the origin of each entity separately.

Code: [Select]
myChar.SetOrigin(myChar.Getsize());
Title: Origin at bottom left (SFML2!?)
Post by: Caribou on October 04, 2011, 02:47:58 pm
Quote from: "Laurent"
Quote
Thanks for your answers guys

... which you clearly didn't understand :lol:


Baaahhh, it thought it was kinda moving the axes. What my piece of code is doing then ?

Quote
Forget about mirroring, you can't find a solution that can be applied globally. You must adjust the origin of each entity separately.

Code: [Select]
myChar.SetOrigin(myChar.Getsize());
 
I'm gonna need it but their positions are still relatives to the top left corner, how do i change this behaviour !?
Title: Origin at bottom left (SFML2!?)
Post by: Laurent on October 04, 2011, 02:58:46 pm
Quote
I'm gonna need it but their positions are still relatives to the top left corner, how do i change this behaviour !?

Oh, ok I see. Sorry I had forgotten that part of the problem :)

You can't change this. You could scale and translate the projection matrix to invert the Y axis, but then the behaviour of SFML would be undefined (don't forget that it has its own states, and especially its own projection).
Title: Origin at bottom left (SFML2!?)
Post by: Lee R on October 05, 2011, 03:00:33 am
Quote from: "gsaurus"
Quote from: "Laurent"
What kind of use case can you find for mirrored views

Mirrors :lol:


+1

My current project is based on the concept of a mirrored world. Currently, the world is drawn onto a RenderImage which is then drawn onto the window via an axis-flipped Sprite.
Title: Origin at bottom left (SFML2!?)
Post by: pdinklag on January 15, 2012, 12:21:46 pm
Quote from: "Laurent"
Quote
Views can be transformed in all ways a Transformable can, but scaled. So it's not a Transformable but the API looks almost the same. It is transformable but not a sf::Transformable because of one operation.

I don't get it. Which operation?

Anyway, you're right, I could remove the whole sf::View class and use a sf::Transform instead (even more flexible than a sf::Transformable) -- and let the viewport live alone.

But it's still the same problem, to keep the S of SFML I need to provide a higher-level API (otherwise people can just use OpenGL). However it might be a good idea to have a closer look at the sf::View API. It can probably be improved/extended.

Any news or ideas here? Because I was just wondering why sf::View is not a sf::Transformable.
Title: Origin at bottom left (SFML2!?)
Post by: Laurent on January 15, 2012, 04:24:21 pm
I admit that sf::View would need to be redesigned, with something closer to the Transform/Transformable concepts. But to be honest, I don't want to start doing this, now I want to release SFML 2.

I'll see if I can do something in a future minor revision.