Пример #1
0
        /// <summary>
        /// Helper method that draws a basic texture in an inefficient way.
        /// Use SpriteBatch for your actual 2D code if you need to draw more stuff.
        /// This will insert one quad per texture call and no batching will occur.
        /// </summary>
        /// <param name="renderContext"></param>
        /// <param name="texture"></param>
        /// <param name="rectangle"></param>
        /// <param name="color"></param>
        public static void DrawTexture(this IRenderContext renderContext, Texture2D texture, Rectangle rectangle, Color color)
        {
            // default clip space coordinates, filling the entire screen, their range is -1;1, note that -1 for x is left but -1 for Y is bottom, so we need to flip y to get default coordinate system
            // recalculate to the position where the rectangle actually is
            var vp = renderContext.GraphicsDevice.Viewport.Bounds;

            if (!vp.Intersects(rectangle))
            {
                return; // outside of view area
            }
            if (_quad == null)
            {
                // default clip space coordinates to fill entire screen
                const int minX = -1;
                const int maxX = 1;
                const int minY = 1;
                const int maxY = -1;
                // only create mesh once
                var desc = new TextureMeshDescriptionBuilder();
                desc.AddPlaneXy(minX, maxX, minY, maxY, 0, false, Vector2.One);
                _quad = renderContext.MeshCreator.CreateMesh(desc);
            }

            // quad is always filling entire clipspace, apply transform based on rectangle dimensions
            // clip space is -1 to 1 on x and 1 to -1 on y coordinate, so first scale down then transform. also note that origin is center of screen so we translate by 0.5f - .. to offset the origin to top left
            var world = Matrix.CreateScale(new Vector3(rectangle.Width / (float)vp.Width, rectangle.Height / (float)vp.Height, 0)) *
                        Matrix.CreateTranslation(new Vector3(rectangle.X / (float)vp.Width * 2 - 1f + rectangle.Width / (float)vp.Width, 1f - rectangle.Y / (float)vp.Height * 2 - rectangle.Height / (float)vp.Height, 0));

            renderContext.DrawMesh(_quad, world, Matrix.Identity, Matrix.Identity, new TextureColorBrush(texture, color));
        }
Пример #2
0
        private void CreateSkybox(TextureGenerator textureGenerator)
        {
            var skybox = new TextureMeshDescriptionBuilder();
            var bbox   = new BoundingBox(-Vector3.One, Vector3.One);

            // add the 4 walls manually
            skybox.AddPlane(bbox, Plane.NegativeX, false, Vector2.One);
            skybox.AddPlane(bbox, Plane.PositiveX, false, Vector2.One);
            skybox.AddPlane(bbox, Plane.NegativeZ, false, Vector2.One);
            skybox.AddPlane(bbox, Plane.PositiveZ, false, Vector2.One);

            _skybox = _renderContext.MeshCreator.CreateMesh(skybox);

            var skyboxTex = _textureCache.GetOrCreateValue("skybox", key => textureGenerator.CreateSkyboxTexture());

            _skyboxTextureBrush = new SkyboxBrush(skyboxTex);
        }
