-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
330 lines (262 loc) · 9.68 KB
/
main.cpp
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
//
// main.cpp
// GlfwExample
//
// Created by Chan, Michael on 26/08/2016.
// Copyright © 2016 Dolby. All rights reserved.
//
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#define VISUAL_OBJECT_WINDOW_TITLE_BAR_HEIGHT (30) /* if less than 30, weird things happening on mac os */
#define VISUAL_OBJECT_WINDOW_PROGRESS_WINDOW_HEIGHT (35)
#define VISUAL_OBJECT_WINDOW_PROGRESS_WINDOW_WIDTH_OFFSET (20)
#define FRAME_PER_SECOND (24.0f)
#define FRAME_RATE (1.0f / FRAME_PER_SECOND)
#define MAX_FRAMES (20 * (int)FRAME_PER_SECOND)
#define JOC_MAX_OBJECT_NUM (16)
#define JOC_VOLUME_METER_RANGE (-100)
/* global variabls */
bool g_stop_signal = false;
int g_rendering_scale = 2;
int g_current_frame_num = 0;
GLFWwindow* window;
GLFWwindow* window1;
GLFWwindow* window2;
void draw_local_coordinates()
{
glBegin(GL_LINES);
/* red for x-axis */
glColor3f(1.0,0.0,0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
/* green for y-axis */
glColor3f(0.0,1.0,0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
/* blue for z-axis */
glColor3f(0.0,0.0,1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 1.0);
glEnd();
}
void draw_2d_dynamic_bars()
{
GLfloat start_pos = -0.5f;
GLfloat end_pos = start_pos*(-1.0f);
GLfloat offset = (end_pos - start_pos)/JOC_MAX_OBJECT_NUM;
GLfloat gap = offset/4;
GLfloat xpos = start_pos+gap/2;
GLfloat bar_range = 0.6f;
/* Draw 16 empty bars for representing 16 objects */
int i=0;
for (;i<JOC_MAX_OBJECT_NUM; ++i, xpos+=offset)
{
glBegin(GL_LINE_LOOP);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(xpos, bar_range);
glVertex2f(xpos+offset-gap, bar_range);
glVertex2f(xpos+offset-gap, -1.0f*bar_range);
glVertex2f(xpos, -1.0f*bar_range);
glEnd();
}
/* Draw solid bar for all valid objects */
xpos = start_pos+gap/2;
GLfloat bar_height;
GLfloat ypos;
for (size_t i = 0; i < JOC_MAX_OBJECT_NUM; ++i, xpos+=offset)
{
float r = -1.0*(float)(abs(rand()) % JOC_VOLUME_METER_RANGE);
bar_height = (r/(float)JOC_VOLUME_METER_RANGE) * (bar_range * 2);
ypos = bar_height-bar_range;
glBegin(GL_POLYGON);
glColor3f(0.87f, 0.96f, 0.02f); // yellow bar
glVertex2f(xpos, ypos);
glVertex2f(xpos+offset-gap, ypos);
glVertex2f(xpos+offset-gap, -1.0f*bar_range);
glVertex2f(xpos, -1.0f*bar_range);
glEnd();
glBegin(GL_LINES);
/* grey for x-axis */
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(xpos, ypos);
glVertex2f(xpos+offset-gap, ypos);
/* y-axis */
glEnd();
}
}
void draw_2d_progress_bar()
{
if (g_current_frame_num > MAX_FRAMES)
{
g_stop_signal = true;
return;
}
GLfloat start_pos = -1.0f;
GLfloat end_pos = start_pos*(-1.0f);
GLfloat ratio = g_current_frame_num++ / (GLfloat)MAX_FRAMES;
GLfloat current_pos = (end_pos - start_pos)*ratio + start_pos;
/* setup background */
glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
glLineWidth(5);
glBegin(GL_LINES);
/* grey for x-axis */
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(start_pos, 0.0, 0.0);
glVertex3f(end_pos, 0.0, 0.0);
/* draw y-axis as progressing bar */
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(current_pos, -0.5, 0.0);
glVertex3f(current_pos, 0.5, 0.0);
glEnd();
}
void draw_floor()
{
glEnable(GL_LINE_SMOOTH);
glBegin(GL_LINES);
/* grey for floor lattice */
glColor3f(0.67f, 0.67f, 0.67f);
GLfloat upper_boundary = 1.0f * (GLfloat)g_rendering_scale;
GLfloat lower_boundary = -1.0f * (GLfloat)g_rendering_scale;
GLfloat step = 0.2f * (GLfloat)g_rendering_scale;
for (GLfloat i = lower_boundary; i <= upper_boundary; i+=step)
{
glVertex3f(i, lower_boundary, 0.0);
glVertex3f(i, upper_boundary, 0.0);
for (GLfloat j = lower_boundary; j <= upper_boundary; j+=step)
{
glVertex3f(lower_boundary, j, 0.0);
glVertex3f(upper_boundary, j, 0.0);
}
}
glVertex3f(upper_boundary, lower_boundary, 0.0);
glVertex3f(upper_boundary, upper_boundary, 0.0);
glVertex3f(lower_boundary, upper_boundary, 0.0);
glVertex3f(upper_boundary, upper_boundary, 0.0);
glEnd();
}
void draw_3d_objects()
{
/* Based on inital view perspective, apply coornidate transform */
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(0.0f, 0.0f, -6.0f); // Move the center of scenary towards inside
//glTranslatef(0.0f, -1.0f, 0.0f); // Move the center of scenary downwards a bit
glTranslatef(0.2f, 0.0f, 0.0f); // Move the center of scenary to right side a bit to compensate the rotation around Z axis
/* Rotate around X axis so that Z axis is upwards */
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
/* rotate around Z axis a little bit to show angle between X and Y */
//glRotatef(-10.0f, 0.0f, 0.0f, 1.0f);
GLfloat angle = (g_current_frame_num%36/(GLfloat)36) * (GLfloat)360;
glRotatef(angle, 0.0f, 0.0f, 1.0f);
/* rotate around X axis a little bit to lift the camera a bit */
glRotatef(15.0f, 1.0f, 0.0f, 0.0f);
draw_floor();
draw_local_coordinates();
}
typedef void (*GL_DRAWING_FUNC)(void) ;
void on_windows_close_callback(GLFWwindow* window)
{
printf("disable slave window closing.\n");
glfwSetWindowShouldClose(window, GL_FALSE);
}
void align_slave_windows()
{
int xpos, ypos, width, height;
/* Get main windows postion and size */
glfwGetWindowPos(window, &xpos, &ypos);
glfwGetWindowSize(window, &width, &height);
/* Set slave windows position only */
glfwSetWindowPos(window1, xpos, ypos + VISUAL_OBJECT_WINDOW_TITLE_BAR_HEIGHT + height);
glfwSetWindowSize(window1, width - VISUAL_OBJECT_WINDOW_PROGRESS_WINDOW_WIDTH_OFFSET, VISUAL_OBJECT_WINDOW_PROGRESS_WINDOW_HEIGHT);
glfwSetWindowPos(window2, xpos + width, ypos);
}
void on_windows_move_callback(GLFWwindow *window, int cx, int cy)
{
printf("window move callback.\n");
align_slave_windows();
}
int main(void)
{
GLFWwindow *windows[3];
GL_DRAWING_FUNC drawing_funcs[3];
/* Initialize the library */
if (!glfwInit())
return -1;
int width = 640, height = 480;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(width, height, "Hello World", NULL, NULL);
window1 = glfwCreateWindow(width, 40, "Hello World", NULL, NULL);
window2 = glfwCreateWindow(width/2, height/2, "Hello World", NULL, NULL);
if (!window || !window1 || !window2)
{
printf("unable to create glfw windows, terminate...\n");
glfwTerminate();
return -1;
}
windows[0] = window;
windows[1] = window1;
windows[2] = window2;
drawing_funcs[0] = draw_3d_objects;
drawing_funcs[1] = draw_2d_progress_bar;
drawing_funcs[2] = draw_2d_dynamic_bars;
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* glew function seems to work with opengl 2d rendering ?? */
/* To be noticed:
* glewInit has to be called after glfwMakeContextCurrent(window) which setup opengl context
*/
GLenum ret = glewInit();
if( ret != GLEW_OK ){
printf("Failed to init GLEW, error: %s.\n", glewGetString(ret));
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
/* layout all windows at the right position with proper size */
align_slave_windows();
/* setup callback functions */
glfwSetWindowPosCallback(window, &on_windows_move_callback);
glfwSetWindowPosCallback(window1, &on_windows_move_callback);
glfwSetWindowPosCallback(window2, &on_windows_move_callback);
glfwSetWindowCloseCallback(window2, &on_windows_close_callback);
glfwSetWindowCloseCallback(window1, &on_windows_close_callback);
/* setup viewport for 1st window */
{
glViewport(0, 0, width, height);
/* set initial veiw perspective */
glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity (); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
// gluXXXX are all deprecated, gluPerspective can be replaced by glFrustum
gluPerspective(45.0f, (GLfloat)(width)/(GLfloat)(height), 1.0f, 100.0f);
glMatrixMode (GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity (); // Reset The Modelview Matrix
}
double last_time, current_time;
last_time = glfwGetTime();
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window) && !g_stop_signal)
{
current_time = glfwGetTime();
if (current_time - last_time > FRAME_RATE)
{
for (int i=0; i<3; ++i)
{
GLFWwindow* window = windows[i];
/* switch window context */
glfwMakeContextCurrent(window);
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
(*drawing_funcs[i])();
//glFlush(); /* comment out dosen't hurt */
/* Swap front and back buffers */
glfwSwapBuffers(window);
}
last_time = current_time;
}
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}