-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
346 lines (279 loc) · 8.46 KB
/
util.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
#include <iostream>
#include <fstream>
#include "util.h"
using namespace std;
const GLuint WINDOWN_BOARDER_GAP = 6;
const GLuint SUB_WINDOW_COL = 5, SUB_WINDOW_ROW = 5,
SUB_WINDOW_WIDTH = 128, SUB_WINDOW_HEIGHT = 128;
const GLuint MAIN_WINDOW_WIDTH = SUB_WINDOW_WIDTH * SUB_WINDOW_COL +
(SUB_WINDOW_COL + 1) * WINDOWN_BOARDER_GAP;
const GLuint MAIN_WINDOW_HEIGHT = SUB_WINDOW_HEIGHT * SUB_WINDOW_ROW +
(SUB_WINDOW_ROW + 1) * WINDOWN_BOARDER_GAP;
// TODO: make as c++ class
// TODO: use boost
GLuint sphere(float radius, int slices, int stacks) {
GLuint vbo;
int n = 2 * (slices + 1) * stacks;
int i = 0;
// TODO: n dynamic right?
glm::vec3 points[n];
for (float theta = -M_PI / 2; theta < M_PI / 2 - 0.0001;
theta += M_PI / stacks) {
for (float phi = -M_PI; phi <= M_PI + 0.0001;
phi += 2 * M_PI / slices) {
points[i++] = glm::vec3(cos(theta) * sin(phi),
-sin(theta),
cos(theta) * cos(phi));
points[i++] = glm::vec3(cos(theta + M_PI / stacks) * sin(phi),
-sin(theta + M_PI / stacks),
cos(theta + M_PI / stacks) * cos(phi));
}
}
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof points, points, GL_STATIC_DRAW);
return vbo;
}
glm::vec3 get_arcball_vector(int x, int y)
{
glm::vec3 P = glm::vec3(1.0 * x / SUB_WINDOW_WIDTH * 2 - 1.0,
1.0 * y / SUB_WINDOW_HEIGHT * 2 - 1.0,
0);
P.y = -P.y;
float OP_squared = P.x * P.x + P.y * P.y;
if (OP_squared <= 1 * 1) {
P.z = sqrt(1 * 1 - OP_squared);
} else {
P = glm::normalize(P); // nearest point
}
return P;
}
GLint get_uniform(GLuint program, const char *name) {
GLint uniform = glGetUniformLocation(program, name);
if(uniform == -1)
fprintf(stderr, "Could not bind uniform %s\n", name);
return uniform;
}
char *load_file(const char *path)
{
char *content = NULL;
ifstream glslFile(path);
if (glslFile.is_open()){
glslFile.seekg (0, ios::end);
streampos size = glslFile.tellg();
// TODO: this is ugly
streamoff extraSpace = 1;
content = new char[size + extraSpace];
glslFile.seekg(0, ios::beg);
glslFile.read(content, size);
content[size] = '\0';
glslFile.close();
} else {
cerr << "can not load file: " << path << endl;
}
return content;
// FILE *file_handler = fopen(path, "r");
// if (file_handler == NULL){
// return NULL;
// }
// if (fseek(file_handler, 0, SEEK_END) == -1){
// return NULL;
// }
// long size = ftell(file_handler);
// if (size == -1){
// return NULL;
// }
// if (fseek(file_handler, 0, SEEK_SET) == -1){
// return NULL;
// }
// char *content = (char *)malloc(size * sizeof(char) + 1);
// if (content == NULL){
// return NULL;
// }
// fread(content, 1, (size_t)size, file_handler);
// if (ferror(file_handler)){
// free(content);
// fclose(file_handler);
// return NULL;
// }
// fclose(file_handler);
// content[size] = '\0';
// return content;
}
void printLog(GLuint object)
{
char *logBuffer = NULL;
GLint logLength;
if (glIsShader(object)){
glGetShaderiv(object, GL_INFO_LOG_LENGTH, &logLength);
logBuffer = (char *)malloc(sizeof(char) * logLength);
glGetShaderInfoLog(object, logLength, NULL, logBuffer);
} else if (glIsProgram(object)){
glGetProgramiv(object, GL_INFO_LOG_LENGTH, &logLength);
logBuffer = (char *)malloc(sizeof(char) * logLength);
glGetProgramInfoLog(object, logLength, NULL, logBuffer);
} else {
return;
}
cerr << "pringLog : " << logBuffer << endl;
free(logBuffer);
}
GLuint create_shader(const char *path, GLenum type)
{
GLuint shader = glCreateShader(type);
char *shader_src = load_file(path);
if (shader_src == NULL){
cerr << "load file " << path << "failed" << endl;
return 0;
}
char *sources[2] = {
(char *)"#version 130\n",
shader_src
};
glShaderSource(shader, 2, (const char **)&sources, NULL);
free(shader_src);
glCompileShader(shader);
GLint status = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == 0){
cerr << "compile shader failed: " << path << endl;
printLog(shader);
return 0;
}
return shader;
}
GLint get_attrib(GLuint program, const char *attr_name)
{
GLint attr = glGetAttribLocation(program, attr_name);
if (attr == -1){
cerr << "glGetAttribLocation attr_name "
<< attr_name << " failed!" << endl;
return -1;
}
return attr;
}
GLuint create_program(const char *vertex_path, const char *fragment_path)
{
GLint status = GL_FALSE;
GLuint program = glCreateProgram();
GLuint shader = 0;
if (vertex_path != NULL){
// create vertex shader
if ((shader = create_shader(vertex_path, GL_VERTEX_SHADER)) == -1){
cerr << "can not create vertex shader" << endl;
return -1;
}
glAttachShader(program, shader);
} else {
cerr << "vertex shader file is NULL" << endl;
}
if (fragment_path != NULL){
// create fragment shader
if ((shader = create_shader(fragment_path, GL_FRAGMENT_SHADER)) == -1){
cerr << "can not create fragment shader" << endl;
return -1;
}
glAttachShader(program, shader);
} else {
cerr << "fragment shader file is NULL" << endl;
}
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status == 0){
cerr << "link shader failed" << endl;
printLog(program);
return -1;
}
return program;
}
void viewEntry(int state)
{
if (state == GLUT_LEFT){
glClearColor(1, 1, 1, 1);
} else if (state == GLUT_ENTERED) {
glClearColor(0.5, 0.5, 0.5, 1.0);
}
}
void resetWindow(Window *window)
{
window->window = -1;
window->x = 0;
window->y = 0;
window->width = MAIN_WINDOW_WIDTH;
window->height = MAIN_WINDOW_HEIGHT;
window->display = NULL;
window->entry = NULL;
window->init = NULL;
window->free = NULL;
window->idle = NULL;
window->reshape = NULL;
window->special = NULL;
window->specialUp = NULL;
window->keyboard = NULL;
window->motion = NULL;
window->mouse = NULL;
window->internalMouse = NULL;
}
void mini_idle()
{
glutPostRedisplay();
}
int mini_initWindow(int argc, char *argv[], Window *window)
{
glutInit(&argc, argv);
// 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);
window->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");
if ((*window->init)() != 0){
cerr << "window->init failed" << endl;
return -1;
}
glutDisplayFunc(window->display);
if (window->keyboard != NULL){
glutKeyboardFunc(window->keyboard);
}
if (window->reshape != NULL){
glutReshapeFunc(window->reshape);
}
if (window->mouse != NULL){
glutMouseFunc(window->mouse);
}
if (window->special != NULL){
glutSpecialFunc(window->special);
}
if (window->specialUp != NULL){
glutSpecialUpFunc(window->specialUp);
}
if (window->motion != NULL){
glutMotionFunc(window->motion);
}
if (window->free != NULL){
glutCloseFunc(window->free);
}
if (window->idle != NULL){
glutIdleFunc(window->idle);
} else {
glutIdleFunc(mini_idle);
}
return 0;
}