Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 3 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Showing with 187 additions and 11 deletions.
  1. +11 −0 DEW/Plugins/Fluids.h
  2. +175 −10 DEW/Plugins/Fluids.mm
  3. +1 −1 ofxCocoaPlugins
View
11 DEW/Plugins/Fluids.h
@@ -2,6 +2,8 @@
#import <ofxCocoaPlugins/Plugin.h>
#import "MSAFluid.h"
+#import <ofxCocoaPlugins/BlobTracker2d.h>
+
using namespace MSA;
@@ -16,6 +18,15 @@ using namespace MSA;
NSButton *controlMouseColorEnabled;
NSButton *controlMouseForceEnabled;
NSSlider *controlMouseForce;
+
+ ofVec2f * opticalFlowField;
+
+ int opticalW;
+ int opticalH;
+
+ float surfaceAspect;
+
+ ofxCvColorImage fluidImage;
}
@property (assign) IBOutlet NSButton *controlMouseColorEnabled;
@property (assign) IBOutlet NSColorWell *controlMouseColor;
View
185 DEW/Plugins/Fluids.mm
@@ -1,4 +1,7 @@
#import "Fluids.h"
+#import <ofxCocoaPlugins/CustomGraphics.h>
+#import <ofxCocoaPlugins/Keystoner.h>
+#import <ofxCocoaPlugins/Tracker.h>
@implementation Fluids
@synthesize controlMouseColor;
@@ -15,7 +18,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,13 +40,14 @@ -(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;
+ fluidImage.allocate(200,100);
}
-(void)awakeFromNib{
@@ -45,13 +57,54 @@ -(void)awakeFromNib{
//----------------
//
+-(void) addImageToFluids:(ofxCvGrayscaleImage*)inputImage withFactor:(float)factor color:(Color)color{
+ Vec3f * fluidColor = fluids->color;
+ unsigned char * pixel = inputImage->getPixels();
+ for(int i=0 ; i<fluids->getNumCells() ; i++,fluidColor++, pixel++){
+ float pixelValue = factor * (*pixel);
+ *fluidColor += Vec3f(pixelValue*color.r, pixelValue*color.g , pixelValue * color.b);
+ }
+}
+
+-(void) multInverseImageWithFluidsForces:(ofxCvGrayscaleImage*)inputImage withFactor:(float)factor {
+ Vec2f * fluidUv = fluids->uv;
+ unsigned char * pixel = inputImage->getPixels();
+ for(int i=0 ; i<fluids->getNumCells() ; i++,fluidUv++, pixel++){
+ float pixelValue = factor * (*pixel);
+ *fluidUv *= (1-pixelValue);
+ }
+
+}
+
+//
+//----------------
+//
+
-(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 trackerImageWithSize:CGSizeMake(fluids->getWidth(), fluids->getHeight())];
+ CachePropF(trackerColorAdd);
+ if(trackerColorAdd){
+ [self addImageToFluids:&trackerImage withFactor:trackerColorAdd color:Color(1.0,0.0,0.0)];
+ }
+
+ //------ Tracker Block Force ----------
+ CachePropF(trackerForceBlock);
+ if(trackerForceBlock){
+ [self multInverseImageWithFluidsForces:&trackerImage withFactor:trackerForceBlock];
+ }
+
+ //-------- globalForce --------
+
CachePropF(globalForce);
if(globalForce){
// NSLog(@"%f %f",fluids->uv[10].x, fluids->uv[10].y);
@@ -63,17 +116,104 @@ -(void)update:(NSDictionary *)drawingInformation{
}
}
+ //---------- fluidWeight --------
+ CachePropF(fluidWeight);
+ if(fluidWeight){
+ for(int i=0;i<fluids->getNumCells();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;y<fluids->getSize().y;y++){
+ for(int x=0;x<fluids->getSize().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<opticalH;y++){
+ for(int x=0;x<opticalW;x++){
+ ofVec2f f = *field++;
+ float l = f.length();
+ if(l > 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;y<fluids->getSize().y;y++){
+ for(int x=0;x<fluids->getSize().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);
+ unsigned char * pixel = (unsigned char*) fluidImage.getCvImage()->imageData;
+ Vec3f * fluidPixel = fluids->color;
+ for(int i=0;i<fluidImage.width*fluidImage.height*3; i++,pixel++){
+ if(i%3 == 0){
+ fluidPixel++;
+ }
+ if(i% fluids->getWidth() == 0){
+ fluidPixel++;
+ }
+
+ *pixel = (*fluidPixel)[i%3]*255;
}
+ fluidImage.flagImageChanged();
}
//
@@ -81,8 +221,13 @@ -(void)update:(NSDictionary *)drawingInformation{
//
-(void)draw:(NSDictionary *)drawingInformation{
- fluidsDrawer->setDrawMode(kFluidDrawColor);
- fluidsDrawer->draw(0,0,1,1);
+// fluidsDrawer->setDrawMode(kFluidDrawColor);
+// fluidsDrawer->draw(0,0,1,1);
+/* ApplySurface(@"Floor"){
+ // fluidsDrawer->getTextureReference().draw(0, 0, surfaceAspect, 1);
+ } PopSurface();*/
+
+ fluidImage.draw(0, 0,1,1);
}
//
@@ -90,13 +235,33 @@ -(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<opticalH;y++){
+ for(int x=0;x<opticalW;x++){
+ ofVec2f f = *field++;
+ float l = f.length();
+ if(l > 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 {
2 ofxCocoaPlugins
@@ -1 +1 @@
-Subproject commit 80ed7584a60c802ee77eaebc8968488143d2c826
+Subproject commit e9dab32a7bcfa3fc4ccae3724d68229c25c3e944

No commit comments for this range

Something went wrong with that request. Please try again.