This project is read-only.

Camera Integration

Sep 3, 2011 at 2:31 AM

I didn't want to add my problem on top of the other thread, so I created a new one. I too am having trouble getting camera integration to work with Krypton. Below is my camera class and the Krypton facade I use to access the lighting engine. The topic of matrix transformations have always boggled me. I am going to read a primer on them after this to get what they really do. I have a GetTransformation in my camera class (not really mine, but found on the web), but I don't think its enough. The PrepareLights() in the Krypton facade class takes a matrix as a parameter, but I still feel as if I am missing something. Any help that anyone can provide would be appreciated. Thanks in Advance!

 

// Found this @: http://www.david-amador.com/2009/10/xna-camera-2d-with-zoom-and-rotation/
// I can't believe how simple zooming is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace World.Library
{
    public class Camera
    {
        protected float _zoom; // Camera Zoom
        public Matrix _transform; // Matrix Transform
        public Vector2 _position; // Camera Position
        protected float _rotation; // Camera Rotation

        private readonly float MaxZoom = 2.0f;

        private readonly float MinZoom = 1.0f;

        public Camera(Vector2 position, float zoom, float rotation)
        {
            _zoom = zoom;
            _rotation = rotation;
            _position = position;
        }

        // Sets and gets zoom
        public float Zoom
        {
            get { return _zoom; }
            //set { _zoom = value; if (_zoom < MinZoom) _zoom = MinZoom; else if (_zoom > MaxZoom) _zoom = MaxZoom; } // Negative zoom will flip image
            set { _zoom = MathHelper.Clamp(value, MinZoom, MaxZoom);}
        }

        public float Rotation
        {
            get { return _rotation; }
            set { _rotation = value; }
        }

        // Auxiliary function to move the camera
        public void Move(Vector2 amount)
        {
            _position += amount;
        }
        // Get set position
        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }

        public Matrix Matrix { get; set; }

        public Matrix GetTransformation(GraphicsDevice graphicsDevice)
        {
            _transform =       // Thanks to o KB o for this solution
              Matrix.CreateTranslation(new Vector3(-_position.X, -_position.Y, 0)) *
                                         Matrix.CreateRotationZ(Rotation) *
                                         Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
                                         Matrix.CreateTranslation(new Vector3(graphicsDevice.Viewport.Width * 0.5f, graphicsDevice.Viewport.Height * 0.5f, 0));
            return _transform;
        }

        //public Matrix GetMatrix()
        //{

        //}

    }
}

//Krypton Facade
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using Krypton;
using Krypton.Common;
using Krypton.Lights;

namespace Graphics.Library.Lighting
{
    public enum LightType
    {
        Null,
        Cone,
        Point
    }

    public enum HullType
    {
        Null,
        Rectangle,
        Circle,
        Convex
    }

    public static class KryptonLightEngine
    {

        private static KryptonEngine krypton;

        private static Texture2D Conic;

        private static Texture2D Point;


        private static GraphicsDevice GraphicsDevice;

        /// <summary>
        /// Initailizes the lighting engine with the current graphics device
        /// </summary>
        /// <param name="graphicsDevice"></param>
        public static void Initialize(Game game, GraphicsDevice graphicsDevice)
        {
            krypton = new KryptonEngine(game, "Effects//KryptonEffect");

            GraphicsDevice = graphicsDevice;

            // Create a new simple point light texture to use for the lights
            Conic = LightTextureBuilder.CreateConicLight(graphicsDevice, 4000, 1000);

            Point = LightTextureBuilder.CreatePointLight(graphicsDevice, 100);

            krypton.Initialize();
        }

        /// <summary>
        /// Creates a light with the specified options
        /// </summary>
        /// <param name="color"></param>
        /// <param name="range"></param>
        /// <param name="intensity"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public static void CreateLight(Color color, float range, float intensity, Vector2 position, float angle, float fieldOfView, LightType type)
        {
            Light2D light = new Light2D();
            Random mRandom = new Random();

            if (type == LightType.Point)
            {
                light.Texture = Point;
            }

            else if (type == LightType.Cone)
            {
                light.Texture = Conic;
            }

            light.Color = color;
            light.Range = range;
            light.Intensity = intensity;
            light.X = position.X;
            light.Y = position.Y;
            light.Angle = angle;
            light.Fov = fieldOfView;
            //byte r = (byte)(mRandom.Next(255 - 64) + 64);
            //byte g = (byte)(mRandom.Next(255 - 64) + 64);
            //byte b = (byte)(mRandom.Next(255 - 64) + 64);

            //Light2D light = new Light2D()
            //{
            //    Texture = Point,
            //    Range = (float)(mRandom.NextDouble() * 5 + 1),
            //    Color = new Color(r, g, b),
            //    //Intensity = (float)(this.mRandom.NextDouble() * 0.25 + 0.75),
            //    Intensity = 1f,
            //    Angle = MathHelper.TwoPi * (float)mRandom.NextDouble(),
            //    X = (float)(mRandom.NextDouble() * 50 - 25),
            //    Y = (float)(mRandom.NextDouble() * 50 - 25),
            //};

            //// Here we set the light's field of view
            //if (10 % 2 == 0)
            //{
            //    light.Fov = MathHelper.PiOver2 * (float)(mRandom.NextDouble() * 0.75 + 0.25);
            //}

            krypton.Lights.Add(light);
        }

        public static void CreateHull(Vector2 position, Vector2 size, HullType hullType)
        {
            if (hullType == HullType.Rectangle)
            {
                var hull = ShadowHull.CreateRectangle(size);

                hull.Position = position;

                hull.Scale = new Vector2(1, 1);

                krypton.Hulls.Add(hull);
            }
        }

        public static void PrepareLights(Matrix cameraTranslation)
        {
            GraphicsDevice.RasterizerState = RasterizerState.CullNone;

            // Create a world view projection matrix to use with krypton
            Matrix world = Matrix.Identity;
            Matrix view = Matrix.CreateTranslation(new Vector3(50 * GraphicsDevice.Viewport.AspectRatio, 0, 0) * -1f);
            Matrix projection = Matrix.CreateOrthographic(50 * 1.7f, 50, 0, 1);
            //Matrix projection = Matrix.CreateOrthographicOffCenter(0f, 1280, 720, 0, 0, 1f);

            //Matrix projection = Matrix.CreateOrthographic(1280, 720, 0, 1);
            
            krypton.Matrix = world * projection * view;
            //krypton.Matrix = matrix;

            krypton.LightMapPrepare();
            //krypton.SpriteBatchCompatablityEnabled = true;

            GraphicsDevice.Clear(Color.White);
        }

        public static void Draw(GameTime gameTime, Matrix world)
        {
            krypton.Draw(gameTime);
        }
    }
}


Sep 5, 2011 at 4:01 AM

Solved.. Not using the camera matrix caused a normal looking light using the settings from the Krypton sample. With my camera, the light was just tiny, 1.5px*1.5px.