Пример #3
0
        protected override void Initialize()
        {
            Window.Title = "RenderTargetSample - Esc to quit";
            base.Initialize();
            IsMouseVisible  = true;
            IsFixedTimeStep = false;

            _pixel = new RenderTarget2D(GraphicsDevice, 1, 1);
            _pixel.SetData(new[] { Color.White });
            _renderContext = new DefaultRenderContext(_graphicsDeviceManager, Content);
            var builder = new TextureMeshDescriptionBuilder();

            builder.AddBox(new BoundingBox(Vector3.Zero, Vector3.One * 10), Vector2.One);
            var creator = new MeshCreator(GraphicsDevice);

            _mesh = creator.CreateMesh(builder);

            _camera = new StaticCamera(GraphicsDevice, Vector3.UnitZ * 50);

            _rt1 = CreateRenderTarget(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            _rt2 = CreateRenderTarget(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            _rt3 = CreateRenderTarget(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
        }
        /// <summary>
        /// Creates a new instance of the visualizer which will display a cube in the current working array.
        /// </summary>
        /// <param name="renderContext"></param>
        /// <param name="inputData"></param>
        /// <param name="backgroundWorker"></param>
        /// <param name="camera"></param>
        public ActiveCubeVisualizer(IRenderContext renderContext, IInputData inputData, VisualizerBackgroundWorker backgroundWorker, ICamera camera)
        {
            _renderContext    = renderContext;
            _inputData        = inputData;
            _backgroundWorker = backgroundWorker;
            _camera           = camera;
            _pen = new SolidColorPen(Color.Red, CullMode.None);
            // we want to draw the outside surfaces of the currently worked on cube and its outline
            // currently this requires 2 meshes: one with the surfaces as triangles (for coloring) and one with only the outline as lines
            // if we used e.g. only the first mesh, the pen would also draw lines across the surfaces which we don't want
            // again: inefficient, but we don't care because it doesn't have to be efficient
            var cube = new TextureMeshDescriptionBuilder();
            var box  = new BoundingBox(Vector3.Zero, Vector3.One);

            cube.AddRoom(box, Vector2.One);
            _visualizerMesh = renderContext.MeshCreator.CreateMesh(cube);
            var cube2 = new LineMeshDescriptionBuilder();

            cube2.AddBox(box, Color.Black);
            _visualizerLineMesh = renderContext.MeshCreator.CreateMesh(cube2);

            _cubesPerTick = 1;
        }
Пример #5
0
        private void CreateTerrain(TextureGenerator textureGenerator)
        {
            // for now just render a cube so we have a point of reference

            var height    = 45;
            var heightmap = CalculateRandomHeightmap(_width + 1, _height + 1, height);
            var terrain   = new TextureMeshDescriptionBuilder();
            var vertices  = new List <VertexPositionColorTexture>();

            for (int y = 0; y < _height; y++)
            {
                for (int x = 0; x < _width; x++)
                {
                    // add a quad per cell
                    var xCoord = x * CellSize;
                    var zCoord = y * CellSize;
                    var bbox   = new BoundingBox(new Vector3(xCoord, 0, zCoord), new Vector3(xCoord + CellSize, 0, zCoord + CellSize));

                    vertices.AddRange(new[]
                    {
                        new VertexPositionColorTexture(new Vector3(bbox.Max.X, heightmap[x + 1, y], bbox.Min.Z), Color.White, new Vector2(heightmap[x + 1, y] / (float)height)),
                        new VertexPositionColorTexture(new Vector3(bbox.Min.X, heightmap[x, y + 1], bbox.Max.Z), Color.White, new Vector2(heightmap[x, y + 1] / (float)height)),
                        new VertexPositionColorTexture(new Vector3(bbox.Min.X, heightmap[x, y], bbox.Min.Z), Color.White, new Vector2(heightmap[x, y] / (float)height)),
                        new VertexPositionColorTexture(new Vector3(bbox.Min.X, heightmap[x, y + 1], bbox.Max.Z), Color.White, new Vector2(heightmap[x, y + 1] / (float)height)),
                        new VertexPositionColorTexture(new Vector3(bbox.Max.X, heightmap[x + 1, y], bbox.Min.Z), Color.White, new Vector2(heightmap[x + 1, y] / (float)height)),
                        new VertexPositionColorTexture(new Vector3(bbox.Max.X, heightmap[x + 1, y + 1], bbox.Max.Z), Color.White, new Vector2(heightmap[x + 1, y + 1] / (float)height))
                    });
                }
            }

            terrain.AddVertices(vertices);
            _terrain = _renderContext.MeshCreator.CreateMesh(terrain);

            var terrainTex = _textureCache.GetOrCreateValue("terrain", key => textureGenerator.CreateTerrainTexture());

            _terrainBrush = new TextureBrush(terrainTex);
        }