This project is read-only.

How Make ReflectiveMap?

May 11, 2009 at 11:09 AM
Edited May 11, 2009 at 5:45 PM

Xen I would like to achieve in ReflectiveMap, but some parts I do not know how to achieve in xen, 

I would like to ask StatusUnknown can add this feature to do xen?

In addition to publication of your e-mail? I would like to put my test files to you. 


I am following this tutorial for reference.

http://www.ziggyware.com/news.php?readmore=757

May 11, 2009 at 5:37 PM

 

 

namespace Tutorials.Tutorial_13

{

    struct DiskVertex

    {

        public Vector3 position;

        public Vector3 normal, binormal, tangent;

        public Vector2 tex;

        public DiskVertex(Vector3 positions, Vector2 texScale)

        {

            position = positions;

            normal = new Vector3(0, 0, 1);

            binormal = new Vector3(0, 1, 0);

            tangent = new Vector3(1, 0, 0);

            tex =  texScale;

        }

    }

    class GroundDisk : IDraw, IContentOwner

    {

        private IVertices vertices;

        private MaterialShader material;

        private Shader.Technique1 shader;

        private Matrix reflectionViewMatrix;

        public float planeHeight;

        private int WIDTH;

        private int HEIGHT;

        private Vector3 POSITION;

        public delegate void RenderReflectionHandler(Matrix view, Vector3 reflectionCamPosition);

 

        public GroundDisk(ContentRegister content, MaterialLightCollection lights, int width, int height, float waterHeight,Vector3 position)

        {

            this.shader = new Shader.Technique1();

            this.WIDTH = width;

            this.HEIGHT = height;

            this.POSITION = position;

            this.planeHeight = waterHeight;

            int vertexCount = 6;

            //create the vertices. Note the DiskVertex() constructor takes an angle/size

            DiskVertex[] verts = new DiskVertex[vertexCount];

            verts[0] = new DiskVertex(new Vector3(0, 0, planeHeight), new Vector2(0, 1));

            verts[1] = new DiskVertex(new Vector3(WIDTH, HEIGHT, planeHeight), new Vector2(1, 0));

            verts[2] = new DiskVertex(new Vector3(0, HEIGHT, planeHeight), new Vector2(0, 0));

            verts[3] = new DiskVertex(new Vector3(0, 0, planeHeight), new Vector2(0, 1));

            verts[4] = new DiskVertex(new Vector3(WIDTH, 0, planeHeight), new Vector2(1, 1));

            verts[5] = new DiskVertex(new Vector3(WIDTH, HEIGHT, planeHeight), new Vector2(1, 0));

            this.vertices = new Vertices<DiskVertex>(verts);

            //load the textures for this material

            content.Add(this);

        }

        public void LoadContent(ContentRegister content, DrawState state, ContentManager manager)

        {

            //load the box texture, and it's normal map.

            this.shader.FloorMap = manager.Load<Texture2D>(@"floor");

            this.shader.MaskMap = manager.Load<Texture2D>(@"spotlight");

            this.shader.NormalMap = manager.Load<Texture2D>(@"normal");

        }

        public void DrawReflectionMap(Camera3D camera, RenderReflectionHandler render)

        {

            reflectionViewMatrix = camera.UpdateReflectiveMatrices();

            float reflectionCamYCoord = -camera.Position.Y + (planeHeight * 2);

            Vector3 reflectionCamPosition = new Vector3(camera.Position.X, reflectionCamYCoord, camera.Position.Z);

            Vector3 planeNormalDirection = new Vector3(0, 1, 0);

            planeNormalDirection.Normalize();

            Vector4 planeCoefficients = new Vector4(planeNormalDirection, -3f);


            Matrix camMatrix = reflectionViewMatrix * camera.Projection.ProjectionMatrix;

            Matrix invCamMatrix = Matrix.Invert(camMatrix);

            invCamMatrix = Matrix.Transpose(invCamMatrix);


            planeCoefficients = Vector4.Transform(planeCoefficients, invCamMatrix);

            Plane refractionClipPlane = new Plane(planeCoefficients);

  device.ClipPlanes[0].Plane = refractionClipPlane; ->How to make?

            device.ClipPlanes[0].IsEnabled = true; ->How to make?

            device.SetRenderTarget(0, reflectionRT); ->How to make?

            device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);     ->How to make?

            render.Invoke(reflectionViewMatrix, reflectionCamPosition);

            device.SetRenderTarget(0, null); ->How to make?

            reflectionMap = reflectionRT.GetTexture(); ->How to make?

            device.ClipPlanes[0].IsEnabled = false; ->How to make?

        }

        //draw the ground plane..

        public void Draw(DrawState state)

        {

            Matrix worldMatrix = Matrix.CreateTranslation(POSITION - new Vector3(WIDTH / 2, HEIGHT / 2, 0));

            shader.World = worldMatrix;

            //this.DrawReflectionMap(state.Camera,new RenderReflectionHandler(RenderReflection) ;

            shader.Bind(state);

            //then draw the vertices

            vertices.Draw(state, null, PrimitiveType.TriangleFan);

        }

        public bool CullTest(ICuller culler)

        {

            return culler.TestBox(new Vector3(-10, -10, 0), new Vector3(10, 10, 0));

        }

 

        public void UnloadContent(ContentRegister content, DrawState state)

        {

        }

    }

//NEW CODE

class CameraOrbit : IUpdate

