-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_texture.cpp
299 lines (235 loc) · 9.51 KB
/
test_texture.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
#include <softrender/glSoft.h>
#include "utils/TestUtil.h"
#include "program/shader.h"
#include "program/texture.h"
#include <cmath>
class TestTexture:public TestUtil
{
public:
void GenVboIbo()
{
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// texture coord attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
unsigned int EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}
void GenVboIbo2()
{
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
// positions // colors // texture coords (note that we changed them to 2.0f!)
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 2.0f, 2.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 2.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 2.0f // top left
};
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// texture coord attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
unsigned int EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}
protected:
void _processInput(GLFWwindow* window)override
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
{
m_mix += 0.001f; // change this value accordingly (might be too slow or too fast based on system hardware)
if(m_mix >= 1.0f)
m_mix = 1.0f;
}
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
{
m_mix -= 0.001f; // change this value accordingly (might be too slow or too fast based on system hardware)
if (m_mix <= 0.0f)
m_mix = 0.0f;
}
}
float m_mix=0;
};
TEST_F(TestTexture, merge)
{
unsigned int VAO;
glGenVertexArrays(1, &VAO);
// 1. 绑定VAO
glBindVertexArray(VAO);
// 2. 把顶点数组复制到缓冲中供OpenGL使用
GenVboIbo();
// 3. 生成纹理
auto texture1= generateTexture("driver/awesomeface.png");
// build and compile our shader program
// ------------------------------------
Shader ourShader("driver/test_texture/laugh.vs", "driver/test_texture/merge.fs"); // you can name your shader files however you like
auto fn=[&](int frame)
{
ourShader.use();
ourShader.setTextureUnit("ourTexture", texture1, 0);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
};
PlayWindow(fn,20);
glDeleteVertexArrays(1, &VAO);
glDeleteProgram(ourShader.ID);
}
TEST_F(TestTexture, laugh)
{
unsigned int VAO;
glGenVertexArrays(1, &VAO);
// 1. 绑定VAO
glBindVertexArray(VAO);
// 2. 把顶点数组复制到缓冲中供OpenGL使用
GenVboIbo();
// 3. 生成纹理
auto texture1= generateTexture("driver/container.jpg");
auto texture2= generateTexture("driver/awesomeface.png");
// build and compile our shader program
// ------------------------------------
Shader ourShader("driver/test_texture/laugh.vs", "driver/test_texture/laugh.fs"); // you can name your shader files however you like
auto fn=[&](int frame)
{
ourShader.use();
ourShader.setTextureUnit("texture1", texture1, 0);
ourShader.setTextureUnit("texture2", texture2, 1);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
};
PlayWindow(fn,20);
glDeleteVertexArrays(1, &VAO);
glDeleteProgram(ourShader.ID);
}
TEST_F(TestTexture, right)
{
unsigned int VAO;
glGenVertexArrays(1, &VAO);
// 1. 绑定VAO
glBindVertexArray(VAO);
// 2. 把顶点数组复制到缓冲中供OpenGL使用
GenVboIbo();
// 3. 生成纹理
auto texture1= generateTexture("driver/container.jpg");
auto texture2= generateTexture("driver/awesomeface.png");
// build and compile our shader program
// ------------------------------------
Shader ourShader("driver/test_texture/laugh.vs", "driver/test_texture/right.fs"); // you can name your shader files however you like
auto fn=[&](int frame)
{
ourShader.use();
ourShader.setTextureUnit("texture1", texture1, 0);
ourShader.setTextureUnit("texture2", texture2, 1);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
};
PlayWindow(fn,20);
glDeleteVertexArrays(1, &VAO);
glDeleteProgram(ourShader.ID);
}
TEST_F(TestTexture, wrapping)
{
unsigned int VAO;
glGenVertexArrays(1, &VAO);
// 1. 绑定VAO
glBindVertexArray(VAO);
// 2. 把顶点数组复制到缓冲中供OpenGL使用
GenVboIbo2();
// 3. 生成纹理
auto texture1= generateTexture("driver/container.jpg");
// note that we set the container wrapping method to GL_CLAMP_TO_EDGE
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
auto texture2= generateTexture("driver/awesomeface.png");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// we want to repeat the awesomeface pattern so we kept it at GL_REPEAT
// build and compile our shader program
// ------------------------------------
Shader ourShader("driver/test_texture/laugh.vs", "driver/test_texture/laugh.fs"); // you can name your shader files however you like
auto fn=[&](int frame)
{
ourShader.use();
ourShader.setTextureUnit("texture1", texture1, 0);
ourShader.setTextureUnit("texture2", texture2, 1);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
};
PlayWindow(fn,20);
glDeleteVertexArrays(1, &VAO);
glDeleteProgram(ourShader.ID);
}
TEST_F(TestTexture, mix)
{
unsigned int VAO;
glGenVertexArrays(1, &VAO);
// 1. 绑定VAO
glBindVertexArray(VAO);
// 2. 把顶点数组复制到缓冲中供OpenGL使用
GenVboIbo2();
// 3. 生成纹理
auto texture1= generateTexture("driver/container.jpg");
// note that we set the container wrapping method to GL_CLAMP_TO_EDGE
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
auto texture2= generateTexture("driver/awesomeface.png");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// we want to repeat the awesomeface pattern so we kept it at GL_REPEAT
// build and compile our shader program
// ------------------------------------
Shader ourShader("driver/test_texture/laugh.vs", "driver/test_texture/mix.fs"); // you can name your shader files however you like
auto fn=[&](int frame)
{
ourShader.use();
ourShader.setFloat("mixIndex",m_mix+frame/100.0);
ourShader.setTextureUnit("texture1", texture1, 0);
ourShader.setTextureUnit("texture2", texture2, 1);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
};
PlayWindow(fn,100);
glDeleteVertexArrays(1, &VAO);
glDeleteProgram(ourShader.ID);
}