void Scene::Render(ci::Surface8u *surface) { Surface8u::Iter iter = surface->getIter(); int width = surface->getWidth(); int height = surface->getHeight(); float aspectRatio = width / (float)height; Color8u bgColor(25, 25, 25); while (iter.line()) { while (iter.pixel()) { ColorA aaColor(0, 0, 0); int count = 0; int numSamples = 4; float deltaX = 1.0f / (width * numSamples); float deltaY = 1.0f / (height * numSamples); for (int i = 0; i < numSamples; i++) { for (int j = 0; j < numSamples; j++) { float inDist = 10000.0f; float u = iter.x()/(float)width + deltaX * i; float v = iter.y()/(float)height + deltaY * j; ci::Ray ray = m_camera->generateRay(u, v, aspectRatio); aaColor += Raytrace(ray, 0, inDist, 1.0f); count++; } } ColorA color = aaColor / (float)count; iter.r() = ci::math<float>::clamp(bgColor.r * (1.0f - color.a) + color.r * 255.0f, 0.0f, 255.0f); iter.g() = ci::math<float>::clamp(bgColor.g * (1.0f - color.a) + color.g * 255.0f, 0.0f, 255.0f); iter.b() = ci::math<float>::clamp(bgColor.b * (1.0f - color.a) + color.b * 255.0f, 0.0f, 255.0f); } //iter.line(); } }
void RayMarcher::renderScanline( int scanline, ci::Surface8u *surface ) { int width = surface->getWidth(); int height = surface->getHeight(); float imageAspect = width / (float)height; Color8u backgroundColor( 0, 95, 249 ); Surface8u::Iter iter = surface->getIter( Area( 0, scanline, surface->getWidth(), scanline + 1 ) ); while( iter.pixel() ) { ColorA color = march( mCamera->generateRay( iter.x() / (float)width, 1.0f - scanline / (float)height, imageAspect ) ); iter.r() = backgroundColor.r * ( 1.0f - color.a ) + color.r * 255; iter.g() = backgroundColor.g * ( 1.0f - color.a ) + color.g * 255; iter.b() = backgroundColor.b * ( 1.0f - color.a ) + color.b * 255; } }
void _TBOX_PREFIX_App::setup() { mCubeRotation.setToIdentity(); // Create a blue-green gradient as an OpenGL texture Surface8u surface( 256, 256, false ); Surface8u::Iter iter = surface.getIter(); while( iter.line() ) { while( iter.pixel() ) { iter.r() = 0; iter.g() = iter.x(); iter.b() = iter.y(); } } mTex = gl::Texture( surface ); }
void RayMarcher::render( ci::Surface8u *surface ) { Surface8u::Iter iter = surface->getIter(); // transform the spheres int width = surface->getWidth(); int height = surface->getHeight(); float imageAspect = width / (float)height; Color8u backgroundColor( 0, 95, 249 ); while( iter.line() ) { while( iter.pixel() ) { ColorA color = march( mCamera->generateRay( iter.x() / (float)width, 1.0f - iter.y() / (float)height, imageAspect ) ); iter.r() = backgroundColor.r * ( 1.0f - color.a ) + color.r * 255; iter.g() = backgroundColor.g * ( 1.0f - color.a ) + color.g * 255; iter.b() = backgroundColor.b * ( 1.0f - color.a ) + color.b * 255; } iter.line(); } }