示例#1
0
        private void AddReflectionTest()
        {
            var environmentMap = new TextureCubemap();
            var images         = new Bitmap[6] {
                (Bitmap)Bitmap.FromFile("Resources\\Textures\\Cubemap\\xneg.png"),
                (Bitmap)Bitmap.FromFile("Resources\\Textures\\Cubemap\\xpos.png"),
                (Bitmap)Bitmap.FromFile("Resources\\Textures\\Cubemap\\yneg.png"),
                (Bitmap)Bitmap.FromFile("Resources\\Textures\\Cubemap\\ypos.png"),
                (Bitmap)Bitmap.FromFile("Resources\\Textures\\Cubemap\\zneg.png"),
                (Bitmap)Bitmap.FromFile("Resources\\Textures\\Cubemap\\zpos.png"),
            };

            environmentMap.SetImages(images);

            var mesh = Icosahedron.Create(0.4f, 3);

            var material = new Material(MaterialType.RimReflectionDiffuseTextureColor, RenderQueue.Opaque);

            material.Color = Vector4.Zero;
            material.SetTexture("uniform_ReflectionTexture", environmentMap);

            var so       = new Node(_scene, "reflective");
            var renderer = so.AddComponent <MeshRenderable>();

            renderer.Material = material;
            renderer.Mesh     = mesh;
        }
        /// <summary>
        /// Creates a <see cref="TextureCubemap"/> with the given files as texture image data.
        /// </summary>
        /// <param name="graphicsDevice">The <see cref="GraphicsDevice"/> the resource will use.</param>
        /// <param name="imgNegX">An image file with the cubemap's negative X face.</param>
        /// <param name="imgPosX">An image file with the cubemap's positive X face.</param>
        /// <param name="imgNegY">An image file with the cubemap's negative Y face.</param>
        /// <param name="imgPosY">An image file with the cubemap's positive Y face.</param>
        /// <param name="imgNegZ">An image file with the cubemap's negative Z face.</param>
        /// <param name="imgPosZ">An image file with the cubemap's positive Z face.</param>
        /// <param name="generateMipmaps">Whether to generate mipmaps for the <see cref="TextureCubemap"/>.</param>
        public static TextureCubemap FromFiles(GraphicsDevice graphicsDevice, string imgNegX, string imgPosX, string imgNegY, string imgPosY, string imgNegZ, string imgPosZ, bool generateMipmaps = false)
        {
            using Image <Rgba32> negx = Image.Load <Rgba32>(imgNegX);

            if (negx.Width != negx.Height)
            {
                throw new InvalidOperationException("The width and height of all the images must be the same for a cubemap.");
            }

            TextureCubemap cubemap = new TextureCubemap(graphicsDevice, (uint)negx.Width);

            try
            {
                cubemap.SetData(CubemapFace.NegativeX, negx);
                cubemap.SetData(CubemapFace.PositiveX, imgPosX);
                cubemap.SetData(CubemapFace.NegativeY, imgNegY);
                cubemap.SetData(CubemapFace.PositiveY, imgPosY);
                cubemap.SetData(CubemapFace.NegativeZ, imgNegZ);
                cubemap.SetData(CubemapFace.PositiveZ, imgPosZ);

                if (generateMipmaps)
                {
                    cubemap.GenerateMipmaps();
                }

                return(cubemap);
            }
            catch
            {
                cubemap.Dispose();
                throw;
            }
        }
        /// <summary>
        /// Sets the data of one of the <see cref="TextureCubemap"/>'s faces from an <see cref="Image{Rgba32}"/>.
        /// </summary>
        /// <param name="texture">The <see cref="TextureCubemap"/> to set data for.</param>
        /// <param name="face">The face of the cubemap to set data for.</param>
        /// <param name="image">The image with the pixel data to set.</param>
        public static void SetData(this TextureCubemap texture, CubemapFace face, Image <Rgba32> image)
        {
            if (texture == null)
            {
                throw new ArgumentNullException(nameof(texture));
            }

            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            if (texture.ImageFormat != TextureImageFormat.Color4b)
            {
                throw new InvalidOperationException(nameof(TextureCubemap.ImageFormat) + " must be " + nameof(TextureImageFormat.Color4b) + " in order to do this");
            }

            if (image.Width != texture.Size || image.Height != texture.Size)
            {
                throw new InvalidOperationException("The width and height of the image must match " + nameof(TextureCubemap.Size));
            }

            if (!image.TryGetSinglePixelSpan(out Span <Rgba32> pixels))
            {
                throw new InvalidDataException(ImageUtils.ImageNotContiguousError);
            }
            texture.SetData <Rgba32>(face, pixels, PixelFormat.Rgba);
        }
