private void DrawToRenderTarget(GlobalData gd) { //GraphicsDevice.Viewport.Width GraphicsDevice.Viewport.Height //san BasicEffect basicEffect = new BasicEffect(GraphicsDevice); basicEffect.Projection = gd.Projection; basicEffect.View = Matrix.CreateLookAt( new Vector3(0f, 0f, gd.CameraDistance), Vector3.Zero, new Vector3(0f, 1f, 0f)); float t = renderTarget2D.Width/2; // * gd.PixelsToCenter.X / gd.ViewportResolution.X; basicEffect.Projection = Matrix.CreateOrthographicOffCenter( -t, t, -t, t, 1.0f, 10000.0f); //san //GraphicsDevice.SetRenderTarget(0, renderTarget2D); GraphicsDevice.SetRenderTarget(renderTarget2D); Viewport renderTargetViewPort = new Viewport(); renderTargetViewPort.Width = renderTarget2D.Width; renderTargetViewPort.Height = renderTarget2D.Height; GraphicsDevice.Viewport = renderTargetViewPort; GraphicsDevice.Clear(ClearOptions.Target, Color.Black,1.0f, 0); { ObjectShip obj = playerShips[0]; Matrix renderMatrix = Matrix.CreateTranslation(new Vector3(obj.centerOffset.X, obj.centerOffset.Y, obj.centerOffset.Z))* Matrix.CreateRotationY(obj.Rotation.Y)* Matrix.CreateRotationZ(obj.Rotation.Z)* Matrix.CreateScale(10); //Matrix.CreateTranslation(obj.Position); foreach(ModelMesh mesh in obj.Model.Meshes) { foreach(BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); effect.Projection = basicEffect.Projection; effect.View = basicEffect.View; effect.World = renderMatrix; } mesh.Draw(); } obj.Render(GraphicsDevice, basicEffect, 0, 0); } //san //GraphicsDevice.SetRenderTarget(0, null); GraphicsDevice.SetRenderTarget(null); }
private static Matrix CalculateView(GlobalData gd, Vector3 target) { Vector3 cameraPosition = new Vector3( Math.Min(Math.Max(target.X, gd.PixelsToCenter.X), gd.LevelDimensions.X - gd.PixelsToCenter.X), Math.Min(Math.Max(target.Y, gd.PixelsToCenter.Y), gd.LevelDimensions.Y - gd.PixelsToCenter.Y), gd.CameraDistance); return Matrix.CreateLookAt( cameraPosition, new Vector3(cameraPosition.X, cameraPosition.Y, 0), new Vector3(0f, 1f, 0f)); }
/// <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) { graphics.GraphicsDevice.Clear (Color.CornflowerBlue); //TODO: Add your drawing code here Viewport defaultViewport = GraphicsDevice.Viewport; Viewport newView = defaultViewport; newView.Width = newView.Width / InputHandler.Player.Length; GlobalData gd = new GlobalData(); SetupMatrices(gd, new Vector2(newView.Width, newView.Height)); DrawToRenderTarget(gd); //Clear the backbuffer to the cornflower blue GraphicsDevice.Clear(ClearOptions.DepthBuffer | ClearOptions.Target, Color.CornflowerBlue, 1.0f, 0); try { for(int player = 0; player < InputHandler.Player.Length; player++) { GraphicsDevice.Viewport = newView; //san //BasicEffect basicEffect = new BasicEffect(GraphicsDevice, null); BasicEffect basicEffect = new BasicEffect(GraphicsDevice); basicEffect.Projection = gd.Projection; basicEffect.View = CalculateView(gd, playerShips[player].Position); //san // GraphicsDevice.RenderState.DepthBufferEnable = false; levelBackground.Render(GraphicsDevice, basicEffect); //san // GraphicsDevice.RenderState.DepthBufferEnable = true; SetUpLights(basicEffect); for(int i = 0; i < InputHandler.Player.Length; i++) { //playerShips[i].Render(device, vScrollBar1.Value, vScrollBar2.Value); playerShips[i].Render(GraphicsDevice, basicEffect, 0, 0); } SpriteBatch sb = new SpriteBatch(GraphicsDevice); sb.Begin(); sb.DrawString(spriteFont, string.Format("{0:00.00}x {1:00.00}y", playerShips[player].Position.X, playerShips[player].Position.Y), new Vector2(50, newView.Height - 30), Color.Blue, -0.025f, Vector2.Zero, 1, SpriteEffects.None, 0); sb.End(); //Direct3D.Font font = new Direct3D.Font(GraphicsDevice, new System.Drawing.Font("Arial", 10)); //font.DrawString(null, // string.Format("{0:00.00} {1:00.00}", playerShips[player].Position.X, // playerShips[player].Position.Y), // new Rectangle(newView.X + 20, 20, 200, 30), DrawStringFormat.None, Color.Yellow); //font.Dispose(); for(int i = 0; i < InputHandler.Player.Length; i++) { bullerBuffer.Render(GraphicsDevice, basicEffect, playerShips[i], levelBackground); } //End the scene newView.X += newView.Width + 0; } } finally { GraphicsDevice.Viewport = defaultViewport; } //san Texture2D texture = renderTarget2D;// .GetTexture(); { SpriteBatch sb = new SpriteBatch(GraphicsDevice); sb.Begin(); sb.Draw(texture, new Vector2(220, 220), null, Color.Azure, 0, Vector2.Zero, gd.ViewportResolution.X/(gd.PixelsToCenter.X*2), SpriteEffects.None, 0); sb.End(); } base.Draw (gameTime); }
private static void SetupMatrices(GlobalData gd, Vector2 screenResolution) { gd.LevelDimensions = new Vector2(21 * 16, 63 * 16); gd.CameraDistance = 250f; gd.ViewportResolution = screenResolution; gd.Projection = Matrix.CreatePerspectiveFieldOfView( (float)(Math.PI / 4), gd.ViewportResolution.X / gd.ViewportResolution.Y, 1.0f, 10000.0f); gd.PixelsToCenter = new Vector2( gd.CameraDistance / gd.Projection.M11, gd.CameraDistance / gd.Projection.M22); }