Hello
This is related to my earlier post about OpenMP and SFML. I have tried to write a program which creates two windows, and starts two threads, one for each of the windows. Each thread then does some OpenGL rendering (a simple triangle) and displays it in its own window.
I have tried to be careful by using mutexes around the rendering and displaying commands.
The problem is that the result is wrong in one of the windows. In the first window, the triangle is placed near the middle as it should be, but in the other window the triangle is placed in the top right corner.
If I remove the OpenGL commands in main(), the results are both correct, but then I have no choices for the perspective and camera position.
#include <SFML/Window.hpp>
sf::Mutex globalMutex;
void* threadFunc(void* a)
{
sf::Window* Win = (sf::Window*)(a);
while(true) {
globalMutex.Lock();
Win->SetActive();
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex3f(0.0,0.0,0.0); glVertex3f(0.5,0.0,0.0); glVertex3f(0.5,0.5,0.0);
glEnd();
Win->Display();
globalMutex.Unlock();
sf::Sleep(1);
}
return(0);
}
int main()
{
sf::Window Win1(sf::VideoMode(200, 200, 32), "Window 1");
sf::Window Win2(sf::VideoMode(200, 200, 32), "Window 2");
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, 300, 300);
gluPerspective(45, 1, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
pthread_t thread1, thread2;
pthread_create(&thread1, 0, threadFunc, (void*)(&Win1));
pthread_create(&thread2, 0, threadFunc, (void*)(&Win2));
pthread_join(thread1, 0); pthread_join(thread2, 0);
return(0);
}