Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Quad Billboarding Issue #2

By: Neurotrace on Oct 29th, 2011  |  syntax: C#  |  size: 8.29 KB  |  hits: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace QuadRotationProblem
  13. {
  14.  
  15.     public class Game1 : Microsoft.Xna.Framework.Game
  16.     {
  17.         GraphicsDeviceManager graphics;
  18.         SpriteBatch spriteBatch;
  19.  
  20.         Effect Billboard;
  21.         TexturedQuad Quad;
  22.  
  23.         public Game1()
  24.         {
  25.             graphics = new GraphicsDeviceManager(this);
  26.             Content.RootDirectory = "Content";
  27.         }
  28.  
  29.         protected override void Initialize()
  30.         {
  31.             RasterizerState rs = new RasterizerState();
  32.             rs.CullMode = CullMode.None;
  33.             GraphicsDevice.RasterizerState = rs;
  34.             Camera.Instance.Initialize(GraphicsDevice);
  35.             Quad = new TexturedQuad(GraphicsDevice, Content.Load<Texture2D>("Claudius"), new Vector3(0, 0, 0), 32, 64);
  36.             base.Initialize();
  37.         }
  38.  
  39.         protected override void LoadContent()
  40.         {
  41.             // Create a new SpriteBatch, which can be used to draw textures.
  42.             spriteBatch = new SpriteBatch(GraphicsDevice);
  43.             Billboard = Content.Load<Effect>("Billboard");
  44.         }
  45.  
  46.         protected override void UnloadContent()
  47.         {
  48.             // TODO: Unload any non ContentManager content here
  49.         }
  50.  
  51.         protected override void Update(GameTime gameTime)
  52.         {
  53.             // Allows the game to exit
  54.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  55.                 this.Exit();
  56.  
  57.             KeyboardState KBState = Keyboard.GetState();
  58.             if (KBState.IsKeyDown(Keys.Escape))
  59.                 this.Exit();
  60.  
  61.             if (KBState.IsKeyDown(Keys.R))
  62.                 Camera.Instance.Rotation = 0;
  63.             else if (KBState.IsKeyDown(Keys.Left))
  64.             {
  65.                 Camera.Instance.Rotate(.05f);
  66.             }
  67.             else if (KBState.IsKeyDown(Keys.Right))
  68.             {
  69.                 Camera.Instance.Rotate(-.05f);
  70.             }
  71.  
  72.             base.Update(gameTime);
  73.         }
  74.  
  75.         protected override void Draw(GameTime gameTime)
  76.         {
  77.             GraphicsDevice.Clear(Color.CornflowerBlue);
  78.  
  79.             Quad.Draw(GraphicsDevice, ref Billboard);
  80.  
  81.             base.Draw(gameTime);
  82.         }
  83.     }
  84.  
  85.     class TexturedQuad
  86.     {
  87.         public Texture2D Texture;
  88.         public VertexPositionTexture[] Vertices;
  89.  
  90.         public float Width { get; set; }
  91.         public float Height { get; set; }
  92.         private Vector3 position;
  93.         public Vector3 Position
  94.         {
  95.             get
  96.             {
  97.                 return position;
  98.             }
  99.             set
  100.             {
  101.                 position = value;
  102.                 Vector3 difference = position - Vertices[0].Position;
  103.                 Vertices[0].Position = position;
  104.                 Vertices[1].Position += difference;
  105.                 Vertices[2].Position += difference;
  106.                 Vertices[3].Position += difference;
  107.             }
  108.         }
  109.  
  110.         public TexturedQuad(GraphicsDevice GFX, Texture2D texture, Vector3 origin, float width, float height, int frameNum = 0, int frameRow = 0)
  111.         {
  112.             Texture = texture;
  113.             position = origin;
  114.             Width = width;
  115.             Height = height;
  116.             Vertices = new VertexPositionTexture[4];
  117.             Vertices[0] = new VertexPositionTexture(Position, new Vector2((Width * frameNum) / Texture.Width, (Height * frameRow) / Texture.Height));
  118.             Vertices[1] = new VertexPositionTexture(new Vector3(Position.X + Width, Position.Y, Position.Z),
  119.                 new Vector2(((Width * frameNum) + Width) / Texture.Width, 0));
  120.             Vertices[2] = new VertexPositionTexture(new Vector3(Position.X, Position.Y + Height, Position.Z), new Vector2(0, ((Height * frameRow) + Height) / Texture.Height));
  121.             Vertices[3] = new VertexPositionTexture(new Vector3(Position.X + Width, Position.Y + Height, Position.Z),
  122.                 new Vector2(((Width * frameNum) + Width) / Texture.Width, ((Height * frameRow) + Height) / Texture.Height));
  123.         }
  124.  
  125.         public void Draw(GraphicsDevice GFX, ref Effect Effect)
  126.         {
  127.             Effect.CurrentTechnique = Effect.Techniques["BasicShader"];
  128.             Effect.Parameters["WorldView"].SetValue(Matrix.Identity * Camera.Instance.View);
  129.             Effect.Parameters["Projection"].SetValue(Camera.Instance.Projection);
  130.             foreach (EffectPass pass in Effect.CurrentTechnique.Passes)
  131.             {
  132.                 pass.Apply();
  133.                 GFX.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, Vertices, 0, 2);
  134.             }
  135.         }
  136.     }
  137.  
  138.     public sealed class Camera
  139.     {
  140.         private static readonly Camera instance = new Camera();
  141.         private static BasicEffect effect;
  142.         private static Viewport viewport;
  143.         private static Matrix orthographic;
  144.         private static Vector3 translatedModifier;
  145.         private float zoom;
  146.  
  147.         public static Camera Instance { get { return instance; } }
  148.         public void Initialize(GraphicsDevice gfx)
  149.         {
  150.             viewport = gfx.Viewport;
  151.             orthographic = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, 1);
  152.  
  153.             effect = new BasicEffect(gfx) { TextureEnabled = true, VertexColorEnabled = true };
  154.             effect.World = effect.View = Matrix.Identity;
  155.  
  156.             Zoom = 1f;
  157.  
  158.             translatedModifier = new Vector3(gfx.Viewport.Width / 2, gfx.Viewport.Height / 2, 0);
  159.         }
  160.  
  161.         public Effect Effect
  162.         {
  163.             get
  164.             {
  165.                 effect.Projection = Projection;
  166.                 return effect;
  167.             }
  168.         }
  169.         public Vector3 Position { set; get; }
  170.         public Matrix Projection { get { return Transformation * orthographic; } }
  171.         public float Rotation { get; set; }
  172.         public Matrix Transformation
  173.         {
  174.             get
  175.             {
  176.  
  177.                 return Matrix.CreateTranslation(new Vector3(-Position.X - .5f, -Position.Y - .5f, 0))
  178.                      * Matrix.CreateRotationY(Rotation)
  179.                      * Matrix.CreateScale(new Vector3(Zoom, Zoom, 0))
  180.                      * Matrix.CreateTranslation(translatedModifier);
  181.  
  182.             }
  183.         }
  184.         public Matrix View { get { return effect.View; } }
  185.         public float Zoom
  186.         {
  187.             get
  188.             {
  189.                 // Prevent flipping
  190.                 if (zoom < 0.01f) zoom = 0.01f;
  191.                 return zoom;
  192.             }
  193.             set
  194.             {
  195.                 zoom = value;
  196.             }
  197.         }
  198.  
  199.         public void Rotate(float radians)
  200.         {
  201.             Rotation = MathHelper.WrapAngle(Rotation + radians);
  202.         }
  203.  
  204.         public void Translate(Vector3 amount)
  205.         {
  206.             Position += amount;
  207.         }
  208.     }
  209. }
  210.  
  211.  
  212. /*****Billboard.fx*****
  213. float4x4 WorldView  : WorldView;
  214. float4x4 Projection : Projection;
  215.  
  216. texture texture0 : DIFFUSE <
  217.     string ResourceName = "default_color.dds";
  218.     string UIName =  "Diffuse Texture";
  219.     string ResourceType = "2D";
  220. >;
  221.  
  222. sampler2D colorMap = sampler_state
  223. {
  224.     Texture = <texture0>;
  225.     MagFilter = Linear;
  226.     MinFilter = Anisotropic;
  227.     MipFilter = Linear;
  228.     MaxAnisotropy = 16;
  229. };
  230.  
  231. struct VertexShaderInput
  232. {
  233.     float4 Position : POSITION0;
  234.     float2 TextureCoordinate : TEXCOORD0;
  235. };
  236.  
  237. struct VertexShaderOutput
  238. {
  239.     float4 Position : POSITION0;
  240.     float2 TextureCoordinate : TEXCOORD0;
  241. };
  242.  
  243. VertexShaderOutput VertexShaderFunction(VertexShaderInput IN)
  244. {
  245.     VertexShaderOutput OUT;
  246.     OUT.Position = mul(IN.Position + float4(WorldView[3].xyz, 0), Projection);
  247.     OUT.TextureCoordinate = IN.TextureCoordinate;
  248.  
  249.     return OUT;
  250. }
  251.  
  252. float4 PixelShaderFunction(VertexShaderOutput IN) : COLOR0
  253. {
  254.     return tex2D(colorMap, IN.TextureCoordinate);
  255. }
  256.  
  257. technique BasicShader
  258. {
  259.     pass Pass1
  260.     {
  261.         VertexShader = compile vs_2_0 VertexShaderFunction();
  262.         PixelShader = compile ps_2_0 PixelShaderFunction();
  263.     }
  264. }
  265. *****/
  266.