Showing posts from 2012

Reconstructing Position From Depth

Matt posted an excellent article about reconstructing position from depth. Check out his article here: He has the following function to reconstruct View Space Position from Post Clip Space Position: // Function for converting depth to view-space position // in deferred pixel shader pass. vTexCoord is a texture // coordinate for a full-screen quad, such that x=0 is the // left of the screen, and y=0 is the top of the screen. float3 VSPositionFromDepth(float2 vTexCoord) { // Get the depth value for this pixel float z = tex2D(DepthSampler, vTexCoord); // Get x/w and y/w from the viewport position float x = vTexCoord.x * 2 - 1; float y = (1 - vTexCoord.y) * 2 - 1; float4 vProjectedPos = float4(x, y, z, 1.0f); // Transform by the inverse projection matrix float4 vPositionVS = mul(vProjectedPos, g_matInvProjection); // Divide by w to get the view-space position r…

C# Integer to String Builder

As many of you know, StringBuilder.Append(int) method creates a garbage. This is bad for XNA games that do this conversion every frame. In this article, I provide one implementation to convert int to string without creating garbage. I tried to be as efficient as possible; if you find better way to do this, please let me know. public static class StringBuilderExtension { private static char[] charToInt = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; public static void Swap(this StringBuilder sb, int startIndex, int endIndex) { // Swap the integers Debug.Assert(endIndex >= startIndex); int count = (endIndex - startIndex + 1) / 2; for (int i = 0; i < count; ++i) { char temp = sb[startIndex + i]; sb[startIndex + i] = sb[endIndex - i]; sb[endIndex - i] = temp; } } public static void…

World, View and Projection Matrix Internals

This convention below is applicable to Direct3D and XNA matrices
World Matrix
Given a position and basis vectors rightup and look of an object, a world matrix can be formed by the following arrangement:
View Matrix
Given a position and basis vectors rightup and look of a viewer, a view matrix can be formed by the following arrangement:
Projection Matrix
Given field of view FOVaspect ratio, near clip plane Zn and far clip plane Zf, a perspective projection matrix can be formed by the following arrangment: