-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
366 lines (304 loc) · 10.7 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include <GL/glew.h>
// soemthing about window
#include <GL/freeglut.h>
#include <iostream>
#include <vector>
#include <string>
#include "lib/Mesh.h"
#include "view1_triangle.h"
#include "view2_triangle.h"
#include "view3_triangle.h"
#include "view4_triangle.h"
#include "view5_cube.h"
#include "view6_cube.h"
// #include "view7_suzanne.h"
// #include "view8.h"
// #include "view9_sphere.h"
// #include "view10_cube.h"
// #include "view11.h"
// #include "view12.h"
// #include "view13.h"
// #include "view14.h"
// #include "view15.h"
// #include "view16_teapot.h"
// #include "view17_cube.h"
// #include "view18.h"
// #include "view19.h"
// #include "view20_dice.h"
// #include "view21_portal.h"
#include "global.h"
#include "util.h"
using namespace std;
// TODO: different view from monkey
// TODO: problem in fps and mini-portal problem in different pc
// calculate frames speed
GLfloat fps = 0.0;
static unsigned int fps_frames = 0, fps_start = 0;
GLuint active_window = 0;
GLuint main_window = 0;
struct Window win_array[100];
// TODO: create subsub window and category
// TODO: add background, bbox, light
// TODO: add quad view
// TODO: reshape func
// TODO: design object class like object viewer
// TODO: use nice font to draw numbers
// TODO: load other obj than suzanne
// TODO: not use subwindow, make all in one window
int get_window_index(GLuint win)
{
for(int i = 0; i < SUB_WINDOW_COL * SUB_WINDOW_ROW; ++i){
if (win_array[i].window == win){
return i;
}
}
return 0;
}
// TODO: when click subwindow, main window will get event?
void mainDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
// TODO: add effect of slow max or min sub window
// TODO: add sub screen in sub window
// TODO: check G3D Innovation Engine
// http://g3d.sourceforge.net/
// TODO: add support to chinese utf8 font
// TODO: draw 3d mesh letter
// TODO: float over subwindow and shadow the boarder
// TODO: add drag window function
int initResources()
{
glClearColor(0, 0, 0, 0);
return 0;
}
// TODO: check the test_res.c file
void windowIdle()
{
/* FPS count */
fps_frames++;
int delta_t = glutGet(GLUT_ELAPSED_TIME) - fps_start;
if (delta_t > 1000) {
// cout << "fps: "<< 1000.0 * fps_frames / delta_t << endl;
fps = 1000.0 * fps_frames / delta_t;
fps_frames = 0;
fps_start = glutGet(GLUT_ELAPSED_TIME);
}
// TODO: this is not good.
for(int i = 0; i < SUB_WINDOW_COL * SUB_WINDOW_ROW; ++i){
if (win_array[i].init == NULL){
break;
}
// TODO: all window share the same idle
// TODO: how to use idle
if (active_window == 0 || active_window == win_array[i].window){
glutSetWindow(win_array[i].window);
glutShowWindow();
glutPostRedisplay();
if (win_array[i].idle != NULL){
win_array[i].idle();
}
} else {
glutSetWindow(win_array[i].window);
glutHideWindow();
}
}
}
// TODO: write your own framework like glut
// http://stackoverflow.com/questions/11555588/how-should-i-
// use-glutpostredisplay-in-a-loop-to-call-display-multiple-times-when
void freeResources()
{
for(int i = 0; i < SUB_WINDOW_COL * SUB_WINDOW_ROW; ++i){
glutDestroyWindow(win_array[i].window);
}
}
void viewMouse(int button, int state, int x, int y)
{
int index = get_window_index(active_window);
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
if (active_window == 0){
active_window = glutGetWindow();
glutPositionWindow(0, 0);
glutReshapeWindow(MAIN_WINDOW_WIDTH, MAIN_WINDOW_HEIGHT);
} else {
glutPositionWindow(win_array[index].x, win_array[index].y);
glutReshapeWindow(SUB_WINDOW_WIDTH, SUB_WINDOW_HEIGHT);
active_window = 0;
}
}
if (win_array[index].internalMouse != NULL){
win_array[index].internalMouse(button, state, x, y);
}
}
// TODO: what's the function of glclear deapth and other
int initWindow(int argc, char *argv[])
{
glutInit(&argc, argv);
// TODO: glutInitContextVersion(3, 0);
// GLUT_RGB is alias for GLUT_RGBA
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA |
GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize(MAIN_WINDOW_WIDTH, MAIN_WINDOW_HEIGHT);
main_window = glutCreateWindow("all in one");
if (glewInit() != GLEW_OK){
return 1;
}
// TODO: upper version, support opengl es
if (!GLEW_VERSION_2_0) {
cerr << "Error: your graphic card does not support OpenGL 2.0" << endl;
return 1;
}
GLint max_units;
glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &max_units);
if (max_units < 1) {
cerr << "Your GPU does not have any vertex texture image units" << endl;
return 1;
}
// TODO: check why it is not tranparent now
glutSetWindowTitle("all in one");
// TODO: seems not work
glutSetIconTitle("icon");
// TODO: add clear color to window, different
glutDisplayFunc(mainDisplay);
glutIdleFunc(windowIdle);
// TODO: make this as function hook, handle these by itself
for(int i = 0; i < SUB_WINDOW_COL; ++i){
for(int j = 0; j < SUB_WINDOW_ROW; ++j){
int winNum = i * SUB_WINDOW_COL + j;
win_array[winNum].window = -1;
win_array[winNum].x = WINDOWN_BOARDER_GAP * (j + 1)
+ SUB_WINDOW_WIDTH * j;
win_array[winNum].y = WINDOWN_BOARDER_GAP * (i + 1)
+ SUB_WINDOW_HEIGHT * i;
win_array[winNum].width = SUB_WINDOW_WIDTH;
win_array[winNum].height = SUB_WINDOW_HEIGHT;
win_array[winNum].display = NULL;
win_array[winNum].entry = NULL;
win_array[winNum].init = NULL;
win_array[winNum].free = NULL;
win_array[winNum].idle = NULL;
win_array[winNum].reshape = NULL;
win_array[winNum].special = NULL;
win_array[winNum].specialUp = NULL;
win_array[winNum].keyboard = NULL;
win_array[winNum].motion = NULL;
win_array[winNum].mouse = viewMouse;
win_array[winNum].internalMouse = NULL;
}
}
// TODO: D macro problem confused
// TODO: make game crazy bird to list
// TODO: add menu tips like model load
// TODO: select subwindow not work
// TODO: view7 mouse float not work
// TODO: destroy window
view1_entry(&win_array[0]);
view2_entry(&win_array[1]);
view3_entry(&win_array[2]);
view4_entry(&win_array[3]);
view5_entry(&win_array[4]);
view6_entry(&win_array[5]);
// view7_entry(&win_array[6]);
// view8_entry(&win_array[7]);
// view9_entry(&win_array[8]);
// view10_entry(&win_array[9]);
// view11_entry(&win_array[10]);
// view12_entry(&win_array[11]);
// view13_entry(&win_array[12]);
// view14_entry(&win_array[13]);
// view15_entry(&win_array[14]);
// view16_entry(&win_array[15]);
// view17_entry(&win_array[16]);
// view18_entry(&win_array[17]);
// view19_entry(&win_array[18]);
// view20_entry(&win_array[19]);
// view21_entry(&win_array[20]);
for(int i = 0; i < SUB_WINDOW_COL * SUB_WINDOW_ROW; ++i){
if (win_array[i].init == NULL){
break;
}
win_array[i].window = glutCreateSubWindow(main_window,
win_array[i].x,
win_array[i].y,
win_array[i].width,
win_array[i].height);
// TODO: move to init
if ((*win_array[i].init)() != 0){
continue;
}
glutDisplayFunc(win_array[i].display);
glutEntryFunc(win_array[i].entry);
// TODO: view8 disappear
// TODO: glsl list all attr and uniform
// http://stackoverflow.com/questions/440144/in-opengl-is-there-a-way-to-get-a-list-of-all-uniforms-attribs-used-by-a-shade
// https://www.opengl.org/wiki/Program_Introspection
// TODO: design the camera class
// TODO: support directx and multi platform
// TODO: add c++11 and boost feature
if (win_array[i].keyboard != NULL){
glutKeyboardFunc(win_array[i].keyboard);
}
if (win_array[i].reshape != NULL){
glutReshapeFunc(win_array[i].reshape);
}
if (win_array[i].mouse != NULL){
glutMouseFunc(win_array[i].mouse);
}
if (win_array[i].special != NULL){
glutSpecialFunc(win_array[i].special);
}
if (win_array[i].specialUp != NULL){
glutSpecialUpFunc(win_array[i].specialUp);
}
if (win_array[i].motion != NULL){
glutMotionFunc(win_array[i].motion);
}
if (win_array[i].free != NULL){
glutCloseFunc(win_array[i].free);
}
}
// milliseconds
fps_start = glutGet(GLUT_ELAPSED_TIME);
return 0;
}
// TODO: how to control fps precisely
// TODO: add record vidoe file
// TODO: http://nehe.gamedev.net
// TODO: add a subwindow to play video
// http://nehe.gamedev.net/tutorial/playing_avi_files_in_opengl/23001/
// TODO: draw boarder for 2d shape
// TODO: use boost
int main(int argc, char *argv[])
{
if (initWindow(argc, argv) != 0){
return 0;
}
if (initResources() == 0){
glutMainLoop();
}
freeResources();
return 0;
}
// TODO: check this
// http://feelmare.blogspot.jp/2012/01/sample-source-to-make-subwindow-in.html
// TODO: generate a sphere mannually
// http://www.andrewnoske.com/wiki/Generating_a_sphere_as_a_3D_mesh
// http://blog.andreaskahler.com/2009/06/creating-icosphere-mesh-in-code.html
// http://sol.gfxile.net/sphere/
// TODO: make view6 cube in blender and export as obj and add mtl
// TODO: make a more complete advanced obj loader
// TODO: obj loader https://github.com/syoyo/tinyobjloader
// TODO: nice site for mesh http://graphics.cs.williams.edu/data/meshes.xml
// TODO: uv, http://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro/UV_Map_Basics
// TODO: read simple object viewer and glm object viewer souce code. wirte self
// 1. https://github.com/syoyo/tinyobjloader
// 2. https://github.com/tamato/simple-obj-loader/blob/master/objloader.cpp
// 3. http://people.cs.clemson.edu/~dhouse/courses/405/hw/hw6/hw6.html
// 4. https://github.com/devernay/glm
// TODO: continue the tutorial
// TODO: 1. declare init hook in sub program, in main only call the init.
// TODO: 2. study stencil and depth buffer, fov
// TODO: why first draw display not work in new computer
// TODO: the internal of glutDisplayFunc, mini and max call once