This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Quad Billboard Problem

By: Neurotrace on Oct 27th, 2011  |  syntax: C#  |  size: 9.39 KB  |  views: 87  |  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["CylBillboard"];
  128.             Effect.Parameters["xView"].SetValue(Camera.Instance.View);
  129.             Effect.Parameters["xProjection"].SetValue(Camera.Instance.Projection);
  130.             Effect.Parameters["xWorld"].SetValue(Matrix.Identity);
  131.             Effect.Parameters["xCamPos"].SetValue(Camera.Instance.Position);
  132.             Effect.Parameters["xAllowedRotDir"].SetValue(Vector3.Up);
  133.             Effect.Parameters["xBottomRightUV"].SetValue(Vertices[3].TextureCoordinate);
  134.             Effect.Parameters["xBillboardTexture"].SetValue(Texture);
  135.             foreach (EffectPass pass in Effect.CurrentTechnique.Passes)
  136.             {
  137.                 pass.Apply();
  138.                 GFX.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, Vertices, 0, 2);
  139.             }
  140.         }
  141.     }
  142.  
  143.     public sealed class Camera
  144.     {
  145.         private static readonly Camera instance = new Camera();
  146.         private static BasicEffect effect;
  147.         private static Viewport viewport;
  148.         private static Matrix orthographic;
  149.         private static Vector3 translatedModifier;
  150.         private float zoom;
  151.  
  152.         public static Camera Instance { get { return instance; } }
  153.         public void Initialize(GraphicsDevice gfx)
  154.         {
  155.             viewport = gfx.Viewport;
  156.             orthographic = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, 1);
  157.  
  158.             effect = new BasicEffect(gfx) { TextureEnabled = true, VertexColorEnabled = true };
  159.             effect.World = effect.View = Matrix.Identity;
  160.  
  161.             Zoom = 1f;
  162.  
  163.             translatedModifier = new Vector3(gfx.Viewport.Width / 2, gfx.Viewport.Height / 2, 0);
  164.         }
  165.  
  166.         public Effect Effect
  167.         {
  168.             get
  169.             {
  170.                 effect.Projection = Projection;
  171.                 return effect;
  172.             }
  173.         }
  174.         public Vector3 Position { set; get; }
  175.         public Matrix Projection { get { return Transformation * orthographic; } }
  176.         public float Rotation { get; set; }
  177.         public Matrix Transformation
  178.         {
  179.             get
  180.             {
  181.  
  182.                 return Matrix.CreateTranslation(new Vector3(-Position.X - .5f, -Position.Y - .5f, 0))
  183.                      * Matrix.CreateRotationY(Rotation)
  184.                      * Matrix.CreateScale(new Vector3(Zoom, Zoom, 0))
  185.                      * Matrix.CreateTranslation(translatedModifier);
  186.  
  187.             }
  188.         }
  189.         public Matrix View { get { return effect.View; } }
  190.         public float Zoom
  191.         {
  192.             get
  193.             {
  194.                 // Prevent flipping
  195.                 if (zoom < 0.01f) zoom = 0.01f;
  196.                 return zoom;
  197.             }
  198.             set
  199.             {
  200.                 zoom = value;
  201.             }
  202.         }
  203.  
  204.         public void Rotate(float radians)
  205.         {
  206.             Rotation = MathHelper.WrapAngle(Rotation + radians);
  207.         }
  208.  
  209.         public void Translate(Vector3 amount)
  210.         {
  211.             Position += amount;
  212.         }
  213.     }
  214. }
  215.  
  216. /**********Billboard.fx***********
  217. //------- XNA interface --------
  218. float4x4 xView;
  219. float4x4 xProjection;
  220. float4x4 xWorld;
  221. float3 xCamPos;
  222. float3 xAllowedRotDir;
  223. float2 xBottomRightUV;
  224.  
  225. //------- Texture Samplers --------
  226. Texture xBillboardTexture;
  227. sampler textureSampler = sampler_state { texture = <xBillboardTexture> ; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = CLAMP; AddressV = CLAMP;};
  228.  
  229. struct BBVertexToPixel
  230. {
  231.         float4 Position : POSITION;
  232.         float2 TexCoord : TEXCOORD0;
  233. };
  234. struct BBPixelToFrame
  235. {
  236.     float4 Color        : COLOR0;
  237. };
  238.  
  239. //------- Technique: CylBillboard --------
  240. BBVertexToPixel CylBillboardVS(float3 inPos: POSITION0, float2 inTexCoord: TEXCOORD0)
  241. {
  242.         BBVertexToPixel Output = (BBVertexToPixel)0;   
  243.  
  244.         float3 center = mul(inPos, xWorld);
  245.         float3 eyeVector = center - xCamPos;   
  246.        
  247.         float3 upVector = xAllowedRotDir;
  248.         //upVector = normalize(upVector);
  249.         float3 sideVector = cross(eyeVector,upVector);
  250.         sideVector = normalize(sideVector);
  251.        
  252.         float3 finalPosition = center;
  253.         float2 adjustment;
  254.         if(inTexCoord.x < xBottomRightUV.x) {
  255.                 adjustment.x = 0;
  256.         } else {
  257.                 adjustment.x = 1.0f;
  258.         }
  259.  
  260.         if(inTexCoord.y < xBottomRightUV.y) {
  261.                 adjustment.y = 0;
  262.         } else {
  263.                 adjustment.y = 1.0f;
  264.         }
  265.  
  266.         finalPosition += (adjustment.x-0.5f)*sideVector;
  267.         finalPosition += (1.5f-adjustment.y*1.5f)*upVector;    
  268.        
  269.         float4 finalPosition4 = float4(finalPosition, 1);
  270.                
  271.         float4x4 preViewProjection = mul (xView, xProjection);
  272.         Output.Position = mul(finalPosition4, preViewProjection);
  273.        
  274.         Output.TexCoord = inTexCoord;
  275.        
  276.         return Output;
  277. }
  278.  
  279. BBPixelToFrame BillboardPS(BBVertexToPixel PSIn)
  280. {
  281.         BBPixelToFrame Output = (BBPixelToFrame)0;             
  282.         Output.Color = tex2D(textureSampler, PSIn.TexCoord);
  283.        
  284.         return Output;
  285. }
  286.  
  287. technique CylBillboard
  288. {
  289.         pass Pass0
  290.     {          
  291.         VertexShader = compile vs_2_0 CylBillboardVS();
  292.         PixelShader  = compile ps_2_0 BillboardPS();        
  293.     }
  294. }
  295. ***/
  296.  
clone this paste RAW Paste Data