-
Notifications
You must be signed in to change notification settings - Fork 0
/
particles-3d.cpp
277 lines (253 loc) · 8.36 KB
/
particles-3d.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
// particles-3d.cpp : Devon McKee
#define _USE_MATH_DEFINES
#include <glad.h>
#include <GLFW/glfw3.h>
#include "GLXtras.h"
#include <time.h>
#include <vector>
#include "VecMat.h"
#include "Camera.h"
#include "Mesh.h"
#include "Misc.h"
GLuint vBuffer = 0;
GLuint program = 0;
GLuint texUnit = 0;
GLuint texName;
const vec3 GRAVITY = vec3(0.0f, -0.0025f, 0.0f);
const float LIFE_DT = 0.02f;
const float PARTICLE_SIZE = 0.03f;
const vec2 H_RANGE = vec2(-0.01f, 0.01f);
const vec2 V_RANGE = vec2(0.03f, 0.06f);
const int NUM_PARTICLES_SPAWNED = 10;
const char* texture = "textures/lava.tga";
int win_width = 800;
int win_height = 800;
Camera camera((float)win_width / win_height, vec3(0, 0, 0), vec3(0, 0, -5));
vec3 lightSource = vec3(1, 1, 0);
float rand_float(float min = 0, float max = 1) { return min + (float)rand() / (RAND_MAX / (max - min)); }
vec3 rand_vec3(float min = -1, float max = 1) { return vec3(min + (float)rand() / (RAND_MAX / (max - min)), min + (float)rand() / (RAND_MAX / (max - min)), min + (float)rand() / (RAND_MAX / (max - min))); }
struct Particle {
vec3 pos, vel;
vec4 col;
float life;
Particle() {
pos = vec3(0.0f), vel = vec3(0.0f), col = vec4(1.0f), life = 0.0f;
}
void Revive(vec3 new_pos = vec3(0.0f)) {
life = 1.0f;
pos = new_pos;
vel = vec3(
rand_float(H_RANGE.x, H_RANGE.y),
rand_float(V_RANGE.x, V_RANGE.y),
rand_float(H_RANGE.x, H_RANGE.y)
);
}
void Run() {
if (life > 0.0f) {
life -= LIFE_DT;
pos += vel;
vel += GRAVITY;
col = vec4(1.0f, 1 - life, 0.0f, 0.0f);
}
}
};
int num_particles = 1000;
std::vector<Particle> particles = std::vector<Particle>();
int lastUsedParticle = 0;
float cube_points[][3] = {
{-1, -1, 1}, {1, -1, 1}, {1, 1, 1}, {-1, 1, 1}, // front (0, 1, 2, 3)
{-1, -1, -1}, {1, -1, -1}, {1, 1, -1}, {-1, 1, -1}, // back (4, 5, 6, 7)
{-1, -1, 1}, {-1, -1, -1}, {-1, 1, -1}, {-1, 1, 1}, // left (8, 9, 10, 11)
{1, -1, 1}, {1, -1, -1}, {1, 1, -1}, {1, 1, 1}, // right (12, 13, 14, 15)
{-1 , 1, 1}, {1, 1, 1}, {1, 1, -1}, {-1, 1, -1}, // top (16, 17, 18, 19)
{-1, -1, 1}, {1, -1, 1}, {1, -1, -1}, {-1, -1, -1} // bottom (20, 21, 22, 23)
};
float cube_normals[][3] = {
{0, 0, 1}, {0, 0, 1}, {0, 0, 1}, {0, 0, 1}, // front
{0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, // back
{-1, 0, 0}, {-1, 0, 0}, {-1, 0, 0}, {-1, 0, 0}, // left
{1, 0, 0}, {1, 0, 0}, {1, 0, 0}, {1, 0, 0}, // right
{0, 1, 0}, {0, 1, 0}, {0, 1, 0}, {0, 1, 0}, // top
{0, -1, 0}, {0, -1, 0}, {0, -1, 0}, {0, -1, 0} // bottom
};
float cube_uvs[][2] = {
{0.25, 0}, {0.5, 0}, {0.5, 0.33333}, {0.25, 0.33333},
{0.25, 1}, {0.5, 1}, {0.5, 0.66666}, {0.25, 0.66666},
{0, 0.33333}, {0, 0.66666}, {0.25, 0.66666}, {0.25, 0.33333},
{0.75, 0.33333}, {0.75, 0.66666}, {0.5, 0.66666}, {0.5, 0.33333},
{0.25, 0.33333}, {0.5, 0.33333}, {0.5, 0.666}, {0.25, 0.66666},
{1, 0.33333}, {0.75, 0.33333}, {0.75, 0.66666}, {1, 0.66666}
};
int cube_triangles[][3] = {
{0, 1, 2}, {2, 3, 0}, // front
{4, 5, 6}, {6, 7, 4}, // back
{8, 9, 10}, {10, 11, 8}, // left
{12, 13, 14}, {14, 15, 12}, // right
{16, 17, 18}, {18, 19, 16}, // top
{20, 21, 22}, {22, 23, 20} // bottom
};
const char* vertexShader = R"(
#version 130
in vec3 point;
in vec3 normal;
in vec2 uv;
out vec3 vPoint;
out vec3 vNormal;
out vec2 vuv;
uniform mat4 modelview;
uniform mat4 persp;
void main() {
vPoint = (modelview * vec4(point, 1)).xyz;
vNormal = (modelview * vec4(normal, 0)).xyz;
gl_Position = persp * vec4(vPoint, 1);
vuv = uv;
}
)";
const char* fragmentShader = R"(
#version 130
in vec3 vPoint;
in vec3 vNormal;
in vec2 vuv;
out vec4 pColor;
uniform vec3 light = vec3(-.2, .1, -3);
uniform vec4 color = vec4(1.0, 1.0, 0.4, 1);
uniform float amb = 0.6; // ambient intensity
uniform float dif = 0.7; // diffusivity
uniform float spc = 0.5; // specularity
uniform sampler2D texImage;
void main() {
vec3 N = normalize(vNormal); // surface normal
vec3 L = normalize(light-vPoint); // light vector
vec3 E = normalize(vPoint); // eye vertex
vec3 R = reflect(L, N); // highlight vector
float d = dif*max(0, dot(N, L)); // one-sided Lambert
float h = max(0, dot(R, E)); // highlight term
float s = spc*pow(h, 100); // specular term
float intensity = clamp(amb+d+s, 0, 1);
pColor = vec4(intensity*color.rgb, 1) * texture(texImage, vuv);
}
)";
void Resize(GLFWwindow* window, int width, int height) {
camera.Resize(win_width = width, win_height = height);
glViewport(0, 0, win_width, win_height);
}
void Keyboard(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
}
bool Shift(GLFWwindow* w) {
return glfwGetKey(w, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS ||
glfwGetKey(w, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS;
}
void MouseButton(GLFWwindow* w, int butn, int action, int mods) {
double x, y;
glfwGetCursorPos(w, &x, &y);
y = win_height - y;
if (action == GLFW_PRESS)
camera.MouseDown((int)x, (int)y);
if (action == GLFW_RELEASE)
camera.MouseUp();
}
void MouseMove(GLFWwindow* w, double x, double y) {
if (glfwGetMouseButton(w, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) { // drag
y = win_height - y;
camera.MouseDrag((int)x, (int)y, Shift(w));
}
}
void MouseWheel(GLFWwindow* w, double ignore, double spin) {
camera.MouseWheel(spin > 0, Shift(w));
}
int FindNextParticle() {
// Search for dead particle from last used particle
for (int i = lastUsedParticle; i < num_particles; i++) {
if (particles[i].life <= 0.0f) {
lastUsedParticle = i;
return i;
}
}
// Search linearly through rest of particles
for (int i = 0; i < lastUsedParticle; i++) {
if (particles[i].life <= 0.0f) {
lastUsedParticle = i;
return i;
}
}
// If no dead particles exist, simply take the first particle
lastUsedParticle = 0;
return 0;
}
void InitVertexBuffer() {
glGenBuffers(1, &vBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vBuffer);
size_t size = sizeof(cube_points) + sizeof(cube_normals) + sizeof(cube_uvs);
glBufferData(GL_ARRAY_BUFFER, size, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(cube_points), cube_points);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(cube_points), sizeof(cube_normals), cube_normals);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(cube_points) + sizeof(cube_normals), sizeof(cube_uvs), cube_uvs);
}
void Display() {
glUseProgram(program);
glClearColor(0.3f, 0.3f, 0.3f, 1.);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
SetUniform(program, "persp", camera.persp);
VertexAttribPointer(program, "point", 3, 0, (void*)0);
VertexAttribPointer(program, "normal", 3, 0, (void*)sizeof(cube_points));
VertexAttribPointer(program, "uv", 2, 0, (void*)(sizeof(cube_points) + sizeof(cube_normals)));
glActiveTexture(GL_TEXTURE0 + texUnit);
glBindTexture(GL_TEXTURE_2D, texName);
SetUniform(program, "texImage", (int)texUnit);
for (int i = 0; i < num_particles; i++) {
if (particles[i].life > 0.0f) {
particles[i].Run();
mat4 scale = Scale(PARTICLE_SIZE);
mat4 trans = Translate(particles[i].pos);
mat4 modelview = camera.modelview * trans * scale;
SetUniform(program, "modelview", modelview);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, cube_triangles);
}
}
glFlush();
}
int main() {
srand((int)time(NULL));
if (!glfwInit())
return 1;
GLFWwindow* window = glfwCreateWindow(win_width, win_height, "Particles 3D", NULL, NULL);
if (!window) {
glfwTerminate();
return 1;
}
glfwSetWindowPos(window, 100, 100);
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
PrintGLErrors();
if (!(program = LinkProgramViaCode(&vertexShader, &fragmentShader)))
return 1;
InitVertexBuffer();
texName = LoadTexture(texture, texUnit);
for (int i = 0; i < num_particles; i++) {
particles.push_back(Particle());
}
glfwSetCursorPosCallback(window, MouseMove);
glfwSetMouseButtonCallback(window, MouseButton);
glfwSetScrollCallback(window, MouseWheel);
glfwSetKeyCallback(window, Keyboard);
glfwSetWindowSizeCallback(window, Resize);
glfwSwapInterval(1);
while (!glfwWindowShouldClose(window)) {
particles[FindNextParticle()].Revive();
Display();
glfwPollEvents();
glfwSwapBuffers(window);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &vBuffer);
glDeleteBuffers(1, &texName);
glfwDestroyWindow(window);
glfwTerminate();
}