void TextParticlesApp::update() { if( !mActive ) return; // Update particles on the GPU gl::ScopedGlslProg prog( mUpdateProg ); gl::ScopedState rasterizer( GL_RASTERIZER_DISCARD, true ); // turn off fragment stage mPerlin3dTex->bind(0); mUpdateProg->uniform( "uPerlinTex", 0 ); mUpdateProg->uniform( "uStep", mStep.value() ); mUpdateProg->uniform( "uDampingSpeed", mDampingSpeed ); mUpdateProg->uniform( "uNoiseOffset", mNoiseOffset ); mUpdateProg->uniform( "uEndColor", mEndColor ); // Bind the source data (Attributes refer to specific buffers). gl::ScopedVao source( mAttributes[mSourceIndex] ); // Bind destination as buffer base. gl::bindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER, 0, mParticleBuffer[mDestinationIndex] ); gl::beginTransformFeedback( GL_POINTS ); // Draw source into destination, performing our vertex transformations. gl::drawArrays( GL_POINTS, 0, mTextParticleCount ); gl::endTransformFeedback(); mPerlin3dTex->unbind(); // Swap source and destination for next loop std::swap( mSourceIndex, mDestinationIndex ); }
void RotatingCubeApp::draw() { gl::clear(); gl::setMatrices( mCam ); gl::ScopedModelMatrix modelScope; gl::multModelMatrix( mCubeRotation ); mTexture->bind(); mBatch->draw(); }
void NormalMappingBasicApp::setup() { mCam.lookAt( vec3( 3, 2, 4 ), vec3( 0 ) ); mDiffuseTex = gl::Texture::create( loadImage( loadAsset( "diffuseMap.jpg" ) ), gl::Texture::Format().mipmap() ); mDiffuseTex->bind(); mNormalTex = gl::Texture::create( loadImage( loadAsset( "normalMap.png" ) ), gl::Texture::Format().mipmap() ); mNormalTex->bind( 1 ); #if defined( CINDER_GL_ES ) mGlsl = gl::GlslProg::create( loadAsset( "shader_es2.vert" ), loadAsset( "shader_es2.frag" ) ); #else mGlsl = gl::GlslProg::create( loadAsset( "shader.vert" ), loadAsset( "shader.frag" ) ); #endif mBatch = gl::Batch::create( geom::Cube() >> geom::Transform( scale( vec3( 1.5f ) ) ), mGlsl ); gl::ScopedGlslProg glslScp( mGlsl ); mGlsl->uniform( "uDiffuseMap", 0 ); mGlsl->uniform( "uNormalMap", 1 ); mGlsl->uniform( "uLightLocViewSpace", vec3( 0, 0, 1 ) ); gl::enableDepthWrite(); gl::enableDepthRead(); }
void Emitter::iterateListExist() { gl::enable( GL_TEXTURE_2D ); particleImg->bind(); for( list<Particle>::iterator it = particles.begin(); it != particles.end(); ) { if( ! it->ISDEAD ) { it->exist(); ++it; } else { it = particles.erase( it ); } } }
void ArcballTestApp::draw() { CameraPersp &cam = ( mUsingCameraUi ) ? mDebugCam : mCam; gl::clear( Color( 0, 0.0f, 0.15f ) ); gl::setMatrices( cam ); // draw the earth gl::enableDepthRead(); gl::enableDepthWrite(); gl::translate( mEarthSphere.getCenter() ); gl::rotate( mArcball.getQuat() ); mEarthTex->bind(); mEarth->draw(); // draw constraint axis if( mArcball.isUsingConstraint() ) { gl::setMatrices( cam ); gl::color( 1, 1, 0 ); gl::translate( mEarthSphere.getCenter() ); gl::rotate( glm::rotation( vec3( 0, 1, 0 ), mArcball.getConstraintAxis() ) ); mConstraintAxis->draw(); } gl::disableDepthRead(); // draw from vector marker gl::setMatrices( cam ); gl::color( 0, 1, 0.25f ); gl::translate( mEarthSphere.getCenter() + mArcball.getFromVector() * mEarthSphere.getRadius() ); mMarker->draw(); // draw to vector marker gl::setMatrices( cam ); gl::color( 1, 0.5f, 0.25f ); gl::translate( mEarthSphere.getCenter() + mArcball.getToVector() * mEarthSphere.getRadius() ); mMarker->draw(); // draw the elliptical axes gl::setMatricesWindow( getWindowSize() ); gl::color( 1, 0, 0 ); vec2 center, axisA, axisB; mCam.calcScreenProjection( mEarthSphere, getWindowSize(), ¢er, &axisA, &axisB ); gl::drawLine( center - axisA, center + axisA ); gl::drawLine( center - axisB, center + axisB ); }
void ObjLoaderApp::setup() { #if defined( CINDER_GL_ES ) mGlsl = gl::GlslProg::create( loadAsset( "shader_es2.vert" ), loadAsset( "shader_es2.frag" ) ); #else mGlsl = gl::GlslProg::create( loadAsset( "shader.vert" ), loadAsset( "shader.frag" ) ); #endif mGlsl->uniform( "uTex0", 0 ); mCam.setPerspective( 45.0f, getWindowAspectRatio(), 0.1, 10000 ); mCamUi = CameraUi( &mCam ); mCheckerTexture = gl::Texture::create( ip::checkerboard( 512, 512, 32 ) ); mCheckerTexture->bind( 0 ); loadObj( loadResource( RES_8LBS_OBJ ) ); mArcball = Arcball( &mCam, mBoundingSphere ); }
void InstancedTeapotsApp::setup() { mCam.lookAt( vec3( 0, CAMERA_Y_RANGE.first, 0 ), vec3( 0 ) ); mTexture = gl::Texture::create( loadImage( loadAsset( "texture.jpg" ) ), gl::Texture::Format().mipmap() ); #if ! defined( CINDER_GL_ES ) mGlsl = gl::GlslProg::create( loadAsset( "shader.vert" ), loadAsset( "shader.frag" ) ); #elif defined( CINDER_GL_ES_3 ) mGlsl = gl::GlslProg::create(loadAsset("shader_es3.vert"), loadAsset("shader_es3.frag")); #else mGlsl = gl::GlslProg::create( loadAsset( "shader_es2.vert" ), loadAsset( "shader_es2.frag" ) ); #endif gl::VboMeshRef mesh = gl::VboMesh::create( geom::Teapot().subdivisions( 4 ) ); // create an array of initial per-instance positions laid out in a 2D grid std::vector<vec3> positions; for( size_t potX = 0; potX < NUM_INSTANCES_X; ++potX ) { for( size_t potY = 0; potY < NUM_INSTANCES_Y; ++potY ) { float instanceX = potX / (float)NUM_INSTANCES_X - 0.5f; float instanceY = potY / (float)NUM_INSTANCES_Y - 0.5f; positions.push_back( vec3( instanceX * vec3( DRAW_SCALE, 0, 0 ) + instanceY * vec3( 0, 0, DRAW_SCALE ) ) ); } } // create the VBO which will contain per-instance (rather than per-vertex) data mInstanceDataVbo = gl::Vbo::create( GL_ARRAY_BUFFER, positions.size() * sizeof(vec3), positions.data(), GL_DYNAMIC_DRAW ); // we need a geom::BufferLayout to describe this data as mapping to the CUSTOM_0 semantic, and the 1 (rather than 0) as the last param indicates per-instance (rather than per-vertex) geom::BufferLayout instanceDataLayout; instanceDataLayout.append( geom::Attrib::CUSTOM_0, 3, 0, 0, 1 /* per instance */ ); // now add it to the VboMesh we already made of the Teapot mesh->appendVbo( instanceDataLayout, mInstanceDataVbo ); // and finally, build our batch, mapping our CUSTOM_0 attribute to the "vInstancePosition" GLSL vertex attribute mBatch = gl::Batch::create( mesh, mGlsl, { { geom::Attrib::CUSTOM_0, "vInstancePosition" } } ); gl::enableDepthWrite(); gl::enableDepthRead(); mTexture->bind(); }
void ShaderToyApp::draw() { // Bind textures. if( mChannel0 ) mChannel0->bind( 0 ); if( mChannel1 ) mChannel1->bind( 1 ); if( mChannel2 ) mChannel2->bind( 2 ); if( mChannel3 ) mChannel3->bind( 3 ); // Render the current shader to a frame buffer. if( mShaderCurrent && mBufferCurrent ) { gl::ScopedFramebuffer fbo( mBufferCurrent ); // Bind shader. gl::ScopedGlslProg shader( mShaderCurrent ); setUniforms(); // Clear buffer and draw full screen quad (flipped). gl::clear(); gl::drawSolidRect( Rectf( 0, (float)getWindowHeight(), (float)getWindowWidth(), 0 ) ); } // Render the next shader to a frame buffer. if( mShaderNext && mBufferNext ) { gl::ScopedFramebuffer fbo( mBufferNext ); // Bind shader. gl::ScopedGlslProg shader( mShaderNext ); setUniforms(); // Clear buffer and draw full screen quad (flipped). gl::clear(); gl::drawSolidRect( Rectf( 0, (float)getWindowHeight(), (float)getWindowWidth(), 0 ) ); } // Perform a cross-fade between the two shaders. double time = getElapsedSeconds() - mTransitionTime; double fade = math<double>::clamp( time / mTransitionDuration, 0.0, 1.0 ); if( fade <= 0.0 ) { // Transition has not yet started. Keep drawing current buffer. gl::draw( mBufferCurrent->getColorTexture(), getWindowBounds() ); } else if( fade < 1.0 ) { // Transition is in progress. // Use a transition shader to avoid having to draw one buffer on top of another. gl::ScopedTextureBind tex0( mBufferCurrent->getColorTexture(), 0 ); gl::ScopedTextureBind tex1( mBufferNext->getColorTexture(), 1 ); gl::ScopedGlslProg shader( mShaderTransition ); mShaderTransition->uniform( "iSrc", 0 ); mShaderTransition->uniform( "iDst", 1 ); mShaderTransition->uniform( "iFade", (float)fade ); gl::drawSolidRect( getWindowBounds() ); } else if( mShaderNext ) { // Transition is done. Swap shaders. gl::draw( mBufferNext->getColorTexture(), getWindowBounds() ); mShaderCurrent = mShaderNext; mShaderNext.reset(); mPathCurrent = mPathNext; mPathNext.clear(); getWindow()->setTitle( std::string( "ShaderToyApp - Showing " ) + mPathCurrent.filename().string() ); } else { // No transition in progress. gl::draw( mBufferCurrent->getColorTexture(), getWindowBounds() ); } }
void Emitter::render() { emitterImg->bind(); renderImage( loc, 150, myColor, 1.0 ); }
void MeshViewApp::draw() { // Clear the window gl::clear(); gl::color(Color::white()); if(isInitialized()) { // Get ready to draw in 3D gl::pushMatrices(); gl::setMatrices(m_camera); gl::enableDepthRead(); gl::enableDepthWrite(); // Bind textures if(m_texDiffuse) m_texDiffuse->enableAndBind(); if (m_texNormal) m_texNormal->bind(1); if (m_texSpecular) m_texSpecular->bind(2); if(m_texAO) m_texAO->bind(3); if(m_texEmissive) m_texEmissive->bind(4); // Bind shader m_shader->bind(); m_shader->uniform("texDiffuse", 0); m_shader->uniform("texNormal", 1); m_shader->uniform("texSpecular", 2); m_shader->uniform("texAO", 3); m_shader->uniform("texEmissive", 4); m_shader->uniform("texDiffusePower", m_texDiffusePower); m_shader->uniform("texNormalPower", m_texNormalPower); m_shader->uniform("texSpecularPower", m_texSpecularPower); m_shader->uniform("texAOPower", m_texAOPower); m_shader->uniform("texEmissivePower", m_texEmissivePower); m_shader->uniform("diffuseEnabled", m_diffuseEnabled); m_shader->uniform("normalEnabled", m_normalEnabled); m_shader->uniform("specularEnabled", m_specularEnabled); m_shader->uniform("aoEnabled", m_aoEnabled); m_shader->uniform("emissiveEnabled", m_emissiveEnabled); m_shader->uniform("material.Ka", m_matAmbient); m_shader->uniform("material.Kd", m_matDiffuse); m_shader->uniform("material.Ks", m_matSpecular); m_shader->uniform("material.Shininess", m_matShininess); m_shader->uniform("gamma", m_gamma); // Enable lights m_light1->enable(); m_light2->enable(); // Render model gl::pushModelView(); gl::multModelView(m_matrix); m_assimpLoader.draw(); gl::popModelView(); // Disable lights m_light1->disable(); m_light2->disable(); // Unbind shader m_shader->unbind(); // Unbind textures gl::disable(m_texDiffuse->getTarget()); // Disable 3D rendering gl::disableDepthWrite(); gl::disableDepthRead(); // Restore matrices gl::popMatrices(); // Enable 2D rendering gl::setMatricesWindow(getWindowSize()); gl::setViewport(getWindowBounds()); // Render parameter window if(m_params) m_params->draw(); } // Render debug information Debug::get().draw(ColorAf::white()); }
void PointCloudApp::draw() { gl::viewport( getWindowSize() ); gl::clear(); gl::setMatrices( mCamUi.getCamera() ); gl::enableAlphaBlending(); gl::enableDepthRead(); gl::enableDepthWrite(); if ( mSurfaceColor ) { if ( mTextureColor ) { mTextureColor->update( *mSurfaceColor ); } else { mTextureColor = gl::Texture::create( *mSurfaceColor ); } mTextureColor->bind( 0 ); } if ( mChannelDepth ) { if ( mTextureDepth ) { gl::ScopedTextureBind scopeTextureBind( mTextureDepth->getTarget(), mTextureDepth->getId() ); glTexSubImage2D( mTextureDepth->getTarget(), 0, 0, 0, mTextureDepth->getWidth(), mTextureDepth->getHeight(), GL_RED_INTEGER, GL_UNSIGNED_SHORT, mChannelDepth->getData() ); } else { mTextureDepth = gl::Texture::create( mChannelDepth->getWidth(), mChannelDepth->getHeight(), gl::Texture::Format().dataType( GL_UNSIGNED_SHORT ).internalFormat( GL_R16UI ) ); } mTextureDepth->bind( 1 ); } if ( mSurfaceDepthToCameraTable && !mTextureDepthToCameraTable ) { mTextureDepthToCameraTable = gl::Texture::create( *mSurfaceDepthToCameraTable ); mTextureDepthToCameraTable->bind( 2 ); } if ( mSurfaceDepthToColorTable ) { if ( mTextureDepthToColorTable ) { mTextureDepthToColorTable->update( *mSurfaceDepthToColorTable ); } else { mTextureDepthToColorTable = gl::Texture::create( *mSurfaceDepthToColorTable, gl::Texture::Format().dataType( GL_FLOAT ) ); } mTextureDepthToColorTable->bind( 3 ); } gl::ScopedGlslProg scopeGlsl( mGlslProg ); gl::setDefaultShaderVars(); mGlslProg->uniform( "uTextureColor", 0 ); mGlslProg->uniform( "uTextureDepth", 1 ); mGlslProg->uniform( "uTextureDepthToCameraTable", 2 ); mGlslProg->uniform( "uTextureDepthToColorTable", 3 ); gl::draw( mVboMesh ); if ( mTextureColor ) { mTextureColor->unbind(); } if ( mTextureDepth ) { mTextureDepth->unbind(); } if ( mTextureDepthToCameraTable ) { mTextureDepthToCameraTable->unbind(); } if ( mTextureDepthToColorTable ) { mTextureDepthToColorTable->unbind(); } mParams->draw(); }