XNA reflection

Started by
2 comments, last by C3rial 14 years ago
Hi guys! I'm trying to use riemers xna perfect mirror tutorial, but I'm not getting the reflection. In fact, when I try rendering out the texture to an image it's just black. Even if I device-clear it to blue. In this code I don't have any objects apart from the plane that should have the reflection. As this should have the reflection...when I set it to the reflectionTexture I shouldn't see it, as it will only reflect the device-clear-color. Anyway...maybe you can tell me what's wrong? Here's the code: http://pastebin.com/1Hu5N2HD
CrazyPoly
Advertisement
Noticed I've gotten some views, but no replies. So, just in case this is because people don't bother going to the link I added I'll post the code here also:)

using System;using System.Collections.Generic;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.GamerServices;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Net;using Microsoft.Xna.Framework.Storage;namespace renderTesting{    /// <summary>    /// This is the main type for your game    /// </summary>    public class Game1 : Microsoft.Xna.Framework.Game    {        GraphicsDeviceManager graphics;        SpriteBatch spriteBatch;        GraphicsDevice device;        Matrix worldMatrix;        Matrix viewMatrix;        Matrix projectionMatrix;        RenderTarget2D reflectionRenderTarget;        Texture2D reflectionMap;        BasicEffect effect;        Model planeModel;        VertexDeclaration vertexDeclaration;        int upDownAngle = 0;        int upDownModel = 0;        int modelRotation = 0;        Texture2D tempTex;        private static bool renderPicture = false;        public Game1()        {            graphics = new GraphicsDeviceManager(this);            Content.RootDirectory = "Content";        }        /// <summary>        /// Allows the game to perform any initialization it needs to before starting to run.        /// This is where it can query for any required services and load any non-graphic        /// related content.  Calling base.Initialize will enumerate through any components        /// and initialize them as well.        /// </summary>        protected override void Initialize()        {            // TODO: Add your initialization logic here            graphics.PreferredBackBufferWidth = 500;            graphics.PreferredBackBufferHeight = 500;            graphics.IsFullScreen = false;            graphics.ApplyChanges();             base.Initialize();        }        /// <summary>        /// LoadContent will be called once per game and is the place to load        /// all of your content.        /// </summary>        protected override void LoadContent()        {            // Create a new SpriteBatch, which can be used to draw textures.            spriteBatch = new SpriteBatch(GraphicsDevice);            device = graphics.GraphicsDevice;            PresentationParameters pp = device.PresentationParameters;            reflectionRenderTarget = new RenderTarget2D(device, pp.BackBufferWidth, pp.BackBufferHeight, 1, device.DisplayMode.Format);            effect = new BasicEffect(device, null);            planeModel = Content.Load<Model>("plane");            tempTex = Content.Load<Texture2D>("pants");            foreach (ModelMesh mesh in planeModel.Meshes)            {                foreach (ModelMeshPart part in mesh.MeshParts)                {                    part.Effect = effect;                }            }            viewMatrix = Matrix.CreateLookAt(new Vector3(0,50,100), new Vector3(0,1,-10), new Vector3(0,1,0));            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 0.2f, 10000.0f);            vertexDeclaration = new VertexDeclaration(device, VertexPositionColorTexture.VertexElements);        }        /// <summary>        /// UnloadContent will be called once per game and is the place to unload        /// all content.        /// </summary>        protected override void UnloadContent()        {            // TODO: Unload any non ContentManager content here        }        /// <summary>        /// Allows the game to run logic such as updating the world,        /// checking for collisions, gathering input, and playing audio.        /// </summary>        /// <param name="gameTime">Provides a snapshot of timing values.</param>        protected override void Update(GameTime gameTime)        {            // Allows the game to exit            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)                this.Exit();            if (Keyboard.GetState().IsKeyDown(Keys.Escape))                this.Exit();            if (Keyboard.GetState().IsKeyDown(Keys.Space))            {                renderPicture = true;            }            if (Keyboard.GetState().IsKeyDown(Keys.Up))            {                upDownAngle += 1;            }            if (Keyboard.GetState().IsKeyDown(Keys.Down))            {                upDownAngle -= 1;            }            if (Keyboard.GetState().IsKeyDown(Keys.W))            {                upDownModel += 1;            }            if (Keyboard.GetState().IsKeyDown(Keys.S))            {                upDownModel -= 1;            }            if (Keyboard.GetState().IsKeyDown(Keys.A))            {                modelRotation += 1;            }            if (Keyboard.GetState().IsKeyDown(Keys.D))            {                modelRotation -= 1;            }            // TODO: Add your update logic here            worldMatrix = Matrix.Identity * Matrix.CreateTranslation(new Vector3(0, upDownModel-20, -1)) * Matrix.CreateRotationY(MathHelper.ToRadians(modelRotation));            viewMatrix = Matrix.CreateLookAt(new Vector3(0, 50, 100), new Vector3(0, upDownAngle, -30), new Vector3(0, 1, 0));            base.Update(gameTime);        }        /// <summary>        /// This is called when the game should draw itself.        /// </summary>        /// <param name="gameTime">Provides a snapshot of timing values.</param>        protected override void Draw(GameTime gameTime)        {            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);            Plane reflectionPlane = CreatePlane(1 - 0.5f, new Vector3(0, -1, 0), viewMatrix, true);            device.ClipPlanes[0].Plane = reflectionPlane;            device.ClipPlanes[0].IsEnabled = true;            device.SetRenderTarget(0, reflectionRenderTarget);            device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);            device.ClipPlanes[0].IsEnabled = false;            device.SetRenderTarget(0, null);            reflectionMap = reflectionRenderTarget.GetTexture();            renderPicture = false;            //reflectionMap = tempTex;            foreach (ModelMesh mesh in planeModel.Meshes)            {                foreach (BasicEffect currentEffect in mesh.Effects)                {                    currentEffect.World = worldMatrix * Matrix.CreateRotationZ(MathHelper.ToRadians(90));                    currentEffect.View = viewMatrix;                    currentEffect.Projection = projectionMatrix;                    currentEffect.TextureEnabled = true;                    currentEffect.Texture = reflectionMap;                }                mesh.Draw();            }            base.Draw(gameTime);        }        private Plane CreatePlane(float height, Vector3 planeNormalDirection, Matrix currentViewMatrix, bool clipSide)        {            planeNormalDirection.Normalize();            Vector4 planeCoeffs = new Vector4(planeNormalDirection, height);            if (clipSide)                planeCoeffs *= -1;            Matrix worldViewProjection = currentViewMatrix * projectionMatrix;            Matrix inverseWorldViewProjection = Matrix.Invert(worldViewProjection);            inverseWorldViewProjection = Matrix.Transpose(inverseWorldViewProjection);            planeCoeffs = Vector4.Transform(planeCoeffs, inverseWorldViewProjection);            Plane finalPlane = new Plane(planeCoeffs);            return finalPlane;        }    }}
CrazyPoly
Unless I'm missing something here, all you're doing between setting your render target to the device and unsetting it is clearing it to black... so it's no surprise it's coming out black.
Yeah, I know. However, I've tried clearing before or after, but it doesn't make much difference. For some reason the reflectionTexture becomes violetblue, even though I'm device.clear'ing with Color.Blue. Maybe it's just the default color.

Anyway - Riemer is determined this code should work.
CrazyPoly

This topic is closed to new replies.

Advertisement