-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloor.cpp
308 lines (249 loc) · 10.7 KB
/
Floor.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
#include "Floor.h"
#include "stb_image.h"
#include <glad/glad.h>
//#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <vector>
#include <queue>
#include <set>
#include "defineList.h"
#include <iostream>
//using namespace std;
std::vector<float> floorVertices;
std::vector<unsigned int> floorElement;
unsigned int floorVAO, floorVBO, floorEBO;
unsigned int floorTexture; // unused
unsigned int noiseTexture;
unsigned int depthTexture, depthFBO;
inline float floorRelocation(int x) {
return 1.0 * (x - (FLOOR_SIZE / 2)) / (FLOOR_SIZE / 2) * FLOOR_MAX_POSITION;
}
void genFloorVertices() {
for (int i = 0; i <= FLOOR_SIZE; ++i) {
for (int j = 0; j <= FLOOR_SIZE; ++j) {
float x = floorRelocation(i);
float y = 0;
float z = floorRelocation(j);
floorVertices.push_back(x);
floorVertices.push_back(y);
floorVertices.push_back(z);
floorVertices.push_back(FLOOR_COLOR);
floorVertices.push_back(FLOOR_COLOR);
floorVertices.push_back(FLOOR_COLOR);
floorVertices.push_back(0);
//floorVertices.push_back(0);
//floorVertices.push_back(0);
floorVertices.push_back(float(i) / 30);
floorVertices.push_back(float(j) / 30);
}
}
for (int i = 0; i < FLOOR_SIZE; ++i) {
for (int j = 0; j < FLOOR_SIZE; ++j) {
int idx = i * (FLOOR_SIZE + 1) + j;
int idy = idx + 1;
int idu = idx + (FLOOR_SIZE + 1);
int idv = idy + (FLOOR_SIZE + 1);
// a triangle
floorElement.push_back(idx);
floorElement.push_back(idy);
floorElement.push_back(idu);
// a triangle
floorElement.push_back(idu);
floorElement.push_back(idv);
floorElement.push_back(idy);
// an amazing effect !!!
// floorElement.push_back(idx);
// floorElement.push_back(idy);
// floorElement.push_back(idv);
}
}
}
bool getTextureID(unsigned int& ID, const char* s) {
int width, height, nrChannels;
unsigned char* data = stbi_load(s, &width, &height, &nrChannels, 0);
if (!data) {
//std::cerr << "Failed to open img." << std::endl;
return false;
}
glGenTextures(1, &ID);
glBindTexture(GL_TEXTURE_2D, ID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
return true;
}
// utility function for loading a 2D texture from file
// ---------------------------------------------------
unsigned int loadTexture(char const* path)
{
unsigned int textureID;
glGenTextures(1, &textureID);
int width, height, nrComponents;
unsigned char* data = stbi_load(path, &width, &height, &nrComponents, 0);
if (data)
{
GLenum format;
if (nrComponents == 1)
format = GL_RED;
else if (nrComponents == 3)
format = GL_RGB;
else if (nrComponents == 4)
format = GL_RGBA;
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, format == GL_RGBA ? GL_CLAMP_TO_EDGE : GL_REPEAT); // for this tutorial: use GL_CLAMP_TO_EDGE to prevent semi-transparent borders. Due to interpolation it takes texels from next repeat
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, format == GL_RGBA ? GL_CLAMP_TO_EDGE : GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
stbi_image_free(data);
}
else
{
std::cout << "Texture failed to load at path: " << path << std::endl;
stbi_image_free(data);
}
return textureID;
}
void floorInit(std::string NoiseImgPath) {
genFloorVertices();
glGenVertexArrays(1, &floorVAO);
glGenBuffers(1, &floorVBO);
glGenBuffers(1, &floorEBO);
glBindVertexArray(floorVAO);
glBindBuffer(GL_ARRAY_BUFFER, floorVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * floorVertices.size(), &floorVertices[0], GL_STATIC_DRAW);
unsigned int floorElementSize = FLOOR_ELEMENT_COUNT * sizeof(float);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, floorElementSize, (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, floorElementSize, (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, floorElementSize, (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, floorElementSize, (void*)(7 * sizeof(float)));
glEnableVertexAttribArray(3);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, floorEBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * floorElement.size(), &floorElement[0], GL_STATIC_DRAW);
//if (!getTextureID(noiseTexture, NoiseImgPath.c_str())) {
// // std::cerr<<"get noise texture failed!"<<std::endl;
//}
noiseTexture = loadTexture(NoiseImgPath.c_str());
glBindVertexArray(0);
}
void drawFloor() {
glBindVertexArray(floorVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, noiseTexture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, depthTexture);
glBindBuffer(GL_ARRAY_BUFFER, floorVBO);
//glEnable(GL_BLEND);
//glColor3f(0.0f, 0.5f, 1.0f, 0.3f);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * floorVertices.size(), &floorVertices[0], GL_STATIC_DRAW);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, FLOOR_ELEMENT_COUNT * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
glDrawElements(GL_TRIANGLES, sizeof(unsigned int) * floorElement.size(), GL_UNSIGNED_INT, 0);
}
void plant(float x, float z, glm::vec3 color) {
using namespace std;
static const char dx[] = { 0, 0, -1, 1 };
static const char dy[] = { 1, -1, 0, 0 };
static queue<BulletPos > q;
static set<BulletPos> s;
if (!checkPos(x, z)) {
//std::cerr << "Error BulletPos " << std::endl;
return;
}
s.clear();
BulletPos src(int(x / FLOOR_UNIT) + (FLOOR_SIZE / 2), int(z / FLOOR_UNIT) + (FLOOR_SIZE / 2));
//cerr << "Going to plant (" << src.x << ", " << src.z << ") : " << src.mapToId() << endl;
q.push(src);
while (!q.empty()) {
BulletPos nowPos = q.front();
q.pop();
if (!nowPos.checkPos()) continue;
// distance between this position and bullet position
float dis = BulletPos::distance(nowPos, src);
// check whether need render
if (dis > OUT_RADIUS * OUT_RADIUS) continue;
int id = nowPos.mapToId();
int idStartPos = id * FLOOR_ELEMENT_COUNT;
float& colorAlpha = floorVertices[idStartPos + 6];
if (dis <= IN_RADIUS * IN_RADIUS) {
colorAlpha = 1.0f;
}
else { // calc difference
float tFactor = 1.0f / (OUT_RADIUS - IN_RADIUS);
float d = sqrt(dis);
float t = 1.0f - (d - IN_RADIUS) * tFactor;
colorAlpha = std::min(colorAlpha + t, 1.0f);
}
floorVertices[idStartPos + 3] = color[0];
floorVertices[idStartPos + 4] = color[1];
floorVertices[idStartPos + 5] = color[2];
// floorVertices[idStartPos + 3] = color[0]*colorAlpha + floorVertices[idStartPos + 3]*(1-colorAlpha);
// floorVertices[idStartPos + 4] = color[1]*colorAlpha + floorVertices[idStartPos + 4]*(1-colorAlpha);
// floorVertices[idStartPos + 5] = color[2]*colorAlpha + floorVertices[idStartPos + 5]*(1-colorAlpha);
// find adjacent
for (int i = 0; i < 4; ++i) {
BulletPos nxt(nowPos + BulletPos(dx[i], dy[i]));
if (s.find(nxt) == s.end()) {
s.insert(nxt);
q.push(nxt);
}
}
}
}
glm::vec3 getColorAt(float x, float z) {
int id = BulletPos(int(x / FLOOR_UNIT) + (FLOOR_SIZE / 2), int(z / FLOOR_UNIT) + (FLOOR_SIZE / 2)).mapToId();
int idStartPos = id * FLOOR_ELEMENT_COUNT;
return glm::vec3(floorVertices[idStartPos + 3], floorVertices[idStartPos + 4], floorVertices[idStartPos + 5]);
}
BulletPos::BulletPos(const int& a, const int& b) : x(a), z(b) {}
float BulletPos::distance(const BulletPos& a, const BulletPos& b) {
return (a.x - b.x) * (a.x - b.x) + (a.z - b.z) * (a.z - b.z);
}
int BulletPos::mapToId() {
return GET_POS(x, z);
}
bool BulletPos::checkPos() {
return x >= 0 && x < FLOOR_SIZE && z >= 0 && z < FLOOR_SIZE;
}
bool checkPos(float x, float z) {
return x >= -FLOOR_MAX_POSITION && x <= FLOOR_MAX_POSITION && z >= -FLOOR_MAX_POSITION && z <= FLOOR_MAX_POSITION;
}
void shadowInit() {
glGenFramebuffers(1, &depthFBO);
//Generate a empty texture
glGenTextures(1, &depthTexture);
glBindTexture(GL_TEXTURE_2D, depthTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Bind frame buffer
glBindFramebuffer(GL_FRAMEBUFFER, depthFBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void shadowBind() {
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
glBindFramebuffer(GL_FRAMEBUFFER, depthFBO);
glClear(GL_DEPTH_BUFFER_BIT);
}
void shadowUnBind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
#ifdef __APPLE__
glViewport(0, 0, SCR_WIDTH * 2, SCR_HEIGHT * 2); // TODO: Ϊʲô SCR_WIDTH ÐèÒªÉèÖÃΪ 2 ±¶ £¿
#else
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
#endif
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}