mirror of
https://github.com/overte-org/overte.git
synced 2025-08-19 10:24:58 +02:00
More coding standard fixes to particle cloud
This commit is contained in:
parent
4afa82d626
commit
ab4b09ca39
4 changed files with 65 additions and 95 deletions
|
@ -16,18 +16,17 @@ const int NUM_PARTICLES = 100000;
|
|||
|
||||
Cloud::Cloud() {
|
||||
// Create and initialize particles
|
||||
unsigned int i;
|
||||
glm::vec3 box = glm::vec3(PARTICLE_WORLD_SIZE);
|
||||
bounds = box;
|
||||
count = NUM_PARTICLES;
|
||||
particles = new Particle[count];
|
||||
field = new Field(PARTICLE_WORLD_SIZE);
|
||||
_bounds = box;
|
||||
_count = NUM_PARTICLES;
|
||||
_particles = new Particle[_count];
|
||||
_field = new Field(PARTICLE_WORLD_SIZE);
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
particles[i].position = randVector() * box;
|
||||
for (int i = 0; i < _count; i++) {
|
||||
_particles[i].position = randVector() * box;
|
||||
const float INIT_VEL_SCALE = 0.03f;
|
||||
particles[i].velocity = randVector() * ((float)PARTICLE_WORLD_SIZE * INIT_VEL_SCALE);
|
||||
particles[i].color = randVector();
|
||||
_particles[i].velocity = randVector() * ((float)PARTICLE_WORLD_SIZE * INIT_VEL_SCALE);
|
||||
_particles[i].color = randVector();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,53 +37,53 @@ void Cloud::render() {
|
|||
glDisable(GL_TEXTURE_2D);
|
||||
glEnable(GL_POINT_SMOOTH);
|
||||
glBegin(GL_POINTS);
|
||||
for (unsigned int i = 0; i < count; i++)
|
||||
for (unsigned int i = 0; i < _count; i++)
|
||||
{
|
||||
glColor3f(particles[i].color.x,
|
||||
particles[i].color.y,
|
||||
particles[i].color.z);
|
||||
glVertex3f(particles[i].position.x,
|
||||
particles[i].position.y,
|
||||
particles[i].position.z);
|
||||
glColor3f(_particles[i].color.x,
|
||||
_particles[i].color.y,
|
||||
_particles[i].color.z);
|
||||
glVertex3f(_particles[i].position.x,
|
||||
_particles[i].position.y,
|
||||
_particles[i].position.z);
|
||||
}
|
||||
glEnd();
|
||||
}
|
||||
|
||||
void Cloud::simulate (float deltaTime) {
|
||||
unsigned int i;
|
||||
field->simulate(deltaTime);
|
||||
for (i = 0; i < count; ++i) {
|
||||
_field->simulate(deltaTime);
|
||||
for (i = 0; i < _count; ++i) {
|
||||
|
||||
// Update position
|
||||
particles[i].position += particles[i].velocity * deltaTime;
|
||||
_particles[i].position += _particles[i].velocity * deltaTime;
|
||||
|
||||
// Decay Velocity (Drag)
|
||||
const float CONSTANT_DAMPING = 0.15f;
|
||||
particles[i].velocity *= (1.f - CONSTANT_DAMPING * deltaTime);
|
||||
_particles[i].velocity *= (1.f - CONSTANT_DAMPING * deltaTime);
|
||||
|
||||
// Interact with Field
|
||||
const float FIELD_COUPLE = 0.005f;
|
||||
field->interact(deltaTime,
|
||||
&particles[i].position,
|
||||
&particles[i].velocity,
|
||||
&particles[i].color,
|
||||
_field->interact(deltaTime,
|
||||
&_particles[i].position,
|
||||
&_particles[i].velocity,
|
||||
&_particles[i].color,
|
||||
FIELD_COUPLE);
|
||||
|
||||
// Update color to velocity
|
||||
particles[i].color = (glm::normalize(particles[i].velocity) * 0.5f) + 0.5f;
|
||||
_particles[i].color = (glm::normalize(_particles[i].velocity) * 0.5f) + 0.5f;
|
||||
|
||||
// Bounce at bounds
|
||||
if ((particles[i].position.x > bounds.x) || (particles[i].position.x < 0.f)) {
|
||||
particles[i].position.x = glm::clamp(particles[i].position.x, 0.f, bounds.x);
|
||||
particles[i].velocity.x *= -1.f;
|
||||
if ((_particles[i].position.x > _bounds.x) || (_particles[i].position.x < 0.f)) {
|
||||
_particles[i].position.x = glm::clamp(_particles[i].position.x, 0.f, _bounds.x);
|
||||
_particles[i].velocity.x *= -1.f;
|
||||
}
|
||||
if ((particles[i].position.y > bounds.y) || (particles[i].position.y < 0.f)) {
|
||||
particles[i].position.y = glm::clamp(particles[i].position.y, 0.f, bounds.y);
|
||||
particles[i].velocity.y *= -1.f;
|
||||
if ((_particles[i].position.y > _bounds.y) || (_particles[i].position.y < 0.f)) {
|
||||
_particles[i].position.y = glm::clamp(_particles[i].position.y, 0.f, _bounds.y);
|
||||
_particles[i].velocity.y *= -1.f;
|
||||
}
|
||||
if ((particles[i].position.z > bounds.z) || (particles[i].position.z < 0.f)) {
|
||||
particles[i].position.z = glm::clamp(particles[i].position.z, 0.f, bounds.z);
|
||||
particles[i].velocity.z *= -1.f;
|
||||
if ((_particles[i].position.z > _bounds.z) || (_particles[i].position.z < 0.f)) {
|
||||
_particles[i].position.z = glm::clamp(_particles[i].position.z, 0.f, _bounds.z);
|
||||
_particles[i].velocity.z *= -1.f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,18 +16,17 @@
|
|||
class Cloud {
|
||||
public:
|
||||
Cloud();
|
||||
|
||||
void simulate(float deltaTime);
|
||||
void render();
|
||||
|
||||
private:
|
||||
struct Particle {
|
||||
glm::vec3 position, velocity, color;
|
||||
} *particles;
|
||||
}* _particles;
|
||||
|
||||
unsigned int count;
|
||||
glm::vec3 bounds;
|
||||
Field *field;
|
||||
unsigned int _count;
|
||||
glm::vec3 _bounds;
|
||||
Field* _field;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -20,9 +20,9 @@ int Field::value(float *value, float *pos)
|
|||
|
||||
if ((index >= 0) && (index < FIELD_ELEMENTS))
|
||||
{
|
||||
value[0] = field[index].val.x;
|
||||
value[1] = field[index].val.y;
|
||||
value[2] = field[index].val.z;
|
||||
value[0] = _field[index].val.x;
|
||||
value[1] = _field[index].val.y;
|
||||
value[2] = _field[index].val.z;
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
|
@ -34,21 +34,20 @@ Field::Field(float worldSize)
|
|||
// Initializes the field to some random values
|
||||
{
|
||||
_worldSize = worldSize;
|
||||
int i;
|
||||
float fx, fy, fz;
|
||||
for (i = 0; i < FIELD_ELEMENTS; i++)
|
||||
for (int i = 0; i < FIELD_ELEMENTS; i++)
|
||||
{
|
||||
const float FIELD_INITIAL_MAG = 0.0f;
|
||||
field[i].val = randVector() * FIELD_INITIAL_MAG * _worldSize;
|
||||
field[i].scalar = 0;
|
||||
_field[i].val = randVector() * FIELD_INITIAL_MAG * _worldSize;
|
||||
_field[i].scalar = 0;
|
||||
// Record center point for this field cell
|
||||
fx = static_cast<float>(i % 10);
|
||||
fy = static_cast<float>(i % 100 / 10);
|
||||
fz = static_cast<float>(i / 100);
|
||||
field[i].center.x = (fx + 0.5f);
|
||||
field[i].center.y = (fy + 0.5f);
|
||||
field[i].center.z = (fz + 0.5f);
|
||||
field[i].center *= _worldSize / 10.f;
|
||||
_field[i].center.x = (fx + 0.5f);
|
||||
_field[i].center.y = (fy + 0.5f);
|
||||
_field[i].center.z = (fz + 0.5f);
|
||||
_field[i].center *= _worldSize / 10.f;
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -62,63 +61,38 @@ void Field::add(float* add, float *pos)
|
|||
|
||||
if ((index >= 0) && (index < FIELD_ELEMENTS))
|
||||
{
|
||||
field[index].val.x += add[0];
|
||||
field[index].val.y += add[1];
|
||||
field[index].val.z += add[2];
|
||||
_field[index].val.x += add[0];
|
||||
_field[index].val.y += add[1];
|
||||
_field[index].val.z += add[2];
|
||||
}
|
||||
}
|
||||
|
||||
void Field::interact(float dt, glm::vec3* pos, glm::vec3* vel, glm::vec3* color, float coupling) {
|
||||
void Field::interact(float deltaTime, glm::vec3* pos, glm::vec3* vel, glm::vec3* color, float coupling) {
|
||||
|
||||
int index = (int)(pos->x/ _worldSize * 10.0) +
|
||||
(int)(pos->y/_worldSize*10.0)*10 +
|
||||
(int)(pos->z/_worldSize*10.0)*100;
|
||||
(int)(pos->y/_worldSize*10.0) * 10 +
|
||||
(int)(pos->z/_worldSize*10.0) * 100;
|
||||
if ((index >= 0) && (index < FIELD_ELEMENTS)) {
|
||||
//
|
||||
// Vector Coupling with particle velocity
|
||||
//
|
||||
*vel += field[index].val * dt; // Particle influenced by field
|
||||
*vel += _field[index].val * deltaTime; // Particle influenced by field
|
||||
|
||||
glm::vec3 temp = *vel * dt; // Field influenced by particle
|
||||
glm::vec3 temp = *vel * deltaTime; // Field influenced by particle
|
||||
temp *= coupling;
|
||||
field[index].val += temp;
|
||||
_field[index].val += temp;
|
||||
}
|
||||
}
|
||||
|
||||
void Field::avg_neighbors(int index, glm::vec3 * result) {
|
||||
// Given index to field element i, return neighbor field values
|
||||
glm::vec3 neighbors(0,0,0);
|
||||
|
||||
int x,y,z;
|
||||
x = (int)(index % 10);
|
||||
y = (int)(index%100 / 10);
|
||||
z = (int)(index / 100);
|
||||
|
||||
neighbors += field[(x+1)%10 + y*10 + z*100].val;
|
||||
neighbors += field[(x-1)%10 + y*10 + z*100].val;
|
||||
|
||||
neighbors += field[x + ((y+1)%10)*10 + z*100].val;
|
||||
neighbors += field[x + ((y-1)%10)*10 + z*100].val;
|
||||
|
||||
neighbors += field[x + y*10 + ((z+1)%10)*100].val;
|
||||
neighbors += field[x%10 + y*10 + ((z-1)%10)*100].val;
|
||||
|
||||
neighbors /= 6;
|
||||
result->x = neighbors.x;
|
||||
result->y = neighbors.y;
|
||||
result->z = neighbors.z;
|
||||
|
||||
}
|
||||
|
||||
void Field::simulate(float dt) {
|
||||
void Field::simulate(float deltaTime) {
|
||||
glm::vec3 neighbors, add, diff;
|
||||
|
||||
for (int i = 0; i < FIELD_ELEMENTS; i++)
|
||||
{
|
||||
const float CONSTANT_DAMPING = 0.5;
|
||||
const float CONSTANT_SCALAR_DAMPING = 2.5;
|
||||
field[i].val *= (1.f - CONSTANT_DAMPING*dt);
|
||||
field[i].scalar *= (1.f - CONSTANT_SCALAR_DAMPING*dt);
|
||||
_field[i].val *= (1.f - CONSTANT_DAMPING * deltaTime);
|
||||
_field[i].scalar *= (1.f - CONSTANT_SCALAR_DAMPING * deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -133,10 +107,10 @@ void Field::render()
|
|||
for (i = 0; i < FIELD_ELEMENTS; i++)
|
||||
{
|
||||
glColor3f(0, 1, 0);
|
||||
glVertex3fv(&field[i].center.x);
|
||||
glVertex3f(field[i].center.x + field[i].val.x * scale_view,
|
||||
field[i].center.y + field[i].val.y * scale_view,
|
||||
field[i].center.z + field[i].val.z * scale_view);
|
||||
glVertex3fv(&_field[i].center.x);
|
||||
glVertex3f(_field[i].center.x + _field[i].val.x * scale_view,
|
||||
_field[i].center.y + _field[i].val.y * scale_view,
|
||||
_field[i].center.z + _field[i].val.z * scale_view);
|
||||
}
|
||||
glEnd();
|
||||
|
||||
|
@ -146,7 +120,7 @@ void Field::render()
|
|||
glBegin(GL_POINTS);
|
||||
for (i = 0; i < FIELD_ELEMENTS; i++)
|
||||
{
|
||||
glVertex3fv(&field[i].center.x);
|
||||
glVertex3fv(&_field[i].center.x);
|
||||
}
|
||||
glEnd();
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ class Field {
|
|||
glm::vec3 center;
|
||||
glm::vec3 fld;
|
||||
float scalar;
|
||||
} field[FIELD_ELEMENTS];
|
||||
} _field[FIELD_ELEMENTS];
|
||||
|
||||
Field(float worldSize);
|
||||
|
||||
|
@ -34,9 +34,7 @@ class Field {
|
|||
void add(float* add, float *loc);
|
||||
void interact(float dt, glm::vec3 * pos, glm::vec3 * vel, glm::vec3 * color, float coupling);
|
||||
void simulate(float dt);
|
||||
glm::vec3 hsv2rgb(glm::vec3 in);
|
||||
private:
|
||||
void avg_neighbors(int index, glm::vec3 * result);
|
||||
float _worldSize;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue