SFML community forums
Help => Graphics => Topic started by: Nexus on December 27, 2009, 11:39:21 pm
-
Hi,
I have a question regarding OpenGL. I want to setup a perspective that represents the current sf::View. I have already experimented with gluOrtho2D(), but I am a little bit confused with the new sf::View interface (concerning viewport and rotation).
Is there an easy way to adapt the view in OpenGL?
-
I don't understand what you're trying to do. Do you want to emulate sf::View with your own OpenGL code? For what? Have you looked at the implementation of sf::View?
-
I don't understand what you're trying to do. Do you want to emulate sf::View with your own OpenGL code? For what?
Sorry for my vague description. Yes, I try to write a simple particle system. I want to learn a little bit more about OpenGL (actually, I am not really experienced with it :)) and to combine it with SFML. So I wondered, how I might achieve an SFML-compatible view in OpenGL.
Have you looked at the implementation of sf::View?
Yes, but in sf::View itsself, I found mostly state variables. My confusion arose because in sf::RenderQueue::Render(), the OpenGL projection matrix is reset to the identity before the objects are rendered... I never saw anywhere calls setting up the GL_PROJECTION matrix. I'm not sure whether you're doing all the work manually in Matrix3::SetFromProjection(). Or are you using magic? ;)
-
I build the matrix manually in Matrix3::SetFromProjection, and then it is sent to the render-queue in RenderTarget::Draw:
myRenderQueue.SetProjection(myCurrentView->GetMatrix());
If you look further, you will see that the render-queue transforms every vertices manually before sending them to OpenGL, that's why I use an identity projection matrix for rendering.
-
Okay, thanks a lot for the explanation, now that's clear. :)
Hm, I will try to make sense of your transformation algorithm. In older SFML versions, you used direct OpenGL calls to set the view, didn't you? That code might simplify the matter for me. But the problem of an own implementation is, I must be very careful to ensure that everything is consistent with SFML. On the other hand, when I used sf::Matrix3::SetFromProjection(), I couldn't let OpenGL do the work anymore and I would have to build up an own transformation system. Hm... :?
P.S.: I'm quite curious: Why did you implement your own matrices and transformations? Did OpenGL restrict you too much?
-
I pre-transform all vertices before sending them to OpenGL, so that I can create bigger batches (groups of triangles using the same states, and that can be rendered with a single OpenGL call). Otherwise I would have to render triangles 2 by 2, just for changing the model-view matrix between calls. And as you can see with SFML 1.5, this is pretty inefficent ;)
-
Okay, thanks.
Now I'll try to find out if there is a simple way to setup a compatible view in OpenGL. Probably, you will hear from me again. :)
[Edit] I found a way. I wrote the following function:
void Setup2DView(const sf::View& View)
{
sf::Vector2f Size = View.GetSize();
sf::Vector2f Center = View.GetCenter();
sf::Vector2f Position = Center - Size / 2.f;
// Edit the OpenGL projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Setup translation (according to left-upper corner) and scale
gluOrtho2D(0.f, Size.x, Size.y, 0.f);
glTranslatef(-Position.x, -Position.y, 0.f);
// Setup rotation
glTranslatef(Center.x, Center.y, 0.f);
glRotatef(View.GetRotation(), 0, 0, -1.f);
glTranslatef(-Center.x, -Center.y, 0.f);
}