Exemple #1
0
        public Texture(string name)
        {
            ImageResult image;

            try
            {
                using (Stream stream = File.OpenRead($"resources/textures/{name}.png"))
                {
                    image = new ImageStreamLoader().Load(stream, ColorComponents.RedGreenBlueAlpha);
                }
            }
            catch (IOException e)
            {
                Console.Error.WriteLine(e);
                return;
            }

            uint[] temp = new uint[1];
            ToolBox.gl.GenTextures(1, temp);
            _textureID = temp[0];

            if (_textureID == 0)
            {
                uint code = ToolBox.gl.GetError();
                Console.Error.WriteLine($"Error while creating texture for {name}: {code}");
                return;
            }

            ToolBox.gl.BindTexture(GL_TEXTURE_2D, _textureID);

            ToolBox.gl.TexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            ToolBox.gl.TexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

            ToolBox.gl.TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.Width, image.Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image.Data);
        }
Exemple #2
0
        public Texture(string path, string type)
        {
            var asset = AssetLoader.UseElement(path + type);

            //checks if the texture has already been loaded
            if (asset != null)
            {
                Texture tex = (Texture)asset;

                id        = tex.id;
                this.type = type;
                this.path = path;

                Console.WriteLine("Loading from hastable " + path + type);
            }
            else
            {
                this.path = path;
                this.type = type;

                GL.ActiveTexture(TextureUnit.Texture0);
                GL.GenTextures(1, out id);
                GL.BindTexture(TextureTarget.Texture2D, id);


                ImageStreamLoader loader = new ImageStreamLoader();
                using (Stream stream = File.Open(path, FileMode.Open))
                {
                    var image = loader.Load(stream, ColorComponents.RedGreenBlueAlpha);

                    GL.TexImage2D(TextureTarget.Texture2D, 0,
                                  PixelInternalFormat.Rgba
                                  , image.Width, image.Height, 0,
                                  PixelFormat.Rgba, PixelType.UnsignedByte, image.Data);
                }


                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter,
                                (int)TextureMinFilter.LinearMipmapLinear);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter,
                                (int)TextureMinFilter.Linear);



                GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);


                GL.BindTexture(TextureTarget.Texture2D, 0);

                Console.WriteLine("Loading into hashable " + path + type);

                AssetLoader.AddElement(path + type, this);
            }
        }
Exemple #3
0
        // Loads BMP using StbSharp. This allows us to load BMP files containing BITMAPV4HEADER and BITMAPV5HEADER
        // structures, which FreeImage does not support.
        TextureContent LoadImage(string filename)
        {
            var output = new Texture2DContent {
                Identity = new ContentIdentity(filename)
            };

            ImageResult result;

            using (var stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                var imageLoader = new ImageStreamLoader();
                result = imageLoader.Load(stream, ColorComponents.RedGreenBlueAlpha);
            }

            var face = new PixelBitmapContent <Color>(result.Width, result.Height);

            face.SetPixelData(result.Data);
            output.Faces[0].Add(face);

            return(output);
        }