日期:2014-05-17 浏览次数:21142 次
GraphicsDeviceManager graphics; VertexPositionColor[] verts; BasicEffect ef; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { verts = new VertexPositionColor[4]; verts[0] = new VertexPositionColor(new Vector3(0, 0, 1), Color.Red); verts[1] = new VertexPositionColor(new Vector3(0, 1, 0), Color.Red); verts[2] = new VertexPositionColor(new Vector3(1, 0, 0), Color.Red); verts[3] = new VertexPositionColor(new Vector3(1, 1, 1), Color.Red); short[] userindex = { 0, 1, 1, 2, 2, 3, 3, 0 }; VertexBuffer vb = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), verts.Length, BufferUsage.None); vb.SetData(verts); GraphicsDevice.SetVertexBuffer(vb); IndexBuffer indexbuffer = new IndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, userindex.Length, BufferUsage.None); indexbuffer.SetData(userindex); GraphicsDevice.Indices = indexbuffer; ef = new BasicEffect(GraphicsDevice); ef.World = Matrix.Identity; ef.View = Matrix.CreateLookAt(new Vector3(0, 0, 4), new Vector3(0, 0, -1), Vector3.Up); ef.Projection = Matrix.CreatePerspective((float)GraphicsDevice.Viewport.Width / GraphicsDevice.Viewport.Height, 1, 1.125f, 10); ef.VertexColorEnabled = true; foreach (EffectPass pass in ef.CurrentTechnique.Passes) pass.Apply(); } protected override void UnloadContent() { } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.LineList, 0, 0, verts.Length, 0, 4); base.Draw(gameTime); }