Advertisement
Sooldierr

Grafika3D - Heksaedr

Nov 15th, 2017
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.43 KB | None | 0 0
  1. http://www78.zippyshare.com/v/ToTOxHJc/file.html
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Audio;
  8. using Microsoft.Xna.Framework.Content;
  9. using Microsoft.Xna.Framework.GamerServices;
  10. using Microsoft.Xna.Framework.Graphics;
  11. using Microsoft.Xna.Framework.Input;
  12. using Microsoft.Xna.Framework.Media;
  13.  
  14. namespace Grafika3D_Hexaedr
  15. {
  16.     /// <summary>
  17.     /// This is the main type for your game
  18.     /// </summary>
  19.     public class Game1 : Microsoft.Xna.Framework.Game
  20.     {
  21.         GraphicsDeviceManager graphics;
  22.         SpriteBatch spriteBatch;
  23.  
  24.         #region Deklaracja potrzebnych zmiennych
  25.         Matrix worldMatrix, viewMatrix, projectionMatrix;       // Macierze: świata, widoku, projekcji
  26.         BasicEffect basicEffect;                                // klasa implementująca odpowiednie programy
  27.         VertexPositionColor[] userPrimitives;                   // tablica przechowująca geometrię modelu
  28.         float angleX = 0.0f, angleY = 0.0f;                     // zmienne położenia kamery obserwatora
  29.         #endregion
  30.         int frameUpdateTime = 50;
  31.         int elapsedUpdateTime = 0;
  32.         public Game1()
  33.         {
  34.             graphics = new GraphicsDeviceManager(this);
  35.             Content.RootDirectory = "Content";
  36.             this.IsMouseVisible = true;
  37.             this.Window.AllowUserResizing = true;
  38.         }
  39.  
  40.         /// <summary>
  41.         /// Allows the game to perform any initialization it needs to before starting to run.
  42.         /// This is where it can query for any required services and load any non-graphic
  43.         /// related content.  Calling base.Initialize will enumerate through any components
  44.         /// and initialize them as well.
  45.         /// </summary>
  46.         protected override void Initialize()
  47.         {
  48.             // TODO: Add your initialization logic here
  49.  
  50.             base.Initialize();
  51.         }
  52.  
  53.         /// <summary>
  54.         /// LoadContent will be called once per game and is the place to load
  55.         /// all of your content.
  56.         /// </summary>
  57.         protected override void LoadContent()
  58.         {
  59.             // Create a new SpriteBatch, which can be used to draw textures.
  60.             spriteBatch = new SpriteBatch(GraphicsDevice);
  61.  
  62.             #region Tworzenie geometrii modelu
  63.             basicEffect = new BasicEffect(GraphicsDevice);
  64.             basicEffect.VertexColorEnabled = true;
  65.             userPrimitives = new VertexPositionColor[18];
  66.  
  67.             #region
  68.             //ściana przednia
  69.                 //Trójkąt dolny
  70.             userPrimitives[0] = new VertexPositionColor(new Vector3(-1, -1, 1), Color.Blue);
  71.             userPrimitives[1] = new VertexPositionColor(new Vector3(-1, 1, 1), Color.Blue);
  72.             userPrimitives[2] = new VertexPositionColor(new Vector3(1, -1, 1), Color.Blue);
  73.  
  74.                 //Trójkąt górny
  75.             userPrimitives[3] = new VertexPositionColor(new Vector3(-1, 1, 1), Color.Blue);
  76.             userPrimitives[4] = new VertexPositionColor(new Vector3(1, 1, 1), Color.Blue);
  77.             userPrimitives[5] = new VertexPositionColor(new Vector3(1, -1, 1), Color.Blue);
  78.             #endregion
  79.  
  80.             //ściana lewa
  81.                 //Trójkąt dolny
  82.             userPrimitives[12] = new VertexPositionColor(new Vector3(-1, -1, -1), Color.Yellow);
  83.             userPrimitives[13] = new VertexPositionColor(new Vector3(-1, -1, 1), Color.Yellow);
  84.             userPrimitives[14] = new VertexPositionColor(new Vector3(-1, 1, 1), Color.Yellow);
  85.  
  86.             //Trójkąt górny
  87.             userPrimitives[15] = new VertexPositionColor(new Vector3(-1, 1, 1), Color.Yellow);
  88.             userPrimitives[16] = new VertexPositionColor(new Vector3(-1, 1, -1), Color.Yellow);
  89.             userPrimitives[17] = new VertexPositionColor(new Vector3(-1, -1, -1), Color.Yellow);
  90.  
  91.  
  92.             //ściana prawa
  93.                 //Trójkąt dolny
  94.             userPrimitives[6] = new VertexPositionColor(new Vector3(1, 1, -1), Color.Green);
  95.             userPrimitives[7] = new VertexPositionColor(new Vector3(1, -1, -1), Color.Green);
  96.             userPrimitives[8] = new VertexPositionColor(new Vector3(1, -1, 1), Color.Green);
  97.  
  98.                 //Trójkąt górny
  99.             userPrimitives[9]  = new VertexPositionColor(new Vector3(1, 1, 1), Color.Green);
  100.             userPrimitives[10] = new VertexPositionColor(new Vector3(1, 1, -1), Color.Green);
  101.             userPrimitives[11] = new VertexPositionColor(new Vector3(1, -1, 1), Color.Green);
  102.  
  103.  
  104.             //ściana tylna
  105.                 //Trójkąt dolny
  106.             userPrimitives[12] = new VertexPositionColor(new Vector3(1, 1, -1), Color.Red);
  107.             userPrimitives[13] = new VertexPositionColor(new Vector3(1, -1, -1), Color.Red);
  108.             userPrimitives[14] = new VertexPositionColor(new Vector3(-1, 1, -1), Color.Red);
  109.  
  110.             //Trójkąt górny
  111.             userPrimitives[15] = new VertexPositionColor(new Vector3(1, -1, -1), Color.Red);
  112.             userPrimitives[16] = new VertexPositionColor(new Vector3(-1, -1, -1), Color.Red);
  113.             userPrimitives[17] = new VertexPositionColor(new Vector3(-1, 1, -1), Color.Red);
  114.  
  115.  
  116.  
  117.             #endregion
  118.         }
  119.  
  120.         /// <summary>
  121.         /// UnloadContent will be called once per game and is the place to unload
  122.         /// all content.
  123.         /// </summary>
  124.         protected override void UnloadContent()
  125.         {
  126.             // TODO: Unload any non ContentManager content here
  127.         }
  128.  
  129.         /// <summary>
  130.         /// Allows the game to run logic such as updating the world,
  131.         /// checking for collisions, gathering input, and playing audio.
  132.         /// </summary>
  133.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  134.         protected override void Update(GameTime gameTime)
  135.         {
  136.             var keyboard = Keyboard.GetState();     //zmienna pomocnicza
  137.             elapsedUpdateTime += gameTime.ElapsedGameTime.Milliseconds;
  138.             // Allows the game to exit
  139.             if (keyboard.IsKeyDown(Keys.Escape))
  140.                 this.Exit();
  141.  
  142.             if (elapsedUpdateTime >= frameUpdateTime)
  143.             {
  144.                 elapsedUpdateTime = 0;
  145.                 // modyfikacja wartości kątów odchylenia
  146.                 if (keyboard.IsKeyDown(Keys.Right))
  147.                     angleY += 0.2f;
  148.                 if (keyboard.IsKeyDown(Keys.Left))
  149.                     angleY -= 0.2f;
  150.                 if (keyboard.IsKeyDown(Keys.Up))
  151.                     angleX += 0.2f;
  152.                 if (keyboard.IsKeyDown(Keys.Down))
  153.                     angleX -= 0.2f;
  154.             }
  155.             #region
  156.             //Tworzenie Macierzy świata, widoku i projekcji
  157.             worldMatrix = Matrix.Identity;
  158.             viewMatrix = Matrix.CreateLookAt(
  159.                 new Vector3(0.0f, 0.0f, 5.0f),  // położenie kamery (jak na rysunku)
  160.                 Vector3.Zero, Vector3.Up);      // cel i orientacja kamery
  161.             projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(50),
  162.                 graphics.GraphicsDevice.Viewport.AspectRatio, 0.2f, 1000.0f);
  163.             #endregion
  164.  
  165.             // realizacja obrotu kamery
  166.             viewMatrix = Matrix.CreateRotationX(angleX) * Matrix.CreateRotationY(angleY) * viewMatrix;
  167.  
  168.             // aktualizacja parametrów shadera
  169.             basicEffect.World = worldMatrix;
  170.             basicEffect.View = viewMatrix;
  171.             basicEffect.Projection = projectionMatrix;
  172.            
  173.  
  174.             base.Update(gameTime);
  175.         }
  176.  
  177.         /// <summary>
  178.         /// This is called when the game should draw itself.
  179.         /// </summary>
  180.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  181.         protected override void Draw(GameTime gameTime)
  182.         {
  183.             GraphicsDevice.Clear(Color.CornflowerBlue);
  184.  
  185.             #region Wyświetlanie sceny
  186.             basicEffect.CurrentTechnique.Passes[0].Apply(); // "uruchamia" shader
  187.             GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList,
  188.                 userPrimitives,     // referencja do tablicy wierzchołków
  189.                 0,                  // offset pierwszego wierzchołka
  190.                 userPrimitives.Length / 3);                 // liczba prymitywów (tutaj trójkątów) do wyświetlenia
  191.             #endregion
  192.  
  193.  
  194.             base.Draw(gameTime);
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement