void FragmentShaderToyApp::draw()
{
	// clear out the window with black
	gl::clear(Color(0.2, 0, 0));
	// use our shader for this draw loop
	//gl::GlslProgScope prog(mProg);
	gl::ScopedGlslProg shader(mProg);
	texture->bind(0);
	// draw our screen rectangle
	gl::draw(mMesh);

	// if you want to save a screenshot
	if (mDoSave)
	{
		mDoSave = false;
		try
		{
			writeImage(getSaveFilePath(), copyWindowSurface());
		}
		catch (ci::Exception &exc)
		{
			console() << "Failed to save image: " << exc.what() << endl;
		}
	}
}
Esempio n. 2
0
void GpGpuApp::draw()
{
	// We're going to draw new data onto the "ping" FBO,
	// using the "pong" FBO's textures as input
	size_t pong = ( mFboIndex + 1 ) % 2;
	
	// Set up OpenGL for data
	gl::disableDepthRead();
	gl::disableDepthWrite();
	gl::setViewport( mFbo[ mFboIndex ].getBounds() );
	gl::color( ColorAf::white() );
	
	// Draw any new input onto the acceleration texture
	mFbo[ pong ].bindFramebuffer();
	glDrawBuffer( GL_COLOR_ATTACHMENT2 );
	if ( mMouseDown ) {
		Vec2f fboSize	= Vec2f( mFbo[ mFboIndex ].getSize() );
		Vec2f winSize	= Vec2f( app::getWindowSize() );
		
		gl::setMatricesWindow( fboSize, true );
		
		Vec2f brushSize	= Vec2f::one() * mBrushSize * fboSize;
		Vec2f pos		= ( mMouse / winSize );
		pos.y			= 1.0f - pos.y;
		pos				*= fboSize;
		
		mGlslProgGpGpu0->bind();
		mGlslProgGpGpu0->uniform( "color", ColorAf( mMouseVelocity.x, 0.0f, 1.0f - mMouseVelocity.y, 1.0f ) );
		mGlslProgGpGpu0->uniform( "tex", 0 );
		
		gl::enable( GL_TEXTURE_2D );
		mTextureBrush->bind();
		gl::drawSolidRect( Rectf( pos - brushSize, pos + brushSize ) );
		mTextureBrush->unbind();
		gl::disable( GL_TEXTURE_2D );
		
		mGlslProgGpGpu0->unbind();
	}
	mFbo[ pong ].unbindFramebuffer();
	
	// Now let's do an update pass in GLSL
	mFbo[ mFboIndex ].bindFramebuffer();
	gl::setMatricesWindow( mFbo[ mFboIndex ].getSize(), false );
	
	// Bind the "pong" textures to use as input data
	for ( int32_t i = 0; i < 3; ++i ) {
		mFbo[ pong ].bindTexture( i, i );
	}
	
	// Set up shader to read data textures
	mGlslProgGpGpu1->bind();
	mGlslProgGpGpu1->uniform( "offsets",		0 );
	mGlslProgGpGpu1->uniform( "velocities",		1 );
	mGlslProgGpGpu1->uniform( "acceleration",	2 );
	
	// Draw a rect to process data
	glDrawBuffers( 3, kColorAttachments );
	gl::drawSolidRect( mFbo[ pong ].getBounds() );
	
	// Unbind everything
	mFbo[ pong ].unbindTexture();
	mGlslProgGpGpu1->unbind();
	mFbo[ mFboIndex ].unbindFramebuffer();
	
	// Swap FBOs
	mFboIndex = pong;
	
	/////////////////////////////////
	
	// Make sure we have data to work with before we draw geometry
	if ( mFbo[ mFboIndex ] &&
		mFbo[ mFboIndex ].getTexture( 0 ) &&
		mFbo[ mFboIndex ].getTexture( 1 ) ) {
		
		// Set up window for 3D drawing
		gl::clear( Colorf( 0.5f, 0.45f, 0.4f ) );
		gl::setViewport( getWindowBounds() );
		gl::setMatrices( mCamera );
		gl::enableDepthRead();
		gl::enableDepthWrite();
		gl::multModelView( mArcball.getQuat() );
		gl::color( ColorAf::black() );
		
		// Set up shader to render scene
		mGlslProgDraw->bind();
		mGlslProgDraw->uniform( "Ax",			mLightAmbient );
		mGlslProgDraw->uniform( "Ac",			mLightAttenuationConstant );
		mGlslProgDraw->uniform( "Al",			mLightAttenuationLinear );
		mGlslProgDraw->uniform( "Aq",			mLightAttenuationQuadratic );
		mGlslProgDraw->uniform( "Dx",			mLightDiffuse );
		mGlslProgDraw->uniform( "eyePoint",		mEyePoint );
		mGlslProgDraw->uniform( "Ka",			mMaterialAmbient );
		mGlslProgDraw->uniform( "Kd",			mMaterialDiffuse );
		mGlslProgDraw->uniform( "Ke",			mMaterialEmissive );
		mGlslProgDraw->uniform( "Ks",			mMaterialSpecular );
		mGlslProgDraw->uniform( "lightPos",		mLightPosition );
		mGlslProgDraw->uniform( "n",			mLightShine );
		mGlslProgDraw->uniform( "offsets",		0 );
		mGlslProgDraw->uniform( "projection",	mCamera.getProjectionMatrix() );
		mGlslProgDraw->uniform( "size",			Vec2f( mSize ) );
		mGlslProgDraw->uniform( "Sx",			mLightSpecular );
		mGlslProgDraw->uniform( "t",			(float)getElapsedSeconds() );
		
		// Bind textures to use as input data
		for ( int32_t i = 0; i <= 2; ++i ) {
			mFbo[ mFboIndex ].bindTexture( i, i );
		}
		
		// Draw instanced
		drawInstanced( mMesh, mSize.x * mSize.y );
		
		// Finished drawing
		mFbo[ mFboIndex ].unbindTexture();
		mGlslProgDraw->unbind();
		
		// Draw textures so we can see what's going on under the hood
		gl::setMatricesWindow( getWindowSize() );
		gl::disableDepthRead();
		gl::disableDepthWrite();
		gl::color( ColorAf::white() );
		gl::pushMatrices();
		
		float x = 20.0f;
		float y = 440.0f;
		
		float width = 64.0f;
		Area srcArea( Vec2i::zero(), mSize );
		Rectf destRect( x, y, x + width, y + width );
		
		gl::draw( mFbo[ 0 ].getTexture( 0 ), srcArea, destRect );
		destRect.x1 += width;
		destRect.x2 += width;
		gl::draw( mFbo[ 1 ].getTexture( 0 ), srcArea, destRect );
		
		destRect.y1 += width;
		destRect.y2 += width;
		destRect.x1 = x;
		destRect.x2 = x + width;
		gl::draw( mFbo[ 0 ].getTexture( 1 ), srcArea, destRect );
		destRect.x1 += width;
		destRect.x2 += width;
		gl::draw( mFbo[ 1 ].getTexture( 1 ), srcArea, destRect );
		
		destRect.y1 += width;
		destRect.y2 += width;
		destRect.x1 = x;
		destRect.x2 = x + width;
		gl::draw( mFbo[ 0 ].getTexture( 2 ), srcArea, destRect );
		destRect.x1 += width;
		destRect.x2 += width;
		gl::draw( mFbo[ 1 ].getTexture( 2 ), srcArea, destRect );
		gl::popMatrices();
	}
	
	// Draw parameters
	if ( getElapsedFrames() > 1 ) { // This condition prevents a memory leak
		mParams->draw();
	}
}