Пример #1
0
        private void initResources(int width, int height)
        {
            Texture2DDescription description = new Texture2DDescription()
            {
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                BindFlags         = BindFlags.None,
                CpuAccessFlags    = CpuAccessFlags.Read,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage       = ResourceUsage.Staging,
                OptionFlags = ResourceOptionFlags.None
            };

            _stagingTexture = SharpDX.Toolkit.Graphics.Texture2D.New(graphicsDevice, description);

            _buffer          = new byte[width * height * 4];
            _writeableBitmap = new WriteableBitmap(
                width, height, 96, 96, PixelFormats.Bgra32, null);
        }
Пример #2
0
        public static Texture2D Load(GraphicsDevice graphicsDevice, ImageC image, ResourceUsage usage = ResourceUsage.Immutable)
        {
            if (graphicsDevice == null)
            {
                return(null);
            }

            Texture2D result = null;

            image.GetIntPtr((IntPtr data) =>
            {
                Texture2DDescription description;
                description.ArraySize         = 1;
                description.BindFlags         = BindFlags.ShaderResource;
                description.CpuAccessFlags    = CpuAccessFlags.None;
                description.Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm;
                description.Height            = image.Height;
                description.MipLevels         = 1;
                description.OptionFlags       = ResourceOptionFlags.None;
                description.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0);
                description.Usage             = usage;
                description.Width             = image.Width;

                //return Texture2D.New(graphicsDevice, description, new DataBox[] { new DataBox(lockData.Scan0, lockData.Stride, 0) }); //Only for the none toolkit version which unfortunately we cannot use currently.
                result = Texture2D.New(graphicsDevice, description.Width, description.Height, description.MipLevels, description.Format,
                                       new[] { new SharpDX.DataBox(data, image.Width * ImageC.PixelSize, 0) }, TextureFlags.ShaderResource, 1, description.Usage);
            });
            return(result);
        }
Пример #3
0
        /// <summary>
        /// Invoked after either control has created its graphics device.
        /// </summary>
        private void OnGraphicsControlLoadContent(object sender, GraphicsDeviceEventArgs e)
        {
            _basicEffect = new BasicEffect(e.GraphicsDevice)
            {
                View  = Matrix.LookAtRH(new Vector3(0, 0, 3), new Vector3(0, 0, 0), Vector3.UnitY),
                World = Matrix.Identity,
                PreferPerPixelLighting = true
            };
            _basicEffect.EnableDefaultLighting();

            _geometricPrimitives = new[]
            {
                GeometricPrimitive.Cube.New(e.GraphicsDevice),
                GeometricPrimitive.Cylinder.New(e.GraphicsDevice),
                GeometricPrimitive.GeoSphere.New(e.GraphicsDevice),
                GeometricPrimitive.Teapot.New(e.GraphicsDevice),
                GeometricPrimitive.Torus.New(e.GraphicsDevice)
            };
            _primitiveIndex = 0;

            _texture                    = Texture2D.Load(e.GraphicsDevice, "Modules/SharpDXSceneViewer/Resources/tile_aqua.png");
            _basicEffect.Texture        = _texture;
            _basicEffect.TextureEnabled = true;

            _yaw   = 0.5f;
            _pitch = 0.3f;
        }
Пример #4
0
        /// <summary>
        /// Invoked after either control has created its graphics device.
        /// </summary>
        private void OnGraphicsControlLoadContent(object sender, GraphicsDeviceEventArgs e)
        {
	        _basicEffect = new BasicEffect(e.GraphicsDevice)
	        {
		        View = Matrix.LookAtRH(new Vector3(0, 0, 3), new Vector3(0, 0, 0), Vector3.UnitY),
		        World = Matrix.Identity,
		        PreferPerPixelLighting = true
	        };
	        _basicEffect.EnableDefaultLighting();

            _geometricPrimitives = new[]
            {
                GeometricPrimitive.Cube.New(e.GraphicsDevice),
                GeometricPrimitive.Cylinder.New(e.GraphicsDevice),
                GeometricPrimitive.GeoSphere.New(e.GraphicsDevice),
                GeometricPrimitive.Teapot.New(e.GraphicsDevice),
                GeometricPrimitive.Torus.New(e.GraphicsDevice)
            };
            _primitiveIndex = 0;

	        _texture = Texture2D.Load(e.GraphicsDevice, "Modules/SceneViewer/Resources/tile_aqua.png");
	        _basicEffect.Texture = _texture;
	        _basicEffect.TextureEnabled = true;

            _yaw = 0.5f;
            _pitch = 0.3f;
        }
Пример #5
0
        /// <summary>
        /// Creates a texture from a file.
        /// Allowed file types are BMP, PNG, JPG and DDS.
        /// </summary>
        /// <param name="fileName">A path to a valid texture file.</param>
        /// <param name="loadAsync">Optional. Pass true if you want the texture to be loaded asynchronously.
        /// This can reduce frame drops but the texture might not be available immediately (see <see cref="Texture.IsReady"/> property).</param>
        /// <exception cref="InvalidOperationException">No renderer is registered.</exception>
        public Texture(string fileName, bool loadAsync = false)
        {
            IsAsync = loadAsync;
            var file = new FileInfo(fileName);

            Renderer renderer = GameEngine.TryQueryComponent<Renderer>();
            if (renderer == null) throw new InvalidOperationException("A renderer must be registered before a texture can be created.");

            if (loadAsync)
            {
                Task.Run(() => renderer.CreateTexture(file.FullName))
                    .ContinueWith((t) =>
                    {
                        var result = t.Result;
                        texture = result.Texture;
                        ResourceView = result.ResourceView;

                        Width = texture.Width;
                        Height = texture.Height;
                        isReady = true;
                    });
            }
            else
            {
                var result = renderer.CreateTexture(file.FullName);
                texture = result.Texture;
                ResourceView = result.ResourceView;

                Width = texture.Width;
                Height = texture.Height;
                isReady = true;
            }
        }
