Skip to content

Instantly share code, notes, and snippets.

@thyrrestrup
Last active November 9, 2015 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thyrrestrup/f52f27654ff88d84bbe9 to your computer and use it in GitHub Desktop.
Save thyrrestrup/f52f27654ff88d84bbe9 to your computer and use it in GitHub Desktop.
bindFramebufferFace
void BasicIBL::prefilterCubeMap()
{
auto texFormat = gl::TextureCubeMap::Format();
texFormat.setInternalFormat( GL_RGB );
texFormat.enableMipmapping();
texFormat.setMinFilter( GL_LINEAR_MIPMAP_LINEAR );
texFormat.setMagFilter( GL_LINEAR );
texFormat.setWrap( GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE );
auto fboFormat = gl::FboCubeMap::Format().textureCubeMapFormat( texFormat );
auto fbo = gl::FboCubeMap::create( 512, 512, fboFormat );
auto shader = gl::GlslProg::create( loadAsset( "shaders/prefilter.vert" ), loadAsset( "shaders/prefilter.frag" ) );
shader->uniform( "uCubeMapTex", 0 );
shader->uniform( "uMaxLod", static_cast<float>( NUM_MIP_LEVELS ) );
auto cube = gl::Batch::create( geom::Cube().size( vec3( 2.0 ) ), shader );
gl::ScopedFramebuffer fboScp( fbo );
gl::ScopedTextureBind texScp( mCubeMapTex );
gl::ScopedMatrices mScp;
ivec2 size = fbo->getSize();
for( int level = 0; level < NUM_MIP_LEVELS; level++ ) {
gl::ScopedViewport vpScp( ivec2( 0, 0 ), size );
for( uint8_t dir = 0; dir < 6; ++dir ) {
gl::setProjectionMatrix( ci::CameraPersp( size.x, size.y, 90.0f, 0.01f, 1000.0f ).getProjectionMatrix() );
gl::setViewMatrix( fbo->calcViewMatrix( GL_TEXTURE_CUBE_MAP_POSITIVE_X + dir, vec3( 0 ) ) );
GLuint id = fbo->getTextureCubeMap()->getId();
glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + dir, id, level );
/*
The following does not work
fbo->bindFramebufferFace( GL_TEXTURE_CUBE_MAP_POSITIVE_X + dir, level );
*/
shader->uniform( "uLod", static_cast<float>( level ) );
gl::clear( Color::black() );
cube->draw();
}
CI_LOG_V( "Mipmap level " << level << " generated at " << size );
size /= 2;
}
mCubeMapFilteredTex = fbo->getTextureCubeMap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment