Some code from my unfinished def renderer (so you get the idea).
It creates 3 RT and renders all of them at once.
init:
Code: Select all
irr::core::array<irr::video::IRenderTarget> multiRenderTarget;
colorRT = driver->addRenderTargetTexture(core::dimension2d<u32>(w,h), "colorMap", ECF_A8R8G8B8);
normalRT = driver->addRenderTargetTexture(core::dimension2d<u32>(w,h), "normalMap", ECF_A8R8G8B8);
depthRT = driver->addRenderTargetTexture(core::dimension2d<u32>(w,h), "depthMap", ECF_R32F); // ECF_R16F
multiRenderTarget.push_back(colorRT);
multiRenderTarget.push_back(normalRT);
multiRenderTarget.push_back(depthRT);
createGBufferEffect = world.loadShader("RenderGBuffer.cg", &deferredRendererShaderCallBack, 1);
render:
Code: Select all
world.useShader(createGBufferEffect); // bind shader (
driver->setRenderTarget(multiRenderTarget, true, true, video::SColor(0,0,0,0));
sceneManager->drawAll();
driver->setRenderTarget(0, true, true, 0);
RenderGBuffer.cg: (not made by me)
Code: Select all
float4x4 World;
float4x4 View;
float4x4 Projection;
float specularIntensity;
float specularPower;
sampler TextureMap;
sampler SpecularMap;
sampler NormalMap;
struct VertexShaderInput
{
float4 Position : POSITION0;
float4 Normal : NORMAL0;
float2 TexCoord : TEXCOORD0;
float4 Binormal : BINORMAL0;
float4 Tangent : TANGENT0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
float2 Depth : TEXCOORD1;
float3x3 tangentToWorld : TEXCOORD2;
};
VertexShaderOutput main_v(VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul(float4(input.Position.xyz,1), World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
output.TexCoord = input.TexCoord;
output.Depth.x = output.Position.z;
output.Depth.y = output.Position.w;
// calculate tangent space to world space matrix using the world space tangent,
// binormal, and normal as basis vectors
output.tangentToWorld[0] = mul(input.Tangent, World);
output.tangentToWorld[1] = mul(input.Binormal, World);
output.tangentToWorld[2] = mul(input.Normal, World);
return output;
}
struct PixelShaderOutput
{
half4 Color : COLOR0;
half4 Normal : COLOR1;
half4 Depth : COLOR2;
};
PixelShaderOutput main_f(VertexShaderOutput input)
{
PixelShaderOutput output;
output.Color = tex2D(TextureMap, input.TexCoord.xy);
// float4 specularAttributes = tex2D(SpecularMap, input.TexCoord.xy);
//specular Intensity
// output.Color.a = specularAttributes.r;
// read the normal from the normal map
float3 normalFromMap = float3(0,1,0); //tex2D(NormalMap, input.TexCoord.xy);
//tranform to [-1,1]
normalFromMap = 2.0f * normalFromMap - 1.0f;
//transform into world space
normalFromMap = mul(normalFromMap, input.tangentToWorld);
//normalize the result
normalFromMap = normalize(normalFromMap);
//output the normal, in [0,1] space
output.Normal.rgb = 0.5f * (normalFromMap + 1.0f);
//specular Power
// output.Normal.a = specularAttributes.a;
output.Depth = input.Depth.x / input.Depth.y;
return output;
}