From c604a88a0735173462857aace578a4aa1515a324 Mon Sep 17 00:00:00 2001 From: Yoz Grahame Date: Mon, 12 Nov 2012 17:48:28 -0800 Subject: [PATCH 1/6] Texture loading only happens at init() --- main.cpp | 11 ++++++----- texture.cpp | 1 + 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index 455ae556fe..ecfc6dddcf 100644 --- a/main.cpp +++ b/main.cpp @@ -112,7 +112,7 @@ ParticleSystem balls(1000, #define RENDER_FRAME_MSECS 10 #define SLEEP 0 -#define NUM_TRIS 10 +#define NUM_TRIS 100000 struct { float vertices[NUM_TRIS * 3]; // float normals [NUM_TRIS * 3]; @@ -259,7 +259,10 @@ void initDisplay(void) void init(void) { - int i, j; + int i, j; + + load_png_as_texture(texture_filename); + printf("Texture loaded.\n"); Audio::init(); printf( "Audio started.\n" ); @@ -578,9 +581,7 @@ void display(void) glTranslatef(location[0], location[1], location[2]); /* Draw Point Sprites */ - - load_png_as_texture(texture_filename); - + //glActiveTexture(GL_TEXTURE0); glEnable( GL_TEXTURE_2D ); diff --git a/texture.cpp b/texture.cpp index 475047a40d..583d1d9ac7 100644 --- a/texture.cpp +++ b/texture.cpp @@ -34,6 +34,7 @@ int load_png_as_texture(char* filename) unsigned int width = 1, height = 1; unsigned error = lodepng::decode(image, width, height, filename); if (error) { + std::cout << "Error loading texture" << std::endl; return (int) error; } From 1f6134b14beeacac24fd4ce0681b9929d39b6baf Mon Sep 17 00:00:00 2001 From: Yoz Grahame Date: Mon, 12 Nov 2012 17:48:45 -0800 Subject: [PATCH 2/6] Lock build SDK to 10.7 --- interface.xcodeproj/project.pbxproj | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/interface.xcodeproj/project.pbxproj b/interface.xcodeproj/project.pbxproj index 7b58f146e6..19f14f81cd 100644 --- a/interface.xcodeproj/project.pbxproj +++ b/interface.xcodeproj/project.pbxproj @@ -55,7 +55,7 @@ /* Begin PBXFileReference section */ 08FB7796FE84155DC02AAC07 /* main.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = ""; }; - 8DD76F6C0486A84900D96B5E /* interface */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = interface; sourceTree = BUILT_PRODUCTS_DIR; }; + 8DD76F6C0486A84900D96B5E /* interface */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = interface; sourceTree = BUILT_PRODUCTS_DIR; }; B6BDADD115F4084F002A07DF /* audio.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = audio.h; sourceTree = ""; }; B6BDADD315F4085B002A07DF /* audio.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = audio.cpp; sourceTree = ""; }; B6BDADD515F40B04002A07DF /* libportaudio.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libportaudio.a; sourceTree = ""; }; @@ -289,6 +289,7 @@ "$(OTHER_CFLAGS)", ); PRODUCT_NAME = interface; + SDKROOT = macosx10.7; }; name = Debug; }; @@ -312,6 +313,7 @@ "$(OTHER_CFLAGS)", ); PRODUCT_NAME = interface; + SDKROOT = macosx10.7; }; name = Release; }; From ea66f0c2a83abe9de063a214c22bf9bc129da46f Mon Sep 17 00:00:00 2001 From: Yoz Grahame Date: Mon, 19 Nov 2012 13:18:37 -0800 Subject: [PATCH 3/6] use glDrawElements() to draw the particles - no major speedup so far, but less code --- main.cpp | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/main.cpp b/main.cpp index af3a6c2b06..734d10ffbf 100644 --- a/main.cpp +++ b/main.cpp @@ -618,16 +618,7 @@ void display(void) glTexEnvf( GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE ); glEnable( GL_POINT_SPRITE_ARB ); if (!display_head) { - glBegin( GL_POINTS ); - { - for (i = 0; i < NUM_TRIS; i++) - { - glVertex3f(tris.vertices[i*3], - tris.vertices[i*3+1], - tris.vertices[i*3+2]); - } - } - glEnd(); + glDrawElements(GL_POINTS, NUM_TRIS, GL_FLOAT, tris.vertices); } glDisable( GL_POINT_SPRITE_ARB ); glDisable( GL_TEXTURE_2D ); From 410e7b2d7e59e4c8fcdb3caa997aae1d7757e08e Mon Sep 17 00:00:00 2001 From: Yoz Grahame Date: Mon, 26 Nov 2012 14:47:30 -0800 Subject: [PATCH 4/6] Commented out cubes, added particle count, misc cleanup --- main.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index 87cbc5c2c7..5010dcf1d5 100644 --- a/main.cpp +++ b/main.cpp @@ -49,9 +49,6 @@ #include "cloud.h" #include "agent.h" - -//TGAImg Img; - using namespace std; // Junk for talking to the Serial Port @@ -95,7 +92,7 @@ ParticleSystem balls(0, 0.0 // Gravity ); -Cloud cloud(0, // Particles +Cloud cloud(100000, // Particles box, // Bounding Box false // Wrap ); @@ -243,6 +240,8 @@ void initDisplay(void) glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); + + load_png_as_texture(texture_filename); } void init(void) @@ -276,20 +275,24 @@ void init(void) myHead.setNoise(noise); } + // turning cubes off for the moment - + // uncomment to re-enable + /* + int index = 0; float location[] = {0,0,0}; float scale = 10.0; int j = 0; while (index < (MAX_CUBES/2)) { + index = 0; j++; makeCubes(location, scale, &index, cubes_position, cubes_scale, cubes_color); std::cout << "Run " << j << " Made " << index << " cubes\n"; cube_count = index; } + */ - //load_png_as_texture(texture_filename); - if (serial_on) { // Call readsensors for a while to get stable initial values on sensors @@ -525,8 +528,6 @@ void display(void) /* Draw Point Sprites */ - load_png_as_texture(texture_filename); - glDisable( GL_POINT_SPRITE_ARB ); glDisable( GL_TEXTURE_2D ); if (!display_head) cloud.render(); From 4ebcc0cff35867a58641433ebd30c9e659f35086 Mon Sep 17 00:00:00 2001 From: Yoz Grahame Date: Mon, 26 Nov 2012 18:17:00 -0800 Subject: [PATCH 5/6] Look at all the pretty colours! We now have color values for the field elements, which are gradually added to the particles as they move. --- cloud.cpp | 19 ++++++++++++++----- cloud.h | 2 +- field.cpp | 19 +++++++++++-------- field.h | 14 +++++++++++--- 4 files changed, 37 insertions(+), 17 deletions(-) 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 From 8b720d53df0a049e2eab15287fbc4baad6868342 Mon Sep 17 00:00:00 2001 From: Yoz Grahame Date: Wed, 28 Nov 2012 14:01:35 -0800 Subject: [PATCH 6/6] Move constants to macros; increase particle count --- cloud.cpp | 9 ++++++--- field.cpp | 11 +++++++---- interface.xcodeproj/project.pbxproj | 3 ++- main.cpp | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/cloud.cpp b/cloud.cpp index 02b65534b9..329b840ab5 100644 --- a/cloud.cpp +++ b/cloud.cpp @@ -10,6 +10,8 @@ #include "cloud.h" #include "util.h" +#define COLOR_MIN 0.3f // minimum R/G/B value at 0,0,0 - also needs setting in field.cpp + Cloud::Cloud(int num, glm::vec3 box, int wrap) { @@ -32,9 +34,10 @@ Cloud::Cloud(int num, 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); + float color_mult = 1 - COLOR_MIN; + particles[i].color = glm::vec3(x*color_mult/WORLD_SIZE + COLOR_MIN, + y*color_mult/WORLD_SIZE + COLOR_MIN, + z*color_mult/WORLD_SIZE + COLOR_MIN); } } diff --git a/field.cpp b/field.cpp index 17570c31ed..011fb6515f 100644 --- a/field.cpp +++ b/field.cpp @@ -9,6 +9,8 @@ #include "field.h" #include "glm/glm.hpp" #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.3f // minimum R/G/B value at 0,0,0 - also needs setting in cloud.cpp // A vector-valued field over an array of elements arranged as a 3D lattice @@ -39,9 +41,10 @@ void field_init() 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); + float color_mult = 1 - COLOR_MIN; + fieldcolors[i].rgb = glm::vec3(((i%10)*(color_mult/10.0f)) + COLOR_MIN, + ((i%100)*(color_mult/100.0f)) + COLOR_MIN, + (i*(color_mult/1000.0f)) + COLOR_MIN); } } @@ -73,7 +76,7 @@ void field_interact(glm::vec3 * pos, glm::vec3 * vel, glm::vec3 * color, float c field[index].val += temp; // add a fraction of the field color to the particle color - *color = (*color * 0.999f) + (fieldcolors[index].rgb * 0.001f); + *color = (*color * (1 - COLOR_DRIFT_RATE)) + (fieldcolors[index].rgb * COLOR_DRIFT_RATE); } } diff --git a/interface.xcodeproj/project.pbxproj b/interface.xcodeproj/project.pbxproj index 46d29d8132..166c6ad19c 100644 --- a/interface.xcodeproj/project.pbxproj +++ b/interface.xcodeproj/project.pbxproj @@ -57,7 +57,7 @@ /* Begin PBXFileReference section */ 08FB7796FE84155DC02AAC07 /* main.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = ""; }; - 8DD76F6C0486A84900D96B5E /* interface */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = interface; sourceTree = BUILT_PRODUCTS_DIR; }; + 8DD76F6C0486A84900D96B5E /* interface */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = interface; sourceTree = BUILT_PRODUCTS_DIR; }; B6BDADD115F4084F002A07DF /* audio.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = audio.h; sourceTree = ""; }; B6BDADD315F4085B002A07DF /* audio.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = audio.cpp; sourceTree = ""; }; B6BDADD515F40B04002A07DF /* libportaudio.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libportaudio.a; sourceTree = ""; }; @@ -321,6 +321,7 @@ /usr/local/lib, /usr/local/Cellar/libpng/1.5.13/lib, ); + ONLY_ACTIVE_ARCH = NO; OTHER_CPLUSPLUSFLAGS = ( "-O3", "$(OTHER_CFLAGS)", diff --git a/main.cpp b/main.cpp index 5010dcf1d5..256a550058 100644 --- a/main.cpp +++ b/main.cpp @@ -92,7 +92,7 @@ ParticleSystem balls(0, 0.0 // Gravity ); -Cloud cloud(100000, // Particles +Cloud cloud(200000, // Particles box, // Bounding Box false // Wrap );