Glut + Pthread

Hi guys, i'm a beginner about open gl, but during this week i learned the basic about it... So hmmmmm i would like to program with two distinct windows, which shows their own drawn with their own input handler. So i tried this code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Ho scoperto che le glut fanno cacare...perĂ² va aiutano
*/

#include <stdio.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <malloc.h>
#include <pthread.h>
#include <semaphore.h>

pthread_mutex_t mtx; //credo serva...

void init();
void display1();
void display2();

void keyboardEvent1(unsigned char key, int x, int y);
void keyboardEvent2(unsigned char key, int x, int y);

void* window1(void* arg);
void* window2(void* arg);

int teta1;
int teta2;

int main(int argc, char** argv) {
    
    /*
    Inizializzo il sistema...
    */
    glutInit(&argc,argv);
    pthread_t th_1, th_2;
    pthread_create(&th_1,NULL,&window1,NULL);
    pthread_create(&th_2,NULL,&window2,NULL);
    
    pthread_join(th_1,NULL);
    pthread_join(th_2,NULL);
    return 0;
    
    }

void init1() {
     teta1 = 0;
     glClearColor(0.0,0.0,0.0,0.0);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
     }

void init2() {
     teta2 = 0;
     glClearColor(0.0,0.0,0.0,0.0);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
     }
     
void display1() {
     glClear(GL_COLOR_BUFFER_BIT);
     
     glColor3f(1.0,1.0,1.0);
     
     GLfloat pt[] = {
             0.25,0.25,0.0,
             0.75,0.75,0.0
             };
     
     glEnableClientState(GL_VERTEX_ARRAY);
     glVertexPointer(3,GL_FLOAT,0,pt);
     
     GLubyte index[] = {0,1};
     
     glMatrixMode(GL_MODELVIEW);
     glTranslatef(0.5,0.5,0.0);
     glRotatef((GLfloat)teta1,0.0,0.0,1.0);
     glTranslatef(-0.5,-0.5,0.0);
     glDrawElements(GL_LINES,2,GL_UNSIGNED_BYTE,index);
     glLoadIdentity();
     
     glutSwapBuffers();
     }

void display2() {
     glClear(GL_COLOR_BUFFER_BIT);
     
     glColor3f(1.0,1.0,1.0);
     
     GLfloat pt[] = {
             0.25,0.25,0.0,
             0.75,0.75,0.0
             };
     
     glEnableClientState(GL_VERTEX_ARRAY);
     glVertexPointer(3,GL_FLOAT,0,pt);
     
     GLubyte index[] = {0,1};
     
     glMatrixMode(GL_MODELVIEW);
     glTranslatef(0.5,0.5,0.0);
     glRotatef((GLfloat)teta2,0.0,0.0,1.0);
     glTranslatef(-0.5,-0.5,0.0);
     glDrawElements(GL_LINES,2,GL_UNSIGNED_BYTE,index);
     glLoadIdentity();
     
     glutSwapBuffers();
     }

void keyboardEvent1(unsigned char key, int x, int y) {
     
     switch(key) {
                 case 'a':
                      teta1 = (teta1 + 5)%360;
                      glutPostRedisplay();
                      break;
                 case 'd':
                      teta1 = (teta1 - 5)%360;
                      glutPostRedisplay();
                      break;
                 }
                 
     }

void keyboardEvent2(unsigned char key, int x, int y) {
     
     switch(key) {
                 case 'a':
                      teta2 = (teta2 + 5)%360;
                      glutPostRedisplay();
                      break;
                 case 'd':
                      teta2 = (teta2 - 5)%360;
                      glutPostRedisplay();
                      break;
                 }
                 
     }

void* window1(void* arg) {
      
      glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
      glutInitWindowSize(800,600);
      glutInitWindowPosition(100,100);
      glutCreateWindow("Window 1");
      
      init1();
      
      glutDisplayFunc(&display1);
      glutKeyboardFunc(&keyboardEvent1);
      glutMainLoop();
      
      }

void* window2(void* arg) {
      
      glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
      glutInitWindowSize(800,600);
      glutInitWindowPosition(100,100);
      glutCreateWindow("Window 2");
      
      init2();
      
      glutDisplayFunc(&display2);
      glutKeyboardFunc(&keyboardEvent2);
      glutMainLoop();
      
      }


but it doesn't work...my idea was to make a thread for a single window, but the prompt show me a context problem... or it doesn't work well... you will understand me if you try this code... can you help me?

thx for attention
IIRC, OpenGL is not thread-safe.
Topic archived. No new replies allowed.