[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
using Microsoft.Xna.Framework; using JigLibX.Physics; using JigLibX.Collision; public class BasicWorldGame : Game { GraphicsDeviceManager graphics; PhysicsSystem world; BoxRenderer boxRenderer; public BasicWorldGame() { graphics = new GraphicsDeviceManager(this); InitializePhysics(); } private void InitializePhysics() { world = new PhysicsSystem(); world.CollisionSystem = new CollisionSystemSAP(); } protected override void LoadContent() { boxRenderer = new BoxRenderer(GraphicsDevice); } protected override void UnloadContent() { boxRenderer.Dispose(); } protected override void Update(GameTime gameTime) { world.Integrate(1 / 60f); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); boxRenderer.SetCamera( Matrix.CreateLookAt( new Vector3(2, 1, 5), new Vector3(), Vector3.Up ), Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 0.1f, 1000 ) ); boxRenderer.Draw(Matrix.Identity); base.Draw(gameTime); } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; class BoxRenderer:System.IDisposable { public GraphicsDevice GraphicsDevice { get; private set; } VertexBuffer vertexBuffer; BasicEffect basicEffect; public BoxRenderer(GraphicsDevice graphicsDevice) { GraphicsDevice = graphicsDevice; basicEffect = new BasicEffect(GraphicsDevice) { VertexColorEnabled = true}; Vector3[] positions = new [] { new Vector3(1, 1, 1), new Vector3(1, -1, 1), new Vector3(-1, -1,1), new Vector3(-1, 1, 1), new Vector3(1, 1, -1), new Vector3(1, -1, -1), new Vector3(-1, -1,-1), new Vector3(-1, 1, -1), }; VertexPositionColor[] vertices = new[] { new VertexPositionColor(positions[0], Color.Blue), new VertexPositionColor(positions[1], Color.Blue), new VertexPositionColor(positions[2], Color.Blue), new VertexPositionColor(positions[3], Color.Blue), new VertexPositionColor(positions[0], Color.Blue), new VertexPositionColor(positions[2], Color.Blue), new VertexPositionColor(positions[3], Color.Red), new VertexPositionColor(positions[2], Color.Red), new VertexPositionColor(positions[7], Color.Red), new VertexPositionColor(positions[7], Color.Red), new VertexPositionColor(positions[2], Color.Red), new VertexPositionColor(positions[6], Color.Red), new VertexPositionColor(positions[0], Color.Green), new VertexPositionColor(positions[3], Color.Green), new VertexPositionColor(positions[7], Color.Green), new VertexPositionColor(positions[0], Color.Green), new VertexPositionColor(positions[7], Color.Green), new VertexPositionColor(positions[4], Color.Green), new VertexPositionColor(positions[4], Color.Cyan), new VertexPositionColor(positions[6], Color.Cyan), new VertexPositionColor(positions[5], Color.Cyan), new VertexPositionColor(positions[4], Color.Cyan), new VertexPositionColor(positions[7], Color.Cyan), new VertexPositionColor(positions[6], Color.Cyan), new VertexPositionColor(positions[5], Color.Magenta), new VertexPositionColor(positions[2], Color.Magenta), new VertexPositionColor(positions[1], Color.Magenta), new VertexPositionColor(positions[5], Color.Magenta), new VertexPositionColor(positions[6], Color.Magenta), new VertexPositionColor(positions[2], Color.Magenta), new VertexPositionColor(positions[0], Color.Yellow), new VertexPositionColor(positions[5], Color.Yellow), new VertexPositionColor(positions[1], Color.Yellow), new VertexPositionColor(positions[0], Color.Yellow), new VertexPositionColor(positions[4], Color.Yellow), new VertexPositionColor(positions[5], Color.Yellow), }; vertexBuffer = new VertexBuffer( graphicsDevice, typeof(VertexPositionColor), vertices.Length, BufferUsage.WriteOnly ); vertexBuffer.SetData<VertexPositionColor>(vertices); } public void SetCamera(Matrix view, Matrix projection) { basicEffect.View = view; basicEffect.Projection = projection; } public void Draw(Matrix world) { basicEffect.World = Matrix.CreateScale(1 / 2f) * world; basicEffect.CurrentTechnique.Passes[0].Apply(); GraphicsDevice.SetVertexBuffer(vertexBuffer); GraphicsDevice.DrawPrimitives( PrimitiveType.TriangleList, 0, vertexBuffer.VertexCount / 3 ); } public void Dispose() { basicEffect.Dispose(); vertexBuffer.Dispose(); } }