void ImageBasedLensFlare::Render(const Ptr<RenderView> & view) { auto sceneTex = view->GetViewRenderContext()->GetSharedTexture("RenderResult"); auto brightPassTexRef = BrightPass(sceneTex); auto setupTexRef = Setup(brightPassTexRef->Get()->Cast<Texture>()); LensBlur(setupTexRef->Get()->Cast<Texture>(), sceneTex); }
//-------------------------------------------------------------------------------------- // Name: CreateBlurredScene() // Desc: //-------------------------------------------------------------------------------------- VOID CSample34::CreateBlurredImage() { // Part 1: downsize the framebuffer to a smaller render target { m_QuarterRT.SetFramebuffer(); // Render a full-screen quad glClearColor( 0.04f, 0.04f, 0.04f, 1.0f ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glDisable( GL_CULL_FACE ); glDisable( GL_BLEND ); glDisable( GL_DEPTH_TEST ); glUseProgram( m_DownsizeShader.ShaderId ); SetTexture( m_DownsizeShader.ColorTextureId, m_SharpRT.TextureHandle, 0 ); glUniform2f( m_DownsizeShader.StepSizeId, (1.0f / m_SharpRT.Width), (1.0f / m_SharpRT.Height)); RenderScreenAlignedQuad(); m_QuarterRT.DetachFramebuffer(); } // Part 2: blur GaussBlur( m_QuarterRT, m_BlurredRT ); // generate 64x64 luminance render target Generate64x64Lum(m_BlurredRT); // generate 16x16 luminance render target Generate16x16Lum(m_64x64RT); // generate 4x4 luminance render target Generate4x4Lum(m_16x16RT); // generate 1x1 luminance render target Generate1x1Lum(m_4x4RT); // adapt luminance GenerateAdaptLum(m_1x1RT, m_PreviousAverageLumRT); // copy the result of this operation into another 1x1 render target to store it for the next frame Copy1x1Lum(m_PreviousAverageLumRT, m_LastAverageLumRT); // bright pass filter BrightPass( m_BrightPassRT, m_QuarterRT, m_LastAverageLumRT); // bloom GaussBlur( m_BrightPassRT, m_QuarterRT); }
CompositorPtr HDRCompositor::Create(void) { m_Compositor = CompositorManager::getSingleton().create("HDR", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); m_HDRTechnique = m_Compositor->createTechnique(); m_HDRTechnique->setCompositorLogicName("HDR"); CompositionTechnique::TextureDefinition *texdef = m_HDRTechnique->createTextureDefinition("Scene"); texdef->refTexName = "mrt_output"; texdef->refCompName="gbuffer"; // texdef->scope=Ogre::CompositionTechnique::TextureScope::TS_GLOBAL; //render scene // CreateTextureDef("Scene",0,0,PF_FLOAT16_RGB); // CompositionTargetPass *tp = m_HDRTechnique->createTargetPass(); // tp->setInputMode(CompositionTargetPass::IM_PREVIOUS); // tp->setOutputName("Scene"); RenderDownSample(); CalculateLuminance(); CalculateKey(); if(m_GlareType || m_StarType) { BrightPass(); if(m_GlareType) BuildGlare(); if(m_StarType) BuildStar(); } if(m_ToneMapper == TM_REINHARDLOCAL) BuildScales(); FinalRendering(); return m_Compositor; }
void ToneMapping::Render(const Ptr<RenderView> & view) { auto rc = Global::GetRenderEngine()->GetRenderContext(); auto sceneTex = view->GetViewRenderContext()->GetSharedTexture("RenderResult"); auto sceneDownSampleTex = view->GetViewRenderContext()->GetSharedTexture("HalfScene"); auto adaptedExposureScale = view->GetViewRenderContext()->GetSharedTexture("AdaptedExposureScale"); // Bright pass auto brightPassTexRef = BrightPass(sceneDownSampleTex, adaptedExposureScale); auto brightPassTex = brightPassTexRef->Get()->Cast<Texture>(); static const int32_t numBlurTex = 6; // Bloom down sample std::vector<PooledRenderResourceReference<Texture>> bloomDownSampleTexList; bloomDownSampleTexList.push_back(brightPassTexRef); for (int32_t i = 1; i < numBlurTex; ++i) { bloomDownSampleTexList.push_back(BloomDownSample(bloomDownSampleTexList.back()->Get()->Cast<Texture>())); } // Bloom blur float radius[numBlurTex] = { 16.0f, 16.0f, 16.0f, 16.0f, 16.0f, 16.0f }; for (int i = 0; i < numBlurTex; ++i) { /*Blur::GaussBlur( bloomDownSampleTexList[i]->Get()->Cast<Texture>()->GetShaderResourceView(), bloomDownSampleTexList[i]->Get()->Cast<Texture>()->GetRenderTargetView(0, 0, 1), std::min<int32_t>(31, (int32_t)radius[i] * 2 + 1), radius[i]);*/ BloomBlur(bloomDownSampleTexList[i]->Get()->Cast<Texture>(), radius[i]); } // Bloom up sample for (int i = numBlurTex - 2; i >= 0; --i) { BloomUpSample( 1.0f / (float)numBlurTex, bloomDownSampleTexList[i + 1]->Get()->Cast<Texture>(), bloomDownSampleTexList[i]->Get()->Cast<Texture>()); } /*auto bloomFinalDesc = bloomDownSampleTexList[0]->Get()->Cast<Texture>()->GetDesc(); auto bloomFinalRef = TexturePool::Instance().FindFree({ TEXTURE_2D, bloomFinalDesc }); auto bloomFinal = bloomFinalRef->Get()->Cast<Texture>(); BloomUpSample( 1.0f / (float)numBlurTex, bloomDownSampleTexList[0]->Get()->Cast<Texture>(), bloomFinal);*/ //// Streak //auto streakTexDesc = brightPassTexRef->Get()->Cast<Texture>()->GetDesc(); //auto streakTexRef = TexturePool::Instance().FindFree({ TEXTURE_2D, streakTexDesc }); //auto streakTexTmpRef = TexturePool::Instance().FindFree({ TEXTURE_2D, streakTexDesc }); //Blur::BoxBlurX( // brightPassTexRef->Get()->Cast<Texture>()->GetShaderResourceView(), // streakTexRef->Get()->Cast<Texture>()->GetRenderTargetView(0, 0, 1), // 5, 3.0f); //for (int i = 0; i < 5; ++i) //{ // Blur::BoxBlurX( // streakTexRef->Get()->Cast<Texture>()->GetShaderResourceView(), // streakTexTmpRef->Get()->Cast<Texture>()->GetRenderTargetView(0, 0, 1), // 5, 3.0f); // streakTexRef.swap(streakTexTmpRef); //} auto lutRef = ComputeToneMappingLUT(); auto newTargetDesc = sceneTex->GetDesc(); auto newTargetRef = TexturePool::Instance().FindFree({ TEXTURE_2D, newTargetDesc }); DoToneMapping( sceneTex, bloomDownSampleTexList[0]->Get()->Cast<Texture>(), adaptedExposureScale, lutRef->Get()->Cast<Texture>(), newTargetRef->Get()->Cast<Texture>()->GetRenderTargetView(0, 0, 1)); view->GetViewRenderContext()->SetSharedResource("RenderResult", newTargetRef); }
//************************************************************************************************************* void Render(float alpha, float elapsedtime) { LPDIRECT3DSURFACE9 oldtarget = NULL; D3DXMATRIX vp, inv, tmp1, tmp2; D3DXVECTOR3 axis(0, 1, 0); D3DXVECTOR3 eye(0, 0, -5); D3DXVECTOR3 look(0, 0, 0); D3DXVECTOR3 up(0, 1, 0); D3DXVECTOR2 cangle = cameraangle.smooth(alpha); D3DXVECTOR2 oangle = objectangle.smooth(alpha); float expo = exposure.smooth(alpha); D3DXMatrixRotationYawPitchRoll(&world, cangle.x, cangle.y, 0); D3DXVec3TransformCoord(&eye, &eye, &world); D3DXMatrixLookAtLH(&view, &eye, &look, &up); D3DXMatrixMultiply(&vp, &view, &proj); D3DXMatrixInverse(&inv, NULL, &view); memcpy(&eye, inv.m[3], 3 * sizeof(float)); if( mesh == mesh1 ) { // skullocc D3DXMatrixScaling(&world, 0.4f, 0.4f, 0.4f); world._42 = -1.5f; } else if( mesh == mesh2 ) { // knot D3DXMatrixScaling(&world, 0.8f, 0.8f, 0.8f); } else { // teapot D3DXMatrixScaling(&world, 1.5f, 1.5f, 1.5f); } D3DXMatrixRotationYawPitchRoll(&tmp1, oangle.x, oangle.y, 0); D3DXMatrixMultiply(&world, &world, &tmp1); D3DXMatrixInverse(&inv, NULL, &world); fresnel->SetVector("eyePos", (D3DXVECTOR4*)&eye); fresnel->SetMatrix("matWorld", &world); fresnel->SetMatrix("matWorldInv", &inv); fresnel->SetMatrix("matViewProj", &vp); D3DXMatrixScaling(&world, 20, 20, 20); skyeffect->SetMatrix("matWorld", &world); D3DXMatrixIdentity(&world); skyeffect->SetMatrix("matWorldSky", &world); skyeffect->SetMatrix("matViewProj", &vp); memcpy(tmpvert, quadvertices, 36 * sizeof(float)); if( SUCCEEDED(device->BeginScene()) ) { device->SetRenderState(D3DRS_SRGBWRITEENABLE, false); device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); // STEP 1: render sky device->GetRenderTarget(0, &oldtarget); if( firstframe ) { device->SetRenderTarget(0, aftersurfaces[0]); device->Clear(0, NULL, D3DCLEAR_TARGET, 0, 1.0f, 0); device->SetRenderTarget(0, aftersurfaces[1]); device->Clear(0, NULL, D3DCLEAR_TARGET, 0, 1.0f, 0); device->SetRenderTarget(0, avglumsurfaces[4]); device->Clear(0, NULL, D3DCLEAR_TARGET, 0x11111111, 1.0f, 0); device->SetRenderTarget(0, avglumsurfaces[5]); device->Clear(0, NULL, D3DCLEAR_TARGET, 0x11111111, 1.0f, 0); firstframe = false; } device->SetRenderTarget(0, scenesurface); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff6694ed, 1.0f, 0); device->SetRenderState(D3DRS_ZENABLE, FALSE); device->SetTexture(0, skytexture); skyeffect->Begin(NULL, 0); skyeffect->BeginPass(0); { skymesh->DrawSubset(0); } skyeffect->EndPass(); skyeffect->End(); device->SetRenderState(D3DRS_ZENABLE, TRUE); // STEP 2: render object device->SetTexture(0, texture); device->SetTexture(1, fresneltexture); device->SetTexture(2, skytexture); device->SetTexture(3, roughspecular); fresnel->Begin(NULL, 0); fresnel->BeginPass(0); { mesh->DrawSubset(0); } fresnel->EndPass(); fresnel->End(); device->SetVertexDeclaration(vertexdecl); // STEP 3: measure average luminance MeasureLuminance(); // STEP 4: adapt luminance to eye AdaptLuminance(elapsedtime); // STEP 5: bright pass BrightPass(); // STEP 6: downsample bright pass texture DownSample(); // STEP 7: blur downsampled textures Blur(); // STEP 8: ghost LensFlare(); // STEP 9: star Star(); // STEP 10: final combine hdreffect->SetTechnique("final"); hdreffect->SetFloat("targetluminance", targetluminance); device->SetRenderTarget(0, oldtarget); device->SetTexture(0, scenetarget); // scene device->SetTexture(1, blurtargets[0]); // blur device->SetTexture(2, blurtargets[1]); // star device->SetTexture(3, ghosttargets[0]); // ghost device->SetTexture(4, afterimages[1 - afterimagetex]); device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); device->SetRenderState(D3DRS_SRGBWRITEENABLE, true); oldtarget->Release(); hdreffect->Begin(NULL, 0); hdreffect->BeginPass(0); { device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, quadvertices, sizeof(D3DXVECTOR4) + sizeof(D3DXVECTOR2)); } hdreffect->EndPass(); hdreffect->End(); if( drawhelp ) { // render text device->SetFVF(D3DFVF_XYZRHW|D3DFVF_TEX1); device->SetRenderState(D3DRS_ZENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); device->SetTexture(0, text); device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, textvertices, 6 * sizeof(float)); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_ZENABLE, TRUE); } // clean up device->SetTexture(1, NULL); device->SetTexture(2, NULL); device->SetTexture(3, NULL); device->SetTexture(4, NULL); device->SetTexture(5, NULL); device->EndScene(); } device->Present(NULL, NULL, NULL, NULL); }