void Fluid2DTextureApp::draw()
{
	// clear out the window with black
	gl::clear( Color( 0, 0, 0 ) ); 
	gl::setMatricesWindow( getWindowWidth(), getWindowHeight() );

	// Update the positions and tex coords
	Rectf drawRect = getWindowBounds();
	int limX = mFluid2D.resX() - 1;
	int limY = mFluid2D.resY() - 1;
	float dx = drawRect.getWidth()/(float)limX;
	float dy = drawRect.getHeight()/(float)limY;
	
	for( int j = 0; j < mFluid2D.resY(); ++j ) {
		for( int i = 0; i < mFluid2D.resX(); ++i ) {
			Vec2f P = Vec2f( i*dx, j*dy );
			Vec2f uv = mFluid2D.texCoordAt( i, j );

			int idx = j*mFluid2D.resX() + i;
			mTriMesh.getVertices()[idx] = P;
			mTriMesh.getTexCoords()[idx] = uv;
			
		}
	}

	mTex.bind();
	gl::draw( mTriMesh ); 
	mTex.unbind();
	
	mParams.draw();	
}
Example #2
0
void Pumpkin::drawPumpkin(const ci::gl::Texture & PumpkinTexture)
{
  PumpkinTexture.bind();
  ci::gl::translate(mPos);
  ci::gl::rotate(ci::Vec3f(180.f, 0.f, 0.f));
  ci::gl::drawCube(ci::Vec3f(ci::Vec3f::zero()), mSize);
  PumpkinTexture.unbind();
}
Example #3
0
void TownwindowHit::drawTownwindow(const ci::gl::Texture& mTownwindowTexture)
{
  mTownwindowTexture.bind();
  ci::gl::translate(mPos);
  ci::gl::rotate(ci::Vec3f(180.f, 0.f, 0.f));
  ci::gl::drawCube(ci::Vec3f(ci::Vec3f::zero()), mSize);
  mTownwindowTexture.unbind();
}
Example #4
0
void King2::drawKing2(const ci::gl::Texture& texture)
{
  texture.bind();
  ci::gl::translate(mPos);
  ci::gl::rotate(mRotate);
  ci::gl::drawCube(ci::Vec3f(ci::Vec3f::zero()), mSize);
  texture.unbind();
}
Example #5
0
// Receives Color data
void MeshApp::onColorData( Surface8u surface, const DeviceOptions& deviceOptions )
{
	if ( mTextureColor ) {
		mTextureColor.update( surface, surface.getBounds() );
	} else {
		mTextureColor = gl::Texture( surface );
		mTextureColor.setWrap( GL_REPEAT, GL_REPEAT );
	}
}
Example #6
0
// Runs update logic
void UiApp::update()
{
	// Update frame rate
	mFrameRate = getAverageFps();

	// Toggle fullscreen
	if ( mFullScreen != isFullScreen() ) {
		setFullScreen( mFullScreen );
	}

	// Interact with first hand
	const Leap::HandList& hands = mFrame.hands();
	if ( hands.isEmpty() ) {
		mCursorType		= CursorType::NONE;
	} else {
		const Leap::Hand& hand = *hands.begin();
		
		// Update cursor position
		mCursorPositionTarget	= warpVector( hand.palmPosition() );
		if ( mCursorType == CursorType::NONE ) {
			mCursorPosition = mCursorPositionTarget;
		}
		
		// Choose cursor type based on number of exposed fingers
		switch ( hand.fingers().count() ) {
			case 0:
				mCursorType	= CursorType::GRAB;
				
				// Slider
				if ( mSlider.getBounds().contains( mCursorPosition - mSliderPosition ) ) {
					float x1			= mTrackPosition.x;
					float x2			= mTrackPosition.x + (float)( mTrack.getWidth() - mSlider.getWidth() );
					mSliderPosition.x	= math<float>::clamp( mCursorPosition.x, x1, x2 );
				}
				break;
			case 1:
				mCursorType	= CursorType::TOUCH;
				
				// Buttons
				mFingerTipPosition = warpPointable( *hand.fingers().begin() );
				for ( size_t i = 0; i < 3; ++i ) {
					mButtonState[ i ] = false;
					if ( mButton[ 0 ].getBounds().contains( mFingerTipPosition - mButtonPosition[ i ] ) ) {
						mButtonState[ i ] = true;
					}
				}
				break;
			default:
				mCursorType	= CursorType::HAND;
				break;
		}
	}
	
	// Smooth cursor animation
	mCursorPosition = mCursorPosition.lerp( 0.21f, mCursorPositionTarget );
}
Example #7
0
void Shader::uniformTexture( const std::string &name, ci::gl::Texture tex, int slot )
{
    glActiveTexture( GL_TEXTURE0 + slot );
    glEnable( tex.getTarget() );
    glBindTexture( tex.getTarget(), tex.getId() );
    glDisable( tex.getTarget() );
    
	GLint loc = getUniformLocation( name );
	glUniform1i( loc, slot );
    
    glActiveTexture( GL_TEXTURE0 );
}
Example #8
0
void Cinderella::drawCinderella(const ci::gl::Texture & texture, const ci::Vec3f& offset)
{
  ci::gl::pushModelView();
  
  texture.bind();
  ci::gl::translate(mPos);
  ci::gl::rotate(mRotate);
  ci::gl::translate(offset);
  ci::gl::drawCube(ci::Vec3f(ci::Vec3f::zero()), mSize);
  texture.unbind();
  
  ci::gl::popModelView();
}
Example #9
0
// Render
void MeshApp::draw()
{

	// Set up window
	gl::clear( ColorAf::black(), true );
	gl::setMatrices( mCamera );
	gl::color( ColorAf::white() );

	// Check texture and VBO
	if ( mTextureDepth && mTextureColor && mVboMesh ) {

		// Position world
		gl::pushMatrices();
		gl::scale( -1.0f, -1.0f, -1.0f );
		gl::rotate( mRotation );

		// Bind textures
		mTextureDepth.bind( 0 );
		mTextureColor.bind( 1 );
		
		// Bind and configure shader
		mShader.bind();
		mShader.uniform( "brightTolerance",	mBrightTolerance 						);
		mShader.uniform( "eyePoint",		mEyePoint 								);
		mShader.uniform( "lightAmbient",	mLightAmbient 							);
		mShader.uniform( "lightDiffuse",	mLightDiffuse 							);
		mShader.uniform( "lightPosition",	mLightPosition 							);
		mShader.uniform( "lightSpecular",	mLightSpecular 							);
		mShader.uniform( "positions",		0 										);
		mShader.uniform( "scale",			mScale 									);
		mShader.uniform( "showColor",		mShowColor 								);
		mShader.uniform( "shininess",		mLightShininess 						);
		mShader.uniform( "Color",			1 										);
		mShader.uniform( "ColorOffset",		Vec2f( mColorOffsetX, mColorOffsetY )	);
		mShader.uniform( "uvmix",			mMeshUvMix								);

		// Draw VBO
		gl::draw( mVboMesh );

		// Stop drawing
		mShader.unbind();
		mTextureDepth.unbind();
		mTextureColor.unbind();
		gl::popMatrices();

	}

	// Draw params
	params::InterfaceGl::draw();

}
Example #10
0
void SpriteSheet::init(ci::gl::Texture spriteImage, std::string xmlPath, int DataFormat){
		__spriteData = SpriteDataParser::parseSpriteData(xmlPath, DataFormat);
		__spriteImage = spriteImage;
		__currentFrame = 0;
		__totalFrames = __spriteData.size();
		__textureWidth = spriteImage.getWidth();
		__textureHeight = spriteImage.getHeight();

		x = 0;
		y = 0;
		scale = 1.0f;
		rotation = 0.0f;
		alpha = 1.0f;

}
Example #11
0
void BulletTestApp::unbindTexture( uint32_t index )
{
	if ( mTest > 4 ) {
		if ( index == 0 ) {
			mTexTerrain.unbind();
		} else {
			if ( mTest < 7 ) {
				mTexSphere.unbind();
			}
		}
	} else {
		if ( ( ( mTest == 0 || mTest == 3 ) && index > 0 ) || mTest == 1 ) {
			mTexSquare.unbind();
		}
	}
}
Example #12
0
void ClientApp::update()
{
	mClient.poll();

	mFrameRate = getFrameRate();

	if ( mFullScreen != isFullScreen() ) {
		setFullScreen( mFullScreen );
	}

	double e = getElapsedSeconds();
	if ( mPing && e - mPingTime > 3.0 ) {
		mClient.ping();
		mPingTime = e;
	}

	if ( mTextPrev != mText ) {
		mTextPrev = mText;
		if ( mText.empty() ) {
			mTexture.reset();
		} else {
			TextBox tbox = TextBox().alignment( TextBox::CENTER ).font( mFont ).size( Vec2i( mSize.x, TextBox::GROW ) ).text( mText );
			tbox.setColor( ColorAf( 1.0f, 0.8f, 0.75f, 1.0f ) );
			tbox.setBackgroundColor( ColorAf::black() );
			tbox.setPremultiplied( false );
			mSize.y		= tbox.measure().y;
			mTexture	= gl::Texture( tbox.render() );
		}
	}
}
// Receives video data
void SkeletonBitmapApp::onVideoData( Surface8u surface, const DeviceOptions &deviceOptions )
{
	if ( mTexture ) {
		mTexture.update( surface );
	} else {
		mTexture = gl::Texture( surface );
	}
}
Example #14
0
void BulletTestApp::setup()
{
	mDragging	= false;
	mFrameRate	= 0.0f;
	mTest		= 9;
	mTestPrev	= mTest;

	// Set up lighting
	mLight = new gl::Light( gl::Light::DIRECTIONAL, 0 );
	mLight->setDirection( Vec3f( 0.0f, 0.1f, 0.3f ).normalized() );
	mLight->setAmbient( ColorAf( 0.2f, 0.2f, 0.2f, 1.0f ) );
	mLight->setDiffuse( ColorAf( 1.0f, 1.0f, 1.0f, 1.0f ) );
	mLight->enable();

	// Load meshes
	loadModels();

	// Create a Bullet dynamics world
	mWorld = bullet::createWorld();

	// Load texture
	mTexSquare	= gl::Texture( loadImage( loadResource( RES_TEX_SQUARE ) ) );
	mTexSphere	= gl::Texture( loadImage( loadResource( RES_TEX_SPHERE ) ) );
	mTexTerrain	= gl::Texture( loadImage( loadResource( RES_TEX_TERRAIN ) ) );
	mTexTerrain.setWrap( GL_REPEAT, GL_REPEAT );
	mTexTerrain.unbind();

	// Init terrain pointer
	mTerrain = 0;

	// Parameters
	mParams = params::InterfaceGl( "Params", Vec2i( 200, 120 ) );
	mParams.addParam( "Frame Rate",		&mFrameRate,								"", true );
	mParams.addParam( "Test",			&mTest,										"min=0 max=9 step=1 keyDecr=t keyIncr=T" ); 
	mParams.addButton( "Drop",			bind( &BulletTestApp::drop, this ),			"key=space" );
	mParams.addButton( "Screen shot",	bind( &BulletTestApp::screenShot, this ),	"key=s" );
	mParams.addButton( "Quit",			bind( &BulletTestApp::quit, this ),			"key=q" );

	// Initialize
	initTest();

	// Run first resize to initialize view
	resize( ResizeEvent( getWindowSize() ) );

}
Example #15
0
void FloorView::draw()
{

	const ci::gl::Texture fbotex = fbo.getTexture();
	
	gl::enableAdditiveBlending(); 

	fbotex.enableAndBind(); 

	//draw fbo with shader
	shader->bind();
	shader->uniform( "alpha", alpha->value() );
	drawQuad();
	shader->unbind(); 
	fbotex.unbind(); 

	gl::enableAlphaBlending(); 
}
Example #16
0
void BulletTestApp::unbindTexture( TextureType type )
{
    switch( type )
    {
    case TT_GROUND     :
        mTexGround.unbind();
        break;
    case TT_BACKBOARD  :
        mTexBackBoard.unbind();
        break;
    case TT_RING       :
        mTexRing.unbind();
        break;
    case TT_BASKETBALL :
        mTexBasketBall.unbind();
        break;
    }
}
Example #17
0
void BulletTestApp::setup()
{

	// Set test mode
	mTest = 0;
	mTestPrev = mTest;

	// Set up lighting
	mLight = new gl::Light( gl::Light::DIRECTIONAL, 0 );
	mLight->setDirection( Vec3f( 0.0f, 0.1f, 0.3f ).normalized() );
	mLight->setAmbient( ColorAf( 0.2f, 0.2f, 0.2f, 1.0f ) );
	mLight->setDiffuse( ColorAf( 1.0f, 1.0f, 1.0f, 1.0f ) );
	mLight->enable();

	// Load meshes
	loadModels();

	// Create a Bullet dynamics world
	mWorld = bullet::createWorld();

	// Load texture
	mTexSquare = gl::Texture( loadImage( loadResource( RES_TEX_SQUARE ) ) );
	mTexSphere = gl::Texture( loadImage( loadResource( RES_TEX_SPHERE ) ) );
	mTexTerrain = gl::Texture( loadImage( loadResource( RES_TEX_TERRAIN ) ) );
	mTexTerrain.setWrap( GL_REPEAT, GL_REPEAT );
	mTexTerrain.unbind();

	// Init terrain pointer
	mTerrain = 0;

	// Parameters
	mFrameRate = 0.0f;
	mParams = params::InterfaceGl( "Params", Vec2i( 150, 100) );
	mParams.addParam( "Frame Rate", &mFrameRate, "", true );
	mParams.addParam( "Test", &mTest, "min=0 max=7 step=1 keyDecr=d keyIncr=D" ); 

	// Initialize
	initTest();

	// Run first resize to initialize view
	resize( ResizeEvent( getWindowSize() ) );

}
Example #18
0
KeyBackground::KeyBackground(const Vec2f& initPosition, const ci::gl::Texture& closeKeyboard, const ci::ColorA& color)	
	:alphaColorPlashka(0.0f),
	showing(false),
	initPosition(initPosition),
	plashkaHeight(592.0f),
	bgHeight(1920.0f - 592.0f - initPosition.x),
	closeKeyboard(closeKeyboard)
{
#ifdef PORTRAIT_RES
	auto positionY = 885.0f - closeKeyboard.getHeight() * 0.5f;
#else
	auto positionY = 600.0f - closeKeyboard.getHeight() * 0.5f;
#endif

	auto positionX = (1080.0f - closeKeyboard.getWidth()) * 0.5f;

	btn = ImageButtonSpriteRef(new ImageButtonSprite(closeKeyboard));
	btn->setPosition(initPosition + Vec2f(positionX, positionY));
	addChild(btn);
	mainColor = color;
}
void MovieLoaderTestApp::draw()
{
	// clear out the window with black
	gl::clear( Color( 0, 0, 0 ) );
	
	if (!mMovieSelected) return;
	
	if (mTexture) {
		Rectf centeredRect = Rectf( mTexture.getBounds() ).getCenteredFit( getWindowBounds(), true );
		gl::draw(mTexture, centeredRect);
	}
}
void _TBOX_PREFIX_App::draw()
{
	gl::clear( Color( 0.2f, 0.2f, 0.3f ) );
	gl::enableDepthRead();
	
	mTex.bind();
	gl::setMatrices( mCam );
	glPushMatrix();
		gl::multModelView( mCubeRotation );
		gl::drawCube( Vec3f::zero(), Vec3f( 2.0f, 2.0f, 2.0f ) );
	glPopMatrix();
}
// Render
void SkeletonBitmapApp::draw()
{

	// Clear window
	gl::setViewport( getWindowBounds() );
	gl::clear();
	gl::setMatricesWindow( getWindowSize() );

	// We're capturing
	if ( mKinect->isCapturing() && mTexture ) {
		
		// Draw color image
		gl::color( ColorAf::white() );
		gl::draw( mTexture, getWindowBounds() );
		
		// Scale skeleton to fit
		gl::pushMatrices();
		gl::scale( Vec2f( getWindowSize() ) / Vec2f( mTexture.getSize() ) );

		// Iterate through skeletons
		uint32_t i = 0;
		for ( vector<Skeleton>::const_iterator skeletonIt = mSkeletons.cbegin(); skeletonIt != mSkeletons.cend(); ++skeletonIt, i++ ) {

			// Set color
			gl::color( mKinect->getUserColor( i ) );

			// Draw bones and joints
			for ( Skeleton::const_iterator boneIt = skeletonIt->cbegin(); boneIt != skeletonIt->cend(); ++boneIt ) {
				
				// Get joint positions 
				const Bone& bone		= boneIt->second;
				Vec3f position			= bone.getPosition();
				Vec3f destination		= skeletonIt->at( bone.getStartJoint() ).getPosition();
				Vec2f positionScreen	= Vec2f( mKinect->getSkeletonVideoPos( position ) );
				Vec2f destinationSceen	= Vec2f( mKinect->getSkeletonVideoPos( destination ) );

				// Draw bone
				gl::drawLine( positionScreen, destinationSceen );

				// Draw joint
				gl::drawSolidCircle( positionScreen, 10.0f, 16 );

			}

		}

		gl::popMatrices();

	}

}
void LocationOrbitalOverlay::draw()
{

	const ci::gl::Texture fbotex = mFbo->getTexture();
	
	gl::enableAdditiveBlending(); 
	
	mShader->bind();
	
	fbotex.bind(0); 
	mSphereAlpha->getTexture()->bind(1);

	mShader->uniform("mapTexture", 0);
	mShader->uniform("alphaTexture", 1);

	drawQuad();
	
	fbotex.unbind(0);
	mSphereAlpha->getTexture()->unbind(1);

	mShader->unbind();

	gl::enableAlphaBlending(); 
}
Example #23
0
void Fluid2DBasicApp::draw()
{
	// clear out the window with black
	gl::clear( Color( 0, 0, 0 ) ); 

	Channel32f chan( mFluid2D.resX(), mFluid2D.resY(), mFluid2D.resX()*sizeof(float), 1, const_cast<float*>( mFluid2D.density().data() ) );

	if( ! mTex ) {
		mTex = gl::Texture( chan );
	} else {
		mTex.update( chan );
	}
	gl::color( Color( 1, 1, 1 ) );
	gl::draw( mTex, getWindowBounds() );

	mParams.draw();
}
Example #24
0
// Handles window resize
void UiApp::resize()
{
	// Initialize buttons
	float h = (float)getWindowHeight() * 0.333f;
	float w = (float)getWindowWidth() * 0.25f;
	Vec2f position( w, h );
	position -= Vec2f( mButton[ 0 ].getSize() ) * 0.5f;
	for ( size_t i = 0; i < 3; ++i, position.x += w ) {
		mButtonPosition[ i ]	= position;
		mButtonState[ i ]		= false;
	}

	// Initialize slider
	position = Vec2f( w * 2.0f, h * 2.0f );
	mTrackPosition		= position - Vec2f( mTrack.getSize() ) * 0.5f;
	mSliderPosition		= mTrackPosition;
	mSliderPosition.y	-= 45.0f;
}
Example #25
0
void Fluid2DCamAppApp::draw()
{
	// clear out the window with black
	gl::clear( Color( 0, 0, 0 ) ); 
	gl::color( Colorf::white() );
	gl::setMatricesWindow( getWindowWidth(), getWindowHeight() );
	
	if( mTexCam ) {
		gl::draw( mTexCam, Rectf( 0*kDrawScale*mFluid2DResX, 0*kDrawScale*mFluid2DResY, 1*kDrawScale*mFluid2DResX, 1*kDrawScale*mFluid2DResY ) );
	}

	gl::draw( mTexVel0, Rectf( 0*kDrawScale*mFluid2DResX, 1*kDrawScale*mFluid2DResY, 1*kDrawScale*mFluid2DResX, 2*kDrawScale*mFluid2DResY ) );
	gl::draw( mTexVel1, Rectf( 1*kDrawScale*mFluid2DResX, 1*kDrawScale*mFluid2DResY, 2*kDrawScale*mFluid2DResX, 2*kDrawScale*mFluid2DResY ) );
	gl::draw( mTexDen0, Rectf( 2*kDrawScale*mFluid2DResX, 1*kDrawScale*mFluid2DResY, 3*kDrawScale*mFluid2DResX, 2*kDrawScale*mFluid2DResY ) );
	gl::draw( mTexDen1, Rectf( 3*kDrawScale*mFluid2DResX, 1*kDrawScale*mFluid2DResY, 4*kDrawScale*mFluid2DResX, 2*kDrawScale*mFluid2DResY ) );

	gl::draw( mTexDiv,     Rectf( 0*kDrawScale*mFluid2DResX, 2*kDrawScale*mFluid2DResY, 1*kDrawScale*mFluid2DResX, 4*kDrawScale*mFluid2DResY ) );
	gl::draw( mTexPrs,     Rectf( 1*kDrawScale*mFluid2DResX, 2*kDrawScale*mFluid2DResY, 2*kDrawScale*mFluid2DResX, 4*kDrawScale*mFluid2DResY ) );
	gl::draw( mTexCurl,    Rectf( 2*kDrawScale*mFluid2DResX, 2*kDrawScale*mFluid2DResY, 3*kDrawScale*mFluid2DResX, 4*kDrawScale*mFluid2DResY ) );
	gl::draw( mTexCurlLen, Rectf( 3*kDrawScale*mFluid2DResX, 2*kDrawScale*mFluid2DResY, 4*kDrawScale*mFluid2DResX, 4*kDrawScale*mFluid2DResY ) );

	mTexCurlLen.unbind();

	gl::color( Color( 1, 0, 0 ) );
	glLineWidth( 0.5f );
	glBegin( GL_LINES );
		glVertex2f( Vec2f( 0, 1*kDrawScale*mFluid2DResY ) );
		glVertex2f( Vec2f( (float)getWindowWidth(), 1*kDrawScale*mFluid2DResY ) );
		glVertex2f( Vec2f( 0, 2*kDrawScale*mFluid2DResY ) );
		glVertex2f( Vec2f( (float)getWindowWidth(), 2*kDrawScale*mFluid2DResY ) );

		glVertex2f( Vec2f( 1*kDrawScale*mFluid2DResX, 0 ) );
		glVertex2f( Vec2f( 1*kDrawScale*mFluid2DResX, (float)getWindowHeight() ) );
		glVertex2f( Vec2f( 2*kDrawScale*mFluid2DResX, 0 ) );
		glVertex2f( Vec2f( 2*kDrawScale*mFluid2DResX, (float)getWindowHeight() ) );
		glVertex2f( Vec2f( 3*kDrawScale*mFluid2DResX, 0 ) );
		glVertex2f( Vec2f( 3*kDrawScale*mFluid2DResX, (float)getWindowHeight() ) );
	glEnd();
	mParams.draw();
}
void VboMeshSampleApp::draw()
{
	// Set up window
	gl::setViewport( getWindowBounds() );
	gl::setMatrices( mCamera );
	gl::clear( ColorAf::gray( 0.6f ) );

	// Use arcball to rotate model view
	glMultMatrixf( mArcball.getQuat() );

	// Enabled lighting, texture mapping, wireframe
	if ( mLightEnabled ) {
		gl::enable( GL_LIGHTING );
	}
	if ( mTextureEnabled && mTexture ) {
		gl::enable( GL_TEXTURE_2D );
		mTexture.bind();
	}
	if ( mWireframe ) {
		gl::enableWireframe();
	}

	// Apply scale
	gl::pushMatrices();
	gl::scale( mScale );
	
	// Draw selected mesh
	switch ( (MeshType)mMeshIndex ) {
	case MESH_TYPE_CIRCLE:
		gl::draw( mCircle );
		break;
	case MESH_TYPE_CONE:
		gl::draw( mCone );
		break;
	case MESH_TYPE_CUBE:
		gl::draw( mCube );
		break;
	case MESH_TYPE_CUSTOM:
		gl::draw( mCustom );
		break;
	case MESH_TYPE_CYLINDER:
		gl::draw( mCylinder );
		break;
	case MESH_TYPE_RING:
		gl::draw( mRing );
		break;
	case MESH_TYPE_SPHERE:
		gl::draw( mSphere );
		break;
	case MESH_TYPE_SQUARE:
		gl::draw( mSquare );
		break;
	}
	
	// End scale
	gl::popMatrices();

	// Disable wireframe, texture mapping, lighting
	if ( mWireframe ) {
		gl::disableWireframe();
	}
	if ( mTextureEnabled && mTexture ) {
		mTexture.unbind();
		gl::disable( GL_TEXTURE_2D );
	}
	if ( mLightEnabled ) {
		gl::disable( GL_LIGHTING );
	}

	// Draw params GUI
	mParams.draw();
}
Example #27
0
void Fluid2DCamAppApp::update()
{
	
	if( mCapture && mCapture.checkNewFrame() ) {
		if( ! mTexCam ) {
			mTexCam = gl::Texture( mCapture.getSurface() );
		}

		// Flip the image
		if( ! mFlipped ) {
			Surface8u srcImg = mCapture.getSurface();
			mFlipped = Surface8u( srcImg.getWidth(), srcImg.getHeight(), srcImg.hasAlpha(), srcImg.getChannelOrder() );
		}
		Surface8u srcImg = mCapture.getSurface();
		mFlipped = Surface8u( srcImg.getWidth(), srcImg.getHeight(), srcImg.hasAlpha(), srcImg.getChannelOrder() );
		for( int y = 0; y < mCapture.getHeight(); ++y ) {
			const Color8u* src = (const Color8u*)(srcImg.getData() + (y + 1)*srcImg.getRowBytes() - srcImg.getPixelInc());
			Color8u* dst = (Color8u*)(mFlipped.getData() + y*mFlipped.getRowBytes());
			for( int x = 0; x < mCapture.getWidth(); ++x ) {
				*dst = *src;
				++dst;
				--src;
			} 
		}
		
		// Create scaled image
		if( ! mCurScaled  ) {
			mCurScaled = Surface8u( mFlipped.getWidth()/kFlowScale, mFlipped.getHeight()/kFlowScale, mFlipped.hasAlpha(), mFlipped.getChannelOrder() );
		}		
		ip::resize( mFlipped, &mCurScaled );

		// Optical flow 
		if( mCurScaled && mPrvScaled ) {
			mPrvCvData = mCurCvData;
			mCurCvData = cv::Mat( toOcv( Channel( mCurScaled ) ) );

			if( mPrvCvData.data && mCurCvData.data ) {
				int pyrLvels		= 3;
				int winSize			= 3;
				int iters			= 5;
				int poly_n			= 7;
				double poly_sigma	= 1.5;
				cv::calcOpticalFlowFarneback( mPrvCvData, mCurCvData, mFlow, 0.5, pyrLvels, 2*winSize + 1, iters, poly_n, poly_sigma, cv::OPTFLOW_FARNEBACK_GAUSSIAN );

				if( mFlow.data ) {
					if( mFlowVectors.empty() ) {
						mFlowVectors.resize( mCurScaled.getWidth()*mCurScaled.getHeight() );
					}
					
					//memset( &mFlowVectors[0], 0, mCurScaled.getWidth()*mCurScaled.getHeight()*sizeof( Vec2f ) );
					mNumActiveFlowVectors = 0;
					for( int j = 0; j < mCurScaled.getHeight(); ++j ) {
						for( int i = 0; i < mCurScaled.getWidth(); ++i ) {
							const float* fptr = reinterpret_cast<float*>(mFlow.data + j*mFlow.step + i*sizeof(float)*2);
							//
							Vec2f v = Vec2f( fptr[0], fptr[1] ); 
							if( v.lengthSquared() >= mVelThreshold ) {
								if( mNumActiveFlowVectors >= (int)mFlowVectors.size() ) {
									mFlowVectors.push_back( std::make_pair( Vec2i( i, j ), v ) );
								}
								else {
									mFlowVectors[mNumActiveFlowVectors] = std::make_pair( Vec2i( i, j ), v );
								}
								++mNumActiveFlowVectors;
							}
						}
					}
				}
			}
		}

		// Update texture
		mTexCam.update( mFlipped );

		// Save previous frame
		if( ! mPrvScaled ) {
			mPrvScaled = Surface8u( mCurScaled.getWidth(), mCurScaled.getHeight(), mCurScaled.hasAlpha(), mCurScaled.getChannelOrder() );
		}
		memcpy( mPrvScaled.getData(), mCurScaled.getData(), mCurScaled.getHeight()*mCurScaled.getRowBytes() );
	}

	// Update fluid
	float dx = (mFluid2DResX - 2)/(float)(640/kFlowScale);
	float dy = (mFluid2DResY - 2)/(float)(480/kFlowScale);
	for( int i = 0; i < mNumActiveFlowVectors; ++i ) {
		Vec2f P = mFlowVectors[i].first;
		const Vec2f& v = mFlowVectors[i].second;
		mFluid2D.splatDensity( P.x*dx + 1, P.y*dy + 1, mDenScale*v.lengthSquared() );
		mFluid2D.splatVelocity( P.x*dx + 1, P.y*dy + 1, v*mVelScale );
	}
	mFluid2D.step();

	// Update velocity
	const Vec2f* srcVel0 = mFluid2D.dbgVel0().data();
	const Vec2f* srcVel1 = mFluid2D.dbgVel1().data();
	Colorf* dstVel0 = (Colorf*)mSurfVel0.getData();
	Colorf* dstVel1 = (Colorf*)mSurfVel1.getData();
	for( int j = 0; j < mFluid2DResY; ++j ) {
		for( int i = 0; i < mFluid2DResX; ++i ) {
			*dstVel0 = Colorf( srcVel0->x, srcVel0->y, 0.0f );
			*dstVel1 = Colorf( srcVel1->x, srcVel1->y, 0.0f );
			++srcVel0;
			++srcVel1;
			++dstVel0;
			++dstVel1;
		}
	}
	
	// Update Density
	mChanDen0 = Channel32f( mFluid2DResX, mFluid2DResY, mFluid2DResX*sizeof(float), 1, mFluid2D.dbgDen0().data() );
	mChanDen1 = Channel32f( mFluid2DResX, mFluid2DResY, mFluid2DResX*sizeof(float), 1, mFluid2D.dbgDen1().data() );
	
	mTexDen0.update( mChanDen0 );
	mTexDen1.update( mChanDen1 );
	
	// Update velocity textures
	mTexVel0.update( mSurfVel0 );
	mTexVel1.update( mSurfVel1 );
	
	// Update Divergence
	mChanDiv = Channel32f( mFluid2DResX, mFluid2DResY, mFluid2DResX*sizeof(float), 1, mFluid2D.dbgDivergence().data() );
	mTexDiv.update( mChanDiv );

	// Update Divergence
	mChanPrs = Channel32f( mFluid2DResX, mFluid2DResY, mFluid2DResX*sizeof(float), 1, mFluid2D.dbgPressure().data() );
	mTexPrs.update( mChanPrs );

	// Update Curl, Curl Length
	mChanCurl = Channel32f( mFluid2DResX, mFluid2DResY, mFluid2DResX*sizeof(float), 1, mFluid2D.dbgCurl().data() );
	mTexCurl.update( mChanCurl );
	mChanCurlLen = Channel32f( mFluid2DResX, mFluid2DResY, mFluid2DResX*sizeof(float), 1, mFluid2D.dbgCurlLength().data() );
	mTexCurlLen.update( mChanCurlLen );
}
Example #28
0
// Set up
void UiApp::setup()
{
	glShadeModel( GL_FLAT );
		
	// Start device
	mDevice 		= Device::create();
	mDevice->connectEventHandler( &UiApp::onFrame, this );
	
	// Load cursor textures
	for ( size_t i = 0; i < 3; ++i ) {
		switch ( (CursorType)i ) {
			case CursorType::GRAB:
				mTexture[ i ] = gl::Texture( loadImage( loadResource( RES_TEX_GRAB ) ) );
				break;
			case CursorType::HAND:
				mTexture[ i ] = gl::Texture( loadImage( loadResource( RES_TEX_HAND ) ) );
				break;
			case CursorType::TOUCH:
				mTexture[ i ] = gl::Texture( loadImage( loadResource( RES_TEX_TOUCH ) ) );
				break;
			case NONE:
				break;
		}
		mTexture[ i ].setMagFilter( GL_NEAREST );
		mTexture[ i ].setMinFilter( GL_NEAREST );
	}
	
	// Initialize cursor
	mCursorType				= CursorType::NONE;
	mCursorPosition			= Vec2f::zero();
	mCursorPositionTarget	= Vec2f::zero();
	mFingerTipPosition		= Vec2i::zero();
	
	// Load UI textures
	mButton[ 0 ]	= gl::Texture( loadImage( loadResource( RES_TEX_BUTTON_OFF ) ) );
	mButton[ 1 ]	= gl::Texture( loadImage( loadResource( RES_TEX_BUTTON_ON ) ) );
	mSlider			= gl::Texture( loadImage( loadResource( RES_TEX_SLIDER ) ) );
	mTrack			= gl::Texture( loadImage( loadResource( RES_TEX_TRACK ) ) );
	
	// Disable anti-aliasing
	mButton[ 0 ].setMagFilter( GL_NEAREST );
	mButton[ 0 ].setMinFilter( GL_NEAREST );
	mButton[ 1 ].setMagFilter( GL_NEAREST );
	mButton[ 1 ].setMinFilter( GL_NEAREST );
	mSlider.setMagFilter( GL_NEAREST );
	mSlider.setMinFilter( GL_NEAREST );
	mTrack.setMagFilter( GL_NEAREST );
	mTrack.setMinFilter( GL_NEAREST );
	
	// Params
	mFrameRate	= 0.0f;
	mFullScreen	= false;
	mParams = params::InterfaceGl( "Params", Vec2i( 200, 105 ) );
	mParams.addParam( "Frame rate",		&mFrameRate,						"", true );
	mParams.addParam( "Full screen",	&mFullScreen,						"key=f"		);
	mParams.addButton( "Screen shot",	bind( &UiApp::screenShot, this ),	"key=space" );
	mParams.addButton( "Quit",			bind( &UiApp::quit, this ),			"key=q" );
	
	// Run resize to initialize layout
	resize();
}
Example #29
0
// Set up
void UiApp::setup()
{
	glShadeModel( GL_FLAT );
	
	// Set up camera
	mCamera = CameraPersp( getWindowWidth(), getWindowHeight(), 45.0f, 0.01f, 1000.0f );
	mOffset = Vec3f( 240.0f, -480.0f, 0.0f );
	
	// Start device
	mLeap = Device::create();
	mLeap->addCallback( &UiApp::onFrame, this );
	
	// Load cursor textures
	for ( size_t i = 0; i < 3; ++i ) {
		switch ( (CursorType)i ) {
			case CursorType::GRAB:
				mTexture[ i ] = gl::Texture( loadImage( loadResource( RES_TEX_GRAB ) ) );
				break;
			case CursorType::HAND:
				mTexture[ i ] = gl::Texture( loadImage( loadResource( RES_TEX_HAND ) ) );
				break;
			case CursorType::TOUCH:
				mTexture[ i ] = gl::Texture( loadImage( loadResource( RES_TEX_TOUCH ) ) );
				break;
			case NONE:
				break;
		}
		mTexture[ i ].setFlipped( true );
		mTexture[ i ].setMagFilter( GL_NEAREST );
		mTexture[ i ].setMinFilter( GL_NEAREST );
	}
	
	// Initialize cursor
	mCursorType				= CursorType::NONE;
	mCursorPosition			= Vec3f::zero();
	mCursorPositionTarget	= Vec3f::zero();
	
	// Load UI textures
	mButton[ 0 ]	= gl::Texture( loadImage( loadResource( RES_TEX_BUTTON_OFF ) ) );
	mButton[ 1 ]	= gl::Texture( loadImage( loadResource( RES_TEX_BUTTON_ON ) ) );
	mSlider			= gl::Texture( loadImage( loadResource( RES_TEX_SLIDER ) ) );
	mTrack			= gl::Texture( loadImage( loadResource( RES_TEX_TRACK ) ) );

	// Flip textures
	mButton[ 0 ].setFlipped( true );
	mButton[ 1 ].setFlipped( true );
	mSlider.setFlipped( true );
	mTrack.setFlipped( true );
	
	// Disable anti-aliasing
	mButton[ 0 ].setMagFilter( GL_NEAREST );
	mButton[ 0 ].setMinFilter( GL_NEAREST );
	mButton[ 1 ].setMagFilter( GL_NEAREST );
	mButton[ 1 ].setMinFilter( GL_NEAREST );
	mSlider.setMagFilter( GL_NEAREST );
	mSlider.setMinFilter( GL_NEAREST );
	mTrack.setMagFilter( GL_NEAREST );
	mTrack.setMinFilter( GL_NEAREST );
	
	// Initialize buttons
	Vec3f position( -120.0f, 950.0f, 0.0f );
	for ( size_t i = 0; i < 3; ++i, position.x += 320.0f ) {
		mButtonPosition[ i ]	= position;
		mButtonState[ i ]		= false;
	}
	
	// Initialize sliider
	mTrackPosition		= Vec3f( 0.0f, 700.0f, 0.0f );
	mSliderPosition		= mTrackPosition;
	mSliderPosition.y	-= 45.0f;
	
	// Params
	mFrameRate	= 0.0f;
	mFullScreen	= false;
	mParams = params::InterfaceGl( "Params", Vec2i( 200, 105 ) );
	mParams.addParam( "Frame rate",		&mFrameRate,						"", true );
	mParams.addParam( "Full screen",	&mFullScreen,						"key=f"		);
	mParams.addButton( "Screen shot",	bind( &UiApp::screenShot, this ),	"key=space" );
	mParams.addButton( "Quit",			bind( &UiApp::quit, this ),			"key=q" );
}
Example #30
0
// Runs update logic
void UiApp::update()
{
	// Update frame rate
	mFrameRate = getAverageFps();

	// Toggle fullscreen
	if ( mFullScreen != isFullScreen() ) {
		setFullScreen( mFullScreen );
	}

	// Update device
	if ( mLeap && mLeap->isConnected() ) {		
		mLeap->update();
	}
	
	// Interact with first hand
	if ( mHands.empty() ) {
		mCursorType		= CursorType::NONE;
	} else {
		const Hand& hand = mHands.begin()->second;
		
		// Update cursor position
		mCursorPositionTarget	= warp( hand.getPosition() );
		if ( mCursorType == CursorType::NONE ) {
			mCursorPosition = mCursorPositionTarget;
		}
		
		// Choose cursor type based on number of exposed fingers
		switch ( hand.getFingers().size() ) {
			case 0:
				mCursorType	= CursorType::GRAB;
				break;
			case 1:
				mCursorType	= CursorType::TOUCH;
				break;
			default:
				mCursorType	= CursorType::HAND;
				break;
		}
	}
	
	// Smooth cursor animation
	mCursorPosition = mCursorPosition.lerp( 0.21f, mCursorPositionTarget );
	mCursorPosition.z = 0.0f;
	
	// Hit buttons
	for ( size_t i = 0; i < 3; ++i ) {
		mButtonState[ i ] = false;
		if ( mCursorType == CursorType::TOUCH &&
			mCursorPosition.distance( mButtonPosition[ i ] ) < (float)mButton[ 0 ].getSize().length() * 0.5f ) {
			mButtonState[ i ] = true;
		}
	}
	
	// Slider
	if ( mCursorType == CursorType::GRAB &&
		math<float>::abs( mCursorPosition.x - mSliderPosition.x ) < (float)mSlider.getWidth() * 0.5f &&
		math<float>::abs( mCursorPosition.y - mSliderPosition.y ) < (float)mSlider.getHeight() * 0.5f ) {
		float x1			= mTrackPosition.x;
		float x2			= mTrackPosition.x + (float)( mTrack.getWidth() - mSlider.getWidth() );
		mSliderPosition.x	= math<float>::clamp( mCursorPosition.x, x1, x2 );
	}
}