1. This has been done a thousand times, but here goes anyway...
    Ok, sort of. After much experimenting, this is the simplest method I could come up with.

    This method uses linear depth, and reconstructs the position in world space.
    To store the position:

    /* Vertex Shader */
    float4 viewPosition = mul(input.Position, mul(World, View));
    output.Depth = (-viewPosition.z - NearPlane) / (FarPlane - NearPlane);
    
    /* Pixel Shader */
    output.Depth = input.Depth; // Um... yeah... just output the depth...
    

    To reconstruct the position, we're going to interpolate between the camera's frustum corners.
    So:

    /* Your Code (C#) */
    BoundingFrustum frustum = new BoundingFrustum(view * projection);
    effect.SetParameter["FrustumCorners"].SetValue(frustum.GetCorners());
    

    /* Screen Space Pixel Shader */
    float depthSample = tex2D(DepthTextureSampler, screenCoord);
    
    float3 position = lerp(
        lerp(
            lerp(FrustumCorners[0], FrustumCorners[1], screenCoord.x),
            lerp(FrustumCorners[3], FrustumCorners[2], screenCoord.x),
            screenCoord.y),
        lerp(
            lerp(FrustumCorners[4], FrustumCorners[5], screenCoord.x),
            lerp(FrustumCorners[7], FrustumCorners[6], screenCoord.x),
            screenCoord.y),
        depthSample);
    

    So there you have it.
    1

    View comments

  2. Say you have a simple class which implements the singleton pattern.



    Now you want to create a class which inherits the singleton functionality.



    So, you call Derived.GetInstance() and... you get an instance of a Singleton, not a Derived!!

    An interesting (and somewhat wacky) solution to this problem:
    The Curiously Reccuring Template Pattern



    Trust me, it compiles. Now, calling Derived.GetInstance() returns an instance of Derived, not Singleton. Hooray for CRTP!!

    One problem though - you can't do this:



    Definitions of Singleton must have type arguments defined. This can be worked around by implementing an interface:

    0

    Add a comment

  3. In an effort to emulate Reactive Programming, I have created this nifty little class. It's preliminary (created five minutes ago), but seems useful enough for now.

    January 17, 2011:
    I've decided to make the structure immutable. The type is meant to be used as a property, so any time the value is retrieved, a copy would be returned. A call to BindValue() would screw the whole thing over.

    ALSO:
    Fixed an embarassingly dumb error in GetValue().

    January 19, 2011:
    Just realized that most of my generic arguments magically became lower case.

    0

    Add a comment

  4. Every once in a while, I run into this sort of ugly situation:

    public MyClass(GraphicsDevice graphicsDevice)
        : this(graphicsDevice, graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height)
    { }


    public MyClass(GraphicsDevice graphicsDevice, int width, int height)
    {
        if (graphicsDevice == null)
            throw new ArgumentNullException("graphicsDevice");


        // etc.
    }

    I just want to do a basic check. Throw an exception if graphicsDevice is null. Simple enough. So what happens when I call the overload? Since the overload calls the Viewport property of the graphicsDevice, a NullReferenceException will be thrown instead of an ArgumentNullException, which is considerably less informative to the end user. The obvious solution is to not call the other constructor, but that leads to duplicated code. How am I supposed to solve this one?

    Code Contracts. The above now becomes this.

    public MyClass(GraphicsDevice graphicsDevice)

        : this(graphicsDevice, graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height)
    {
        Contract.Requires<NullReferenceException>(graphicsDevice != null);
    }

    public MyClass(GraphicsDevice graphicsDevice, int width, int height)
    {
        Contract.Requires<NullReferenceException>(graphicsDevice != null);

        // etc.
    }

    Since Code Contracts modifies code at compile time, the contract will actually be executed before the other constructor is called. I'm still duplicating some code. But it's better than duplicating the entire constructor.

    Now if only I could get static checking without having to pay extra for it...
    0

    Add a comment

  5. Below is a rendering of a model I've been working on for a game. It's the first one I've made with Blender 2.5.

    I finally figured out how to make shiny looking metal without using any actual reflection. Just set the diffuse really low, and lower the specular exponent (in this shot, it's 50). What makes the real difference is the specular color. White doesn't quite look right - in this, I changed it to more closely match the diffuse color.
    0

    Add a comment

Blog Archive
Active Projects
Active Projects
Total Pageviews
Total Pageviews
10816
Loading