Пример #6
0
        internal TextureCreationResult CreateTexture(Stream stream)
        {
            TK.Texture2D texture      = TK.Texture2D.Load(tkDevice, stream);
            var          resourceView = new D3D11.ShaderResourceView(device, texture);

            return(new TextureCreationResult()
            {
                Texture = texture, ResourceView = resourceView
            });
        }
Пример #7
0
        public DXImageSource(GraphicsDevice graphics, int width, int height)
        {
            if (width < 10)
            {
                width = 10;
            }
            if (height < 10)
            {
                height = 10;
            }

            _renderTarget     = RenderTarget2D.New(graphics, width, height, SharpDX.Toolkit.Graphics.PixelFormat.B8G8R8A8.UNorm);
            _renderTargetView = new RenderTargetView(graphics, (SharpDX.Toolkit.Graphics.GraphicsResource)_renderTarget);

            SharpDX.Direct3D11.Texture2D depthBuffer = new SharpDX.Direct3D11.Texture2D(graphics, new Texture2DDescription()
            {
                Format            = SharpDX.DXGI.Format.D24_UNorm_S8_UInt,
                ArraySize         = 1,
                MipLevels         = 0,
                Width             = width,
                Height            = height,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                BindFlags         = SharpDX.Direct3D11.BindFlags.DepthStencil
            });

            //_depthStencilBuffer = DepthStencilBuffer.New(graphics,width,height,DepthFormat.Depth24Stencil8);
            _depthStencilView = new DepthStencilView(graphics, depthBuffer);

            Texture2DDescription description = new Texture2DDescription()
            {
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                BindFlags         = BindFlags.None,
                CpuAccessFlags    = CpuAccessFlags.Read,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage       = ResourceUsage.Staging,
                OptionFlags = ResourceOptionFlags.None
            };

            _stagingTexture = SharpDX.Toolkit.Graphics.Texture2D.New(graphics, description);

            _buffer          = new byte[width * height * 4];
            _writeableBitmap = new WriteableBitmap(
                width, height, 96, 96, PixelFormats.Bgr32, null);
        }
Пример #8
0
        public DXImageSource(GraphicsDevice graphics, int width, int height)
        {
            if (width < 10) width = 10;
            if (height < 10) height = 10;

            _renderTarget = RenderTarget2D.New(graphics, width, height, SharpDX.Toolkit.Graphics.PixelFormat.B8G8R8A8.UNorm);
            _renderTargetView = new RenderTargetView(graphics, (SharpDX.Toolkit.Graphics.GraphicsResource)_renderTarget);

            SharpDX.Direct3D11.Texture2D depthBuffer = new SharpDX.Direct3D11.Texture2D(graphics, new Texture2DDescription()
            {
                Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt,
                ArraySize=1,
                MipLevels=0,
                Width = width,
                Height = height,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1,0),
                BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil
            });

            //_depthStencilBuffer = DepthStencilBuffer.New(graphics,width,height,DepthFormat.Depth24Stencil8);
            _depthStencilView = new DepthStencilView(graphics, depthBuffer);

            Texture2DDescription description = new Texture2DDescription()
            {
                Width = width,
                Height = height,
                MipLevels = 1,
                ArraySize = 1,
                Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                BindFlags = BindFlags.None,
                CpuAccessFlags = CpuAccessFlags.Read,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count = 1,
                    Quality = 0
                },
                Usage = ResourceUsage.Staging,
                OptionFlags = ResourceOptionFlags.None
            };
            _stagingTexture = SharpDX.Toolkit.Graphics.Texture2D.New(graphics, description);

            _buffer = new byte[width * height * 4];
            _writeableBitmap = new WriteableBitmap(
                width, height, 96, 96, PixelFormats.Bgr32, null);
        }
Пример #9
0
        /// <summary>
        /// Invoked after either control has created its graphics device.
        /// </summary>
        private void OnGraphicsControlLoadContent(object sender, GraphicsDeviceEventArgs e)
        {
            _effect = new BasicEffect(e.GraphicsDevice)
            {
                View  = Matrix.LookAtRH(new Vector3(0, 0, 3), new Vector3(0, 0, 0), Vector3.UnitY),
                World = Matrix.Identity,
                PreferPerPixelLighting = true,
                FogEnabled             = true,
                FogStart = 1,
                FogEnd   = 100,
            };

            _effect.EnableDefaultLighting();


            _texture               = Texture2D.Load(e.GraphicsDevice, "Modules/Scene/Resources/aima_1.png");
            _effect.Texture        = _texture;
            _effect.TextureEnabled = true;

            //var content = new SharpDX.Toolkit.Content.ContentManager(IoC.Get<IServiceProvider>());
            //content.Resolvers.Add(new FileSystemContentResolver("C:\\Users\\boris\\Documents\\stealthshifter\\Assets\\Models\\"));

            //var path = "C:\\Users\\boris\\Documents\\stealthshifter\\Assets\\Models\\assets.fbx";
            //var path = "Z:\\Общие Проекты\\monkey.x";
            //_model = content.Load<Model>("Z:\\Общие Проекты\\monkey.x");
            //_model = content.Load<Model>("assets.fbx");

            //_model = SharpDX.Toolkit.Graphics.Model.Load(e.GraphicsDevice, File.Open(path, FileMode.Open), name => _texture);

            _geometricPrimitives = new[]
            {
                GeometricPrimitive.Cube.New(e.GraphicsDevice),
                GeometricPrimitive.Cylinder.New(e.GraphicsDevice),
                GeometricPrimitive.GeoSphere.New(e.GraphicsDevice),
                GeometricPrimitive.Teapot.New(e.GraphicsDevice),
                GeometricPrimitive.Torus.New(e.GraphicsDevice),
                GeometricPrimitive.Plane.New(e.GraphicsDevice, 10f, 10f)
            };
            _primitiveIndex = 0;

            _yaw   = 0.5f;
            _pitch = 0.3f;
        }
