Sunday, 3 January 2010

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;

}

0 comments:

Post a Comment

Please ask here