{

private Camera3D camera;

private Vector2 cameraRotation;

 

//The class is constructed with a camera and a reference to the Applications

//UpdateManager

public CameraOrbit(Camera3D camera, UpdateManager manager)

{

//keep a reference to the camera

this.camera = camera;

//Add this object to the update manager

manager.Add(this);

}

public UpdateFrequency Update(UpdateState state)

{

InputState playerInput = state.PlayerInput[PlayerIndex.One].InputState;

float sensitivity = 5;

//make sure it's scaled by the time step (in this case, it'll be 1/60)

sensitivity *= state.DeltaTimeSeconds;

this.cameraRotation.X += playerInput.ThumbSticks.RightStick.X * sensitivity;

this.cameraRotation.Y += playerInput.ThumbSticks.RightStick.Y * sensitivity;

 

//clamp the rotation up/down

if (this.cameraRotation.Y > MathHelper.PiOver2)

this.cameraRotation.Y = MathHelper.PiOver2;

if (this.cameraRotation.Y < -MathHelper.PiOver2)

this.cameraRotation.Y = -MathHelper.PiOver2;

 

float viewDistance = 8;

if (playerInput.Buttons.A.IsDown)

viewDistance = 6;

Matrix cameraMatrix =

Matrix.CreateTranslation(0, 0, viewDistance) * //move +viewDistance on the z-axis (negative z-axis is into the screen, so positive moves away)

Matrix.CreateRotationX(this.cameraRotation.Y + MathHelper.PiOver2) * // rotate up/down around x-axis (the model is rotated 90 deg, hence the PiOver2)

Matrix.CreateRotationZ(this.cameraRotation.X); //then finally rotate around the z-axis left/right

this.camera.SetCameraMatrix(ref cameraMatrix);

return UpdateFrequency.FullUpdate60hz;

}

}

[DisplayName(Name = "Tutorial 13: IUpdate and Player Input")]

public class Tutorial : Application

{

//screen draw target

private DrawTargetScreen drawToScreen;

private CameraOrbit cameraOrbit;

        private GroundDisk ground;

        private MaterialLightCollection lights;

 

protected override void Initialise()

{

Camera3D camera = new Camera3D();

 

//create the draw target.

drawToScreen = new DrawTargetScreen(this, camera);

drawToScreen.ClearBuffer.ClearColour = Color.CornflowerBlue;

 

            //create the light collection

lights = new MaterialLightCollection();

//set a dark blue ambient colour

lights.AmbientLightColour = new Color(40, 40, 80).ToVector3();

 

//positions for two lights

Vector3[] lightPositions = new Vector3[] 

new Vector3(0, 30, 2), 

new Vector3(0, -30, 2) 

};

 

//geometry for a light (shared for each light)

IDraw lightGeometry = null;

 

for (int i = 0; i < lightPositions.Length; i++)

{

float lightHalfFalloffDistance = 15;

Color lightColor = Color.LightYellow;

Color lightSpecularColour = Color.WhiteSmoke;

bool perPixel = i < 2;//first two lights are per-pixel

 

//interface to the light about to be created

IMaterialPointLight light = null;

 

//create the point light

light = lights.AddPointLight(perPixel, lightPositions[i], lightHalfFalloffDistance, lightColor, lightSpecularColour);

 

//adjust the lighting attenuation model, the constant defaults to 1, which prevents the light being brighter than 1.0 in the falloff equation

//set to 0.25, the light will get really bright in close (up to 4)

//(see light.QuadraticAttenuation remarks for an explanation of the falloff model)

light.ConstantAttenuation = 0.25f;

 

//create the light geometry (a sphere)

if (lightGeometry == null)

lightGeometry = new Xen.Ex.Geometry.Sphere(Vector3.One, 8, true, false, false);

 

//visually show the light with a light drawer

IDraw lightSourceDrawer = new LightSourceDrawer(lightPositions[i], lightGeometry,lightColor);

 

//add the light geometry to the screen

drawToScreen.Add(lightSourceDrawer);

}

//create the CameraOrbit object..

//pass in the application update manager

cameraOrbit = new CameraOrbit(camera, this.UpdateManager);

 

//create the actor instance from Tutorial_10

drawToScreen.Add(new Tutorial_10.Actor(this.Content, Vector3.Zero));

            ground = new GroundDisk(this.Content, lights, 20, 20, 1,new Vector3(0,0,-3.5f));

            drawToScreen.Add(ground);

}

 

//NEW CODE

//Finally, the method 'InitialisePlayerInput' can be overridden to change

//player input settings when the application starts up.

//This method is called right after Initialise

 

protected override void InitialisePlayerInput(Xen.Input.PlayerInputCollection playerInput)

{

//if using the mouse, then make it centre to the window

 

if (playerInput[PlayerIndex.One].ControlInput == Xen.Input.ControlInput.KeyboardMouse)

playerInput[PlayerIndex.One].InputMapper.CentreMouseToWindow = true;

}

 

protected override void Draw(DrawState state)

{

drawToScreen.Draw(state);

}

 

protected override void Update(UpdateState state)

{

if (state.PlayerInput[PlayerIndex.One].InputState.Buttons.Back.OnPressed)

this.Shutdown();

}

}

    }

}


 

May 13, 2009 at 5:12 AM

The tutorial code you are trying to convert cannot be converted directly. Render targets are handled differently in Xen (they are introduced in Tutorial 07).

As for the clip planes, to set the clip planes you need to directly access the GraphicsDevice. To get the GraphicsDevice, you have to call BeginGetGraphicsDevice() on the DrawState object. (Pass in 'None' as the argument as you will not be changing render states that are internally tracked).