-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview19.cpp
373 lines (311 loc) · 11.2 KB
/
view19.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
367
368
369
370
371
372
373
#include "view19.h"
#include "util.h"
GLuint view19_program = 0;
#include <glm/gtx/closest_point.hpp>
// TODO: why redefine problem
//#undef glm_gtx_closest_point
//#include <glm/gtx/closest_point.inl>
#include "data/view19_res_texture.c"
int view19_screen_width = 800, view19_screen_height = 600;
GLuint view19_vbo_cube_vertices, view19_vbo_cube_texcoords;
GLuint view19_ibo_cube_elements;
GLuint view19_texture_id;
GLint view19_attribute_coord3d, view19_attribute_texcoord;
GLint view19_uniform_mvp, view19_uniform_mytexture, view19_uniform_color;
#define NCUBES 10
glm::vec3 view19_positions[3 * NCUBES];
glm::vec3 view19_rotspeeds[3 * NCUBES];
bool view19_highlight[NCUBES];
float view19_angle = 0;
float view19_camera_angle = 0;
glm::vec3 view19_camera_position(0.0, 2.0, 4.0);
int view19_initResources()
{
glClearColor(1.0, 1.0, 1.0, 0);
// TODO: these function on, index return valid
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
static const GLfloat cube_vertices[] = {
// front
-1.0, -1.0, +1.0,
+1.0, -1.0, +1.0,
+1.0, +1.0, +1.0,
-1.0, +1.0, +1.0,
// top
-1.0, +1.0, +1.0,
+1.0, +1.0, +1.0,
+1.0, +1.0, -1.0,
-1.0, +1.0, -1.0,
// back
+1.0, -1.0, -1.0,
-1.0, -1.0, -1.0,
-1.0, +1.0, -1.0,
+1.0, +1.0, -1.0,
// bottom
-1.0, -1.0, -1.0,
+1.0, -1.0, -1.0,
+1.0, -1.0, +1.0,
-1.0, -1.0, +1.0,
// left
-1.0, -1.0, -1.0,
-1.0, -1.0, +1.0,
-1.0, +1.0, +1.0,
-1.0, +1.0, -1.0,
// right
+1.0, -1.0, +1.0,
+1.0, -1.0, -1.0,
+1.0, +1.0, -1.0,
+1.0, +1.0, +1.0,
};
glGenBuffers(1, &view19_vbo_cube_vertices);
glBindBuffer(GL_ARRAY_BUFFER, view19_vbo_cube_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof cube_vertices, cube_vertices,
GL_STATIC_DRAW);
static const GLfloat cube_texcoords[2*4*6] = {
0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
};
glGenBuffers(1, &view19_vbo_cube_texcoords);
glBindBuffer(GL_ARRAY_BUFFER, view19_vbo_cube_texcoords);
glBufferData(GL_ARRAY_BUFFER, sizeof cube_texcoords, cube_texcoords,
GL_STATIC_DRAW);
static const GLushort cube_elements[] = {
// front
0, 1, 2,
2, 3, 0,
// top
4, 5, 6,
6, 7, 4,
// back
8, 9, 10,
10, 11, 8,
// bottom
12, 13, 14,
14, 15, 12,
// left
16, 17, 18,
18, 19, 16,
// right
20, 21, 22,
22, 23, 20,
};
glGenBuffers(1, &view19_ibo_cube_elements);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, view19_ibo_cube_elements);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof cube_elements, cube_elements,
GL_STATIC_DRAW);
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &view19_texture_id);
glBindTexture(GL_TEXTURE_2D, view19_texture_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, view19_res_texture.width,
view19_res_texture.height, 0, GL_RGB, GL_UNSIGNED_BYTE,
view19_res_texture.pixel_data);
view19_program = create_program("glsl/cube.19.v.glsl",
"glsl/cube.19.f.glsl");
if(view19_program == 0)
return 1;
view19_attribute_coord3d = get_attrib(view19_program, "coord3d");
view19_attribute_texcoord = get_attrib(view19_program, "texcoord");
view19_uniform_mvp = get_uniform(view19_program, "mvp");
view19_uniform_mytexture = get_uniform(view19_program, "mytexture");
view19_uniform_color = get_uniform(view19_program, "color");
if(view19_attribute_coord3d == -1 || view19_attribute_texcoord == -1 ||
view19_uniform_mvp == -1 || view19_uniform_mytexture == -1 ||
view19_uniform_color == -1) {
return 1;
}
srand48(time(NULL));
for(int i = 0; i < NCUBES; i++) {
view19_positions[i] = glm::vec3((drand48() - 0.5) * 2,
(drand48() - 0.5) * 2,
(drand48() - 0.5) * 2);
view19_rotspeeds[i] = glm::vec3(drand48() * 5, drand48() * 5, drand48() * 5);
}
return 0;
}
void view19Idle()
{
// base 15° per second
view19_angle = glutGet(GLUT_ELAPSED_TIME) /
1000.0 * glm::radians(15.0);
glutPostRedisplay();
}
void view19Display()
{
glClearStencil(0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glUseProgram(view19_program);
glUniform1i(view19_uniform_mytexture, /*GL_TEXTURE*/ 0);
glEnableVertexAttribArray(view19_attribute_coord3d);
// Describe our vertices array to OpenGL
// (it can't guess its format automatically)
glBindBuffer(GL_ARRAY_BUFFER, view19_vbo_cube_vertices);
glVertexAttribPointer(view19_attribute_coord3d, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(view19_attribute_texcoord);
glBindBuffer(GL_ARRAY_BUFFER, view19_vbo_cube_texcoords);
glVertexAttribPointer(view19_attribute_texcoord, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, view19_ibo_cube_elements);
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glm::mat4 view = glm::lookAt(view19_camera_position,
glm::vec3(0.0, 0.0, 0.0),
glm::vec3(0.0, 1.0, 0.0));
glm::mat4 projection = glm::perspective(45.0f,
1.0f*view19_screen_width/\
view19_screen_height, 0.1f, 10.0f);
static const GLfloat color_normal[4] = {1, 1, 1, 1};
static const GLfloat color_view19_highlight[4] = {2, 2, 2, 1};
for(int i = 0; i < NCUBES; i++) {
glm::mat4 model = glm::scale(glm::translate(glm::mat4(1.0f),
view19_positions[i]),
glm::vec3(0.2, 0.2, 0.2));
glm::mat4 anim =
// X axis
glm::rotate(glm::mat4(1.0f),
view19_angle * view19_rotspeeds[i].x,
glm::vec3(1, 0, 0)) *
// Y axis
glm::rotate(glm::mat4(1.0f),
view19_angle * view19_rotspeeds[i].y,
glm::vec3(0, 1, 0)) *
// Z axis
glm::rotate(glm::mat4(1.0f),
view19_angle * view19_rotspeeds[i].z,
glm::vec3(0, 0, 1));
glm::mat4 mvp = projection * view * model * anim;
glUniformMatrix4fv(view19_uniform_mvp, 1, GL_FALSE, glm::value_ptr(mvp));
if(view19_highlight[i]) {
glUniform4fv(view19_uniform_color, 1, color_view19_highlight);
} else {
glUniform4fv(view19_uniform_color, 1, color_normal);
}
glStencilFunc(GL_ALWAYS, i + 1, -1);
/* Push each element in buffer_vertices to the vertex shader */
glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
}
glDisableVertexAttribArray(view19_attribute_coord3d);
glDisableVertexAttribArray(view19_attribute_texcoord);
glutSwapBuffers();
}
void view19Reshape(int width, int height)
{
view19_screen_width = width;
view19_screen_height = height;
glViewport(0, 0, view19_screen_width, view19_screen_height);
}
void view19Special(int key, int x, int y)
{
switch(key) {
case GLUT_KEY_LEFT:
view19_camera_angle -= glm::radians(5.0);
break;
case GLUT_KEY_RIGHT:
view19_camera_angle += glm::radians(5.0);
break;
}
view19_camera_position = glm::rotateY(glm::vec3(0.0, 2.0, 4.0),
view19_camera_angle);
}
// TODO: add drag function to drag the object
void view19Mouse(int button, int state, int x, int y)
{
if (!(state == GLUT_DOWN && button == GLUT_RIGHT_BUTTON)) {
return;
}
/* Read color, depth and stencil index from the framebuffer */
GLbyte color[4];
GLfloat depth;
GLuint index;
glReadPixels(x, view19_screen_height - y - 1, 1, 1,
GL_RGBA, GL_UNSIGNED_BYTE, color);
glReadPixels(x, view19_screen_height - y - 1, 1, 1,
GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
glReadPixels(x, view19_screen_height - y - 1, 1, 1,
GL_STENCIL_INDEX, GL_UNSIGNED_INT, &index);
printf("Clicked on pixel %d, %d, color %02hhx%02hhx%02hhx%02hhx, "\
"depth %f, stencil index %u\n",
x, y, color[0], color[1], color[2], color[3], depth, index);
/* Convert from window coordinates to object coordinates */
glm::mat4 view = glm::lookAt(view19_camera_position, glm::vec3(0.0, 0.0, 0.0),
glm::vec3(0.0, 1.0, 0.0));
glm::mat4 projection = glm::perspective(45.0f,
1.0f*view19_screen_width\
/view19_screen_height,
0.1f, 10.0f);
glm::vec4 viewport = glm::vec4(0, 0,
view19_screen_width, view19_screen_height);
glm::vec3 wincoord = glm::vec3(x, view19_screen_height - y - 1, depth);
glm::vec3 objcoord = glm::unProject(wincoord, view, projection, viewport);
/* Which box is nearest to those object coordinates? */
int closest_i = 0;
for(int i = 1; i < NCUBES; i++) {
if(glm::distance(objcoord, view19_positions[i]) <
glm::distance(objcoord, view19_positions[closest_i]))
closest_i = i;
}
// TODO: there is a block can not be selected
// printf("Coordinates in object space: %f, %f, "\
// "%f, closest to center of box %d\n",
// objcoord.x, objcoord.y, objcoord.z, closest_i + 1);
/* Ray casting */
closest_i = -1;
float closest_distance = 1.0 / 0.0; // infinity
wincoord.z = 0.99;
glm::vec3 point1 = view19_camera_position;
glm::vec3 point2 = glm::unProject(wincoord, view, projection, viewport);
for(int i = 0; i < NCUBES; i++) {
glm::vec3 cpol = glm::closestPointOnLine(view19_positions[i], point1, point2);
float dtol = glm::distance(view19_positions[i], cpol);
float distance = glm::distance(view19_positions[i], view19_camera_position);
if(dtol < 0.2 * sqrtf(3.0) && distance < closest_distance) {
closest_i = i;
closest_distance = distance;
}
}
// printf("Closest along camera ray: %d\n", closest_i + 1);
// TODO: segmentfalut here
/* Toggle view19_highlighting of the selected object */
if(index != 0 && index < NCUBES) {
view19_highlight[index - 1] = !view19_highlight[index - 1];
}
}
void view19_freeResources()
{
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDisable(GL_STENCIL_TEST);
glDeleteProgram(view19_program);
glDeleteBuffers(1, &view19_vbo_cube_vertices);
glDeleteBuffers(1, &view19_vbo_cube_texcoords);
glDeleteBuffers(1, &view19_ibo_cube_elements);
glDeleteTextures(1, &view19_texture_id);
}
void view19_entry(Window *window)
{
window->display = view19Display;
window->entry = viewEntry;
window->init = view19_initResources;
window->free = view19_freeResources;
window->idle = view19Idle;
window->reshape = view19Reshape;
window->special = view19Special;
}
#ifdef TEST_ALONE
int main(int argc, char *argv[])
{
Window window;
resetWindow(&window);
view19_entry(&window);
if (mini_initWindow(argc, argv, &window) == 0){
glutMainLoop();
}
return 0;
}
#endif /* TEST_ALONE */