diff --git a/cloud.cpp b/cloud.cpp index 18a05ced44..02b65534b9 100644 --- a/cloud.cpp +++ b/cloud.cpp @@ -21,14 +21,20 @@ Cloud::Cloud(int num, particles = new Particle[count]; for (i = 0; i < count; i++) { - particles[i].position.x = randFloat()*box.x; - particles[i].position.y = randFloat()*box.y; - particles[i].position.z = randFloat()*box.z; + float x = randFloat()*box.x; + float y = randFloat()*box.y; + float z = randFloat()*box.z; + particles[i].position.x = x; + particles[i].position.y = y; + particles[i].position.z = z; particles[i].velocity.x = 0; //randFloat() - 0.5; particles[i].velocity.y = 0; //randFloat() - 0.5; particles[i].velocity.z = 0; //randFloat() - 0.5; + particles[i].color = glm::vec3(x*0.8f/WORLD_SIZE + 0.2f, + y*0.8f/WORLD_SIZE + 0.2f, + z*0.8f/WORLD_SIZE + 0.2f); } } @@ -38,7 +44,7 @@ void Cloud::render() { float particle_attenuation_quadratic[] = { 0.0f, 0.0f, 2.0f }; glEnable( GL_TEXTURE_2D ); - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glPointParameterfvARB( GL_POINT_DISTANCE_ATTENUATION_ARB, particle_attenuation_quadratic ); float maxSize = 0.0f; @@ -52,6 +58,9 @@ void Cloud::render() { glBegin( GL_POINTS ); for (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); @@ -75,7 +84,7 @@ void Cloud::simulate (float deltaTime) { // Interact with Field const float FIELD_COUPLE = 0.0000001; - field_interact(&particles[i].position, &particles[i].velocity, FIELD_COUPLE); + field_interact(&particles[i].position, &particles[i].velocity, &particles[i].color, FIELD_COUPLE); // Bounce or Wrap if (wrapBounds) { diff --git a/cloud.h b/cloud.h index 575501be25..8b995cc0c3 100644 --- a/cloud.h +++ b/cloud.h @@ -22,7 +22,7 @@ public: private: struct Particle { - glm::vec3 position, velocity; + glm::vec3 position, velocity, color; } *particles; unsigned int count; glm::vec3 bounds; diff --git a/field.cpp b/field.cpp index d08c93822c..17570c31ed 100644 --- a/field.cpp +++ b/field.cpp @@ -7,15 +7,11 @@ // #include "field.h" +#include "glm/glm.hpp" #define FIELD_SCALE 0.00050 // A vector-valued field over an array of elements arranged as a 3D lattice -struct { - glm::vec3 val; -} field[FIELD_ELEMENTS]; - - int field_value(float *value, float *pos) // sets the vector value (3 floats) to field value at location pos in space. // returns zero if the location is outside world bounds @@ -33,7 +29,6 @@ int field_value(float *value, float *pos) else return 0; } - void field_init() // Initializes the field to some random values { @@ -43,7 +38,11 @@ void field_init() field[i].val.x = (randFloat() - 0.5)*FIELD_SCALE; field[i].val.y = (randFloat() - 0.5)*FIELD_SCALE; field[i].val.z = (randFloat() - 0.5)*FIELD_SCALE; - } + // and set up the RGB values for each field element. + fieldcolors[i].rgb = glm::vec3(((i%10)*0.08) + 0.2f, + ((i%100)*0.008) + 0.2f, + (i*0.0008) + 0.2f); + } } void field_add(float* add, float *pos) @@ -60,7 +59,7 @@ void field_add(float* add, float *pos) } } -void field_interact(glm::vec3 * pos, glm::vec3 * vel, float coupling) { +void field_interact(glm::vec3 * pos, glm::vec3 * vel, glm::vec3 * color, float coupling) { int index = (int)(pos->x/WORLD_SIZE*10.0) + (int)(pos->y/WORLD_SIZE*10.0)*10 + @@ -72,6 +71,9 @@ void field_interact(glm::vec3 * pos, glm::vec3 * vel, float coupling) { glm::vec3 temp = *vel; temp *= coupling; field[index].val += temp; + + // add a fraction of the field color to the particle color + *color = (*color * 0.999f) + (fieldcolors[index].rgb * 0.001f); } } @@ -172,3 +174,4 @@ void field_render() } + diff --git a/field.h b/field.h index e1d26206f2..33004554d2 100644 --- a/field.h +++ b/field.h @@ -20,14 +20,22 @@ #include "glm/glm.hpp" // Field is a lattice of vectors uniformly distributed FIELD_ELEMENTS^(1/3) on side - const int FIELD_ELEMENTS = 1000; +struct { + glm::vec3 val; +} field[FIELD_ELEMENTS]; + +// Pre-calculated RGB values for each field element +struct { + glm::vec3 rgb; +} fieldcolors[FIELD_ELEMENTS]; + void field_init(); int field_value(float *ret, float *pos); void field_render(); void field_add(float* add, float *loc); -void field_interact(glm::vec3 * pos, glm::vec3 * vel, float coupling); +void field_interact(glm::vec3 * pos, glm::vec3 * vel, glm::vec3 * color, float coupling); void field_simulate(float dt); - +glm::vec3 hsv2rgb(glm::vec3 in); #endif