Friday, 29 January 2010

Dynamic Cube Mapping - Part 3

In this example we will take the processes and clean them up.

Most math is now handled by the GPU, and a few extras have been added, which include HDR, Blur and Tone-Mapping.



XNA3.1 PROJECT DOWNLOAD

Saturday, 16 January 2010

Dynamic Cube Mapping in XNA - Part 2

okay guys. Here goes...

I thought I would save everyone some hassle and deliver you the WHOLE code needed to create Dynamic Environment Maps. The code produces a result as below:




The FX file used is a simple Reflection.fx effect. The secret is the manipulation of the 6 reflections rendered to RenderTargetCube.faces.

Cubemaps are generated from inside the virtual cube, so looking at these images from the outside of the cube-mesh creates unwanted results, as the UV's and normals are flipped.

It is just like turning your socks inside out !


XNA3.1 PROJECT DOWNLOAD

The regenerated cube-maps will work with any shader.

It would also be very easy to use these 'corrected' maps and blur/filter them to create ambience maps with very little overheads.

Included in the archive are various other shaders that you are free to use.

An XboxPC controller is required to rotate the scene objects. L_Stick for orbiters and R_Stick for the BMW car.

Enjoy!

Any questions?

Monday, 4 January 2010

Uber Shader

Here is my Uber Shader that i wrote, and promised to share with the XNA/Sunburn Community.