Пример #10
0
        public static void Update(GraphicsDevice graphicsDevice, Texture2D texture, ImageC image, VectorInt2 position)
        {
            if (image.Width == 0 || image.Height == 0)
            {
                return;
            }
            var deviceContext = (DeviceContext)graphicsDevice;

            image.GetIntPtr((IntPtr data) =>
            {
                ResourceRegion region;
                region.Left   = position.X;
                region.Right  = position.X + image.Width;
                region.Top    = position.Y;
                region.Bottom = position.Y + image.Height;
                region.Front  = 0;
                region.Back   = 1;
                texture.SetData(graphicsDevice, new SharpDX.DataPointer(data, image.DataSize), 0, 0, region);
            });
        }
Пример #11
0
        internal bool Resize(int width, int height)
        {
            if (this._width != width || this._height != height || this._offscreenBuffer == null)
            {
                this._width  = width;
                this._height = height;
                //if (this._mainTexture != null)
                //{
                //    this._mainTexture.Dispose();
                //}
                if (this._offscreenBuffer != null)
                {
                    this._offscreenBuffer.Dispose();
                }

                this._offscreenBuffer = SharpDX.Toolkit.Graphics.Texture2D.New(this.GraphicsDevice, width, height, SharpDX.Toolkit.Graphics.PixelFormat.B8G8R8A8.UNorm, TextureFlags.ShaderResource, 1, ResourceUsage.Default);

                //this._offscreenBuffer = new SharpDX.Direct3D11.Texture2D((Device)this.GraphicsDevice, new Texture2DDescription
                //{
                //    ArraySize = 1,
                //    BindFlags = BindFlags.ShaderResource,
                //    CpuAccessFlags = CpuAccessFlags.None,
                //    Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm_SRgb,
                //    Width = width,
                //    Height = height,
                //    MipLevels = 1,
                //    OptionFlags = ResourceOptionFlags.Shared,
                //    Usage = ResourceUsage.Default,
                //    SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0)
                //});
                //using (SharpDX.DXGI.Resource resource = ((SharpDX.Direct3D11.Texture2D)this._offscreenBuffer).QueryInterface<SharpDX.DXGI.Resource>())
                //{
                //    this._mainTexture = SharpDX.Toolkit.Graphics.Texture2D.New(this.GraphicsDevice, ((Device)this.GraphicsDevice).OpenSharedResource<SharpDX.Direct3D11.Texture2D>(resource.SharedHandle));
                //}
                return(true);
            }
            return(false);
        }
Пример #12
0
        /// <summary>
        /// Creates a texture from a file.
        /// Allowed file types are BMP, PNG, JPG and DDS.
        /// </summary>
        /// <param name="fileName">A path to a valid texture file.</param>
        /// <param name="loadAsync">Optional. Pass true if you want the texture to be loaded asynchronously.
        /// This can reduce frame drops but the texture might not be available immediately (see <see cref="Texture.IsReady"/> property).</param>
        /// <exception cref="InvalidOperationException">No renderer is registered.</exception>
        public Texture(string fileName, bool loadAsync = false)
        {
            IsAsync = loadAsync;
            var file = new FileInfo(fileName);

            Renderer renderer = GameEngine.TryQueryComponent <Renderer>();

            if (renderer == null)
            {
                throw new InvalidOperationException("A renderer must be registered before a texture can be created.");
            }

            if (loadAsync)
            {
                Task.Run(() => renderer.CreateTexture(file.FullName))
                .ContinueWith((t) =>
                {
                    var result   = t.Result;
                    texture      = result.Texture;
                    ResourceView = result.ResourceView;

                    Width   = texture.Width;
                    Height  = texture.Height;
                    isReady = true;
                });
            }
            else
            {
                var result = renderer.CreateTexture(file.FullName);
                texture      = result.Texture;
                ResourceView = result.ResourceView;

                Width   = texture.Width;
                Height  = texture.Height;
                isReady = true;
            }
        }
Пример #13
0
        // RefreshAndTransferTexture() updates the texture on the gpu
        // Call this before the texture is used at the rendering stage or else a race condition will occur
        // If the image dimension change the texture must be recreated or else the directx update call will fail when the image is being transfered to the gpu
        public void RefreshAndTransferTexture(GraphicsDevice gd)
        {
            // Lock for thread safety (used in setimage())
            lock (_textureLock)
            {
                if (_imageUpdated && Image != null)
                {
                    // Destroy the existing texture if the new image does not fit the current texture description
                    if (Texture != null && (Texture.Description.Width != Image.Description.Width ||
                                            Texture.Description.Height != Image.Description.Height ||
                                            Texture.Description.ArraySize != Image.Description.ArraySize ||
                                            Texture.Description.Depth != Image.Description.Depth ||
                                            Texture.Description.Dimension != Image.Description.Dimension ||
                                            Texture.Description.Format != Image.Description.Format ||
                                            Texture.Description.MipLevels != Image.Description.MipLevels))
                    {
                        Texture.Dispose();
                        Texture = null;
                    }
                    // (Re)create the texture context as a dynamic texture (cpu update possible) and store the image dimensions
                    if (Texture == null)
                    {
                        Texture = Texture2D.New(gd, Image, TextureFlags.ShaderResource, ResourceUsage.Dynamic);
                        // Update the image plane size to match the image's aspect ratio
                        UpdatePlaneAspectRatio();
                    }
                    else
                    {
                        // Update the texture
                        Texture.SetData(Image);
                    }

                    _imageUpdated = false;
                    FPS.Tick();
                }
            }
        }
Пример #14
0
        /// <summary>
        /// Executes once on device creation.
        /// </summary>
        protected override void LoadContent()
        {
            base.LoadContent();

// ================================== NEW CODE START =================================
            _primitives = new[]
            {
                GeometricPrimitive.Cube.New(GraphicsDevice, 1.5f),
                GeometricPrimitive.Cylinder.New(GraphicsDevice, 2f),
                GeometricPrimitive.Sphere.New(GraphicsDevice, 2f),
                GeometricPrimitive.GeoSphere.New(GraphicsDevice, 2f),
                GeometricPrimitive.Plane.New(GraphicsDevice, 2f, 2f),
                GeometricPrimitive.Torus.New(GraphicsDevice, 2f, 0.6f),
                GeometricPrimitive.Teapot.New(GraphicsDevice, 2f),
            };
// ================================== NEW CODE END ==================================

            // load effect file
            _effect = ToDisposeContent(Content.Load <Effect>("ShaderGame5"));

            // load texture resource
            //_texture = ToDisposeContent(Content.Load<Texture2D>("WoodCrate01"));
            _texture = ToDisposeContent(Content.Load <Texture2D>("GeneticaMortarlessBlocks"));
        }
Пример #15
0
        public MeshCamera(Room room, float zoom, Vector2R?pos) : base(room, zoom, pos)
        {
            pendingVertices = new List <SpriteVertex>();
            //permVertices = new List<SpriteVertex>();
            //Perms = new HashSet<SpriteVertex>();
            pendingVertexQueue = new BlockingCollection <List <SpriteVertex> >(3);
            device             = OrbIt.Game.GraphicsDevice;
            Mesh   = Buffer.Vertex.New <SpriteVertex>(OrbIt.Game.GraphicsDevice, 16 * 1024);
            layout = VertexInputLayout.FromBuffer(0, Mesh);

            effect  = OrbIt.Game.Content.Load <Effect>("Effects/MixedShaders2");
            texture = OrbIt.Game.Content.Load <Texture2D>("Textures/spritesheet");

            mvpParam         = effect.Parameters["mvp"];
            spriteCountParam = effect.Parameters["SpriteCount"];
            textureParam     = effect.Parameters["ModelTexture"];

            textureSamplerParameter = effect.Parameters["_sampler"];
            effectPass = effect.Techniques["Render"].Passes[0];
            textureParam.SetResource(texture);

            textureSamplerParameter.SetResource(device.SamplerStates.LinearClamp);
            spriteCountParam.SetValue((float)Enum.GetValues(typeof(Textures)).Length);
        }
Пример #16
0
        //public static GeometricPrimitive CreateBoxMesh(Texture2D tex)
        //{
        //}
        public static GeometricPrimitive<VertexPositionNormalColor> CreateTextureMesh(Texture2D tex)
        {
            var height = (float) tex.Height;
            var width = (float) tex.Width;
            var data = new TextureColors(tex);
            var stride = data.Stride;

            var buffer = new List<VertexPositionNormalColor>();
            var ind = new List<int>();

            int i = 0;

            var backn = Vector3.BackwardRH;
            var leftn= Vector3.Left;
            var rightn = Vector3.Right;
            var upn = Vector3.Up;

            for (int y = 0; y < height; ++y)
            {
                for (int x = 0; x < width; ++x)
                {
                    var color = data[y*stride + x];
                    if (color.A == 0)
                    {
                        //alpha is zero, skip this cube
                        continue;
                    }
                    //BACK FACE
                    buffer.Add(new VertexPositionNormalColor(
                        new Vector3(x/width, -y/height, 1), backn, color));
                    buffer.Add(new VertexPositionNormalColor(
                        new Vector3((x + 1)/width, -y/height, 1), backn, color));
                    buffer.Add(new VertexPositionNormalColor(
                        new Vector3((x + 1) / width, -(y + 1) / height, 1), backn, color));
                    buffer.Add(new VertexPositionNormalColor(
                        new Vector3(x / width, -(y + 1) / height, 1), backn, color));
                    ind.Add(i);
                    ind.Add(i+1);
                    ind.Add(i+2);
                    ind.Add(i);
                    ind.Add(i+2);
                    ind.Add(i+3);
                    i += 4;

                    //check edges
                    if (x == 0 || data[y*stride + x - 1].A != 255)
                    {
                        //LEFT FACE
                        buffer.Add(new VertexPositionNormalColor(
                            new Vector3(x/width, -y/height, 1), leftn, color));

                        buffer.Add(new VertexPositionNormalColor(
                            new Vector3(x/width, -(y + 1)/height, 0), leftn, color));

                        buffer.Add(new VertexPositionNormalColor(
                            new Vector3(x/width, -y/height, 0), leftn, color));

                        buffer.Add(new VertexPositionNormalColor(
                            new Vector3(x/width, -(y + 1)/height, 1), leftn, color));
                        ind.Add(i);
                        ind.Add(i+1);
                        ind.Add(i+2);
                        ind.Add(i);
                        ind.Add(i+3);
                        ind.Add(i+1);
                        i += 4;
                    }
                    if (x + 1 == width || data[y*stride + x + 1].A != 255)
                    {
                        //RIGHT FACE
                        buffer.Add(new VertexPositionNormalColor(
                            new Vector3((x+1) / width, -y / height, 1), rightn, color));

                        buffer.Add(new VertexPositionNormalColor(
                            new Vector3((x + 1) / width, -y / height, 0), rightn, color));

                        buffer.Add(new VertexPositionNormalColor(
                            new Vector3((x + 1) / width, -(y+1) / height, 0), rightn, color));

                        buffer.Add(new VertexPositionNormalColor(
                            new Vector3((x + 1) / width, -(y + 1) / height, 1), rightn, color));
                        ind.Add(i);
                        ind.Add(i+1);
                        ind.Add(i+2);
                        ind.Add(i);
                        ind.Add(i+2);
                        ind.Add(i+3);
                        i += 4;
                    }
                    if (y == 0 || data[(y - 1)*stride + x].A != 255)
                    {
                        //TOP FACE
                        buffer.Add(new VertexPositionNormalColor(
                            new Vector3(x / width, -y / height, 0), upn, color));

                        buffer.Add(new VertexPositionNormalColor(
                            new Vector3((x+1) / width, -y / height, 0), upn, color));

                        buffer.Add(new VertexPositionNormalColor(
                            new Vector3((x + 1) / width, -y / height, 1), upn, color));

                        buffer.Add(new VertexPositionNormalColor(
                            new Vector3(x / width, -y / height, 1), upn, color));
                        ind.Add(i);
                        ind.Add(i+1);
                        ind.Add(i+2);
                        ind.Add(i);
                        ind.Add(i+2);
                        ind.Add(i+3);
                        i += 4;
                    }
                    if (y + 1 == height || data[(y + 1)*stride + x].A != 255)
                    {
                        //BOTTOM FACE
                        buffer.Add(new VertexPositionNormalColor(
                            new Vector3(x / width, -(y+1) / height, 0), upn, color));

                        buffer.Add(new VertexPositionNormalColor(
                            new Vector3((x + 1) / width, -(y+1) / height, 0), upn, color));

                        buffer.Add(new VertexPositionNormalColor(
                            new Vector3((x + 1) / width, -(y+1) / height, 1), upn, color));

                        buffer.Add(new VertexPositionNormalColor(
                            new Vector3(x / width, -(y+1) / height, 1), upn, color));
                        ind.Add(i);
                        ind.Add(i + 2);
                        ind.Add(i + 1);
                        ind.Add(i);
                        ind.Add(i + 3);
                        ind.Add(i + 2);
                        i += 4;
                    }
                }
            }
            return new GeometricPrimitive<VertexPositionNormalColor>(tex.GraphicsDevice, buffer.ToArray(), ind.ToArray());
        }
Пример #17
0
 public TextureColors(Texture2D tex)
 {
     switch (tex.Format.Value)
     {
         case Format.B8G8R8A8_UNorm:
             _bgras = tex.GetData<ColorBGRA>();
             Stride = tex.CalculateWidth<ColorBGRA>();
             break;
         case Format.R8G8B8A8_UNorm:
             _colors = tex.GetData<Color>();
             Stride = tex.CalculateWidth<Color>();
             break;
         default:
             throw new NotSupportedException("Texture pixel format " + tex.Format.Value + " is not supported");
     }
 }
Пример #18
0
        protected override void LoadContent()
        {
            base.LoadContent();

            _basicEffect = ToDisposeContent(new BasicEffect(GraphicsDevice));

            _basicEffect.EnableDefaultLighting();
            _basicEffect.SpecularPower = 64;
            var desc = _basicEffect.Sampler.Description;
            desc.Filter = Filter.ComparisonMinMagMipPoint;
            desc.MaximumAnisotropy = 8;
            desc.AddressU = TextureAddressMode.Wrap;
            desc.AddressV = TextureAddressMode.Wrap;
            desc.AddressW = TextureAddressMode.Wrap;
            desc.MaximumLod = float.MaxValue;
            desc.MinimumLod = 0;
            _basicEffect.Sampler = SamplerState.New(GraphicsDevice, desc);

            _manTexture = Content.Load<Texture2D>("textures/mobs/man");
            _man = ToDisposeContent(MeshGenerator.CreateTextureMesh(_manTexture));
            _manTransform = Matrix.Scaling(.6f, 1.6f, .05f)*Matrix.Translation(-.4f, 1.4f, 1);

            _bookshelfTexture = Content.Load<Texture2D>("textures/walls/decorations/bookshelf");
            _bookshelf = ToDisposeContent(MeshGenerator.CreateTextureMesh(_bookshelfTexture));
            _bookshelfTransform = Matrix.Scaling(1.54f, 1f, .5f) * Matrix.Translation(-3.9f, 1f, -5f);

            _windowTexture = Content.Load<Texture2D>("textures/walls/decorations/window outside");
            _window = ToDisposeContent(MeshGenerator.CreateTextureMesh(_windowTexture));
            _windowTransform = Matrix.Scaling(1.5f, 1.5f, .03f) * Matrix.Translation(3f, 1.75f, -5f);

            _curtainTexture = Content.Load<Texture2D>("textures/walls/decorations/red curtain open");
            _curtain = ToDisposeContent(MeshGenerator.CreateTextureMesh(_curtainTexture));
            _curtainTransform = Matrix.Scaling(1.5f, 1.5f, .1f) * Matrix.Translation(3.1f, 1.75f, -5f);

            _curtain2Texture = Content.Load<Texture2D>("textures/walls/decorations/red curtain open inverse");
            _curtain2 = ToDisposeContent(MeshGenerator.CreateTextureMesh(_curtain2Texture));
            _curtain2Transform = Matrix.Scaling(1.5f, 1.5f, .1f) * Matrix.Translation(2.9f, 1.75f, -5f);

            _borderTexture = Content.Load<Texture2D>("textures/floors/moss border");
            _border = ToDisposeContent(MeshGenerator.CreateTextureMesh(_borderTexture));
            _borderTransform = Matrix.RotationX(-MathUtil.PiOverTwo)*Matrix.Scaling(10, 0.1f, 10) * Matrix.Translation(-5, 0, -5);

            _plane = ToDisposeContent(GeometricPrimitive.Plane.New(GraphicsDevice, 10, 10, 1, new Vector2(2)));
            _planeTexture = Content.Load<Texture2D>("textures/floors/floorboards 1 large dds");
            _planeTransform = Matrix.RotationX(-MathUtil.PiOverTwo)*Matrix.Translation(0, 0, 0);

            _carpetTexture = Content.Load<Texture2D>("textures/floors/carpets/carpet brown");
            _carpet = ToDisposeContent(MeshGenerator.CreateTextureMesh(_carpetTexture));
            _carpetTransform = Matrix.Scaling(4f, 2f, .05f) * Matrix.RotationX(-MathUtil.PiOverTwo) * Matrix.Translation(-2, 0f, 0);

            _pumpkinTexture = Content.Load<Texture2D>("textures/misc/pumkin");
            _pumpkin = ToDisposeContent(MeshGenerator.CreateTextureMesh(_pumpkinTexture));
            _pumpkinTransform = Matrix.Scaling(1f, 1f, .5f) * Matrix.Translation(3, 0.9f, -2);

            _wall = ToDisposeContent(GeometricPrimitive.Plane.New(GraphicsDevice, 10, 2, 1, new Vector2(4, 1f)));
            _wallTexture = Content.Load<Texture2D>("textures/walls/wall testure warped");
            _wallTransform1 = Matrix.Translation(0, 1, -5);
            _wallTransform2 = _wallTransform1*Matrix.RotationY(-MathUtil.PiOverTwo);
            _wallTransform3 = _wallTransform2*Matrix.RotationY(-MathUtil.PiOverTwo);
            _wallTransform4 = _wallTransform3*Matrix.RotationY(-MathUtil.PiOverTwo);

            _doorTexture = Content.Load<Texture2D>("textures/walls/doors/door");
            _door = ToDisposeContent(MeshGenerator.CreateTextureMesh(_doorTexture));
            _doorTransform1 = Matrix.Scaling(1f, 1.8f, .1f)*Matrix.Translation(-0.46f, 1.8f, -5)*
                             Matrix.RotationY(-MathUtil.PiOverTwo);
            _doorTransform2 = _doorTransform1*Matrix.RotationY(-MathUtil.PiOverTwo);
            _doorTransform3 = _doorTransform2 * Matrix.RotationY(-MathUtil.PiOverTwo);
            _doorTransform4 = _doorTransform3 * Matrix.RotationY(-MathUtil.PiOverTwo);

            var blendStateDesc = new BlendStateDescription
            {
                AlphaToCoverageEnable = true,
                IndependentBlendEnable = false
            };
            blendStateDesc.RenderTarget[0].IsBlendEnabled = true;
            blendStateDesc.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
            blendStateDesc.RenderTarget[0].SourceBlend = BlendOption.SourceAlpha;
            blendStateDesc.RenderTarget[0].DestinationBlend = BlendOption.InverseSourceAlpha;
            blendStateDesc.RenderTarget[0].BlendOperation = BlendOperation.Add;
            blendStateDesc.RenderTarget[0].SourceAlphaBlend = BlendOption.SourceAlpha;
            blendStateDesc.RenderTarget[0].DestinationAlphaBlend = BlendOption.DestinationAlpha;
            blendStateDesc.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add;
            blendStateDesc.RenderTarget[0].SourceBlend = BlendOption.One;
            _blend = BlendState.New(GraphicsDevice, blendStateDesc);
        }
Пример #19
0
        protected override void LoadContent()
        {
            spriteBatch = ToDisposeContent(new SpriteBatch(GraphicsDevice));

            // Sprite Font
            // [Arial16.xml] Padrão do Sharp DX
            arial16Font = Content.Load <SpriteFont>("Arial16");

            // EfeitoBásico Objeto Primitiva 1
            basicEffectPrimitive1 = ToDisposeContent(new BasicEffect(GraphicsDevice));
            basicEffectPrimitive1.PreferPerPixelLighting = true;
            basicEffectPrimitive1.Alpha = 1;
            basicEffectPrimitive1.EnableDefaultLighting();
            basicEffectPrimitive1.TextureEnabled = true;

            // EfeitoBásico Objeto Primitiva 2
            basicEffectPrimitive2 = ToDisposeContent(new BasicEffect(GraphicsDevice));
            basicEffectPrimitive2.PreferPerPixelLighting = true;
            basicEffectPrimitive2.EnableDefaultLighting();
            basicEffectPrimitive2.TextureEnabled = true;

            // EfeitoBásico Objeto Primitiva 3
            basicEffectPrimitive3 = ToDisposeContent(new BasicEffect(GraphicsDevice));
            basicEffectPrimitive3.PreferPerPixelLighting = true;
            basicEffectPrimitive3.EnableDefaultLighting();
            basicEffectPrimitive3.TextureEnabled = true;

            // EfeitoBásico Objeto Primitiva 4
            basicEffectPrimitive4 = ToDisposeContent(new BasicEffect(GraphicsDevice));
            basicEffectPrimitive4.PreferPerPixelLighting = true;
            basicEffectPrimitive4.EnableDefaultLighting();
            basicEffectPrimitive4.TextureEnabled = true;

            // EfeitoBásico Objeto Primitiva 5
            basicEffectPrimitive5 = ToDisposeContent(new BasicEffect(GraphicsDevice));
            basicEffectPrimitive5.PreferPerPixelLighting = true;
            basicEffectPrimitive5.EnableDefaultLighting();
            basicEffectPrimitive5.TextureEnabled = true;

            // EfeitoBásico Objeto Primitiva 6
            basicEffectPrimitive6 = ToDisposeContent(new BasicEffect(GraphicsDevice));
            basicEffectPrimitive6.PreferPerPixelLighting = true;
            basicEffectPrimitive6.EnableDefaultLighting();
            basicEffectPrimitive6.TextureEnabled = true;

            // EfeitoBásico Objeto Primitiva 7
            basicEffectPrimitive7 = ToDisposeContent(new BasicEffect(GraphicsDevice));
            basicEffectPrimitive7.PreferPerPixelLighting = true;
            basicEffectPrimitive7.EnableDefaultLighting();
            basicEffectPrimitive7.TextureEnabled = true;

            // PILAR 1
            basicEffectpilar1 = ToDisposeContent(new BasicEffect(GraphicsDevice));
            basicEffectpilar1.PreferPerPixelLighting = true;
            basicEffectpilar1.EnableDefaultLighting();
            basicEffectpilar1.TextureEnabled = true;

            // PILAR 2
            basicEffectpilar2 = ToDisposeContent(new BasicEffect(GraphicsDevice));
            basicEffectpilar2.PreferPerPixelLighting = true;
            basicEffectpilar2.EnableDefaultLighting();
            basicEffectpilar2.TextureEnabled = true;

            // Cria Objetos
            primitive  = ToDisposeContent(GeometricPrimitive.Cylinder.New(GraphicsDevice));
            primitive2 = ToDisposeContent(GeometricPrimitive.Torus.New(GraphicsDevice));
            primitive3 = ToDisposeContent(GeometricPrimitive.Sphere.New(GraphicsDevice));
            primitive4 = ToDisposeContent(GeometricPrimitive.Cylinder.New(GraphicsDevice));
            primitive5 = ToDisposeContent(GeometricPrimitive.Cylinder.New(GraphicsDevice));
            primitive6 = ToDisposeContent(GeometricPrimitive.Torus.New(GraphicsDevice));
            primitive7 = ToDisposeContent(GeometricPrimitive.Cylinder.New(GraphicsDevice));
            pilar1     = ToDisposeContent(GeometricPrimitive.Cylinder.New(GraphicsDevice));
            pilar2     = ToDisposeContent(GeometricPrimitive.Cylinder.New(GraphicsDevice));

            primitiveTexture       = Content.Load <SharpDX.Toolkit.Graphics.Texture2D>("CANHAO");   // CANHAO
            primitive2Texture      = Content.Load <SharpDX.Toolkit.Graphics.Texture2D>("RODA");     // RODA1
            primitive3Texture      = Content.Load <SharpDX.Toolkit.Graphics.Texture2D>("BALA");     // BALA
            primitive4Texture      = Content.Load <SharpDX.Toolkit.Graphics.Texture2D>("CHAO");     // CHAO
            primitive5Texture      = Content.Load <SharpDX.Toolkit.Graphics.Texture2D>("ALVO");     // ALVO
            primitive6Texture      = Content.Load <SharpDX.Toolkit.Graphics.Texture2D>("RODA");     // RODA2
            primitive7Texture      = Content.Load <SharpDX.Toolkit.Graphics.Texture2D>("UNIVERSO"); // HORIZONTE
            primitivepilar1Texture = Content.Load <SharpDX.Toolkit.Graphics.Texture2D>("COLUNA");   // Pilar 1
            primitivepilar2Texture = Content.Load <SharpDX.Toolkit.Graphics.Texture2D>("COLUNA");   // Pilar 2

            //SomAcerto = Content.Load<SoundEffect>("Acerto"); // SOM ACERTO
            //SomErro   = Content.Load<SoundEffect>("Acerto"); // SOM ERRO
            //SomTiro   = Content.Load<SoundEffect>("SOMTIRO"); // SOM TIRO
            //SomMusica = Content.Load<SoundEffect>("Acerto"); // MUSICA



            base.LoadContent();
        }
Пример #20
0
        private void initResources(int width, int height)
        {
            Texture2DDescription description = new Texture2DDescription()
            {
                Width = width,
                Height = height,
                MipLevels = 1,
                ArraySize = 1,
                Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                BindFlags = BindFlags.None,
                CpuAccessFlags = CpuAccessFlags.Read,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count = 1,
                    Quality = 0
                },
                Usage = ResourceUsage.Staging,
                OptionFlags = ResourceOptionFlags.None
            };

            _stagingTexture = SharpDX.Toolkit.Graphics.Texture2D.New(graphicsDevice, description);

            _buffer = new byte[width * height * 4];
            _writeableBitmap = new WriteableBitmap(
                width, height, 96, 96, PixelFormats.Bgra32, null);
        }
Пример #21
0
 public void SetTexture(SharpDX.Toolkit.Graphics.Texture2D texture, string param)
 {
     gbufferEffect.Parameters[param].SetResource(texture);
 }
Пример #22
0
        private unsafe void InitializeGraphics(GraphicsDevice graphicsDevice)
        {
            var colorBitmap = new Bitmap(gridlet.XLength, gridlet.YLength, PixelFormat.Format32bppRgb);
             var bitmapData = colorBitmap.LockBits(new System.Drawing.Rectangle(0, 0, gridlet.XLength, gridlet.YLength), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
             var pScan0 = (byte*)bitmapData.Scan0;
             for (var y = 0; y < gridlet.YLength; y++) {
            var pCurrentPixel = pScan0 + bitmapData.Stride * y;
            for (var x = 0; x < gridlet.XLength; x++) {
               var cell = gridlet.Cells[x + gridlet.XLength * y];
               uint color = 0xEFEFEF;
               if (cell.Flags.HasFlag(CellFlags.Debug)) {
                  color = 0x0000FF;
               } else if (cell.Flags.HasFlag(CellFlags.Connector)) {
                  color = 0xFF7F00;
               } else if (cell.Flags.HasFlag(CellFlags.Edge)) {
                  color = 0x00FF00;
               } else if (cell.Flags.HasFlag(CellFlags.Blocked)) {
                  color = 0xFF0000;
               }
               *(uint*)pCurrentPixel = color;
               pCurrentPixel += 4;
            }
             }
             colorBitmap.UnlockBits(bitmapData);
             var ms = new MemoryStream();
             colorBitmap.Save(ms, ImageFormat.Bmp);
             ms.Position = 0;
             var image = SharpDX.Toolkit.Graphics.Image.Load(ms);
            //         debugTexture = Texture2D.Load(graphicsDevice, @"E:\lolmodprojects\Project Master Yi\masteryi_base_r_cas_shockwave.dds");
             debugTexture = Texture2D.New(graphicsDevice, image);
             var vertices = new VertexPositionNormalTexture[gridlet.XLength * gridlet.YLength * 4];
             var indices = new int[gridlet.XLength * gridlet.YLength * 6 + (gridlet.XLength - 1) * (gridlet.YLength - 1) * 12];
             int ibOffsetBase = 0;
             var xOffset = -gridlet.XLength / 2.0f;
             var yOffset = -gridlet.YLength / 2.0f;
             for (var y = 0; y < gridlet.YLength; y++) {
            for (var x = 0; x < gridlet.XLength; x++) {
               var cellIndex = x + y * gridlet.XLength;
               var cell = gridlet.Cells[cellIndex];
               var cellHeight = cell.Height;
               var vbOffset = cellIndex * 4;
               var uv = new Vector2(x / (float)(gridlet.XLength - 1), y / (float)(gridlet.YLength - 1));
               vertices[vbOffset + 0] = new VertexPositionNormalTexture(new Vector3(x + xOffset, y + yOffset, cellHeight), Vector3.UnitZ, uv);
               vertices[vbOffset + 1] = new VertexPositionNormalTexture(new Vector3(x + 1 + xOffset, y + yOffset, cellHeight), Vector3.UnitZ, uv);
               vertices[vbOffset + 2] = new VertexPositionNormalTexture(new Vector3(x + 1 + xOffset, y + 1 + yOffset, cellHeight), Vector3.UnitZ, uv);
               vertices[vbOffset + 3] = new VertexPositionNormalTexture(new Vector3(x + xOffset, y + 1 + yOffset, cellHeight), Vector3.UnitZ, uv);

               var ibOffset = ibOffsetBase + cellIndex * 6;
               indices[ibOffset + 0] = vbOffset;
               indices[ibOffset + 1] = vbOffset + 3;
               indices[ibOffset + 2] = vbOffset + 1;
               indices[ibOffset + 3] = vbOffset + 1;
               indices[ibOffset + 4] = vbOffset + 3;
               indices[ibOffset + 5] = vbOffset + 2;
            }
             }

             ibOffsetBase = gridlet.XLength * gridlet.YLength * 6;
             for (var y = 0; y < gridlet.YLength - 1; y++) {
            for (var x = 0; x < gridlet.XLength - 1; x++) {
               var cellIndex = x + y * gridlet.XLength;
               var cell = gridlet.Cells[cellIndex];
               var cellHeight = cell.Height;
               var vbOffset = cellIndex * 4;
               var rightVbOffset = vbOffset + 4;
               var downVbOffset = vbOffset + gridlet.XLength * 4;

               var ibOffset = ibOffsetBase + (x + y * (gridlet.XLength - 1)) * 12;
               indices[ibOffset + 0] = vbOffset + 1;
               indices[ibOffset + 1] = vbOffset + 2;
               indices[ibOffset + 2] = rightVbOffset;
               indices[ibOffset + 3] = rightVbOffset;
               indices[ibOffset + 4] = vbOffset + 2;
               indices[ibOffset + 5] = rightVbOffset + 3;
               indices[ibOffset + 6] = vbOffset + 2;
               indices[ibOffset + 7] = vbOffset + 3;
               indices[ibOffset + 8] = downVbOffset;
               indices[ibOffset + 9] = downVbOffset;
               indices[ibOffset + 10] = downVbOffset + 1;
               indices[ibOffset + 11] = vbOffset + 2;
            }
             }
             vertexBuffer = Buffer.Vertex.New(graphicsDevice, vertices);
             vertexInputLayout = VertexInputLayout.FromBuffer(0, vertexBuffer);
             indexBuffer = Buffer.Index.New(graphicsDevice, indices);
        }