Jump to content

  • Log In with Google      Sign In   
  • Create Account


CursedTyrant

Member Since 29 Jun 2006
Offline Last Active Oct 24 2012 03:06 PM
-----

Posts I've Made

In Topic: Deferred Rendering

23 February 2012 - 04:00 PM

DirectionalLight.fx


float3 lightDirection;

float3 Color;

float3 cameraPosition;

float4x4 InvertViewProjection;

float2 halfPixel;



texture colorMap;

texture normalMap;

texture depthMap;



sampler colorSampler = sampler_state

{

    Texture = (colorMap);

    AddressU = WRAP;

    AddressV = WRAP;

    MagFilter = LINEAR;

    MinFilter = LINEAR;

    MipFilter = LINEAR;

};



sampler depthSampler = sampler_state

{

    Texture = (depthMap);

    AddressU = WRAP;

    AddressV = WRAP;

    MagFilter = LINEAR;

    MinFilter = LINEAR;

    MipFilter = LINEAR;

};



sampler normalSampler = sampler_state

{

    Texture = (normalMap);

    AddressU = WRAP;

    AddressV = WRAP;

    MagFilter = LINEAR;

    MinFilter = LINEAR;

    MipFilter = LINEAR;

};



struct VertexShaderInput

{

    float3 Position : POSITION0;

    float2 TexCoord : TEXCOORD0;

};



struct VertexShaderOutput

{

    float4 Position : POSITION0;

    float2 TexCoord : TEXCOORD0;

};



VertexShaderOutput VertexShaderFunction(VertexShaderInput input)

{

    VertexShaderOutput output;



    output.Position = float4(input.Position, 1);

    output.TexCoord = input.TexCoord - halfPixel;



    return output;

}



float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0

{

    // Get normal data from the normalMap

    float4 normalData = tex2D(normalSampler, input.TexCoord);



    // Transform normal back to [-1, 1] range

    float3 normal = 2.0f * normalData.xyz - 1.0f;

    

    // Get specular power and transform it into [0, 255] range

    float specularPower = normalData.a * 255;

    

    // Get specular intensity from the colorMap

    float specularIntensity = tex2D(colorSampler, input.TexCoord).a;



    // Read depth

    float depthVal = tex2D(depthSampler, input.TexCoord).r;



    float4 position;

    position.x = input.TexCoord.x * 2.0f - 1.0f;

    position.y = -(input.TexCoord.y * 2.0f - 1.0f);

    position.z = depthVal;

    position.w = 1.0f;



    // Transform to world space

    position = mul(position, InvertViewProjection);

    position /= position.w;



    // Surface-to-light vector

    float3 lightVector = -normalize(lightDirection);



    // Compute diffuse light

    float NdL = max(0, dot(normal, lightVector));

    float3 diffuseLight = NdL * Color.rgb;



    // Reflection vector

    float3 reflectionVector = normalize(reflect(lightVector, normal));



    // Camera-to-surface vector

    float3 directionToCamera = normalize(cameraPosition - position);



    // Compute specular light

    float specularLight = specularIntensity * pow(saturate(dot(

        reflectionVector, directionToCamera)), specularPower);



    float4 outColor = float4(diffuseLight, specularLight);



    return saturate(outColor);

}



technique Technique1

{

    pass Pass1

    {

	    VertexShader = compile vs_3_0 VertexShaderFunction();

	    PixelShader = compile ps_3_0 PixelShaderFunction();

    }

}



CombineFinal.fx


texture colorMap;

texture lightMap;

texture depthMap;



float2 halfPixel;

float3 cameraPosition;

float4x4 InvertViewProjection;



float4 FogColor = float4(0.0f, 0.05f, 0.1f, 0.0f);



sampler colorSampler = sampler_state

{

    Texture = (colorMap);

    AddressU = WRAP;

    AddressV = WRAP;

    MagFilter = LINEAR;

    MinFilter = LINEAR;

    Mipfilter = LINEAR;

};



sampler lightSampler = sampler_state

{

    Texture = (lightMap);

    AddressU = WRAP;

    AddressV = WRAP;

    MagFilter = LINEAR;

    MinFilter = LINEAR;

    Mipfilter = LINEAR;

};



sampler depthSampler = sampler_state

{

    Texture = (depthMap);

    AddressU = WRAP;

    AddressV = WRAP;

    MagFilter = LINEAR;

    MinFilter = LINEAR;

    Mipfilter = LINEAR;

};



struct VertexShaderInput

{

    float3 Position : POSITION0;

    float2 TexCoord : TEXCOORD0;

};



struct VertexShaderOutput

{

