-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathParticles.h
More file actions
163 lines (129 loc) · 5.15 KB
/
Particles.h
File metadata and controls
163 lines (129 loc) · 5.15 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
/* SimShip by Edouard Halbert
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License
http://creativecommons.org/licenses/by-nc-nd/4.0/ */
#pragma once
#include "Shader.h"
#include "Camera.h"
#include <glm/glm.hpp>
#include <vector>
using namespace std;
using namespace glm;
struct ParticleGPU
{
vec3 position;
vec3 velocity;
float life;
vec4 color;
};
class Spray
{
public:
bool bVisible = true;
Spray()
{
lifeSpan = (int)((longLife - shortLife) * 10.0f) + 1;
mvParticles.resize(mMaxParticles);
// Initializing the MultiMesh
glGenBuffers(1, &mVBO);
glBindBuffer(GL_ARRAY_BUFFER, mVBO);
glBufferData(GL_ARRAY_BUFFER, mMaxParticles * sizeof(ParticleGPU), nullptr, GL_DYNAMIC_DRAW);
glGenVertexArrays(1, &mVAO);
glBindVertexArray(mVAO);
// Position (attribute 0)
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(ParticleGPU), (void*)0);
glVertexAttribDivisor(0, 1);
// Velocity (attribute 1)
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(ParticleGPU), (void*)(3 * sizeof(float)));
glVertexAttribDivisor(1, 1);
// Life (attribute 2)
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(ParticleGPU), (void*)(6 * sizeof(float)));
glVertexAttribDivisor(2, 1);
// Color (attribute 3)
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(ParticleGPU), (void*)(7 * sizeof(float)));
glVertexAttribDivisor(3, 1);
glBindVertexArray(0);
glDepthMask(GL_FALSE);
mShader = make_unique<Shader>("Resources/Ship/spray.vert", "Resources/Ship/spray.frag");
glDepthMask(GL_TRUE);
}
~Spray()
{
glDeleteBuffers(1, &mVBO);
glDeleteVertexArrays(1, &mVAO);
}
void Emit(vec3 position, vec3 velocity)
{
if (mActiveParticles >= mMaxParticles)
return;
vec3 randomOffset = vec3( (rand() % 100 - 50) / 1000.0f, (rand() % 100 - 50) / 1000.0f, (rand() % 100 - 50) / 1000.0f );
mvParticles[mActiveParticles].position = position + randomOffset;
// Ajout de variation aléatoire pour un mouvement naturel
vec3 randomVelocity = vec3( (rand() % 100 - 50) / 500.0f, (rand() % 100 - 50) / 500.0f, (rand() % 100 - 50) / 500.0f );
mvParticles[mActiveParticles].velocity = velocity + randomVelocity;
mvParticles[mActiveParticles].life = shortLife + (rand() % lifeSpan) / 10.0f;
float gray = 0.9f + static_cast<float>(rand()) / static_cast<float>(RAND_MAX) * 0.1f;
mvParticles[mActiveParticles].color = vec4(gray, gray, gray, 0.8f);
mActiveParticles++;
}
void Update(float deltaTime)
{
const float gravity = 9.81f;
for (int i = 0; i < mActiveParticles; ++i)
{
mvParticles[i].life -= deltaTime;
if (mvParticles[i].life < 0) // Underwater
{
// Remplace la particule morte par la dernière
mvParticles[i] = mvParticles[mActiveParticles - 1];
mActiveParticles--;
i--;
continue;
}
// Applique la gravité (descente verticale)
mvParticles[i].velocity.y -= gravity * deltaTime;
// Turbulence légère
float turbulenceStrength = 50.0f;
mvParticles[i].velocity += turbulenceStrength * vec3( (rand() % 100 - 50) / 100.0f, (rand() % 100 - 50) / 100.0f, (rand() % 100 - 50) / 100.0f ) * deltaTime;
// Mise à jour position
mvParticles[i].position += mvParticles[i].velocity * deltaTime;
}
UpdateParticleBuffers();
}
void UpdateParticleBuffers()
{
glBindBuffer(GL_ARRAY_BUFFER, mVBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, mActiveParticles * sizeof(ParticleGPU), mvParticles.data());
}
float GetNbActiveParticles()
{
return mActiveParticles;
}
void Render(Camera& camera, float density, float exposure)
{
if (!bVisible)
return;
mShader->use();
mShader->setMat4("view", camera.GetView());
mShader->setMat4("projection", camera.GetProjection());
mShader->setFloat("density", density);
mShader->setFloat("lifeSpan", lifeSpan);
mShader->setFloat("exposure", exposure);
glBindVertexArray(mVAO);
glDrawArraysInstanced(GL_POINTS, 0, 1, mActiveParticles);
glBindVertexArray(0);
}
private:
static const int mMaxParticles = 50000;
vector<ParticleGPU> mvParticles;
GLuint mVBO;
GLuint mVAO;
unique_ptr<Shader> mShader;
int mActiveParticles = 0;
const float shortLife = 1.0f;
const float longLife = 2.0f;
int lifeSpan;
};