-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview6_cube.cpp
175 lines (147 loc) · 4.48 KB
/
view6_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
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
#include <iostream>
#include <vector>
using namespace std;
#include "view6_cube.h"
#include "util.h"
#include "engine/Texture.h"
#include "engine/Mesh.h"
#include "engine/Program.h"
#include "engine/Render.h"
static Program *program = NULL;
static Texture *texture = NULL;
static Mesh *mesh = NULL;
static Render *render = NULL;
void view6CubeDisplay()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// animate
float angle = glutGet(GLUT_ELAPSED_TIME) / 1000.0 * glm::radians(15.0);
glm::mat4 anim = \
glm::rotate(glm::mat4(1.0f), angle*3.0f, glm::vec3(1, 0, 0)) *
glm::rotate(glm::mat4(1.0f), angle*2.0f, glm::vec3(0, 1, 0)) *
glm::rotate(glm::mat4(1.0f), angle*4.0f, glm::vec3(0, 0, 1));
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);
glm::mat4 mvp = projection * view * model * anim;
render->begin();
program->set_uniformMatrix4fv("mvp", 1, GL_FALSE,
glm::value_ptr(mvp));
program->set_uniform1i("mytexture", texture->getTbo());
render->draw();
render->end();
glutSwapBuffers();
}
int view6_cube_initResources()
{
mesh = new Mesh();
glm::vec4 cube_vertices[] = {
// front
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),
// top
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),
// back
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),
// bottom
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),
// left
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),
// right
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");
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,
};
for(int i = 0; i < sizeof(cube_elements); ++i){
mesh->elements.push_back(cube_elements[i]);
}
glm::vec4 cube_texture_vertices[4] = {
// front
glm::vec4(0.0, 0.0, 0.0, 0.0),
glm::vec4(1.0, 0.0, 0.0, 0.0),
glm::vec4(1.0, 1.0, 0.0, 0.0),
glm::vec4(0.0, 1.0, 0.0, 0.0),
};
for(int i = 0; i < 4 * 6; ++i){
mesh->texture_vertices.push_back(cube_texture_vertices[i%4]);
}
mesh->set_attr_tv_name("texcoord");
mesh->upload();
program = new Program("glsl/cube.6.v.glsl",
"glsl/cube.6.f.glsl");
texture = new Texture("data/res_texture.jpg");
render = new Render(mesh, program);
render->disableBlending();
return 0;
}
void view6_cube_freeResources()
{
delete texture;
delete program;
delete mesh;
delete render;
}
void view6_entry(Window *window)
{
window->display = view6CubeDisplay;
window->entry = viewEntry;
window->init = view6_cube_initResources;
window->free = view6_cube_freeResources;
}
#ifdef TEST_ALONE
int main(int argc, char *argv[])
{
Window window;
resetWindow(&window);
view6_entry(&window);
if (mini_initWindow(argc, argv, &window) == 0){
glutMainLoop();
}
return 0;
}
#endif /* TEST_ALONE */