Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- http://www78.zippyshare.com/v/ToTOxHJc/file.html
- 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.Media;
- namespace Grafika3D_Hexaedr
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- #region Deklaracja potrzebnych zmiennych
- Matrix worldMatrix, viewMatrix, projectionMatrix; // Macierze: świata, widoku, projekcji
- BasicEffect basicEffect; // klasa implementująca odpowiednie programy
- VertexPositionColor[] userPrimitives; // tablica przechowująca geometrię modelu
- float angleX = 0.0f, angleY = 0.0f; // zmienne położenia kamery obserwatora
- #endregion
- int frameUpdateTime = 50;
- int elapsedUpdateTime = 0;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- this.IsMouseVisible = true;
- this.Window.AllowUserResizing = true;
- }
- /// <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()
- {
- // TODO: Add your initialization logic here
- base.Initialize();
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- #region Tworzenie geometrii modelu
- basicEffect = new BasicEffect(GraphicsDevice);
- basicEffect.VertexColorEnabled = true;
- userPrimitives = new VertexPositionColor[18];
- #region
- //ściana przednia
- //Trójkąt dolny
- userPrimitives[0] = new VertexPositionColor(new Vector3(-1, -1, 1), Color.Blue);
- userPrimitives[1] = new VertexPositionColor(new Vector3(-1, 1, 1), Color.Blue);
- userPrimitives[2] = new VertexPositionColor(new Vector3(1, -1, 1), Color.Blue);
- //Trójkąt górny
- userPrimitives[3] = new VertexPositionColor(new Vector3(-1, 1, 1), Color.Blue);
- userPrimitives[4] = new VertexPositionColor(new Vector3(1, 1, 1), Color.Blue);
- userPrimitives[5] = new VertexPositionColor(new Vector3(1, -1, 1), Color.Blue);
- #endregion
- //ściana lewa
- //Trójkąt dolny
- userPrimitives[12] = new VertexPositionColor(new Vector3(-1, -1, -1), Color.Yellow);
- userPrimitives[13] = new VertexPositionColor(new Vector3(-1, -1, 1), Color.Yellow);
- userPrimitives[14] = new VertexPositionColor(new Vector3(-1, 1, 1), Color.Yellow);
- //Trójkąt górny
- userPrimitives[15] = new VertexPositionColor(new Vector3(-1, 1, 1), Color.Yellow);
- userPrimitives[16] = new VertexPositionColor(new Vector3(-1, 1, -1), Color.Yellow);
- userPrimitives[17] = new VertexPositionColor(new Vector3(-1, -1, -1), Color.Yellow);
- //ściana prawa
- //Trójkąt dolny
- userPrimitives[6] = new VertexPositionColor(new Vector3(1, 1, -1), Color.Green);
- userPrimitives[7] = new VertexPositionColor(new Vector3(1, -1, -1), Color.Green);
- userPrimitives[8] = new VertexPositionColor(new Vector3(1, -1, 1), Color.Green);
- //Trójkąt górny
- userPrimitives[9] = new VertexPositionColor(new Vector3(1, 1, 1), Color.Green);
- userPrimitives[10] = new VertexPositionColor(new Vector3(1, 1, -1), Color.Green);
- userPrimitives[11] = new VertexPositionColor(new Vector3(1, -1, 1), Color.Green);
- //ściana tylna
- //Trójkąt dolny
- userPrimitives[12] = new VertexPositionColor(new Vector3(1, 1, -1), Color.Red);
- userPrimitives[13] = new VertexPositionColor(new Vector3(1, -1, -1), Color.Red);
- userPrimitives[14] = new VertexPositionColor(new Vector3(-1, 1, -1), Color.Red);
- //Trójkąt górny
- userPrimitives[15] = new VertexPositionColor(new Vector3(1, -1, -1), Color.Red);
- userPrimitives[16] = new VertexPositionColor(new Vector3(-1, -1, -1), Color.Red);
- userPrimitives[17] = new VertexPositionColor(new Vector3(-1, 1, -1), Color.Red);
- #endregion
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// all content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- /// <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)
- {
- var keyboard = Keyboard.GetState(); //zmienna pomocnicza
- elapsedUpdateTime += gameTime.ElapsedGameTime.Milliseconds;
- // Allows the game to exit
- if (keyboard.IsKeyDown(Keys.Escape))
- this.Exit();
- if (elapsedUpdateTime >= frameUpdateTime)
- {
- elapsedUpdateTime = 0;
- // modyfikacja wartości kątów odchylenia
- if (keyboard.IsKeyDown(Keys.Right))
- angleY += 0.2f;
- if (keyboard.IsKeyDown(Keys.Left))
- angleY -= 0.2f;
- if (keyboard.IsKeyDown(Keys.Up))
- angleX += 0.2f;
- if (keyboard.IsKeyDown(Keys.Down))
- angleX -= 0.2f;
- }
- #region
- //Tworzenie Macierzy świata, widoku i projekcji
- worldMatrix = Matrix.Identity;
- viewMatrix = Matrix.CreateLookAt(
- new Vector3(0.0f, 0.0f, 5.0f), // położenie kamery (jak na rysunku)
- Vector3.Zero, Vector3.Up); // cel i orientacja kamery
- projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(50),
- graphics.GraphicsDevice.Viewport.AspectRatio, 0.2f, 1000.0f);
- #endregion
- // realizacja obrotu kamery
- viewMatrix = Matrix.CreateRotationX(angleX) * Matrix.CreateRotationY(angleY) * viewMatrix;
- // aktualizacja parametrów shadera
- basicEffect.World = worldMatrix;
- basicEffect.View = viewMatrix;
- basicEffect.Projection = projectionMatrix;
- 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.CornflowerBlue);
- #region Wyświetlanie sceny
- basicEffect.CurrentTechnique.Passes[0].Apply(); // "uruchamia" shader
- GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList,
- userPrimitives, // referencja do tablicy wierzchołków
- 0, // offset pierwszego wierzchołka
- userPrimitives.Length / 3); // liczba prymitywów (tutaj trójkątów) do wyświetlenia
- #endregion
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement