Beispiel #1
0
void SMAA::draw( gl::Texture2dRef source, const Area& bounds )
{
	if( !mSMAAFirstPass || !mSMAASecondPass || !mSMAAThirdPass )
		return;

	// Create or resize buffers.
	const int width = source->getWidth();
	const int height = source->getHeight();
	createBuffers( width, height );

	// Apply first two passes.
	doEdgePass( source );
	doBlendPass();

	// Apply SMAA.
	gl::ScopedTextureBind tex0( source );
	gl::ScopedTextureBind tex1( mFboBlendPass->getColorTexture(), 1 );
	gl::ScopedGlslProg shader( mSMAAThirdPass->program() );
	mSMAAThirdPass->uniform( "SMAA_RT_METRICS", mMetrics );
	mSMAAThirdPass->uniform( "uColorTex", 0 );
	mSMAAThirdPass->uniform( "uBlendTex", 1 );

	gl::color( Color::white() );
	gl::drawSolidRect( bounds );

	//	gl::draw( mAreaTex );
}
void ImageViewer::update()
{
	mCinderDS->update();
	mTexRgb->update(*mCinderDS->getRgbFrame());
	auto depthChannel = mCinderDS->getDepthFrame();
	auto depthSurface = Surface8u::create(mDepthDims.x, mDepthDims.y, SurfaceChannelOrder::RGB);
	auto iter = depthSurface->getIter();

	while (iter.line())
	{
		while (iter.pixel())
		{
			iter.r() = 0;
			iter.g() = 0;
			iter.b() = 0;

			// Not optimal mapping, TODO: color from histogram
			float depthValue = (float)*depthChannel->getData(iter.x(), iter.y()); 
			if (depthValue > 100 && depthValue < 1000)
			{
				int colorValue = (int)lmap<float>(depthValue, 100, 1000, 255, 0);
				iter.r() = colorValue;
				iter.g() = colorValue;
			}
		}
	}

	mTexDepth->update(*depthSurface);
}
Beispiel #3
0
void Day41App::setup()
{
    mCam.lookAt(vec3(0,0,5), vec3(0., 1.5, 0.));
    mCam.setPerspective(45.f, getWindowAspectRatio(), 1.f, 1000.f);

    auto img = loadImage(loadAsset("box.png"));
    auto img1 = loadImage(loadAsset("box_spec.png"));
    mDiffuseMap = gl::Texture2d::create(img);
    mSpecularMap = gl::Texture2d::create(img1);
    mDiffuseMap->bind(0);
    mSpecularMap->bind(1);


    lightPos = vec3(0.f);

    theta = 0.0;

    setupGlsl();

    mCamUi = CameraUi(&mCam, getWindow());

    auto cube = geom::Cube();

    mBatch = gl::Batch::create(cube, mGlsl);

    auto sphere = geom::Sphere();
    auto scale = geom::Scale(0.1f,0.1f,0.1f);
    auto shader = gl::ShaderDef().color();
    auto rotate = geom::Rotate(angleAxis(2.1f, vec3(1.0,0.,0.f)));
    mLight = gl::Batch::create(sphere >> scale >> rotate, gl::getStockShader(shader));

    gl::enableDepthRead();
    gl::enableDepthWrite();
}
void PointCloudApp::update()
{
	
	mCinderRS->update();
	mPoints.clear();
	Channel16u cChanDepth = mCinderRS->getDepthFrame();

	mTexRgb->update(mCinderRS->getRgbFrame());
	for (int dy = 0; dy < mDepthDims.y; ++dy)
	{
		for (int dx = 0; dx < mDepthDims.x; ++dx)
		{
			float cDepth = (float)*cChanDepth.getData(dx, dy);
			if (cDepth > 100 && cDepth < 1000)
			{
				vec3 cPos = mCinderRS->getDepthSpacePoint(vec3(dx, dy, cDepth));
				vec2 cUV = mCinderRS->getColorCoordsFromDepthImage(static_cast<float>(dx),
																	static_cast<float>(dy),
																	cDepth);
				mPoints.push_back(CloudPoint(cPos, cUV));
			}
		}
	}

	mBufferObj->bufferData(mPoints.size()*sizeof(CloudPoint), mPoints.data(), GL_DYNAMIC_DRAW);
	mMeshObj = gl::VboMesh::create(mPoints.size(), GL_POINTS, { { mAttribObj, mBufferObj } });
	mDrawObj->replaceVboMesh(mMeshObj);
}
void WarpPerspective::draw( const gl::Texture2dRef &texture, const Area &srcArea, const Rectf &destRect )
{
	// clip against bounds
	Area  area = srcArea;
	Rectf rect = destRect;
	clip( area, rect );

	// save current drawing color
	const ColorA &  currentColor = gl::context()->getCurrentColor();
	gl::ScopedColor color( currentColor );

	// adjust brightness
	if( mBrightness < 1.f ) {
		ColorA drawColor = mBrightness * currentColor;
		drawColor.a = currentColor.a;

		gl::color( drawColor );
	}

	// create shader if necessary
	createShader();

	// draw texture
	gl::pushModelMatrix();
	gl::multModelMatrix( getTransform() );

	gl::ScopedTextureBind tex0( texture );
	gl::ScopedGlslProg    shader( mShader );
	mShader->uniform( "uLuminance", mLuminance );
	mShader->uniform( "uGamma", mGamma );
	mShader->uniform( "uEdges", mEdges );
	mShader->uniform( "uExponent", mExponent );

  
	//auto coords = texture->getAreaTexCoords( srcArea );
  float w = static_cast<float>(texture->getActualWidth());
  float h = static_cast<float>(texture->getActualHeight());
  Rectf coords(area.x1 / w, area.y1 / h, area.x2 / w, area.y2 / h);
	gl::drawSolidRect( rect, coords.getUpperLeft(), coords.getLowerRight() );

	gl::popModelMatrix();

	// draw interface
	draw();
}
void PTApp::update()
{
	if (frameDone)
	{
		imgTex->update(frameBuffer, GL_RGB, GL_UNSIGNED_BYTE, 0, frameBufferWidth, frameBufferHeight);
		frameDone = false;
        renderFrame = true;
		console() << "Render time: " << renderTime << "s\n";
	}
}
Beispiel #7
0
void Day62App::loadImage()
{
    auto pic = cinder::loadImage(loadAsset("window.png"));
    gl::Texture2d::Format fmt;
    
    //SET THE INTERNAL FORMAT TO RGBA SO THAT WE CAN TEST THE ALPHA VALUE IN THE FRAG SHADER
    fmt.internalFormat(GL_RGBA);
    fmt.wrap(GL_CLAMP_TO_EDGE);
    //    fmt.wrapS(GL_REPEAT);
    //    fmt.wrapT(GL_REPEAT);
    //    fmt.setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
    //    fmt.setMagFilter(GL_LINEAR);
    
    mTex = gl::Texture2d::create(pic, fmt);
    
    mTex->bind();
}
Beispiel #8
0
void VoronoiGpuApp::draw()
{
	gl::clear();
	gl::setMatricesWindow( getWindowSize() );
	
	gl::color( Color( 1, 1, 1 ) );
	if( mTexture ) {
		gl::draw( mTexture, toPoints( mTexture->getBounds() ) );
	}
	
	// draw the voronoi sites in yellow
	gl::color( Color( 1.0f, 1.0f, 0.0f ) );	
	for( vector<ivec2>::const_iterator ptIt = mPoints.begin(); ptIt != mPoints.end(); ++ptIt )
		gl::drawSolidCircle( toPoints( vec2( *ptIt ) ), 2.0f );
	
	gl::enableAlphaBlending();
	gl::drawStringRight( "Click to add a point", vec2( getWindowWidth() - toPixels( 10 ), getWindowHeight() - toPixels( 20 ) ), Color( 1, 0.3, 0 ), Font( "Arial", toPixels( 12 ) ) );
	gl::disableAlphaBlending();
}
void PTApp::setup()
{

	_main();

	ptScene.renderables.push_back(pt::CreateSphereRenderable(
		glm::vec3(1e5 + 1, 40.8, 81.6), 1e5,
		glm::vec3(.999,0,0), pt::Material::DIFFUSE)); // Left

	ptScene.renderables.push_back(pt::CreateSphereRenderable(
		glm::vec3(-1e5 + 99, 40.8, 81.6), 1e5,
		glm::vec3(0,.999,0), pt::Material::DIFFUSE)); // Right

	ptScene.renderables.push_back(pt::CreateSphereRenderable(
		glm::vec3(50, 40.8, 1e5), 1e5,
		glm::vec3(.75, .75, .75), pt::Material::DIFFUSE)); // Back

	ptScene.renderables.push_back(pt::CreateSphereRenderable(
		glm::vec3(50, 40.8, -1e5 + 170), 1e5,
		glm::vec3(0,0,0), pt::Material::DIFFUSE)); // Front

	ptScene.renderables.push_back(pt::CreateSphereRenderable(
		glm::vec3(50, 1e5, 81.6), 1e5,
		glm::vec3(.75, .75, .75), pt::Material::DIFFUSE)); // Bottom

	ptScene.renderables.push_back(pt::CreateSphereRenderable(
		glm::vec3(50, -1e5 + 81.6, 81.6), 1e5,
		glm::vec3(.75, .75, .75), pt::Material::DIFFUSE)); // Top

	ptScene.renderables.push_back(pt::CreateSphereRenderable(
		glm::vec3(27, 16.5, 47), 16.5,
		glm::vec3(.999), pt::Material::DIFFUSE)); // Object

	ptScene.renderables.push_back(pt::CreateSphereRenderable(
		glm::vec3(73, 16.5, 78), 16.5,
		glm::vec3(.999), pt::Material::DIFFUSE)); // Object

	ptScene.renderables.push_back(pt::CreateSphereRenderable(
		glm::vec3(50,81.6-16.5,81.6), 1.5,
		glm::vec3(0), pt::Material::DIFFUSE, glm::vec3(400, 400, 400))); // Light


	frameBufferWidth = getWindowWidth();
	frameBufferHeight = getWindowHeight();
	frameBufferDepth = 3;
	frameBufferSize = frameBufferWidth * frameBufferHeight * frameBufferDepth;

	frameBuffer = (char*)malloc(frameBufferSize * sizeof(char));

	frameDone = false;
    renderFrame = false;

	frameThread = std::shared_ptr<std::thread>(new std::thread([this](){
        
        std::shared_ptr<pt::PathTracer>  _pt = std::shared_ptr<pt::PathTracer>(new pt::PathTracer());

        _pt->Trace(ptScene,
                              frameBufferWidth,
                              frameBufferHeight,
                              8,
                              glm::dvec3(50, 52, 295.6),
                              glm::dvec3(0, -0.042612, -1),
                              .5135,
                              frameBuffer, &renderTime);

		frameDone = true;

	}));
    
    
    
	
	imgTex = gl::Texture2d::create((void*)0, GL_RGB, frameBufferWidth, frameBufferHeight);
    imgTex->setTopDown(true);

}
vec4 GpuParrallelReductionApp::findMindMax()
{
	// init the programs, fbo and texture used for the parrallel reduction
	static gl::FboRef sReductionFbo, sReadFbo;
	static gl::Texture2dRef sReductionTexture0;
	static gl::GlslProgRef sReductionProg, sCopyProg;
	auto startingSize		= mFbo->getSize() / 2;
	if( !sReductionFbo || sReductionFbo->getSize() != startingSize ) {
		sReductionTexture0	= gl::Texture2d::create( startingSize.x, startingSize.y, gl::Texture2d::Format().minFilter( GL_NEAREST_MIPMAP_NEAREST ).magFilter( GL_NEAREST ).mipmap().immutableStorage() );
		sReductionFbo		= gl::Fbo::create( startingSize.x, startingSize.y, gl::Fbo::Format()
											  .attachment( GL_COLOR_ATTACHMENT0, sReductionTexture0 )
											  .disableDepth() );
		sReductionProg		= gl::GlslProg::create( loadAsset( "minMax.vert" ), loadAsset( "minMax.frag" ) );
	}
	// start reduction profiling
	static auto sTimer0 = gl::QueryTimeSwapped::create();
	sTimer0->begin();
	
	// attach the main level of the texture
	gl::ScopedFramebuffer scopedFbo( sReductionFbo );
	glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, sReductionTexture0->getId(), 0 );
	
	// start by blitting the main fbo into the reduction one
	mFbo->blitTo( sReductionFbo, mFbo->getBounds(), sReductionFbo->getBounds(), GL_NEAREST );
	
	
	// bind the reduction program and texture and disable blending
	gl::ScopedMatrices scopedMatrices;
	gl::ScopedGlslProg scopedGlsl( sReductionProg );
	gl::ScopedBlend scopedBlend( false );
	gl::ScopedTextureBind scopedTexBind0( sReductionTexture0, 0 );
	sReductionProg->uniform( "uTex0", 0 );
	
	// iterate trough each mipmap level
	int numMipMaps = gl::Texture2d::requiredMipLevels( startingSize.x, startingSize.y, 0 );
	for( int level = 1;	level < numMipMaps; ++level ) {
		// attach the current mipmap level to the framebuffer and limit texture sampling to the previous level
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, level - 1 );
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, level - 1 );
		glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, sReductionTexture0->getId(), level );
		
		// get the current mipmap size and update uniforms
		vec2 size = gl::Texture2d::calcMipLevelSize( level, sReductionFbo->getWidth(), sReductionFbo->getHeight() );
		sReductionProg->uniform( "uInvSize", vec2( 1.0f ) / vec2( size ) );
		
		// render a fullscreen quad
		gl::ScopedViewport scopedViewport( ivec2( 0 ), size );
		gl::setMatricesWindow( size.x, size.y );
		gl::drawSolidRect( Rectf( vec2( 0.0f ), vec2( size ) ) );
	}
	// stop reduction profiling
	sTimer0->end();
	mReductionTime = sTimer0->getElapsedMilliseconds();
	
	// start readback profiling
	static auto sTimer1 = gl::QueryTimeSwapped::create();
	sTimer1->begin();
	
	// read back to the cpu and find the max value
	vec4 max( 0.0f );
	ivec2 readSize = gl::Texture2d::calcMipLevelSize( numMipMaps - 1, startingSize.x, startingSize.y );
	Surface8u surface( readSize.x, readSize.y, true );
	glGetTexImage( sReductionTexture0->getTarget(), numMipMaps - 1, GL_RGBA, GL_UNSIGNED_BYTE, surface.getData() );
	auto it = surface.getIter();
	while( it.line() ) { while( it.pixel() ) {
		max = glm::max( max, vec4( it.r(), it.g(), it.b(), it.a() ) );
	} }
	
	// stop readback profiling
	sTimer1->end();
	mReadBackTime = sTimer1->getElapsedMilliseconds();
	
	return max;
}
void PTWeekend::cleanup()
{
    GLuint textureName = imgTex->getId();
    glDeleteTextures(1, &textureName);
}
Beispiel #12
0
void Warp::draw( const gl::Texture2dRef &texture )
{
	draw( texture, texture->getBounds(), Rectf( getBounds() ) );
}
Beispiel #13
0
void Day8App::setup()
{
    mCam.lookAt(vec3(3,2,3), vec3(0));
    
    auto img = loadImage(loadAsset("texture.jpg"));
    
    setFrameRate(60);
    
    mTex = gl::Texture2d::create(img);
    mTex->bind(0);
    
    /*  THIS SHADER PROGRAM IS FOR MULTIPLYING THE TEXTURE BY THE COLOUR
     
    mGlsl = gl::GlslProg::create( gl::GlslProg::Format().
                                 vertex(    CI_GLSL(150,
                                                    uniform mat4    ciModelViewProjection;
                                                    in vec4         ciPosition;
                                                    in vec2         ciTexCoord0; //THIS COMES FROM CINDER AUTOMATICALLY FROM geom::Attrib::TEX_COORD_0
                                                    out vec2        TexCoord0; //SEND IT TO THE FRAG SHADER
                                                    
                                                    void main(void)
                                                    {
                                                        gl_Position = ciModelViewProjection * ciPosition;
                                                        TexCoord0 = ciTexCoord0;
                                                    })).
                                 
                                 fragment(  CI_GLSL(150,
                                                    uniform vec4        uColor;
                                                    uniform sampler2D   uTex0;
                                                    
                                                    in vec2             TexCoord0;
                                                    out vec4            oColor;
                                                    
                                                    void main(void)
                                                    {
                                                        oColor = texture(uTex0, TexCoord0) * uColor;
                                                    })
                                          ));
    
    
    */
    
    mGlsl = gl::GlslProg::create( gl::GlslProg::Format().
                                 vertex( CI_GLSL(150,
                                                 
                                                 uniform mat4   ciModelViewProjection;
                                                 uniform vec2   uRes;
                                                 in vec4        ciPosition;
                                                 in vec2        ciTexCoord0;
                                                 out vec2       TexCoord0;
                                                 uniform float  uAngle; //VARIABLE FROM THE CPU FOR MODULATING THE SINE WAVE
                                                 
                                                 /*
                                                 float offset(vec2 uv)
                                                 {
                                                     //return (sin(uv.x * 15.0) +
                                                     //        cos(uv.y * 7.0f +uv.x * 13.0f )) * 0.1f;
                                                     vec4 pos = ciPosition;
                                                     float z = sin(uAng
                                                     return cos(sqrt( uv.x * uv.x + uv.y * uv.y));
                                                     
                                                   
                                                 }
                                                 */
                                                 
                                                 void main(void)
                                                 {
                                                     vec4 pos       = ciPosition;
                                                     pos.y          = sin(uAngle * sqrt(pow(pos.x,2) + pow(pos.z,2))) / 5.;
                                                     gl_Position    = ciModelViewProjection * pos;
                                                     TexCoord0      = ciTexCoord0;
                                                 }