Capabilities:

  • Blinn

  • Phong

  • Oren-Nayar (Blinn + Phong)

  • Anisotropic

  • Single BDRF

  • Dual BDRF

  • Translusency/Sub-Surface Diffuse

  • Fresnel Reflection

  • Fresnel Color

  • Simple Reflection

  • Glow/Self Illumination

  • Offset Mapping

  • Parallax Occlusion Mapping

  • Micro-Normal Texturing


  • Techniques:

  • OneLight

  • OneLightAlpha2Sided


  • allInOne_FX_Shader

    Enjoy

    All I ask is, if you use it in a commercial game, please tell me and give me a mention in your credits. Thanks

    :-)

    Sunday, 3 January 2010

    Dynamic Cube Mapping Video

    Dynamic Cube Mapping in XNA

    RenderTargetCube RefCubeMap;
    TextureCube EnvironmentMap;

    ...

    protected override void Draw(GameTime gameTime)
    {
    GraphicsDevice.Clear(Color.CornflowerBlue);

    // TODO: perform AI and physics etc...
    // TODO: draw sky
    // TODO: draw all solids (except PLAYER) with FAKE envCubeMap
    // TODO: draw all transparencies (except PLAYER) with FAKE envCubeMap
    // TODO: draw all shadows (except PLAYER) with FAKE envCubeMap

    DrawEnvironmentMap();

    // TODO: draw everything (including PLAYER) solids ONLY with REAL envCubeMap
    DrawSky();
    DrawObjectsInWorld();
    DrawPlayer();
    // TODO: draw everything (including PLAYER) transparencies ONLY with REAL envCubeMap
    // TODO: draw everything (including PLAYER) shadows ONLY with REAL envCubeMap
    }


    //dynamic cubemap routine
    public void DrawEnvironmentMap()
    {
    graphics.GraphicsDevice.SetRenderTarget(0, null);

    viewMatrix = Matrix.CreateLookAt(Vector3.Zero, Vector3.Left, Vector3.Up);
    effect.Parameters["matWorldViewProj"].SetValue(worldMatrix * viewMatrix * projMatrix);
    this.GraphicsDevice.SetRenderTarget(0, RefCubeMap, CubeMapFace.NegativeX);
    this.GraphicsDevice.Clear(Color.White);

    // draw sky
    // draw everything solids (except PLAYER) with a FAKE/static envCubeMap
    // draw everything transparencies (except PLAYER) with a FAKE/static envCubeMap
    // draw everything shadows (except PLAYER) with a FAKE/static envCubeMap
    DrawSky();
    DrawObjectsInWorld();
    graphics.GraphicsDevice.SetRenderTarget(0, null);

    viewMatrix = Matrix.CreateLookAt(Vector3.Zero, Vector3.Down, Vector3.Forward);
    effect.Parameters["matWorldViewProj"].SetValue(worldMatrix * viewMatrix * projMatrix);
    this.GraphicsDevice.SetRenderTarget(0, RefCubeMap, CubeMapFace.NegativeY);
    this.GraphicsDevice.Clear(Color.White);

    // draw sky
    // draw everything solids (except PLAYER) with a FAKE/static envCubeMap
    // draw everything transparencies (except PLAYER) with a FAKE/static envCubeMap
    // draw everything shadows (except PLAYER) with a FAKE/static envCubeMap
    DrawSky();
    DrawObjectsInWorld();
    graphics.GraphicsDevice.SetRenderTarget(0, null);

    viewMatrix = Matrix.CreateLookAt(Vector3.Zero, Vector3.Backward, Vector3.Up);
    effect.Parameters["matWorldViewProj"].SetValue(worldMatrix * viewMatrix * projMatrix);
    this.GraphicsDevice.SetRenderTarget(0, RefCubeMap, CubeMapFace.NegativeZ);
    this.GraphicsDevice.Clear(Color.White);

    // draw sky
    // draw everything solids (except PLAYER) with a FAKE/static envCubeMap
    // draw everything transparencies (except PLAYER) with a FAKE/static envCubeMap
    // draw everything shadows (except PLAYER) with a FAKE/static envCubeMap
    DrawSky();
    DrawObjectsInWorld();
    graphics.GraphicsDevice.SetRenderTarget(0, null);

    viewMatrix = Matrix.CreateLookAt(Vector3.Zero, Vector3.Right, Vector3.Up);
    effect.Parameters["matWorldViewProj"].SetValue(worldMatrix * viewMatrix * projMatrix);
    this.GraphicsDevice.SetRenderTarget(0, RefCubeMap, CubeMapFace.PositiveX);
    this.GraphicsDevice.Clear(Color.White);

    // draw sky
    // draw everything solids (except PLAYER) with a FAKE/static envCubeMap
    // draw everything transparencies (except PLAYER) with a FAKE/static envCubeMap
    // draw everything shadows (except PLAYER) with a FAKE/static envCubeMap
    DrawSky();
    DrawObjectsInWorld();
    graphics.GraphicsDevice.SetRenderTarget(0, null);

    viewMatrix = Matrix.CreateLookAt(Vector3.Zero, Vector3.Up, Vector3.Backward);
    effect.Parameters["matWorldViewProj"].SetValue(worldMatrix * viewMatrix * projMatrix);
    this.GraphicsDevice.SetRenderTarget(0, RefCubeMap, CubeMapFace.PositiveY);
    this.GraphicsDevice.Clear(Color.White);

    // draw sky
    // draw everything solids (except PLAYER) with a FAKE/static envCubeMap
    // draw everything transparencies (except PLAYER) with a FAKE/static envCubeMap
    // draw everything shadows (except PLAYER) with a FAKE/static envCubeMap
    DrawSky();
    DrawObjectsInWorld();
    graphics.GraphicsDevice.SetRenderTarget(0, null);

    viewMatrix = Matrix.CreateLookAt(Vector3.Zero, Vector3.Forward, Vector3.Up);
    effect.Parameters["matWorldViewProj"].SetValue(worldMatrix * viewMatrix * projMatrix);
    this.GraphicsDevice.SetRenderTarget(0, RefCubeMap, CubeMapFace.PositiveZ);
    this.GraphicsDevice.Clear(Color.White);

    // draw sky
    // draw everything solids (except PLAYER) with a FAKE/static envCubeMap
    // draw everything transparencies (except PLAYER) with a FAKE/static envCubeMap
    // draw everything shadows (except PLAYER) with a FAKE/static envCubeMap
    DrawSky();
    DrawObjectsInWorld();
    graphics.GraphicsDevice.SetRenderTarget(0, null);

    this.EnvironmentMap = RefCubeMap.GetTexture();

    // restore our matrix after changing during the cube map rendering
    viewMatrix = Matrix.CreateLookAt(new Vector3(x, y, 80), Vector3.Zero, Vector3.Up);
    effect.Parameters["matWorldViewProj"].SetValue(worldMatrix * viewMatrix * projMatrix);

    // reset buffer
    graphics.GraphicsDevice.RenderState.DepthBufferEnable = true;

    }