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

Author Topic: mulitple sf::window and OpenGL  (Read 3065 times)

0 Members and 1 Guest are viewing this topic.

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
mulitple sf::window and OpenGL
« on: December 31, 2010, 12:26:00 pm »
hi if i have multiple sf::Window, can they be on different threads? and if I am to call glBegin() glEnd() from a different thread will it work?

e,g
thread1 : sf::Window A
thread2 : sf::Window B
thread3 : sf::Window C
thread4 : glBegin()/glEnd() other opengl commands

is this allowed?? or mus all sf::Window(s) reside on the same thread along with all opengl commands?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
mulitple sf::window and OpenGL
« Reply #1 on: December 31, 2010, 12:53:06 pm »
Quote
hi if i have multiple sf::Window, can they be on different threads?

Yes.

Quote
and if I am to call glBegin() glEnd() from a different thread will it work?

It will work, but a window cannot be active on two threads at the same time. So you must call window.SetActive(false) from the initial thread before you can call window.SetActive(true) on the other thread and then call glBegin etc. Be careful, some functions such as Display() automatically activate the window.
Laurent Gomila - SFML developer

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
mulitple sf::window and OpenGL
« Reply #2 on: December 31, 2010, 01:02:25 pm »
ahh thanks, then I guess opengl will then draw to which ever window is active then??

If 2 windows on separate threads are active at the same time, would it cause an error? or what would happen? as in would an exception be thrown or would any error flag be set?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
mulitple sf::window and OpenGL
« Reply #3 on: December 31, 2010, 01:06:49 pm »
OpenGL shares nothing between threads, when you activate a context it is active only on the current thread. So you can have multiple contexts active on multiple threads at the same time, there's no problem.
Laurent Gomila - SFML developer

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
mulitple sf::window and OpenGL
« Reply #4 on: December 31, 2010, 01:13:24 pm »
Hi Laurent, I don't quite understand what you mean, could you explain it abit further?

Does that mean, that each thread with a different sf::Window have a different rendering context?

say I have a function with draw a cube, basically a glBeing, glEnd pair and some glVertex3f. How would I know which window will it appear on?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
mulitple sf::window and OpenGL
« Reply #5 on: December 31, 2010, 01:21:36 pm »
Quote
Does that mean, that each thread with a different sf::Window have a different rendering context?

Absolutely. Calling wglGetCurrentContext() on different threads at the same time will return a different result for each thread.

Quote
say I have a function with draw a cube, basically a glBeing, glEnd pair and some glVertex3f. How would I know which window will it appear on?

The one you activated in this thread before calling your OpenGL functions.
Laurent Gomila - SFML developer

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
mulitple sf::window and OpenGL
« Reply #6 on: January 05, 2011, 08:04:01 pm »
hi, sorry for reviving this thread. Recently I have gotten around to write a simple app with multiple windows on different along with opengl and I am facing a problem.

Basically here is the simplified equivalent flow

thread A: Create WindowOne, and poll input, WindowOne.Display()
thread B: Create WindowTwo, and poll input, WindowTwo.Display()
thread C (command thread): calls WindowOne.SetActive(true), Draw Cube using Opengl

However nothing is being drawn in the window. Is there something I am missing out?

As from the previous posts, what I "understood" is that setting SetActive(true) would set the window to the primary window.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
mulitple sf::window and OpenGL
« Reply #7 on: January 05, 2011, 09:26:20 pm »
Quote
Is there something I am missing out?

WindowOne.SetActive(false). And a mutex to make sure that SetActive and Display are not called concurrently.
Laurent Gomila - SFML developer

 

anything