Esempio n. 1
0
 private static void SetParameters(TextureWraps wrap, TextureFilters filter)
 {
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (int)filter);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (int)filter);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (int)wrap);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (int)wrap);
 }
Esempio n. 2
0
 public Sprite(string filename, ivec2 origin, TextureWraps wrap = TextureWraps.Repeat,
               TextureFilters filter = TextureFilters.Nearest) :
     this(ContentCache.GetTexture(filename, false, true, wrap, filter), new SourceRect(),
          Alignments.Custom)
 {
     this.origin = origin;
 }
Esempio n. 3
0
        public unsafe Texture(int width, int height, TextureWraps wrap = TextureWraps.Repeat,
                              TextureFilters filter = TextureFilters.Nearest) : base(width, height)
        {
            uint id = 0;

            glGenTextures(1, &id);
            SetParameters(wrap, filter);
            Id = id;
        }
Esempio n. 4
0
        public static unsafe Texture Load(string filename, string folder, bool shouldStoreData = false,
                                          TextureWraps wrap = TextureWraps.Repeat, TextureFilters filter = TextureFilters.Nearest)
        {
            LoadData(folder + filename, out var width, out var height, out var data);

            uint id = 0;

            glGenTextures(1, &id);

            // Setting data also binds the texture.
            var texture = new Texture(id, width, height);

            texture.SetData(data, shouldStoreData);

            SetParameters(wrap, filter);

            return(texture);
        }
Esempio n. 5
0
        public static Texture GetTexture(string filename, bool shouldStoreData = false, bool shouldCache = true,
                                         TextureWraps wrap = TextureWraps.Repeat, TextureFilters filter  = TextureFilters.Nearest,
                                         string folder     = "Textures/")
        {
            Debug.Assert(!string.IsNullOrEmpty(filename), "Texture filename can't be null or empty.");
            Debug.Assert(!string.IsNullOrEmpty(folder), "Texture folder can't be null or empty.");

            if (!textures.TryGetValue(filename, out var texture))
            {
                texture = Texture.Load(filename, folder, shouldStoreData, wrap, filter);

                if (shouldCache)
                {
                    textures.Add(filename, texture);
                }
            }

            return(texture);
        }
Esempio n. 6
0
 public Texture(int width, int height, Core.Color[] data, TextureWraps wrap = TextureWraps.Repeat,
                TextureFilters filter = TextureFilters.Nearest) : this(width, height, wrap, filter)
 {
     SetData(width, height, data.Select(color => color.ToRgba()).ToArray());
 }
Esempio n. 7
0
 // These public constructors is useful when generating textures dynamically at runtime.
 public Texture(int width, int height, Core.Color fill, TextureWraps wrap = TextureWraps.Repeat,
                TextureFilters filter = TextureFilters.Nearest) : this(width, height, wrap, filter)
 {
     SetData(width, height, Enumerable.Repeat(fill.ToRgba(), width * height).ToArray());
 }
Esempio n. 8
0
        public unsafe RenderTarget(int width, int height, RenderTargetFlags flags,
                                   TextureWraps wrap = TextureWraps.Repeat, TextureFilters filter = TextureFilters.Nearest) :
            base(width, height)
        {
            fixed(uint *address = &framebufferId)
            {
                glGenFramebuffers(1, address);
            }

            glBindFramebuffer(GL_FRAMEBUFFER, framebufferId);

            uint id;

            glGenTextures(1, &id);
            glBindTexture(GL_TEXTURE_2D, id);
            Id = id;

            var isColorEnabled   = (flags & RenderTargetFlags.Color) > 0;
            var isDepthEnabled   = (flags & RenderTargetFlags.Depth) > 0;
            var isStencilEnabled = (flags & RenderTargetFlags.Stencil) > 0;

            uint texFormat;
            uint texType;
            uint texAttachment;

            if (isColorEnabled)
            {
                texFormat     = GL_RGBA;
                texType       = GL_UNSIGNED_BYTE;
                texAttachment = GL_COLOR_ATTACHMENT0;
            }
            else
            {
                texFormat     = GL_DEPTH_COMPONENT;
                texType       = GL_FLOAT;
                texAttachment = GL_DEPTH_ATTACHMENT;
            }

            glTexImage2D(GL_TEXTURE_2D, 0, (int)texFormat, (uint)width, (uint)height, 0, texFormat, texType, null);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (int)filter);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (int)filter);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (int)wrap);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (int)wrap);
            glFramebufferTexture2D(GL_FRAMEBUFFER, texAttachment, GL_TEXTURE_2D, id, 0);

            if (!isColorEnabled)
            {
                glDrawBuffer(GL_NONE);
                glReadBuffer(GL_NONE);
            }
            else if (isDepthEnabled)
            {
                var format     = (uint)(isStencilEnabled ? GL_DEPTH24_STENCIL8 : GL_DEPTH_COMPONENT);
                var attachment = (uint)(isStencilEnabled ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT);

                fixed(uint *address = &renderbufferId)
                {
                    glGenRenderbuffers(1, address);
                }

                glBindRenderbuffer(GL_RENDERBUFFER, renderbufferId);
                glRenderbufferStorage(GL_RENDERBUFFER, format, (uint)width, (uint)height);
                glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, renderbufferId);
                glBindRenderbuffer(GL_RENDERBUFFER, 0);
            }

            if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
            {
                Debug.Fail("Error creating render target.");
            }

            glBindFramebuffer(GL_FRAMEBUFFER, 0);

            if (isColorEnabled)
            {
                clearBits |= GL_COLOR_BUFFER_BIT;
            }

            if (isDepthEnabled)
            {
                clearBits |= GL_DEPTH_BUFFER_BIT;
            }

            if (isStencilEnabled)
            {
                clearBits |= GL_STENCIL_BUFFER_BIT;
            }
        }
Esempio n. 9
0
 public RenderTarget(ivec2 dimensions, RenderTargetFlags flags, TextureWraps wrap = TextureWraps.Repeat,
                     TextureFilters filter = TextureFilters.Nearest) :
     this(dimensions.x, dimensions.y, flags, wrap, filter)
 {
 }
Esempio n. 10
0
 public Sprite(string filename, SourceRect sourceRect, Alignments alignment = Alignments.Center,
               SpriteModifiers mods  = SpriteModifiers.None, TextureWraps wrap = TextureWraps.Repeat,
               TextureFilters filter = TextureFilters.Nearest) :
     this(ContentCache.GetTexture(filename, false, true, wrap, filter), sourceRect, alignment, mods)
 {
 }