-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview5_cube.cpp
144 lines (117 loc) · 3.37 KB
/
view5_cube.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
#include <iostream>
#include <vector>
using namespace std;
#include "view5_cube.h"
#include "util.h"
#include "engine/Mesh.h"
#include "engine/Program.h"
#include "engine/Render.h"
static Program *program = NULL;
static Mesh *mesh = NULL;
static Render *render = NULL;
void view5CubeDisplay()
{
glClearColor(1.0, 1.0, 1.0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// TODO: perspective internal
// TODO: move to mesh and clean this
glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0, 0.0, -4.0));
glm::mat4 view = glm::lookAt(glm::vec3(0.0, 2.0, 0.0),
glm::vec3(0.0, 0.0, -4.0),
glm::vec3(0.0, 1.0, 0.0));
glm::mat4 projection = glm::perspective(45.0f, 1.0f*128/128, 0.1f, 10.0f);
float angle = glutGet(GLUT_ELAPSED_TIME) / 1000.0 * 3; // 5° per second
glm::vec3 axis_y(0.0, 1.0, 0.0);
glm::mat4 anim = glm::rotate(glm::mat4(1.0f), angle, axis_y);
glm::mat4 mvp = projection * view * model * anim;
render->begin();
program->set_uniformMatrix4fv("mvp", 1, GL_FALSE, glm::value_ptr(mvp));
render->draw();
render->end();
glutSwapBuffers();
}
int view5_cube_initResources()
{
mesh = new Mesh();
glm::vec4 cube_vertices[8] = {
glm::vec4(-1.0, -1.0, 1.0, 1.0),
glm::vec4(1.0, -1.0, 1.0, 1.0),
glm::vec4(1.0, 1.0, 1.0, 1.0),
glm::vec4(-1.0, 1.0, 1.0, 1.0),
glm::vec4(-1.0, -1.0, -1.0, 1.0),
glm::vec4(1.0, -1.0, -1.0, 1.0),
glm::vec4(1.0, 1.0, -1.0, 1.0),
glm::vec4(-1.0, 1.0, -1.0, 1.0),
};
for(int i = 0; i < sizeof(cube_vertices); ++i){
mesh->vertices.push_back(cube_vertices[i]);
}
mesh->set_attr_v_name("coord3d");
glm::vec4 cube_colors[8] = {
glm::vec4(1.0, 0.0, 0.0, 0.0),
glm::vec4(0.0, 1.0, 0.0, 0.0),
glm::vec4(0.0, 0.0, 1.0, 0.0),
glm::vec4(1.0, 1.0, 1.0, 0.0),
glm::vec4(1.0, 0.0, 0.0, 0.0),
glm::vec4(0.0, 1.0, 0.0, 0.0),
glm::vec4(0.0, 0.0, 1.0, 0.0),
glm::vec4(1.0, 1.0, 1.0, 0.0),
};
for(int i = 0; i < sizeof(cube_colors); ++i){
mesh->colors.push_back(cube_colors[i]);
}
mesh->set_attr_c_name("v_color");
GLushort cube_elements[] = {
// front
0, 1, 2,
2, 3, 0,
// top
3, 2, 6,
6, 7, 3,
// back
7, 6, 5,
5, 4, 7,
// bottom
4, 5, 1,
1, 0, 4,
// left
4, 0, 3,
3, 7, 4,
// right
1, 5, 6,
6, 2, 1,
};
for(int i = 0; i < sizeof(cube_elements); ++i){
mesh->elements.push_back(cube_elements[i]);
}
mesh->upload();
program = new Program("glsl/cube.5.v.glsl",
"glsl/cube.5.f.glsl");
render = new Render(mesh, program);
return 0;
}
void view5_cube_freeResources()
{
delete program;
delete mesh;
delete render;
}
void view5_entry(Window *window)
{
window->display = view5CubeDisplay;
window->entry = viewEntry;
window->init = view5_cube_initResources;
window->free = view5_cube_freeResources;
}
#ifdef TEST_ALONE
int main(int argc, char *argv[])
{
Window window;
resetWindow(&window);
view5_entry(&window);
if (mini_initWindow(argc, argv, &window) == 0){
glutMainLoop();
}
return 0;
}
#endif /* TEST_ALONE */