示例#4
0
        protected override void OnLoad()
        {
            inputManager = new InputManager3D(InputContext);

            cubemap = TextureCubemapExtensions.FromFiles(
                graphicsDevice,
                "cubemap/back.png", "cubemap/front.png",
                "cubemap/bottom.png", "cubemap/top.png",
                "cubemap/left.png", "cubemap/right.png"
                );
            cubemap.SetTextureFilters(TextureMinFilter.Linear, TextureMagFilter.Linear);

            shaderProgram = ShaderProgram.FromFiles <VertexPosition>(graphicsDevice, "vs.glsl", "fs.glsl", "vPosition");

            shaderProgram.Uniforms["cubemap"].SetValueTexture(cubemap);
            shaderProgram.Uniforms["World"].SetValueMat4(Matrix4x4.Identity);
            viewUniform = shaderProgram.Uniforms["View"];
            viewUniform.SetValueMat4(Matrix4x4.Identity);

            Span <VertexPosition> vertexData = stackalloc VertexPosition[]
            {
                new Vector3(-0.5f, -0.5f, -0.5f), //4
                new Vector3(-0.5f, -0.5f, 0.5f),  //3
                new Vector3(-0.5f, 0.5f, -0.5f),  //7
                new Vector3(-0.5f, 0.5f, 0.5f),   //8
                new Vector3(0.5f, 0.5f, 0.5f),    //5
                new Vector3(-0.5f, -0.5f, 0.5f),  //3
                new Vector3(0.5f, -0.5f, 0.5f),   //1
                new Vector3(-0.5f, -0.5f, -0.5f), //4
                new Vector3(0.5f, -0.5f, -0.5f),  //2
                new Vector3(-0.5f, 0.5f, -0.5f),  //7
                new Vector3(0.5f, 0.5f, -0.5f),   //6
                new Vector3(0.5f, 0.5f, 0.5f),    //5
                new Vector3(0.5f, -0.5f, -0.5f),  //2
                new Vector3(0.5f, -0.5f, 0.5f),   //1
            };

            vertexBuffer = new VertexBuffer <VertexPosition>(graphicsDevice, vertexData, BufferUsage.StaticDraw);

            graphicsDevice.DepthTestingEnabled = false;
            graphicsDevice.BlendingEnabled     = false;
        }
示例#5
0
 /// <summary>
 /// Attaches a single face of the given cube map texture level to the an attachment point.
 /// </summary>
 /// <param name="target">The framebuffer target to bind to.</param>
 /// <param name="attachment">The attachment point to attach to.</param>
 /// <param name="texture">The texture to attach.</param>
 /// <param name="face">The cube map face of the texture to attach.</param>
 /// <param name="level">The level of the texture to attach.</param>
 public void Attach(FramebufferTarget target, FramebufferAttachment attachment, TextureCubemap texture, int face, int level = 0)
 {
     Attach(target, attachment, (LayeredTexture)texture, face, level);
 }
示例#6
0
 /// <summary>
 /// Binds a single face of the given texture level to an image unit.
 /// </summary>
 /// <param name="imageUnit">The image unit to use.</param>
 /// <param name="texture">The texture to bind.</param>
 /// <param name="level">The mipmap level to bind.</param>
 /// <param name="face">The cube map face to bind.</param>
 /// <param name="access">Specifies the type of access allowed on the image.</param>
 public void Bind(int imageUnit, TextureCubemap texture, int level, int face, TextureAccess access)
 {
     Bind(imageUnit, texture, level, false, face, access);
 }
 /// <summary>
 /// Sets the data of one of the <see cref="TextureCubemap"/>'s faces from a file.
 /// </summary>
 /// <param name="texture">The <see cref="TextureCubemap"/> to set data for.</param>
 /// <param name="face">The face of the cubemap to set data for.</param>
 /// <param name="file">The file containing the image with the pixel data to set.</param>
 public static void SetData(this TextureCubemap texture, CubemapFace face, string file)
 {
     using Image <Rgba32> image = Image.Load <Rgba32>(file);
     SetData(texture, face, image);
 }
 /// <summary>
 /// Sets the data of one of the <see cref="TextureCubemap"/>'s faces from a stream.
 /// </summary>
 /// <param name="texture">The <see cref="TextureCubemap"/> to set data for.</param>
 /// <param name="face">The face of the cubemap to set data for.</param>
 /// <param name="stream">The stream from which to load an image.</param>
 public static void SetData(this TextureCubemap texture, CubemapFace face, Stream stream)
 {
     using Image <Rgba32> image = Image.Load <Rgba32>(stream);
     SetData(texture, face, stream);
 }