Halløj,
jeg har lavet et bræt bestående af 3d hexagoner lavet i Maya3D og lagt dem i en liste.
Derefter "Draw"er jeg modellerne på skærmen, men hvis der er for mange, omkring 50*50, så lagger det når jeg flytter kameraet.
Jeg forstår det simpelthen ikke, er det fordi XNA accelererer på software og ikke på hardware, og i så fald, hvad gør jeg?
MAIN:
- 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 GridWar
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
-
- static public Model myModel;
- float aspectRatio;
-
- // Set the position of the model in world space, and set the rotation.
- Vector3 modelPosition = new Vector3(0,-100,0);
- float modelRotation = 0.0f;
-
- // Set the position of the camera in world space, for our view matrix.
- Vector3 cameraPosition = new Vector3(0.0f, 10.0f, 10.0f);
-
- Grid.HexGrid _hexGrid;
-
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
-
-
-
- DisplayMode displayMode = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode;
-
- this.graphics.PreferredBackBufferFormat = displayMode.Format;
- this.graphics.PreferredBackBufferWidth = displayMode.Width;
- this.graphics.PreferredBackBufferHeight = displayMode.Height;
- graphics.IsFullScreen = 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
-
- _hexGrid = new Grid.HexGrid(10, 30, 10);
-
- 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);
-
- // TODO: use this.Content to load your game content here
-
- myModel = Content.Load<Model>(@"Resources\Models\Hexagon");
- aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
-
- //Grid.HexGrid.LoadContent(Content);
- }
-
- /// <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)
- {
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
-
- // TODO: Add your update logic here
-
- if (Keyboard.GetState().IsKeyDown(Keys.S))
- modelPosition += new Vector3(0.0f, 0.0f, -1.0f);
-
- if (Keyboard.GetState().IsKeyDown(Keys.W))
- modelPosition += new Vector3(0.0f, 0.0f, 1.0f);
-
- if (Keyboard.GetState().IsKeyDown(Keys.Down))
- modelPosition += new Vector3(0.0f, -1.0f, 0.0f);
-
- if (Keyboard.GetState().IsKeyDown(Keys.Up))
- modelPosition += new Vector3(0.0f, 1.0f, 0.0f);
-
- if (Keyboard.GetState().IsKeyDown(Keys.D))
- modelPosition += new Vector3(-1.0f, 0.0f, 0.0f);
-
- if (Keyboard.GetState().IsKeyDown(Keys.A))
- modelPosition += new Vector3(1.0f, 0.0f, 0.0f);
-
- 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);
-
- // TODO: Add your drawing code here
-
- // Copy any parent transforms.
- Matrix[] transforms = new Matrix[myModel.Bones.Count];
- myModel.CopyAbsoluteBoneTransformsTo(transforms);
-
- for (int y = 0; y < _hexGrid.Rows.Count; y++)
- for (int x = 0; x < _hexGrid.Rows[y].Cells.Count; x++)
- {
- float r = myModel.Meshes[0].BoundingSphere.Radius;
-
- float locX = (r * x) - (r /2 / 3 * x);
- float locY = (r / 4) * y;
-
- if (_hexGrid.Rows[y].Odd)
- locX += (r / 2) - r / 11;
-
- // Draw the model. A model can have multiple meshes, so loop.
- foreach (ModelMesh mesh in myModel.Meshes)
- {
- // This is where the mesh orientation is set, as well
- // as our camera and projection.
- foreach (BasicEffect effect in mesh.Effects)
- {
- effect.EnableDefaultLighting();
-
- effect.World = transforms[mesh.ParentBone.Index] *
- Matrix.CreateRotationY(modelRotation)
- * Matrix.CreateTranslation(modelPosition + new Vector3(locX, 0, locY) );
- effect.View = Matrix.CreateLookAt(cameraPosition,
- Vector3.Zero, Vector3.Up);
- effect.Projection = Matrix.CreatePerspectiveFieldOfView(
- MathHelper.ToRadians(45.0f), aspectRatio,
- 1.0f, 10000.0f);
- }
- // Draw the mesh, using the effects set above.
- mesh.Draw();
- }
- }
- base.Draw(gameTime);
- }
- }
- }
HexGrid.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
-
- namespace GridWar.Grid
- {
- enum GridStates
- {
- EditorInstance = 0,
- GameInstance = 1
- }
-
- public class HexGrid
- {
- IList<HexGridRow> _rows = new List<HexGridRow>();
-
- GridStates _gridState = GridStates.EditorInstance;
-
- public IList<HexGridRow> Rows
- {
- get { return _rows; }
- set { _rows = value; }
- }
-
- public HexGrid(int width, int height, int hexRadius)
- {
- for (int y = 0; y < height; y++)
- {
-
- HexGridRow newRow = new HexGridRow();
-
- newRow.Odd = y % 2 != 0 ? true : false;
-
- for (int x = 0; x < width; x++)
- {
- newRow.Cells.Add(new HexGridCell());
- }
- _rows.Add(newRow);
- }
- }
-
- static public void LoadContent(ContentManager content)
- {
-
- }
-
- public void Draw(GraphicsDevice device)
- {
- switch (_gridState)
- {
- case GridStates.EditorInstance:
-
- break;
-
- case GridStates.GameInstance:
-
- break;
- }
- }
- }
- }
HexGridRowsAndCells
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace GridWar.Grid
- {
- public class HexGridRow
- {
- public bool Odd = false;
-
- IList<HexGridCell> _cells = new List<HexGridCell>();
-
- public IList<HexGridCell> Cells
- {
- get
- {
- return _cells;
- }
- set
- {
- _cells = value;
- }
- }
- }
- }
-
-
- namespace GridWar.Grid
- {
- public class HexGridCell
- {
- static public int Radius = 10;
-
- static VertexPositionColor[] _vertices;
- static VertexBuffer _vertexBuffer;
-
- static public VertexPositionColor[] Vertices
- {
- get { return _vertices; }
- set { _vertices = value; }
- }
-
- static public VertexBuffer VertexBuffer
- {
- get { return _vertexBuffer; }
- set { _vertexBuffer = value; }
- }
-
- static public VertexPositionColor[] GetVertices()
- {
- VertexPositionColor[] vertices = new VertexPositionColor[5];
-
- vertices[0] = new VertexPositionColor(new Vector3(-0.5f, 0.5f, 0), Color.Red);
- vertices[1] = new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0), Color.Blue);
- vertices[2] = new VertexPositionColor(new Vector3(0.5f, 0.5f, 0), Color.Green);
- vertices[3] = new VertexPositionColor(new Vector3(0.5f, -0.5f, 0), Color.Brown);
- vertices[4] = new VertexPositionColor(new Vector3(0.7f, 0.5f, 0.5f), Color.Azure);
-
- return vertices;
- }
- }
- }
På forhånd tak for hjælpen