Esempio n. 1
0
// Render the color cube into the FBO
void FboBasicApp::renderSceneToFbo()
{
    // this will restore the old framebuffer binding when we leave this function
    // on non-OpenGL ES platforms, you can just call mFbo->unbindFramebuffer() at the end of the function
    // but this will restore the "screen" FBO on OpenGL ES, and does the right thing on both platforms
    gl::ScopedFramebuffer fbScp( mFbo );
    // clear out the FBO with blue
    gl::clear( Color( 0.25, 0.5f, 1.0f ) );

    // setup the viewport to match the dimensions of the FBO
    gl::ScopedViewport scpVp( ivec2( 0 ), mFbo->getSize() );

    // setup our camera to render the torus scene
    CameraPersp cam( mFbo->getWidth(), mFbo->getHeight(), 60.0f );
    cam.setPerspective( 60, mFbo->getAspectRatio(), 1, 1000 );
    cam.lookAt( vec3( 2.8f, 1.8f, -2.8f ), vec3( 0 ));
    gl::setMatrices( cam );

    // set the modelview matrix to reflect our current rotation
    gl::setModelMatrix( mRotation );

    // render the color cube
    gl::ScopedGlslProg shaderScp( gl::getStockShader( gl::ShaderDef().color() ) );
    gl::color( Color( 1.0f, 0.5f, 0.25f ) );
    gl::drawColorCube( vec3( 0 ), vec3( 2.2f ) );
    gl::color( Color::white() );
}
// Render our scene into the FBO (a cube)
void FboMultipleRenderTargetsApp::renderSceneToFbo()
{
	// setup our camera to render our scene
	CameraPersp cam( mFbo->getWidth(), mFbo->getHeight(), 60 );
	cam.setPerspective( 60, mFbo->getAspectRatio(), 1, 1000 );
	cam.lookAt( vec3( 2.8f, 1.8f, -2.8f ), vec3( 0 ) );

	// bind our framebuffer in a safe way:
	gl::ScopedFramebuffer fboScope( mFbo );

	// clear out both of the attachments of the FBO with black
	gl::clear();

	// setup the viewport to match the dimensions of the FBO, storing the previous state
	gl::ScopedViewport viewportScope( ivec2( 0 ), mFbo->getSize() );

	// store matrices before updating for CameraPersp
	gl::ScopedMatrices matScope;
	gl::setMatrices( cam );

	// set the modelview matrix to reflect our current rotation
	gl::multModelMatrix( mRotation );

	// render the torus with our multiple-output shader
	gl::ScopedGlslProg glslScope( mGlslMultipleOuts );
	gl::drawCube( vec3( 0 ), vec3( 2.2f ) );
}
void ShadowMappingBasic::setup()
{
	mLightPos = vec3( 0.0f, 5.0f, 1.0f );
	
	gl::Texture2d::Format depthFormat;
	
#if defined( CINDER_GL_ES )
	depthFormat.setInternalFormat( GL_DEPTH_COMPONENT16 );
#else
	depthFormat.setInternalFormat( GL_DEPTH_COMPONENT32F );
	depthFormat.setCompareMode( GL_COMPARE_REF_TO_TEXTURE );
#endif
	depthFormat.setMagFilter( GL_LINEAR );
	depthFormat.setMinFilter( GL_LINEAR );
	depthFormat.setWrap( GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE );
	depthFormat.setCompareFunc( GL_LEQUAL );
	
	mShadowMapTex = gl::Texture2d::create( FBO_WIDTH, FBO_HEIGHT, depthFormat );

	mCam.setPerspective( 40.0f, getWindowAspectRatio(), 0.5f, 500.0f );
		
	gl::Fbo::Format fboFormat;
	fboFormat.attachment( GL_DEPTH_ATTACHMENT, mShadowMapTex );
	mFbo = gl::Fbo::create( FBO_WIDTH, FBO_HEIGHT, fboFormat );
	
	// Set up camera from the light's viewpoint
	mLightCam.setPerspective( 100.0f, mFbo->getAspectRatio(), 0.5f, 10.0f );
	mLightCam.lookAt( mLightPos, vec3( 0.0f ) );
	
	try {
#if defined( CINDER_GL_ES )
		mGlsl = gl::GlslProg::create( loadAsset( "shadow_shader_es2.vert" ), loadAsset( "shadow_shader_es2.frag" ) );
#else
		mGlsl = gl::GlslProg::create( loadAsset( "shadow_shader.vert" ), loadAsset( "shadow_shader.frag" ) );
#endif
	}
	catch ( Exception &exc ) {
		CI_LOG_EXCEPTION( "glsl load failed", exc );
		std::terminate();
	}
	
	auto teapot				= geom::Teapot().subdivisions( 8 );
	mTeapotBatch			= gl::Batch::create( teapot, gl::getStockShader( gl::ShaderDef() ) );
	mTeapotShadowedBatch	= gl::Batch::create( teapot, mGlsl );
	
	auto floor				= geom::Cube().size( 10.0f, 0.5f, 10.0f );
	mFloorBatch				= gl::Batch::create( floor, gl::getStockShader( gl::ShaderDef() ) );
	mFloorShadowedBatch		= gl::Batch::create( floor, mGlsl );
	
	gl::enableDepthRead();
	gl::enableDepthWrite();
}