    float4 Position : POSITION0;

    float2 TexCoord : TEXCOORD0;

};



VertexShaderOutput VertexShaderFunction(VertexShaderInput input)

{

    VertexShaderOutput output;



    output.Position = float4(input.Position, 1);

    output.TexCoord = input.TexCoord - halfPixel;



    return output;

}



float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0

{

    float3 diffuseColor = tex2D(colorSampler, input.TexCoord).rgb;

    float4 light = tex2D(lightSampler, input.TexCoord);

    float3 diffuseLight = light.rgb;

    float specularLight = light.a;



    // Read depth

    float depthVal = tex2D(depthSampler, input.TexCoord).r;



    float4 position;

    position.x = input.TexCoord.x * 2.0f - 1.0f;

    position.y = -(input.TexCoord.y * 2.0f - 1.0f);

    position.z = depthVal;

    position.w = 1.0f;



    // Transform to world space

    position = mul(position, InvertViewProjection);

    position /= position.w;



    float Fog = 1.0f - (length(position - cameraPosition) / 500.0f);

    Fog = clamp(Fog, 0.0f, 1.0f);

    float4 Color = float4((diffuseColor * diffuseLight + diffuseColor * specularLight), 0.0f);

    Color = lerp(FogColor, Color, Fog);



    return saturate(Color);

}



technique Technique1

{

    pass Pass1

    {

	    VertexShader = compile vs_3_0 VertexShaderFunction();

	    PixelShader = compile ps_3_0 PixelShaderFunction();

    }

}


In Topic: Deferred Rendering

23 February 2012 - 03:19 PM

Scratch that, when I changed the VertexFormat to VertexFormat.None in the QuadRenderer class, everything seems to work.

What I would like to know is why, and if you have a good link on the subject for me to read, it would be greatly appreciated. :)

In Topic: Deferred Rendering

23 February 2012 - 03:13 PM

I believe it does. When I tried passing 0.5f instead of 1.0f to the QuadRenderer, it covered half of the screen, so that works at least.

Here's the class, but I assume it works fine:

[source lang="csharp"]using System;using System.Collections.Generic;using SlimDX;using SlimDX.Direct3D9;namespace VoxelSubmarineGameSlimDXTest.DeferredRendering{ using VertexPositionTexture = VertexStructures.VertexPositionTexture; public class QuadRenderer { private static Device device; private static VertexPositionTexture[] verts = null; private static short[] ib = null; public static void Init(Device argDevice) { device = argDevice; ib = new short[] { 0, 1, 2, 2, 3, 0 }; } public static void Render(Vector2 v1, Vector2 v2) { device.SetSamplerState(0, SamplerState.MagFilter | SamplerState.MinFilter | SamplerState.MipFilter, TextureFilter.Point); device.SetSamplerState(0, SamplerState.AddressU | SamplerState.AddressV, TextureAddress.Wrap); verts = new VertexPositionTexture[] { new VertexPositionTexture( new Vector3(v2.X, v1.Y, 1), new Vector2(1, 1)), new VertexPositionTexture( new Vector3(v1.X, v1.Y, 1), new Vector2(0, 1)), new VertexPositionTexture( new Vector3(v1.X, v2.Y, 1), new Vector2(0, 0)), new VertexPositionTexture( new Vector3(v2.X, v2.Y, 1), new Vector2(1, 0)) }; device.VertexDeclaration = VertexStructures.VertexPositionTextureDeclaration; device.VertexFormat = VertexPositionTexture.Format; device.DrawIndexedUserPrimitives<short, VertexPositionTexture>(PrimitiveType.TriangleList, 0, 4, 2, ib, Format.Index16, verts, VertexPositionTexture.SizeInBytes); } }}[/source]

As for the light direction vector - it's normalized in the shaders themselves, but I believe things which are within the 0.0 to 1.0 range don't have to be normalized, since that's basically what normalizing does?

In Topic: Deferred Rendering

22 February 2012 - 03:19 PM

Hmm, after doing this:

[source lang="csharp"]Surface.ToFile(colorRTSurface, "colorRTSurface.png", ImageFileFormat.Png);Surface.ToFile(normalRTSurface, "normalRTSurface.png", ImageFileFormat.Png);Surface.ToFile(depthRTSurface, "depthRTSurface.png", ImageFileFormat.Png);[/source]

I get some weird results. I've attached the images.

In Topic: Deferred Rendering

22 February 2012 - 03:12 PM

They do, in fact. Here they are (depth not included, since it's mostly white, but it was the same in XNA and it worked):

colorRT

Posted Image

normalRT

Posted Image

PARTNERS