Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region File Description
- //-----------------------------------------------------------------------------
- // Game1.cs
- //
- // Clément Guérin
- // Built using MonoGame
- //-----------------------------------------------------------------------------
- #endregion
- using System;
- using System.Collections.Generic;
- using System.Linq;
- 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.Input.Touch;
- using Microsoft.Xna.Framework.Media;
- using System.IO;
- namespace App
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- Matrix worldMatrix;
- Matrix viewMatrix;
- Matrix projectionMatrix;
- Vector3 up = Vector3.Up;
- Vector3 forward = Vector3.Forward;
- Vector3 right = Vector3.Right;
- Texture2D tex;
- BasicEffect basicEffect;
- List<VertexPositionNormalTexture> vertexList;
- List<int> indexList;
- VertexBuffer vertexBuffer;
- RasterizerState rasterizerState;
- GraphicsDeviceManager graphics;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- graphics.PreferredBackBufferHeight = 800;
- graphics.PreferredBackBufferWidth = 600;
- 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()
- {
- InitializeTransform();
- InitializeEffect();
- InitializePoints();
- InitializeTriangleList();
- rasterizerState = new RasterizerState();
- rasterizerState.FillMode = FillMode.Solid;
- rasterizerState.CullMode = CullMode.CullClockwiseFace;
- base.Initialize();
- }
- /// <summary>
- /// Initializes the transforms used by the game.
- /// </summary>
- private void InitializeTransform()
- {
- viewMatrix = Matrix.CreateLookAt(
- new Vector3(0.0f, 0.0f, 4.0f),
- Vector3.Zero,
- Vector3.Up
- );
- projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(90), 4.0f/3.0f, 1, 1000);
- }
- /// <summary>
- /// Initializes the effect (loading, parameter setting, and technique selection)
- /// used by the game.
- /// </summary>
- private void InitializeEffect()
- {
- tex = Content.Load<Texture2D>("tex");
- worldMatrix = Matrix.Identity;
- basicEffect = new BasicEffect(GraphicsDevice);
- basicEffect.EnableDefaultLighting();
- basicEffect.VertexColorEnabled = false;
- basicEffect.TextureEnabled = true;
- basicEffect.Texture = tex;
- basicEffect.World = worldMatrix;
- basicEffect.View = viewMatrix;
- basicEffect.Projection = projectionMatrix;
- }
- private Vector2 vecToNormal(Vector2 v)
- {
- return new Vector2(v.Y, -v.X);
- }
- // Builds vertices for front and back faces of a character
- private void buildFrontBack(List<VertexPositionNormalTexture> vertexList, List<Vector2> pointList, int quadCount)
- {
- Vector3 frontNormal = new Vector3(0, 0, 1);
- Vector3 backNormal = new Vector3(0, 0, -1);
- List<Vector2> texCoords = new List<Vector2>();
- texCoords.Add(new Vector2(0.0f, 0.0f));
- texCoords.Add(new Vector2(1.0f, 0.0f));
- texCoords.Add(new Vector2(0.0f, 1.0f));
- texCoords.Add(new Vector2(1.0f, 1.0f));
- for (int i=0; i<quadCount; i++) {
- vertexList.Add(new VertexPositionNormalTexture(new Vector3(pointList[i], 1), frontNormal, texCoords[i%4]));
- }
- for (int i=0; i<quadCount; i++) {
- vertexList.Add(new VertexPositionNormalTexture(new Vector3(pointList[i], 0), backNormal, texCoords[i%4]));
- }
- }
- private void buildContour(List<VertexPositionNormalTexture> vertexList, List<Vector2> pointList, int quadCount)
- {
- int vi = 0;
- int vj = 2;
- int dir = 1;
- for (int i=0; i<quadCount; i++) {
- Vector3 faceNormal = new Vector3(vecToNormal(pointList[vj] - pointList[vi]), 0.0f);
- vertexList.Add(new VertexPositionNormalTexture(new Vector3(pointList[vi], 1), faceNormal, new Vector2(0, 0)));
- vertexList.Add(new VertexPositionNormalTexture(new Vector3(pointList[vi], 0), faceNormal, new Vector2(0, 0)));
- vertexList.Add(new VertexPositionNormalTexture(new Vector3(pointList[vj], 1), faceNormal, new Vector2(0, 0)));
- vertexList.Add(new VertexPositionNormalTexture(new Vector3(pointList[vj], 0), faceNormal, new Vector2(0, 0)));
- // This is the pattern! For example, take four 2d points named 0, 1, 2, 3.
- // Generated contour: (0 2), (2 3), (3 1), (1 0). Here, each pair is (vi vj).
- vi = vj;
- vj += 2*dir;
- if (vj >= quadCount) {
- vj--;
- dir = -1;
- }
- if (vj < 0) {
- vj = 0;
- }
- }
- }
- /// <summary>
- /// Initializes the point list.
- /// </summary>
- private void InitializePoints()
- {
- // Build temporary point list
- List<Vector2> pointList = new List<Vector2>();
- // Common for C and G
- pointList.Add(new Vector2(0.0f, 1.2f));
- pointList.Add(new Vector2(0.2f, 1.0f));
- pointList.Add(new Vector2(-1.2f, 0.3f));
- pointList.Add(new Vector2(-0.8f, 0.3f));
- pointList.Add(new Vector2(-1.2f, -1.0f));
- pointList.Add(new Vector2(-0.8f, -0.8f));
- pointList.Add(new Vector2(0.5f, -1.2f));
- pointList.Add(new Vector2(0.3f, -1.0f));
- // Rest of G
- pointList.Add(new Vector2(0.8f, -0.7f));
- pointList.Add(new Vector2(0.6f, -0.7f));
- pointList.Add(new Vector2(0.4f, -0.4f));
- pointList.Add(new Vector2(0.2f, -0.5f));
- vertexList = new List<VertexPositionNormalTexture>();
- // Build the C character
- buildFrontBack(vertexList, pointList, 8);
- buildContour(vertexList, pointList, 8);
- // Translate each point on horizontal axis to not overlap
- for (int i=0; i<pointList.Count; i++) {
- pointList[i] += new Vector2(1.9f, 0.0f);
- }
- // Build the G character
- buildFrontBack(vertexList, pointList, 12);
- buildContour(vertexList, pointList, 12);
- // Initialize the vertex buffer, allocating memory for each vertex.
- vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, VertexPositionNormalTexture.VertexDeclaration,
- vertexList.Count, BufferUsage.None);
- // Set the vertex buffer data to the array of vertices.
- vertexBuffer.SetData<VertexPositionNormalTexture>(vertexList.ToArray());
- }
- /// <summary>
- /// Initializes the triangle list.
- /// </summary>
- private void InitializeTriangleList()
- {
- int shift = 0;
- indexList = new List<int>();
- //// C
- // front (counter-clockwise)
- for (int i=0; i<3; i++) {
- indexList.Add (shift+2*i+0); indexList.Add (shift+2*i+2); indexList.Add (shift+2*i+1);
- indexList.Add (shift+2*i+1); indexList.Add (shift+2*i+2); indexList.Add (shift+2*i+3);
- }
- shift += 8;
- // back (clockwise)
- for (int i=0; i<3; i++) {
- indexList.Add (shift+2*i+0); indexList.Add (shift+2*i+1); indexList.Add (shift+2*i+2);
- indexList.Add (shift+2*i+2); indexList.Add (shift+2*i+1); indexList.Add (shift+2*i+3);
- }
- shift += 8;
- // contour
- for (int i=0; i<8; i++) {
- indexList.Add (shift+4*i+0); indexList.Add (shift+4*i+1); indexList.Add (shift+4*i+2);
- indexList.Add (shift+4*i+2); indexList.Add (shift+4*i+1); indexList.Add (shift+4*i+3);
- }
- shift += 8 * 4;
- //// G
- // front (counter-clockwise)
- for (int i=0; i<5; i++) {
- indexList.Add (shift+2*i+0); indexList.Add (shift+2*i+2); indexList.Add (shift+2*i+1);
- indexList.Add (shift+2*i+1); indexList.Add (shift+2*i+2); indexList.Add (shift+2*i+3);
- }
- shift += 12;
- // back (clockwise)
- for (int i=0; i<5; i++) {
- indexList.Add (shift+2*i+0); indexList.Add (shift+2*i+1); indexList.Add (shift+2*i+2);
- indexList.Add (shift+2*i+2); indexList.Add (shift+2*i+1); indexList.Add (shift+2*i+3);
- }
- shift += 12;
- // contour
- for (int i=0; i<12; i++) {
- indexList.Add (shift+4*i+0); indexList.Add (shift+4*i+1); indexList.Add (shift+4*i+2);
- indexList.Add (shift+4*i+2); indexList.Add (shift+4*i+1); indexList.Add (shift+4*i+3);
- }
- shift += 12 * 4;
- }
- /// <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)
- {
- Matrix m = Matrix.CreateFromAxisAngle(Vector3.Normalize(new Vector3(3,4,5)), 0.02f);
- up = Vector3.Transform(up, m);
- right = Vector3.Transform(right, m);
- forward = Vector3.Transform(forward, m);
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- 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)
- {
- GraphicsDevice.Clear(Color.SteelBlue);
- // The effect is a compiled effect created and compiled elsewhere
- // in the application.
- basicEffect.World = Matrix.CreateWorld(Vector3.Zero, forward, up);
- foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
- {
- pass.Apply();
- GraphicsDevice.RasterizerState = rasterizerState;
- DrawTriangleList();
- }
- base.Draw(gameTime);
- }
- /// <summary>
- /// Draws the triangle list.
- /// </summary>
- private void DrawTriangleList()
- {
- GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(
- PrimitiveType.TriangleList,
- vertexList.ToArray(),
- 0, // vertex buffer offset to add to each element of the index buffer
- vertexList.Count(), // number of vertices to draw
- indexList.ToArray(),
- 0, // first index element to read
- indexList.Count()/3 // number of primitives to draw
- );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement