Ogre3D Compositor Framework. Kezdeti teendők OgrePostProcBase.zip letöltése Útvonalak...
-
Upload
calvin-gatton -
Category
Documents
-
view
215 -
download
1
Transcript of Ogre3D Compositor Framework. Kezdeti teendők OgrePostProcBase.zip letöltése Útvonalak...
Ogre3D Compositor Framework
Kezdeti teendőkOgrePostProcBase.zip letöltéseÚtvonalak beállításaFordítás futtatás
Futtatás
OgrePostProc.cpp…void setupPostProc(){
}
void setupScene(){…}
int _tmain(int argc, _TCHAR* argv[]){…
setupScene();setupListeners();setupPostProc();
…}
\bin\media\monochrome.compositorcompositor Monochrome{ technique { texture scene target_width target_height PF_FLOAT16_RGBA
target scene { input previous
}
target_output { input none pass render_quad { material PostProc/Monochrome input 0 scene } } }}
\bin\media\PostProc.materialvertex_program FSQuadVS hlsl{ source PostProc.hlsl entry_point FS_QUAD_VS target vs_2_0}
fragment_program MonochromePS hlsl{ source PostProc.hlsl entry_point Monochrome_PS target ps_2_0}
\bin\media\PostProc.materialmaterial PostProc/Monochrome{
technique{
pass{
vertex_program_ref FSQuadVS{}fragment_program_ref MonochromePS{
param_named_auto width viewport_width param_named_auto height viewport_height
} texture_unit { filtering none tex_address_mode clamp }
}}
}
\bin\media\PostProc.hlslstruct VS_OUT{
float4 hPosition: POSITION;float2 texCoord : TEXCOORD0;
};
VS_OUT FS_QUAD_VS(float4 position : POSITION,float4 texCoord : TEXCOORD0)
{ VS_OUT OUT; OUT.hPosition = float4(sign(position.xy),0,1); OUT.texCoord = (float2(OUT.hPosition.x, -OUT.hPosition.y) +
1.0) / 2.0; return OUT;}
\bin\media\PostProc.hlslfloat4 Monochrome_PS(VS_OUT IN,
uniform float width,uniform float height,uniform sampler2D Image: register(s0)):COLOR
{ IN.texCoord += float2(0.5/width, 0.5/height); float4 sample = tex2D(Image,IN.texCoord );
// D65 white conversion and weighting float avg = sample.r * 0.21f + sample.g * 0.39f + sample.b *
0.4f; return float4(avg, avg, avg, 1);}
OgrePostProc.cpp
void setupPostProc(){
CompositorManager::getSingleton().addCompositor(renderWindow->getViewport(0),
"Monochrome");CompositorManager::getSingleton().setCompositorEnabled(renderWindow->getViewport(0),
"Monochrome", true);}
Próba
Önálló feladat
Ne szürke legyenLehet sepia pl.vagy zöld (éjszakai kamera)vagy vörös
Állítsuk vissza szürkére
FeladatÚj compositor: motion blur
Csak másoljuk az eredeti képet DEAlhpa blending-el kombinálva az előző képpelFigyelni kell a rendertarget kezdeti tartalmára
Megoldás…
motionblur.compositorcompositor MotionBlur{ technique { texture scene target_width target_height PF_FLOAT16_RGBA
target scene {
input previous }
target_output { input none pass render_quad {
clear { buffers depth }
material PostProc/BlendedCopy input 0 scene } }
}}
PostProc.materialfragment_program CopyPS hlsl{ source PostProc.hlsl entry_point Copy_PS target ps_2_0}
material PostProc/BlendedCopy{
technique{
pass{
vertex_program_ref FSQuadVS{}fragment_program_ref CopyPS{
param_named_auto width viewport_width param_named_auto height viewport_heightparam_named alpha float 20param_named_auto dt frame_time
} scene_blend alpha_blend texture_unit { filtering none tex_address_mode clamp }
}}
}
PostProc.hlslfloat4 Copy_PS(VS_OUT IN,
uniform float width,uniform float height,uniform float alpha,uniform float dt,uniform sampler2D Image: register(s0)):COLOR
{ IN.texCoord += float2(0.5/width, 0.5/height); float4 sample = tex2D(Image, IN.texCoord); return float4(sample.rgb, alpha * dt); }
OgrePostProc.cppvoid setupPostProc(){
renderWindow->getViewport(0)->setClearEveryFrame(true, FBT_DEPTH);Root::getSingleton().getRenderSystem()->clearFrameBuffer( FBT_COLOUR, ColourValue::Black);
CompositorManager::getSingleton().addCompositor(renderWindow->getViewport(0),
"Monochrome");CompositorManager::getSingleton().setCompositorEnabled(renderWindow->getViewport(0),
"Monochrome", true);
CompositorManager::getSingleton().addCompositor(renderWindow->getViewport(0),
"MotionBlur");CompositorManager::getSingleton().setCompositorEnabled(renderWindow->getViewport(0),
"MotionBlur", true);}
Glow
glow.compositorcompositor Glow{ technique { texture scene target_width target_height PF_FLOAT16_RGBA
texture cut 256 256 PF_FLOAT16_RGBA texture rt0 256 256 PF_FLOAT16_RGBA texture rt1 256 256 PF_FLOAT16_RGBA
target rt1 {
input none only_initial on pass clear {
} } target scene { input previous }
glow.compositor folyt.target cut
{ input none pass render_quad { material PostProc/GlowCut input 0 scene input 1 rt1 } } target rt0 { input none pass render_quad { material PostProc/BlurV input 0 cut } }
glow.compositor vége target rt1
{ input none pass render_quad { material PostProc/BlurH input 0 rt0 }
} target_output
{ input none pass render_quad { material PostProc/Add input 0 scene input 1 rt1 } } }}
PostProc.materialmaterial PostProc/Copy{
technique{
pass{
vertex_program_ref FSQuadVS{}fragment_program_ref CopyPS{
param_named_auto width viewport_width param_named_auto height viewport_heightparam_named alpha float 1param_named dt float 1
} texture_unit { //filtering none tex_address_mode clamp }
}}
}
PostProc.materialfragment_program GlowCutPS hlsl{ source PostProc.hlsl entry_point GlowCut_PS target ps_2_0}
material PostProc/GlowCut{ technique { pass {
vertex_program_ref FSQuadVS { }
fragment_program_ref GlowCutPS {
param_named_auto width viewport_width param_named_auto height viewport_height
param_named cutValue float 3param_named timeBlur float 0.8
} texture_unit
{tex_address_mode clamp
}texture_unit{
tex_address_mode clamp}
} }}
PostProc.materialfragment_program AddPS hlsl{ source PostProc.hlsl entry_point Add_PS target ps_2_0}
material PostProc/Add{
technique{
pass{
vertex_program_ref FSQuadVS{}fragment_program_ref AddPS{
param_named_auto width viewport_width param_named_auto height viewport_height
} texture_unit { tex_address_mode clamp } texture_unit { tex_address_mode clamp }
}}
}
PostProc.materialfragment_program BlurVPS hlsl{ source PostProc.hlsl entry_point BlurV_PS target ps_2_0}material PostProc/BlurV{
technique{
pass{
vertex_program_ref FSQuadVS{}fragment_program_ref BlurVPS{
param_named_auto width viewport_width param_named_auto height viewport_heightparam_named stretch float 1.2
} texture_unit { tex_address_mode clamp }
}}
}
PostProc.materialfragment_program BlurHPS hlsl{ source PostProc.hlsl entry_point BlurH_PS target ps_2_0}material PostProc/BlurH{
technique{
pass{
vertex_program_ref FSQuadVS{}fragment_program_ref BlurHPS{
param_named_auto width viewport_width param_named_auto height viewport_heightparam_named stretch float 1.2
} texture_unit { tex_address_mode clamp }
}}
}
PostProc.hlslfloat4 Add_PS(VS_OUT IN,
uniform float width,uniform float height,uniform sampler2D Image1: register(s0),uniform sampler2D Image2: register(s1)):COLOR
{ IN.texCoord += float2(0.5/width, 0.5/height); float4 sample1 = tex2D(Image1, IN.texCoord); float4 sample2 = tex2D(Image2, IN.texCoord); return sample1 + sample2;}
PostProc.hlslfloat4 GlowCut_PS(VS_OUT IN,
uniform float width,uniform float height,uniform float timeBlur,uniform float cutValue,uniform sampler2D Image: register(s0),uniform sampler2D prevImage: register(s1)):COLOR
{ IN.texCoord += float2(0.5/width, 0.5/height); float4 sample = tex2D(Image, IN.texCoord); float lum = dot(sample.rgb, float3(0.21f, 0.39f, 0.4f)); if (lum < cutValue) sample = float4(0,0,0,1); float4 sample2 = tex2D(prevImage, IN.texCoord); sample = lerp(sample2, sample, timeBlur); return sample;}
PostProc.hlslfloat4 BlurV_PS(VS_OUT IN,
uniform float width,uniform float height,uniform float stretch,uniform sampler2D Image: register(s0)):COLOR
{
float2 tex0 = IN.texCoord + float2(0.5/width, 0.5/height); float4 texLookUp_v = tex2D(Image, float2(tex0.x, tex0.y-(3.86979f*stretch/height)))+
tex2D(Image, float2(tex0.x, tex0.y-(1.72291f*stretch/height)))+
tex2D(Image, tex0)+ tex2D(Image, float2(tex0.x,
tex0.y+(1.72291f*stretch/height)))+ tex2D(Image, float2(tex0.x,
tex0.y+(3.86979f*stretch/height)));
return texLookUp_v / 5.0;}
PostProc.hlslfloat4 BlurH_PS(VS_OUT IN,
uniform float width,uniform float height,uniform float stretch,uniform sampler2D Image: register(s0)) : COLOR
{ float2 tex0 = IN.texCoord + float2(0.5/width, 0.5/height); float4 texLookUp_h =
tex2D(Image, float2(tex0.x-(3.86979f*stretch/width), tex0.y)) +
tex2D(Image, float2(tex0.x-(1.72291f*stretch/width ), tex0.y)) +
tex2D(Image, tex0) + tex2D(Image, float2(tex0.x+(1.72291f*stretch/width),
tex0.y)) + tex2D(Image, float2(tex0.x+(3.86979f*stretch/width),
tex0.y));
return texLookUp_h / 5.0;}
OgrePostProc.cppvoid setupPostProc(){
renderWindow->getViewport(0)->setClearEveryFrame(true, FBT_DEPTH);Root::getSingleton().getRenderSystem()->clearFrameBuffer( FBT_COLOUR, ColourValue::Black);
CompositorManager::getSingleton().addCompositor(renderWindow->getViewport(0), "Glow");
CompositorManager::getSingleton().setCompositorEnabled(renderWindow->getViewport(0), "Glow",
true);…}
Próba
Tone Mapping
Tonemap.compositorcompositor ToneMap{ technique { texture scene target_width target_height PF_FLOAT16_RGBA
texture luminance target_width target_height PF_FLOAT16_RGBA texture rt0 512 512 PF_FLOAT16_RGBA texture rt1 128 128 PF_FLOAT16_RGBA texture rt2 32 32 PF_FLOAT16_RGBA texture rt3 8 8 PF_FLOAT16_RGBA texture rt4 2 2 PF_FLOAT16_RGBA texture globalLum 2 2 PF_FLOAT16_RGBA target scene { input previous
}
Tonemap.compositor folytatás..
target luminance
{
input none
pass render_quad
{
material PostProc/Monochrome
input 0 scene
} }
target rt0
{
input none pass render_quad
{
material PostProc/BlurV
input 0 luminance }
}
Tonemap.compositor folytatás..
target rt1
{
input none
pass render_quad
{
material PostProc/BlurH
input 0 rt0
}}
target rt2
{
input none pass render_quad
{
material PostProc/BlurV
input 0 rt1 }
}
Tonemap.compositor folytatás..
target rt3
{
input none
pass render_quad
{
material PostProc/BlurH
input 0 rt2
}}
target rt4
{
input none pass render_quad
{
material PostProc/BlurV
input 0 rt3 }
}
Tonemap.compositor folytatás..
target globalLum{
input noneonly_initial onpass clear{}
}target globalLum {
input none
pass render_quad {
clear {
buffers depth } material PostProc/BlendedCopy input 0 rt4
} }
Tonemap.compositor vége target_output {
input none pass render_quad {
material PostProc/ToneMap input 0 scene input 1 luminance input 2 globalLum
}}
} }
PostProc.materialfragment_program ToneMapPS hlsl{ source PostProc.hlsl entry_point ToneMap_PS target ps_2_0}material PostProc/ToneMap{ technique { pass {
vertex_program_ref FSQuadVS{}fragment_program_ref ToneMapPS{
param_named_auto width viewport_width param_named_auto height viewport_heightparam_named alpha float 0.4param_named wLum2 float 0.7
} texture_unit{}texture_unit{}texture_unit{}
} }}
PostProc.hlslfloat4 ToneMap_PS(VS_OUT IN,
uniform float width,uniform float height,uniform float alpha,uniform float wLum2,uniform sampler2D Image: register(s0),uniform sampler2D Luminance: register(s1),uniform sampler2D GlobalLuminance: register(s2) ):COLOR
{IN.texCoord += float2(0.5/width, 0.5/height);float lum = tex2D(Luminance, IN.texCoord).r; // Luminance of pixelfloat gLum = tex2D(GlobalLuminance, float2(0.5,0.5)).r;// Global luminancefloat rLum = alpha * lum / gLum; // Relative luminancefloat D = rLum * (1 + rLum / wLum2) / (1 + rLum);float4 color = tex2D(Image, IN.texCoord);color.rgb *= D / lum; // tone mappingreturn color;
}
OgrePostProc.cppvoid setupPostProc(){
renderWindow->getViewport(0)->setClearEveryFrame(true, FBT_DEPTH);Root::getSingleton().getRenderSystem()->clearFrameBuffer( FBT_COLOUR, ColourValue::Black);
CompositorManager::getSingleton().addCompositor(renderWindow->getViewport(0),"Glow");
CompositorManager::getSingleton().setCompositorEnabled(renderWindow->getViewport(0), "Glow", true);
CompositorManager::getSingleton().addCompositor(renderWindow->getViewport(0),"ToneMap");
CompositorManager::getSingleton().setCompositorEnabled(renderWindow->getViewport(0), "ToneMap", true);
}
Próba
Depth of field
OgrePostProc.cppvoid setupScene(){
…Texture* sceneDepthMap =
TextureManager::getSingleton().createManual("sceneDepthMap", "default„,TEX_TYPE_2D,viewport-
>getActualWidth(),viewport->getActualHeight(),0,0,PF_FLOAT16_RGBA,
TU_RENDERTARGET).getPointer();
HardwarePixelBuffer* hpb = (sceneDepthMap->getBuffer(0, 0)).getPointer();RenderTarget* rt = hpb->getRenderTarget(0);Viewport* v = rt->addViewport(camera);v->setOverlaysEnabled(false);v->setMaterialScheme("SceneInfo");
SceneNode* levelNode = rootNode->createChildSceneNode();
level1.material
vertex_program SceneInfoVS hlsl{ source SceneInfo.hlsl entry_point vs target vs_2_0}fragment_program SceneInfoPS hlsl{ source SceneInfo.hlsl entry_point ps target ps_2_0}
level1.materialmaterial level1/base{
technique sceneinfo{
scheme SceneInfopass{
vertex_program_ref SceneInfoVS {
param_named_auto ModelViewProj worldviewproj_matrix
param_named_auto Model world_matrix }fragment_program_ref SceneInfoPS{}
}}
SceneInfo.hlsl
struct VERTEX_IN{ float4 position :POSITION;};struct VERTEX_OUT{ float4 position : POSITION; float3 wPos : TEXCOORD0; float4 hPos : TEXCOORD2;};VERTEX_OUT vs(VERTEX_IN IN,
uniform float4x4 ModelViewProj, uniform float4x4 Model)
{ VERTEX_OUT OUT = (VERTEX_OUT) 0; OUT.position = OUT.hPos = mul(ModelViewProj, IN.position); OUT.wPos = mul(Model, IN.position).xyz; return OUT;}float4 ps(VERTEX_OUT IN):COLOR0{ float hZ = IN.hPos.z / IN.hPos.w; return float4(hZ, IN.wPos);}
dof.compositorcompositor DepthOfField{ technique { texture scene target_width target_height PF_FLOAT16_RGBA
target scene{
input previous }
target_output { input none pass render_quad { material PostProc/DoF input 0 scene } } }}
fragment_program DoFPS hlsl{ source PostProc.hlsl entry_point DoF_PS target ps_2_0}material PostProc/DoF{ technique { pass {
vertex_program_ref FSQuadVS{}fragment_program_ref DoFPS{
param_named_auto width viewport_width
param_named_auto height viewport_height
param_named scale float 80} texture_unit{}texture_unit{
texture sceneDepthMap}
} }}
PostProc.material
PostProc.hlslstatic const float2 filterTaps[] ={
float2(-0.326212f, -0.405805f),float2(-0.840144f, -0.07358f),float2(-0.695914f, 0.457137f),float2(-0.203345f, 0.620716f),float2(0.96234f, -0.194983f),float2(0.473434f, -0.480026f),float2(0.519456f, 0.767022f),float2(0.185461f, -0.893124f),float2(0.507431f, 0.064425f),float2(0.89642f, 0.412458f),float2(-0.32194f, -0.932615f),float2(-0.791559f, -0.597705f)
};
float4 DoF_PS(VS_OUT IN,uniform float width,uniform float height,uniform float scale,uniform sampler2D Image: register(s0),uniform sampler2D SceneInfo: register(s1) ):COLOR
{IN.texCoord += float2(0.5/width, 0.5/height);float dx = 1.0f / width;
float dy = 1.0f / height;
// Depth based blurfloat4 colorSum = tex2D(Image, IN.texCoord); // Center samplefloat depth = tex2D(SceneInfo, IN.texCoord).r; // Center samplefloat focalDist = tex2D(SceneInfo, float2(0.5,0.5)).r; float sizeCoC = abs(depth - focalDist) * scale;
for (int i = 0; i < 12; i++) // Filter{
float2 tapCoord = IN.texCoord + filterTaps[i] * float2(dx,dy) * sizeCoC; //Tap coords colorSum += tex2D(Image, tapCoord);
}return colorSum / 12; // Normalize
}
PostProc.hlsl
OgrePostProc.cpp
void setupPostProc(){
renderWindow->getViewport(0)->setClearEveryFrame(true, FBT_DEPTH);Root::getSingleton().getRenderSystem()->clearFrameBuffer( FBT_COLOUR, ColourValue::Black);
CompositorManager::getSingleton().addCompositor(renderWindow->getViewport(0),"DepthOfField");
CompositorManager::getSingleton().setCompositorEnabled(renderWindow->getViewport(0), "DepthOfField", true);
Próba
Light shafts
Lightshaft.compositorcompositor LightShaft{ technique { texture scene target_width target_height PF_FLOAT16_RGBA
target scene { input previous } target_output { input none pass render_quad {
identifier 1 material PostProc/LightShaft input 0 scene } } }}
fragment_program LightShaftPS hlsl{ source PostProc.hlsl entry_point LightShaft_PS target ps_3_0}material PostProc/LightShaft{ technique { pass {
vertex_program_ref FSQuadVS{
param_named_auto InvViewProj inverse_viewproj_matrix}fragment_program_ref LightShaftPS{
param_named_auto width viewport_width param_named_auto height viewport_height
} texture_unit{}texture_unit{
texture sceneDepthMap}texture_unit{
texture shadowCubeMap}
} }}
PostProc.material
float4 LightShaft_PS(VS_OUT IN,uniform float width,uniform float height,uniform float4 camPos,uniform float3 lightPos,uniform float lightRange,uniform sampler2D Image: register(s0),uniform sampler2D SceneInfo: register(s1),uniform samplerCUBE shadowCubeMap: register(s2) ):COLOR
{static const float samplecount = 200;static const float density = 0.02;
static const float albedo = 0.5; static const float shadowBias = 0.001; IN.texCoord += float2(0.5/width, 0.5/height);
…
PostProc.hlsl
float3 wPos = tex2D(SceneInfo, IN.texCoord).yzw;float3 dir = camPos.xyz - wPos;float dist = length(dir);float dt = 1.0 / samplecount * dist;dir *= dt / dist;float3 cubePos = wPos - lightPos;
float I = 1000;float4 L = 0;//tex2D(Image, IN.texCoord);for(int i = 0; i < samplecount; i++)
{ cubePos += dir; float d = length(cubePos) / lightRange; float dstored = texCUBE(shadowCubeMap,float3(cubePos.xy, -
cubePos.z)).r; float shadow = (float)(d - shadowBias < dstored); L = L * (exp(-dt*density))
+ I * shadow * exp(-d * density) / (12.56) * albedo * (1.0 - exp(-dt*density));
}return tex2D(Image, IN.texCoord) + L;
}
PostProc.hlsl
OgrePostProc.cppclass LSCompListener : public CompositorInstance::Listener{
void notifyMaterialRender(uint32 pass_id, MaterialPtr &mat){
if(pass_id == 1){
GpuProgramParametersSharedPtr fparams = mat->getTechnique(0)->getPass(0)-
>getFragmentProgramParameters();fparams->setNamedConstant("lightPos", Vector3(0,20,-200));fparams->setNamedConstant("lightRange", 600.0f);fparams->setNamedConstant("camPos", camera->getPosition());
}}
};
OgrePostProc.cppvoid setupPostProc(){
renderWindow->getViewport(0)->setClearEveryFrame(true, FBT_DEPTH);Root::getSingleton().getRenderSystem()->clearFrameBuffer( FBT_COLOUR, ColourValue::Black);
CompositorInstance* lightShaftComp = CompositorManager::getSingleton().addCompositor(renderWindow->getViewport(0),
"LightShaft");CompositorManager::getSingleton().setCompositorEnabled(renderWindow->getViewport(0), "LightShaft", true);lightShaftComp->addListener(new LSCompListener());
CompositorManager::getSingleton().addCompositor(renderWindow->getViewport(0),"DepthOfField");
…
Próba