Advertisement
GeeckoDev

ClemG

Oct 17th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.10 KB | None | 0 0
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Game1.cs
  4. //
  5. // Clément Guérin
  6. // Built using MonoGame
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9.  
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Audio;
  15. using Microsoft.Xna.Framework.Content;
  16. using Microsoft.Xna.Framework.GamerServices;
  17. using Microsoft.Xna.Framework.Graphics;
  18. using Microsoft.Xna.Framework.Input;
  19. using Microsoft.Xna.Framework.Input.Touch;
  20. using Microsoft.Xna.Framework.Media;
  21. using System.IO;
  22.  
  23. namespace App
  24. {
  25.     /// <summary>
  26.     /// This is the main type for your game
  27.     /// </summary>
  28.     public class Game1 : Microsoft.Xna.Framework.Game
  29.     {
  30.         Matrix worldMatrix;
  31.         Matrix viewMatrix;
  32.         Matrix projectionMatrix;
  33.  
  34.         Vector3 up = Vector3.Up;
  35.         Vector3 forward = Vector3.Forward;
  36.         Vector3 right = Vector3.Right;
  37.  
  38.         Texture2D tex;
  39.  
  40.         BasicEffect basicEffect;
  41.  
  42.         List<VertexPositionNormalTexture> vertexList;
  43.         List<int> indexList;
  44.  
  45.         VertexBuffer vertexBuffer;
  46.  
  47.         RasterizerState rasterizerState;
  48.  
  49.         GraphicsDeviceManager graphics;
  50.  
  51.         public Game1()
  52.         {
  53.             graphics = new GraphicsDeviceManager(this);
  54.             graphics.PreferredBackBufferHeight = 800;
  55.             graphics.PreferredBackBufferWidth = 600;
  56.             Content.RootDirectory = "Content";
  57.         }
  58.  
  59.         /// <summary>
  60.         /// Allows the game to perform any initialization it needs to before starting to run.
  61.         /// This is where it can query for any required services and load any non-graphic
  62.         /// related content.  Calling base.Initialize will enumerate through any components
  63.         /// and initialize them as well.
  64.         /// </summary>
  65.         protected override void Initialize()
  66.         {
  67.             InitializeTransform();
  68.             InitializeEffect();
  69.             InitializePoints();
  70.             InitializeTriangleList();
  71.  
  72.             rasterizerState = new RasterizerState();
  73.             rasterizerState.FillMode = FillMode.Solid;
  74.             rasterizerState.CullMode = CullMode.CullClockwiseFace;
  75.  
  76.             base.Initialize();
  77.         }
  78.  
  79.         /// <summary>
  80.         /// Initializes the transforms used by the game.
  81.         /// </summary>
  82.         private void InitializeTransform()
  83.         {
  84.             viewMatrix = Matrix.CreateLookAt(
  85.                 new Vector3(0.0f, 0.0f, 4.0f),
  86.                 Vector3.Zero,
  87.                 Vector3.Up
  88.             );
  89.  
  90.             projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(90), 4.0f/3.0f, 1, 1000);
  91.         }
  92.  
  93.         /// <summary>
  94.         /// Initializes the effect (loading, parameter setting, and technique selection)
  95.         /// used by the game.
  96.         /// </summary>
  97.         private void InitializeEffect()
  98.         {
  99.             tex = Content.Load<Texture2D>("tex");
  100.  
  101.             worldMatrix = Matrix.Identity;
  102.  
  103.             basicEffect = new BasicEffect(GraphicsDevice);
  104.             basicEffect.EnableDefaultLighting();
  105.  
  106.             basicEffect.VertexColorEnabled = false;
  107.             basicEffect.TextureEnabled = true;
  108.             basicEffect.Texture = tex;
  109.             basicEffect.World = worldMatrix;
  110.             basicEffect.View = viewMatrix;
  111.             basicEffect.Projection = projectionMatrix;
  112.         }
  113.  
  114.         private Vector2 vecToNormal(Vector2 v)
  115.         {
  116.             return new Vector2(v.Y, -v.X);
  117.         }
  118.  
  119.         // Builds vertices for front and back faces of a character
  120.         private void buildFrontBack(List<VertexPositionNormalTexture> vertexList, List<Vector2> pointList, int quadCount)
  121.         {
  122.             Vector3 frontNormal = new Vector3(0, 0, 1);
  123.             Vector3 backNormal = new Vector3(0, 0, -1);
  124.  
  125.             List<Vector2> texCoords = new List<Vector2>();
  126.             texCoords.Add(new Vector2(0.0f, 0.0f));
  127.             texCoords.Add(new Vector2(1.0f, 0.0f));
  128.             texCoords.Add(new Vector2(0.0f, 1.0f));
  129.             texCoords.Add(new Vector2(1.0f, 1.0f));
  130.  
  131.             for (int i=0; i<quadCount; i++) {
  132.                 vertexList.Add(new VertexPositionNormalTexture(new Vector3(pointList[i], 1), frontNormal, texCoords[i%4]));
  133.             }
  134.             for (int i=0; i<quadCount; i++) {
  135.                 vertexList.Add(new VertexPositionNormalTexture(new Vector3(pointList[i], 0), backNormal, texCoords[i%4]));
  136.             }
  137.         }
  138.  
  139.         private void buildContour(List<VertexPositionNormalTexture> vertexList, List<Vector2> pointList, int quadCount)
  140.         {
  141.             int vi = 0;
  142.             int vj = 2;
  143.             int dir = 1;
  144.  
  145.             for (int i=0; i<quadCount; i++) {
  146.                 Vector3 faceNormal = new Vector3(vecToNormal(pointList[vj] - pointList[vi]), 0.0f);
  147.  
  148.                 vertexList.Add(new VertexPositionNormalTexture(new Vector3(pointList[vi], 1), faceNormal, new Vector2(0, 0)));
  149.                 vertexList.Add(new VertexPositionNormalTexture(new Vector3(pointList[vi], 0), faceNormal, new Vector2(0, 0)));
  150.                 vertexList.Add(new VertexPositionNormalTexture(new Vector3(pointList[vj], 1), faceNormal, new Vector2(0, 0)));
  151.                 vertexList.Add(new VertexPositionNormalTexture(new Vector3(pointList[vj], 0), faceNormal, new Vector2(0, 0)));
  152.  
  153.                 // This is the pattern! For example, take four 2d points named 0, 1, 2, 3.
  154.                 // Generated contour: (0 2), (2 3), (3 1), (1 0). Here, each pair is (vi vj).
  155.                 vi = vj;
  156.                 vj += 2*dir;
  157.                 if (vj >= quadCount) {
  158.                     vj--;
  159.                     dir = -1;
  160.                 }
  161.                 if (vj < 0) {
  162.                     vj = 0;
  163.                 }
  164.             }
  165.         }
  166.  
  167.         /// <summary>
  168.         /// Initializes the point list.
  169.         /// </summary>
  170.         private void InitializePoints()
  171.         {
  172.             // Build temporary point list
  173.             List<Vector2> pointList = new List<Vector2>();
  174.             // Common for C and G
  175.             pointList.Add(new Vector2(0.0f, 1.2f));
  176.             pointList.Add(new Vector2(0.2f, 1.0f));
  177.             pointList.Add(new Vector2(-1.2f, 0.3f));
  178.             pointList.Add(new Vector2(-0.8f, 0.3f));
  179.             pointList.Add(new Vector2(-1.2f, -1.0f));
  180.             pointList.Add(new Vector2(-0.8f, -0.8f));
  181.             pointList.Add(new Vector2(0.5f, -1.2f));
  182.             pointList.Add(new Vector2(0.3f, -1.0f));
  183.             // Rest of G
  184.             pointList.Add(new Vector2(0.8f, -0.7f));
  185.             pointList.Add(new Vector2(0.6f, -0.7f));
  186.             pointList.Add(new Vector2(0.4f, -0.4f));
  187.             pointList.Add(new Vector2(0.2f, -0.5f));
  188.  
  189.             vertexList = new List<VertexPositionNormalTexture>();
  190.  
  191.             // Build the C character
  192.             buildFrontBack(vertexList, pointList, 8);
  193.             buildContour(vertexList, pointList, 8);
  194.  
  195.             // Translate each point on horizontal axis to not overlap
  196.             for (int i=0; i<pointList.Count; i++) {
  197.                 pointList[i] += new Vector2(1.9f, 0.0f);
  198.             }
  199.  
  200.             // Build the G character
  201.             buildFrontBack(vertexList, pointList, 12);
  202.             buildContour(vertexList, pointList, 12);
  203.  
  204.             // Initialize the vertex buffer, allocating memory for each vertex.
  205.             vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, VertexPositionNormalTexture.VertexDeclaration,
  206.                                             vertexList.Count, BufferUsage.None);
  207.  
  208.             // Set the vertex buffer data to the array of vertices.
  209.             vertexBuffer.SetData<VertexPositionNormalTexture>(vertexList.ToArray());
  210.         }
  211.  
  212.  
  213.  
  214.         /// <summary>
  215.         /// Initializes the triangle list.
  216.         /// </summary>
  217.         private void InitializeTriangleList()
  218.         {
  219.             int shift = 0;
  220.             indexList = new List<int>();
  221.  
  222.             //// C
  223.             // front (counter-clockwise)
  224.             for (int i=0; i<3; i++) {
  225.                 indexList.Add (shift+2*i+0);    indexList.Add (shift+2*i+2);    indexList.Add (shift+2*i+1);
  226.                 indexList.Add (shift+2*i+1);    indexList.Add (shift+2*i+2);    indexList.Add (shift+2*i+3);
  227.             }
  228.             shift += 8;
  229.             // back (clockwise)
  230.             for (int i=0; i<3; i++) {
  231.                 indexList.Add (shift+2*i+0);    indexList.Add (shift+2*i+1);    indexList.Add (shift+2*i+2);
  232.                 indexList.Add (shift+2*i+2);    indexList.Add (shift+2*i+1);    indexList.Add (shift+2*i+3);
  233.             }
  234.             shift += 8;
  235.             // contour
  236.             for (int i=0; i<8; i++) {
  237.                 indexList.Add (shift+4*i+0);    indexList.Add (shift+4*i+1);    indexList.Add (shift+4*i+2);
  238.                 indexList.Add (shift+4*i+2);    indexList.Add (shift+4*i+1);    indexList.Add (shift+4*i+3);
  239.             }
  240.             shift += 8 * 4;
  241.  
  242.             //// G
  243.             // front (counter-clockwise)
  244.             for (int i=0; i<5; i++) {
  245.                 indexList.Add (shift+2*i+0);    indexList.Add (shift+2*i+2);    indexList.Add (shift+2*i+1);
  246.                 indexList.Add (shift+2*i+1);    indexList.Add (shift+2*i+2);    indexList.Add (shift+2*i+3);
  247.             }
  248.             shift += 12;
  249.             // back (clockwise)
  250.             for (int i=0; i<5; i++) {
  251.                 indexList.Add (shift+2*i+0);    indexList.Add (shift+2*i+1);    indexList.Add (shift+2*i+2);
  252.                 indexList.Add (shift+2*i+2);    indexList.Add (shift+2*i+1);    indexList.Add (shift+2*i+3);
  253.             }
  254.             shift += 12;
  255.             // contour
  256.             for (int i=0; i<12; i++) {
  257.                 indexList.Add (shift+4*i+0);    indexList.Add (shift+4*i+1);    indexList.Add (shift+4*i+2);
  258.                 indexList.Add (shift+4*i+2);    indexList.Add (shift+4*i+1);    indexList.Add (shift+4*i+3);
  259.             }
  260.             shift += 12 * 4;
  261.         }
  262.  
  263.         /// <summary>
  264.         /// Allows the game to run logic such as updating the world,
  265.         /// checking for collisions, gathering input, and playing audio.
  266.         /// </summary>
  267.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  268.         protected override void Update(GameTime gameTime)
  269.         {
  270.             Matrix m = Matrix.CreateFromAxisAngle(Vector3.Normalize(new Vector3(3,4,5)), 0.02f);
  271.  
  272.             up = Vector3.Transform(up, m);
  273.             right = Vector3.Transform(right, m);
  274.             forward = Vector3.Transform(forward, m);
  275.  
  276.             // Allows the game to exit
  277.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  278.                 this.Exit();
  279.  
  280.             base.Update(gameTime);
  281.         }
  282.  
  283.         /// <summary>
  284.         /// This is called when the game should draw itself.
  285.         /// </summary>
  286.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  287.         protected override void Draw(GameTime gameTime)
  288.         {
  289.             GraphicsDevice.Clear(Color.SteelBlue);
  290.  
  291.             // The effect is a compiled effect created and compiled elsewhere
  292.             // in the application.
  293.             basicEffect.World = Matrix.CreateWorld(Vector3.Zero, forward, up);
  294.  
  295.             foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
  296.             {
  297.                 pass.Apply();
  298.  
  299.                 GraphicsDevice.RasterizerState = rasterizerState;
  300.                 DrawTriangleList();
  301.             }
  302.  
  303.             base.Draw(gameTime);
  304.         }
  305.  
  306.         /// <summary>
  307.         /// Draws the triangle list.
  308.         /// </summary>
  309.         private void DrawTriangleList()
  310.         {
  311.             GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(
  312.                 PrimitiveType.TriangleList,
  313.                 vertexList.ToArray(),
  314.                 0,   // vertex buffer offset to add to each element of the index buffer
  315.                 vertexList.Count(),   // number of vertices to draw
  316.                 indexList.ToArray(),
  317.                 0,   // first index element to read
  318.                 indexList.Count()/3    // number of primitives to draw
  319.             );
  320.         }
  321.     }
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement