diff --git a/DEW/Plugins/Fluids.h b/DEW/Plugins/Fluids.h index 72cb3d9..a0ad84c 100644 --- a/DEW/Plugins/Fluids.h +++ b/DEW/Plugins/Fluids.h @@ -16,6 +16,13 @@ using namespace MSA; NSButton *controlMouseColorEnabled; NSButton *controlMouseForceEnabled; NSSlider *controlMouseForce; + + ofVec2f * opticalFlowField; + + int opticalW; + int opticalH; + + float surfaceAspect; } @property (assign) IBOutlet NSButton *controlMouseColorEnabled; @property (assign) IBOutlet NSColorWell *controlMouseColor; diff --git a/DEW/Plugins/Fluids.mm b/DEW/Plugins/Fluids.mm index c3efb4e..ba17c58 100644 --- a/DEW/Plugins/Fluids.mm +++ b/DEW/Plugins/Fluids.mm @@ -1,4 +1,8 @@ #import "Fluids.h" +#import +#import +#import +#import @implementation Fluids @synthesize controlMouseColor; @@ -15,7 +19,15 @@ -(void)initPlugin{ [[self addPropF:@"fluidsVisc"] setMaxValue:0.0002f]; [self addPropB:@"fluidsReset"]; + [self addPropF:@"opticalFlowForce"]; + + [self addPropF:@"fluidWeight"]; + + [self addPropF:@"trackerForceBlock"]; + [self addPropF:@"trackerColorAdd"]; + [self addPropF:@"globalForce"]; + [self addPropF:@"globalTwirl"]; [[self addPropF:@"globalForceRotation"] setMaxValue:360]; } @@ -29,11 +41,11 @@ -(void)setup{ fluids = new FluidSolver(); fluids->setup(200,100); fluids->enableRGB(YES); - fluidsDrawer = new FluidDrawerGl(); fluidsDrawer->setup(fluids); - + fluidsDrawer->vectorSkipCount = 4; //Vector mode + lastControlMouse.x = -1; } @@ -47,11 +59,29 @@ -(void)awakeFromNib{ -(void)update:(NSDictionary *)drawingInformation{ + Tracker * tracker = GetPlugin(Tracker); + + surfaceAspect = Aspect(@"Floor",0); if(PropB(@"fluidsReset")){ SetPropB(@"fluidsReset", 0); fluids->reset(); } + //------ Tracker Color ---------- + ofxCvGrayscaleImage * trackerImage = [tracker trackerImageWithResolution:NSMakeSize(fluids->getWidth(), fluids->getHeight())]; + CachePropF(trackerColorAdd); + if(trackerColorAdd){ + + } + + //------ Tracker Block Force ---------- + CachePropF(trackerForceBlock); + if(trackerForceBlock){ + + } + + //--------------------------- + CachePropF(globalForce); if(globalForce){ // NSLog(@"%f %f",fluids->uv[10].x, fluids->uv[10].y); @@ -63,17 +93,91 @@ -(void)update:(NSDictionary *)drawingInformation{ } } + //---------- fluidWeight -------- + CachePropF(fluidWeight); + if(fluidWeight){ + for(int i=0;igetNumCells();i++){ + Color c = fluids->getColorAtIndex(i); + fluids->addForceAtIndex(i, Vec2f(0,c.length()*fluidWeight)); + } + } + + //---------- Twirl ------------- + CachePropF(globalTwirl); + if(globalTwirl){ + int i=0; + for(int y=0;ygetSize().y;y++){ + for(int x=0;xgetSize().x;x++){ + ofVec2f p = ofVec2f(x/fluids->getSize().x-surfaceAspect*0.25,y/fluids->getSize().y-0.5); + float l = p.length(); + ofVec2f hat = ofVec2f(-p.y,p.x); + //hat.normalize(); + + fluids->uv[i] += ( Vec2f(hat.x,hat.y) * globalTwirl - fluids->uv[i])*0.2; + i++; +// fluids->addForceAtPos(Vec2f(x/fluids->getSize().x, y/fluids->getSize().y), Vec2f(hat.x,hat.y)*globalTwirl); + } + } + } + + //--------- OpticalFlow -------- + + + CachePropF(opticalFlowForce); + if(opticalFlowForce){ + opticalFlowField = [[GetPlugin(BlobTracker2d) getInstance:0] opticalFlowFieldCalibrated]; + if(opticalFlowField != nil){ + opticalW = [[GetPlugin(BlobTracker2d) getInstance:0] opticalFlowW]; + opticalH = [[GetPlugin(BlobTracker2d) getInstance:0] opticalFlowH]; + + ofVec2f * field = opticalFlowField; + for(int y=0;y 0){ + fluids->addForceAtPos(Vec2f((float)x/opticalW, (float)y/opticalH), Vec2f(f.x,f.y)*opticalFlowForce); + } + } + } + + } + } + + + //--------------------------- + + + if([[self controlMouseColorEnabled] state] && lastControlMouse.x != -1){ + NSColor * color = [[self controlMouseColor] color]; + Color c(MSA::CM_RGB, [color redComponent], [color greenComponent], [color blueComponent] ); +// fluids->addColorAtPos(Vec2f([self controlMouseX]/[[self controlGlView] frame].size.width, [self controlMouseY]/[[self controlGlView] frame].size.height), c*[color alphaComponent]*100.0); + int i=0; + ofVec2f mouse = ofVec2f([self controlMouseX]/[[self controlGlView] frame].size.width, [self controlMouseY]/[[self controlGlView] frame].size.height); + float radius = 0.02; + + for(int y=0;ygetSize().y;y++){ + for(int x=0;xgetSize().x;x++){ + ofVec2f p = ofVec2f(x/fluids->getSize().x, y/fluids->getSize().y); + if (p.distance(mouse) < radius) { + fluids->addColorAtIndex(i, c*[color alphaComponent]*10.0); + } + i++; + // fluids->addForceAtPos(Vec2f(x/fluids->getSize().x, y/fluids->getSize().y), Vec2f(hat.x,hat.y)*globalTwirl); + } + } + + } + + + //--------------------------- + fluids->setDeltaT(PropF(@"fluidsDeltaT")); fluids->setFadeSpeed(PropF(@"fluidsFadeSpeed")); fluids->setSolverIterations(PropI(@"fluidsSolverIterations")); fluids->setVisc(PropF(@"fluidsVisc")); fluids->update(); - if([[self controlMouseColorEnabled] state] && lastControlMouse.x != -1){ - NSColor * color = [[self controlMouseColor] color]; - Color c(MSA::CM_RGB, [color redComponent], [color greenComponent], [color blueComponent] ); - fluids->addColorAtPos(Vec2f([self controlMouseX]/[[self controlGlView] frame].size.width, [self controlMouseY]/[[self controlGlView] frame].size.height), c*[color alphaComponent]*100.0); - } } // @@ -90,13 +194,34 @@ -(void)draw:(NSDictionary *)drawingInformation{ // -(void)controlDraw:(NSDictionary *)drawingInformation{ - ofBackground(0, 0, 0); + ofSetColor(0,0,0,255); + ofRect(0,0,ofGetWidth(), ofGetWidth()); + + ofSetColor(255,255,255,255); + fluidsDrawer->setDrawMode((FluidDrawMode)PropI(@"controlDrawMode")); fluidsDrawer->draw(0,0,ofGetWidth(),ofGetHeight()); + + ofEnableAlphaBlending(); + if(opticalFlowField){ + ofVec2f * field = opticalFlowField; + for(int y=0;y 0){ + float a = l / 50.0; + glColor4f(1.0, 1.0, 1.0, a); + ofVec2f p = ofVec2f(ofGetWidth()*x/opticalW , ofGetHeight()*y/opticalH); + of2DArrow(p, p+f, 4); + } + } + } + } } -(void)controlMouseDragged:(float)x y:(float)y button:(int)button{ - Vec2f pos = Vec2f((float)x/[[self controlGlView] frame].size.width, (float)y/[[self controlGlView] frame].size.height); + Vec2f pos = Vec2f((float)x/[[self controlGlView] frame].size.width, (float)y/[[self controlGlView] frame].size.height); if(lastControlMouse.x == -1){ lastControlMouse = pos; } else { diff --git a/ofxCocoaPlugins b/ofxCocoaPlugins index 80ed758..22191e4 160000 --- a/ofxCocoaPlugins +++ b/ofxCocoaPlugins @@ -1 +1 @@ -Subproject commit 80ed7584a60c802ee77eaebc8968488143d2c826 +Subproject commit 22191e4e9f559cd343230fee0f50d11b097073b7