示例#1
0
        protected virtual void Create()
        {
            LogManager.Log(LogLevel.Debug, "Texture.Create()");
            // Not needed on newer OpenGL
            if (!GlHelper.HasExtension("GL_ARB_texture_non_power_of_two"))
            {
                if (!IsPowerOf2(width) || !IsPowerOf2(height))
                {
                    throw new Exception("Texture size must be power of 2");
                }
            }

            GlUtil.Assert("before creating texture");
            CreateTexture();

            try {
                gl.BindTexture(gl.TEXTURE_2D, Handle);
                gl.TexImage2D(gl.TEXTURE_2D, 0, (int)glformat,
                              (int)width, (int)height, 0,
                              gl.RGBA, gl.UNSIGNED_BYTE, IntPtr.Zero);
                GlUtil.Assert("creating texture (too big?)");
                SetTextureParams();
            } catch (Exception) {
                uint[] handles = { Handle };
                gl.DeleteTextures(1, handles);
                throw;
            }
        }
示例#2
0
        protected override void Create()
        {
            // Not needed on newer OpenGL
            if (!GlHelper.HasExtension("GL_ARB_texture_non_power_of_two"))
            {
                if (!IsPowerOf2(width) || !IsPowerOf2(height))
                {
                    throw new Exception("Texture size must be power of 2");
                }
            }

            GlUtil.Assert("before creating texture");
            CreateTexture();

            try {
                gl.BindTexture(gl.TEXTURE_2D, Handle);

                gl.TexImage2D(gl.TEXTURE_2D, 0, (int)gl.RGBA,
                              (int)width, (int)height, 0, (pixbuf.HasAlpha ? gl.RGBA : gl.RGB),
                              gl.UNSIGNED_BYTE, pixbuf.Pixels);
                GlUtil.Assert("creating texture (too big?)");

                SetTextureParams();

                this.pixbuf = null;                 //gc can take it now
            } catch (Exception) {
                uint[] handles = { Handle };
                gl.DeleteTextures(1, handles);
                throw;
            }
        }
示例#3
0
        protected void CreateEmpty(uint width, uint height, uint glformat)
        {
            if (!IsPowerOf2(width) || !IsPowerOf2(height))
            {
                throw new Exception("Texture size must be power of 2");
            }
            this.width  = width;
            this.height = height;

            GlUtil.Assert("before creating texture");
            CreateTexture();

            try {
                gl.BindTexture(gl.TEXTURE_2D, handle);
                gl.TexImage2D(gl.TEXTURE_2D, 0, (int)glformat,
                              (int)width, (int)height, 0,
                              gl.RGBA, gl.UNSIGNED_BYTE, IntPtr.Zero);
                GlUtil.Assert("creating texture (too big?)");
                SetTextureParams();
            } catch (Exception) {
                uint[] handles = { handle };
                gl.DeleteTextures(1, handles);
                throw;
            }
        }
示例#4
0
        protected unsafe void CreateFromSurface(IntPtr surfacep, uint glformat)
        {
            Sdl.Surface *surface = (Sdl.Surface *)surfacep;
            this.width  = (uint)surface->w;
            this.height = (uint)surface->h;
            if (!IsPowerOf2(width) || !IsPowerOf2(height))
            {
                throw new Exception("Texture size must be power of 2");
            }

            GlUtil.Assert("before creating texture");
            CreateTexture();

            try {
                gl.BindTexture(gl.TEXTURE_2D, handle);
                uint surface_format = SetupPixelFormat(surfacep);

                gl.TexImage2D(gl.TEXTURE_2D, 0, (int)glformat,
                              (int)width, (int)height, 0, surface_format,
                              gl.UNSIGNED_BYTE, surface->pixels);
                GlUtil.Assert("creating texture (too big?)");

                SetTextureParams();
            } catch (Exception) {
                uint[] handles = { handle };
                gl.DeleteTextures(1, handles);
                throw;
            }
        }
示例#5
0
        public unsafe void copy_to(IntPtr surfacep, uint surface_x, uint surface_y,
                                   uint texture_x, uint texture_y,
                                   uint width, uint height)
        {
            Sdl.Surface *surface = (Sdl.Surface *)surfacep;
            PixelFormat *format  = (PixelFormat *)surface->format;

            GlUtil.Assert("Before update texture");

            uint surface_format = SetupPixelFormat(surfacep);


            /* We're extracting sub rectangles from the SDL_Surface pixeldata, by
             * setting the pitch to the real width, but telling OpenGL just our
             * desired image dimensions.
             */
            IntPtr pixeldata = (IntPtr)
                               (((byte *)surface->pixels) + surface_y * surface->pitch
                                + format->BytesPerPixel * surface_x);

            gl.TexSubImage2D(gl.TEXTURE_2D, 0, (int)texture_x, (int)texture_y,
                             (int)width, (int)height, surface_format,
                             gl.UNSIGNED_BYTE, pixeldata);

            GlUtil.Assert("Updating Texture Part");
        }
示例#6
0
        /// <summary>
        /// Helper method: set common texture parameters.
        /// </summary>
        protected static void SetTextureParams()
        {
            gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
            gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
            gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP);
            gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP);
            gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_R, gl.CLAMP);

            GlUtil.Assert("setting texture parameters");
        }