Example #1
0
void CatalogApp::initBrightVbo()
{
	gl::VboMesh::Layout layout;
	layout.setStaticPositions();
	layout.setStaticTexCoords2d();
	layout.setStaticColorsRGB();
	
	int numVertices = FBO_WIDTH * FBO_HEIGHT;
	// 1 quad per particle
	// 2 triangles make up the quad
	// 3 points per triangle
	mBrightVbo		= gl::VboMesh( numVertices * 2 * 3, 0, layout, GL_TRIANGLES );
	
	float s = 0.5f;
	Vec3f p0( -s, -s, 0.0f );
	Vec3f p1( -s,  s, 0.0f );
	Vec3f p2(  s,  s, 0.0f );
	Vec3f p3(  s, -s, 0.0f );
	
	Vec2f t0( 0.0f, 0.0f );
	Vec2f t1( 0.0f, 1.0f );
	Vec2f t2( 1.0f, 1.0f );
	Vec2f t3( 1.0f, 0.0f );
	
	vector<Vec3f>		positions;
	vector<Vec2f>		texCoords;
	vector<Color>		colors;
	
	for( int x = 0; x < FBO_WIDTH; ++x ) {
		for( int y = 0; y < FBO_HEIGHT; ++y ) {
			float u = (float)x/(float)FBO_WIDTH;
			float v = (float)y/(float)FBO_HEIGHT;
			Color c = Color( u, v, 0.0f );
			
			positions.push_back( p0 );
			positions.push_back( p1 );
			positions.push_back( p2 );
			texCoords.push_back( t0 );
			texCoords.push_back( t1 );
			texCoords.push_back( t2 );
			colors.push_back( c );
			colors.push_back( c );
			colors.push_back( c );
			
			positions.push_back( p0 );
			positions.push_back( p2 );
			positions.push_back( p3 );
			texCoords.push_back( t0 );
			texCoords.push_back( t2 );
			texCoords.push_back( t3 );
			colors.push_back( c );
			colors.push_back( c );
			colors.push_back( c );
		}
	}
	
	mBrightVbo.bufferPositions( positions );
	mBrightVbo.bufferTexCoords2d( 0, texCoords );
	mBrightVbo.bufferColorsRGB( colors );
	mBrightVbo.unbindBuffers();
}
void millionParticlesApp::setup()
{
    gl::clear();

    try {
        mPosShader = gl::GlslProg(ci::app::loadResource(POS_VS),ci::app::loadResource(POS_FS));
        mVelShader = gl::GlslProg(ci::app::loadResource(VEL_VS),ci::app::loadResource(VEL_FS));
    }
    catch( gl::GlslProgCompileExc &exc ) {
        std::cout << "Shader compile error: " << std::endl;
        std::cout << exc.what();
    }
    catch( ... ) {
        std::cout << "Unable to load shader" << std::endl;
    }

    //controls
    mDrawTextures = false;
    mIsFullScreen = false;

    mFrameCounter = 0;

    mPerlin = Perlin(32,clock() * .1f);

    //initialize buffer
    Surface32f mPosSurface = Surface32f(PARTICLES,PARTICLES,true);
    Surface32f mVelSurface = Surface32f(PARTICLES,PARTICLES,true);
    Surface32f mInfoSurface = Surface32f(PARTICLES,PARTICLES,true);
    Surface32f mNoiseSurface = Surface32f(PARTICLES,PARTICLES,true);

    Surface32f::Iter iterator = mPosSurface.getIter();


    while(iterator.line())
    {
        while(iterator.pixel())
        {

            mVertPos = Vec3f(Rand::randFloat(getWindowWidth()) / (float)getWindowWidth(),
                             Rand::randFloat(getWindowHeight()) / (float)getWindowHeight(),0.0f);

            //velocity
            Vec2f vel = Vec2f(Rand::randFloat(-.005f,.005f),Rand::randFloat(-.005f,.005f));

            float nX = iterator.x() * 0.005f;
            float nY = iterator.y() * 0.005f;
            float nZ = app::getElapsedSeconds() * 0.1f;
            Vec3f v( nX, nY, nZ );
            float noise = mPerlin.fBm( v );

            float angle = noise * 15.0f ;

            //vel = Vec3f( cos( angle ) * 6.28f, cos( angle ) * 6.28f, 0.0f );

            //noise
            mNoiseSurface.setPixel(iterator.getPos(),
                                   Color( cos( angle ) * Rand::randFloat(.00005f,.0002f), sin( angle ) * Rand::randFloat(.00005f,.0002f), 0.0f ));

            //position + mass
            mPosSurface.setPixel(iterator.getPos(),
                                 ColorA(mVertPos.x,mVertPos.y,mVertPos.z,
                                        Rand::randFloat(.00005f,.0002f)));
            //forces + decay
            mVelSurface.setPixel(iterator.getPos(), Color(vel.x,vel.y, Rand::randFloat(.01f,1.00f)));

            //particle age
            mInfoSurface.setPixel(iterator.getPos(),
                                  ColorA(Rand::randFloat(.007f,1.0f), 1.0f,0.00f,1.00f));

        }
    }

    //gl texture settings
    gl::Texture::Format tFormat;
    tFormat.setInternalFormat(GL_RGBA16F_ARB);

    gl::Texture::Format tFormatSmall;
    tFormat.setInternalFormat(GL_RGBA8);

    mSpriteTex = gl::Texture( loadImage( loadResource( "point.png" ) ), tFormatSmall);


    mNoiseTex = gl::Texture(mNoiseSurface, tFormatSmall);
    mNoiseTex.setWrap( GL_REPEAT, GL_REPEAT );
    mNoiseTex.setMinFilter( GL_NEAREST );
    mNoiseTex.setMagFilter( GL_NEAREST );

    mPosTex = gl::Texture(mPosSurface, tFormat);
    mPosTex.setWrap( GL_REPEAT, GL_REPEAT );
    mPosTex.setMinFilter( GL_NEAREST );
    mPosTex.setMagFilter( GL_NEAREST );

    mVelTex = gl::Texture(mVelSurface, tFormat);
    mVelTex.setWrap( GL_REPEAT, GL_REPEAT );
    mVelTex.setMinFilter( GL_NEAREST );
    mVelTex.setMagFilter( GL_NEAREST );

    mInfoTex = gl::Texture(mInfoSurface, tFormatSmall);
    mInfoTex.setWrap( GL_REPEAT, GL_REPEAT );
    mInfoTex.setMinFilter( GL_NEAREST );
    mInfoTex.setMagFilter( GL_NEAREST );

    //initialize fbo
    gl::Fbo::Format format;
    format.enableDepthBuffer(false);
    format.enableColorBuffer(true, 3);
    format.setMinFilter( GL_NEAREST );
    format.setMagFilter( GL_NEAREST );
    format.setWrap(GL_CLAMP,GL_CLAMP);
    format.setColorInternalFormat( GL_RGBA16F_ARB );

    mFbo[0] = gl::Fbo(PARTICLES,PARTICLES, format);
    mFbo[1] = gl::Fbo(PARTICLES,PARTICLES, format);

    initFBO();

    //fill dummy fbo
    vector<Vec2f> texCoords;
    vector<Vec3f> vertCoords, normCoords;
    vector<uint32_t> indices;

    gl::VboMesh::Layout layout;
    layout.setStaticIndices();
    layout.setStaticPositions();
    layout.setStaticTexCoords2d();
    layout.setStaticNormals();

    mVbo = gl::VboMesh(PARTICLES*PARTICLES,PARTICLES*PARTICLES,layout,GL_POINTS);

    for (int x = 0; x < PARTICLES; ++x) {
        for (int y = 0; y < PARTICLES; ++y) {
            indices.push_back( x * PARTICLES + y);
            texCoords.push_back( Vec2f( x/(float)PARTICLES, y/(float)PARTICLES));
        }
    }

    mVbo.bufferIndices(indices);
    mVbo.bufferTexCoords2d(0, texCoords);

}