-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdualwindow.c
347 lines (284 loc) · 9.31 KB
/
dualwindow.c
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//
// Created by atr on 06.07.17.
//
// Glutdualwindow.c
// By Eric Stringer 2002
// Simple examples of OpenGL and Glut usage.
// Keyboard input
// 'v' = view ortho/perspective
// 'l' = lighting on/off
#include <GL/gl.h>
#include <GL/glut.h>
#include <string.h>
// lighting
GLfloat LightAmbient[]= { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat LightDiffuse[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightPosition[]= { 5.0f, 5.0f, -10.0f, 1.0f };
GLfloat mat_specular[] = { 0.2, 0.2, 0.2, 1.0 };
int window_1, window_2;
static int view_state = 0, light_state = 0;
int spin;
int shape;
// I use this to put text on the screen
void Sprint( int x, int y, char *st)
{
int l,i;
l=strlen( st ); // see how many characters are in text string.
glRasterPos2i( x, y); // location to start printing text
for( i=0; i < l; i++) // loop until i is greater then l
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, st[i]); // Print a character on the screen
}
}
// This creates the spinning of the cube.
static void TimeEvent(int te)
{
spin++; // increase cube rotation by 1
if (spin > 360) spin = 0; // if over 360 degress, start back at zero.
glutSetWindow( window_1 );
glutPostRedisplay(); // Update screen with new rotation data
glutSetWindow( window_2 );
glutPostRedisplay(); // Update screen with new rotation data
glutTimerFunc( 100, TimeEvent, 1); // Reset our timmer.
}
// Setup our Opengl world, called once at startup.
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0); // When screen cleared, use black.
glShadeModel (GL_SMOOTH); // How the object color will be rendered smooth or flat
glEnable(GL_DEPTH_TEST); // Check depth when rendering
// Lighting is added to scene
glLightfv(GL_LIGHT1 ,GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1 ,GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1 ,GL_POSITION, LightPosition);
glEnable(GL_LIGHTING); // Turn on lighting
glEnable(GL_LIGHT1); // Turn on light 1
}
// Draw our world
void display_1(void)
{
char *p;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the screen
glMatrixMode (GL_PROJECTION); // Tell opengl that we are doing project matrix work
glLoadIdentity(); // Clear the matrix
glOrtho(-8.0, 8.0, -8.0, 8.0, 0.0, 30.0); // Setup an Ortho view
glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work. (drawing)
glLoadIdentity(); // Clear the model matrix
glColor3f(1.0, 1.0, 1.0);
if (shape == 0) Sprint(-3, -7 ,"Solid Cube");
if (shape == 1) Sprint(-3, -7 ,"Solid Cone");
if (shape == 2) Sprint(-3, -7 ,"Solid Sphere");
if (shape == 3) Sprint(-3, -7 ,"Solid Torus");
if (shape == 4) Sprint(-3, -7 ,"Solid Dodecahedron");
if (shape == 5) Sprint(-3, -7 ,"Solid Octahedron");
if (shape == 6) Sprint(-3, -7 ,"Solid Tetrahedron");
if (shape == 7) Sprint(-3, -7 ,"Solid Icosahedron");
if (shape == 8) Sprint(-3, -7 ,"Solid Teapot");
// Setup view, and print view state on screen
if (view_state == 1)
{
glColor3f( 1.0, 1.0, 1.0);
Sprint(-2, 4, "Perspective view");
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 1, 30);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}else
{
glColor3f( 1.0, 1.0, 1.0);
Sprint(-2, 4, "Ortho view");
}
glColor3f( 0.0, 0.0, 1.0); // Cube color
// Lighting on/off
if (light_state == 1)
{
glDisable(GL_LIGHTING); // Turn off lighting
glDisable(GL_COLOR_MATERIAL); // Turn off material, which needs lighting to work
}else
{
glEnable(GL_LIGHTING); // Turn on lighting
glEnable(GL_COLOR_MATERIAL); // Turn on material settings
glColorMaterial(GL_FRONT, GL_AMBIENT);
glColor4f(0.65, 0.65, 0.65, 0.4);
glColorMaterial(GL_FRONT, GL_EMISSION);
glColor4f(0.10, 0.10, 0.10, 0.0);
glColorMaterial(GL_FRONT, GL_SPECULAR);
glColor4f(0.5, 0.5, 0.5, 0.4);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glColor4f(0.85, 0.85, 0.85, 0.4);
}
gluLookAt( 0, 0, 20, 0, 0, 0, 0, 1, 0);
//glRotatef( 45, 1.0, 1.0, 0.0); // rotate cube
glRotatef( spin++, 1.0, 1.0, 1.0); // spin cube
if (shape == 0) glutSolidCube(10); // Draw a cube
if (shape == 1) glutSolidCone(5,10, 16,16); // Draw a Cone
if (shape == 2) glutSolidSphere(5, 16,16 ); // Draw a Sphere
if (shape == 3) glutSolidTorus( 2.5, 5, 16, 16);
if (shape == 4)
{
glScalef( 3.5, 3.5, 3.5);
glutSolidDodecahedron();
}
if (shape == 5)
{
glScalef( 5.0, 5.0, 5.0);
glutSolidOctahedron();
}
if (shape == 6)
{
glScalef( 5.0, 5.0, 5.0);
glutSolidTetrahedron();
}
if (shape == 7)
{
glScalef( 5.0, 5.0, 5.0);
glutSolidIcosahedron();
}
if (shape == 8) glutSolidTeapot( 5 );
glutSwapBuffers();
}
void display_2(void)
{
char *p;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the screen
glMatrixMode (GL_PROJECTION); // Tell opengl that we are doing project matrix work
glLoadIdentity(); // Clear the matrix
glOrtho(-8.0, 8.0, -8.0, 8.0, 0.0, 30.0); // Setup an Ortho view
glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work. (drawing)
glLoadIdentity(); // Clear the model matrix
glColor3f(1.0, 1.0, 1.0);
if (shape == 0) Sprint(-3, -7 ,"Wire Cube");
if (shape == 1) Sprint(-3, -7 ,"Wire Cone");
if (shape == 2) Sprint(-3, -7 ,"Wire Sphere");
if (shape == 3) Sprint(-3, -7 ,"Wire Torus");
if (shape == 4) Sprint(-3, -7 ,"Wire Dodecahedron");
if (shape == 5) Sprint(-3, -7 ,"Wire Octahedron");
if (shape == 6) Sprint(-3, -7 ,"Wire Tetrahedron");
if (shape == 7) Sprint(-3, -7 ,"Wire Icosahedron");
if (shape == 8) Sprint(-3, -7 ,"Wire Teapot");
// Setup view, and print view state on screen
if (view_state == 1)
{
glColor3f( 1.0, 1.0, 1.0);
Sprint(-2, 4, "Perspective view");
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 1, 30);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}else
{
glColor3f( 1.0, 1.0, 1.0);
Sprint(-2, 4, "Ortho view");
}
glColor3f( 0.0, 0.0, 1.0); // Cube color
// Lighting on/off
if (light_state == 1)
{
glDisable(GL_LIGHTING); // Turn off lighting
glDisable(GL_COLOR_MATERIAL); // Turn off material, which needs lighting to work
}else
{
glEnable(GL_LIGHTING); // Turn on lighting
glEnable(GL_COLOR_MATERIAL); // Turn on material settings
glColorMaterial(GL_FRONT, GL_AMBIENT);
glColor4f(0.65, 0.65, 0.65, 0.4);
glColorMaterial(GL_FRONT, GL_EMISSION);
glColor4f(0.10, 0.10, 0.10, 0.0);
glColorMaterial(GL_FRONT, GL_SPECULAR);
glColor4f(0.5, 0.5, 0.5, 0.4);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glColor4f(0.85, 0.85, 0.85, 0.4);
}
gluLookAt( 0, 0, 20, 0, 0, 0, 0, 1, 0);
//glRotatef( 45, 1.0, 1.0, 0.0); // rotate cube
glRotatef( spin++, 1.0, 1.0, 1.0); // spin cube
if (shape == 0) glutWireCube(10); // Draw a cube
if (shape == 1) glutWireCone(5,10, 16,16); // Draw a Cone
if (shape == 2) glutWireSphere(5, 16,16 ); // Draw a Sphere
if (shape == 3) glutWireTorus( 2.5, 5, 16, 16);
if (shape == 4)
{
glScalef( 3.5, 3.5, 3.5);
glutSolidDodecahedron();
}
if (shape == 5)
{
glScalef( 5.0, 5.0, 5.0);
glutWireOctahedron();
}
if (shape == 6)
{
glScalef( 5.0, 5.0, 5.0);
glutWireTetrahedron();
}
if (shape == 7)
{
glScalef( 5.0, 5.0, 5.0);
glutWireIcosahedron();
}
if (shape == 8) glutWireTeapot( 5 );
glutSwapBuffers();
}
// This is called when the window has been resized.
void reshape_1 (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
}
// This is called when the window has been resized.
void reshape_2 (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
}
// Read the keyboard
void keyboard (unsigned char key, int x, int y)
{
switch (key)
{
case 'v':
case 'V':
view_state = abs(view_state -1);
break;
case 'l':
case 'L':
light_state = abs(light_state -1);
break;
case 's':
case 'S':
shape++;
break;
case 27:
exit(0); // exit program when [ESC] key presseed
break;
default:
break;
}
if (shape > 8) shape = 0;
}
// Main program
int dualwindow_main(int argc, char** argv)
{
glutInit(argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (10, 10);
glutTimerFunc( 10, TimeEvent, 1);
window_1 = glutCreateWindow (argv[0]);
glutSetWindowTitle("GlutWindow 1");
init ();
glutDisplayFunc(display_1);
glutReshapeFunc(reshape_1);
glutKeyboardFunc(keyboard);
window_2 = glutCreateWindow (argv[0]);
glutSetWindowTitle("GlutWindow 2");
init ();
glutDisplayFunc(display_2);
glutReshapeFunc(reshape_2);
glutMainLoop();
return 0;
}