forked from yiming-cai/OursCraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleGenerator.cpp
More file actions
232 lines (197 loc) · 6.46 KB
/
Copy pathParticleGenerator.cpp
File metadata and controls
232 lines (197 loc) · 6.46 KB
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
#define _CRT_SECURE_NO_WARNINGS
#include "ParticleGenerator.h"
#include <iostream>
const int maxParticles = 1000;
extern glm::mat4 P;
extern glm::mat4 V;
ParticleGenerator::ParticleGenerator() {
toWorld = glm::mat4(1.0f);
GLfloat particle_quad[] = {
0.0f, 1.5f, 0.0f,
1.5f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.5f, 0.0f,
1.5f, 1.5f, 0.0f,
1.5f, 0.0f, 0.0f,
};
GLfloat particle_texCoord[] = {
0.0f, 2.0f,
2.0f, 0.0f,
0.0f, 0.0f,
0.0f, 2.0f,
2.0f, 2.0f,
2.0f, 0.0f
};
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &NBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(particle_quad), particle_quad, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, NBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(particle_texCoord), particle_texCoord, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
glBindVertexArray(0);
for (GLint i = 0; i < maxParticles; ++i)
this->particles.push_back(Particle());
textureID = loadTexture();
}
ParticleGenerator::~ParticleGenerator() {
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &NBO);
}
void ParticleGenerator::update(GLfloat time, GLuint newParticles, glm::vec3 offset)
{
//new
for (GLuint i = 0; i < newParticles; ++i)
{
int unusedParticle = this->firstUnusedParticle();
this->respawnParticle(this->particles[unusedParticle], offset);
}
//update
for (GLuint i = 0; i < maxParticles; ++i)
{
Particle &p = this->particles[i];
p.Life -= time;
if (p.Life > 0.0f)
{
p.Position += p.Velocity * time;
p.Color.a -= time * 2.5;
}
}
}
void ParticleGenerator::draw(GLuint shaderProgram) {
glm::mat4 modelview = V * toWorld;
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glDisable(GL_CULL_FACE);
//use shader
glUseProgram(shaderProgram);
uProjection = glGetUniformLocation(shaderProgram, "projection");
uModelview = glGetUniformLocation(shaderProgram, "modelview");
uModel = glGetUniformLocation(shaderProgram, "model");
glUniformMatrix4fv(uProjection, 1, GL_FALSE, &P[0][0]);
glUniformMatrix4fv(uModelview, 1, GL_FALSE, &modelview[0][0]);
glUniformMatrix4fv(uModel, 1, GL_FALSE, &toWorld[0][0]);
glBindVertexArray(VAO);
for (Particle particle : this->particles)
{
if (particle.Life > 0.0f)
{
glUniform3f(glGetUniformLocation(shaderProgram, "offset"), particle.Position.x, particle.Position.y, particle.Position.z);
glUniform4f(glGetUniformLocation(shaderProgram, "color"), particle.Color.x, particle.Color.y, particle.Color.z, particle.Color.w);
glUniform1i(glGetUniformLocation(shaderProgram, "sprite"), 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
}
glBindVertexArray(0);
//reset to default blending mode
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
}
// Stores the index of the last particle used (for quick access to next dead particle)
GLuint lastUsedParticle = 0;
GLuint ParticleGenerator::firstUnusedParticle()
{
// First search from last used particle, this will usually return almost instantly
for (GLuint i = lastUsedParticle; i < maxParticles; ++i) {
if (this->particles[i].Life <= 0.0f) {
lastUsedParticle = i;
return i;
}
}
// Otherwise, do a linear search
for (GLuint i = 0; i < lastUsedParticle; ++i) {
if (this->particles[i].Life <= 0.0f) {
lastUsedParticle = i;
return i;
}
}
// All particles are taken, override the first one (note that if it repeatedly hits this case, more particles should be reserved)
lastUsedParticle = 0;
return 0;
}
void ParticleGenerator::respawnParticle(Particle &particle, glm::vec3 offset)
{
GLfloat randomX = ((rand() % 100) - 50) / 10.0f;
GLfloat randomY = ((rand() % 100) - 50) / 10.0f;
GLfloat randomZ = ((rand() % 100) - 50) / 10.0f;
GLfloat randomVelocityX = ((rand() % 100) - 50) / 100.0f;
GLfloat randomVelocityY = ((rand() % 100) - 50) / 100.0f;
GLfloat randomVelocityZ = ((rand() % 100) - 50) / 100.0f;
GLfloat rColor = 0.5 + ((rand() % 100) / 100.0f);
particle.Position = glm::vec3(randomX / 3.0f + offset.x, randomY + offset.y, offset.z + randomZ / 3.0f);
particle.Velocity = glm::vec3(0.5f + randomVelocityX, 0.5f + randomVelocityY, 0.5f + randomVelocityZ);
particle.Color = glm::vec4(rColor, rColor, rColor, 1.0f);
particle.Life = 20.0f;
}
unsigned char* ParticleGenerator::loadPPM(const char* filename, int& width, int& height)
{
const int BUFSIZE = 128;
FILE* fp;
unsigned int read;
unsigned char* rawData;
char buf[3][BUFSIZE];
char* retval_fgets;
size_t retval_sscanf;
if ((fp = fopen(filename, "rb")) == NULL)
{
std::cerr << "error reading ppm file, could not locate " << filename << std::endl;
width = 0;
height = 0;
return NULL;
}
// Read magic number:
retval_fgets = fgets(buf[0], BUFSIZE, fp);
// Read width and height:
do
{
retval_fgets = fgets(buf[0], BUFSIZE, fp);
} while (buf[0][0] == '#');
retval_sscanf = sscanf(buf[0], "%s %s", buf[1], buf[2]);
width = atoi(buf[1]);
height = atoi(buf[2]);
// Read maxval:
do
{
retval_fgets = fgets(buf[0], BUFSIZE, fp);
} while (buf[0][0] == '#');
// Read image data:
rawData = new unsigned char[width * height * 3];
read = fread(rawData, width * height * 3, 1, fp);
fclose(fp);
if (read != 1)
{
std::cerr << "error parsing ppm file, incomplete data" << std::endl;
delete[] rawData;
width = 0;
height = 0;
return NULL;
}
return rawData;
}
GLuint ParticleGenerator::loadTexture() {
glGenTextures(1, &textureID);
int width, height;
unsigned char* image;
glBindTexture(GL_TEXTURE_2D, textureID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
image = loadPPM("../assets/orange.ppm", width, height);
std::cout << "\n-----------successful loeading pparticle---------------\n" ;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);
return textureID;
}