From cedb21259788ed2fe9131718335a022751a36478 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Fri, 8 Feb 2013 13:11:56 -0800 Subject: [PATCH] make Field a proper class to avoid duplicate symbols --- interface/src/Cloud.cpp | 3 ++- interface/src/Cloud.h | 2 ++ interface/src/Field.cpp | 18 ++++++++-------- interface/src/Field.h | 46 +++++++++++++++++++++++------------------ interface/src/Util.cpp | 5 ++--- interface/src/main.cpp | 11 +++++----- 6 files changed, 47 insertions(+), 38 deletions(-) diff --git a/interface/src/Cloud.cpp b/interface/src/Cloud.cpp index a195221750..e30cdc093e 100644 --- a/interface/src/Cloud.cpp +++ b/interface/src/Cloud.cpp @@ -21,6 +21,7 @@ Cloud::Cloud(int num, count = num; wrapBounds = wrap; particles = new Particle[count]; + field = new Field(); for (i = 0; i < count; i++) { float x = randFloat()*box.x; @@ -87,7 +88,7 @@ void Cloud::simulate (float deltaTime) { // Interact with Field const float FIELD_COUPLE = 0.005; //0.0000001; - field_interact(deltaTime, &particles[i].position, &particles[i].velocity, &particles[i].color, FIELD_COUPLE); + 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); diff --git a/interface/src/Cloud.h b/interface/src/Cloud.h index c16bb30a65..06cb5a9d7d 100644 --- a/interface/src/Cloud.h +++ b/interface/src/Cloud.h @@ -24,9 +24,11 @@ private: struct Particle { glm::vec3 position, velocity, color; } *particles; + unsigned int count; glm::vec3 bounds; bool wrapBounds; + Field *field; }; #endif diff --git a/interface/src/Field.cpp b/interface/src/Field.cpp index 2793d8de5d..df51207355 100644 --- a/interface/src/Field.cpp +++ b/interface/src/Field.cpp @@ -7,7 +7,7 @@ // #include "Field.h" -#include + #define FIELD_SCALE 0.00050 #define COLOR_DRIFT_RATE 0.001f // per-frame drift of particle color towards field element color #define COLOR_MIN 0.2f // minimum R/G/B value at 0,0,0 - also needs setting in cloud.cpp @@ -16,7 +16,7 @@ // A vector-valued field over an array of elements arranged as a 3D lattice -int field_value(float *value, float *pos) +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 +33,7 @@ int field_value(float *value, float *pos) else return 0; } -void field_init() +Field::Field() // Initializes the field to some random values { int i; @@ -60,7 +60,7 @@ void field_init() } } -void field_add(float* add, float *pos) +void Field::add(float* add, float *pos) // At location loc, add vector add to the field values { int index = (int)(pos[0]/WORLD_SIZE*10.0) + @@ -74,7 +74,7 @@ void field_add(float* add, float *pos) } } -void field_interact(float dt, glm::vec3 * pos, glm::vec3 * vel, glm::vec3 * color, float coupling) { +void Field::interact(float dt, 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 + @@ -104,7 +104,7 @@ void field_interact(float dt, glm::vec3 * pos, glm::vec3 * vel, glm::vec3 * colo } } -void field_avg_neighbors(int index, glm::vec3 * result) { +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); @@ -129,14 +129,14 @@ void field_avg_neighbors(int index, glm::vec3 * result) { } -void field_simulate(float dt) { +void Field::simulate(float dt) { glm::vec3 neighbors, add, diff; float size, distance; int i, j; for (i = 0; i < FIELD_ELEMENTS; i++) { if (0) { //(randFloat() > 0.01) { - field_avg_neighbors(i, &neighbors); + avg_neighbors(i, &neighbors); size = powf(field[i].val.x*field[i].val.x + field[i].val.y*field[i].val.y + field[i].val.z*field[i].val.z, 0.5); @@ -174,7 +174,7 @@ void field_simulate(float dt) { } } -void field_render() +void Field::render() // Render the field lines { int i; diff --git a/interface/src/Field.h b/interface/src/Field.h index 621440d929..e8b5eff2a9 100644 --- a/interface/src/Field.h +++ b/interface/src/Field.h @@ -9,32 +9,38 @@ #ifndef __interface__Field__ #define __interface__Field__ -#include "InterfaceConfig.h" #include +#include +#include "InterfaceConfig.h" #include "world.h" #include "Util.h" -#include // Field is a lattice of vectors uniformly distributed FIELD_ELEMENTS^(1/3) on side const int FIELD_ELEMENTS = 1000; -struct FieldElement { - glm::vec3 val; - glm::vec3 center; - glm::vec3 fld; - float scalar; -} field[FIELD_ELEMENTS]; +class Field { + public: + struct FieldElement { + glm::vec3 val; + glm::vec3 center; + glm::vec3 fld; + float scalar; + } field[FIELD_ELEMENTS]; + + // Pre-calculated RGB values for each field element + struct FieldColor { + glm::vec3 rgb; + } fieldcolors[FIELD_ELEMENTS]; + + Field(); + int value(float *ret, float *pos); + void render(); + 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); +}; -// Pre-calculated RGB values for each field element -struct FieldColor { - 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(float dt, glm::vec3 * pos, glm::vec3 * vel, glm::vec3 * color, float coupling); -void field_simulate(float dt); -glm::vec3 hsv2rgb(glm::vec3 in); #endif diff --git a/interface/src/Util.cpp b/interface/src/Util.cpp index 58512fa10a..aa0d2501b0 100644 --- a/interface/src/Util.cpp +++ b/interface/src/Util.cpp @@ -8,10 +8,9 @@ #include "InterfaceConfig.h" #include -#include "world.h" #include -#include "util.h" - +#include "world.h" +#include "Util.h" // // Standard Deviation Object diff --git a/interface/src/main.cpp b/interface/src/main.cpp index b451f16c5c..66ec1a70ab 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -105,6 +105,7 @@ VoxelSystem voxels(0, box); Lattice lattice(160,100); Finger myFinger(WIDTH, HEIGHT); +Field field; #define RENDER_FRAME_MSECS 8 #define SLEEP 0 @@ -295,8 +296,8 @@ void init(void) head_lean_x = WIDTH/2; head_lean_y = HEIGHT/2; - // Initialize Field values - field_init(); + // Initialize Field values + field = Field(); printf( "Field Initialized.\n" ); if (noise_on) @@ -555,7 +556,7 @@ void display(void) voxels.render(); // Draw field vectors - if (display_field) field_render(); + if (display_field) field.render(); // Render my own head if (display_head) { @@ -739,7 +740,7 @@ void key(unsigned char k, int x, int y) // Add to field vector float pos[] = {5,5,5}; float add[] = {0.001, 0.001, 0.001}; - field_add(add, pos); + field.add(add, pos); } if (k == '1') { @@ -800,7 +801,7 @@ void idle(void) // Simulation update_pos(1.f/FPS); if (simulate_on) { - field_simulate(1.f/FPS); + field.simulate(1.f/FPS); myHead.simulate(1.f/FPS); myHand.simulate(1.f/FPS); balls.simulate(1.f/FPS);