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

Author Topic: OpenGL: Setup perspective according to sf::View  (Read 4410 times)

0 Members and 1 Guest are viewing this topic.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
OpenGL: Setup perspective according to sf::View
« 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?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
OpenGL: Setup perspective according to sf::View
« Reply #1 on: December 28, 2009, 12:05:14 am »
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?
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
OpenGL: Setup perspective according to sf::View
« Reply #2 on: December 28, 2009, 03:56:26 pm »
Quote from: "Laurent"
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.

Quote from: "Laurent"
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? ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
OpenGL: Setup perspective according to sf::View
« Reply #3 on: December 28, 2009, 04:02:13 pm »
I build the matrix manually in Matrix3::SetFromProjection, and then it is sent to the render-queue in RenderTarget::Draw:
Code: [Select]
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.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
OpenGL: Setup perspective according to sf::View
« Reply #4 on: December 28, 2009, 04:31:33 pm »
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?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
OpenGL: Setup perspective according to sf::View
« Reply #5 on: December 28, 2009, 05:14:13 pm »
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 ;)
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
OpenGL: Setup perspective according to sf::View
« Reply #6 on: December 28, 2009, 09:40:50 pm »
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:
Code: [Select]
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);
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: