예제 #1
0
        /// <summary>
        /// Load the texture specified by "tex" with the identifier "textureId".
        /// </summary>
        public void SetTexture(int textureId, ITexture tex)
        {
            // Clear loaded texture
            if (textureIdToGlTextureHandle.ContainsKey(textureId))
            {
                GL.DeleteTexture(textureIdToGlTextureHandle[textureId]);
                textureIdToGlTextureHandle.Remove(textureId);
            }

            // Load new texture
            if (tex.LevelCount != 0)
            {
                int texIdx = GL.GenTexture();
                GL.BindTexture(TextureTarget.Texture2D, texIdx);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
                int textureLevelsToUse = Math.Max(1, tex.LevelCount - 3);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, textureLevelsToUse - 1); // Range is inclusive
                for (int level = 0; level < textureLevelsToUse; level++)
                {
                    int    dataRowLength = (tex.Width >> level);
                    byte[] data          = tex.DecodeLevelToRGBA8(level, dataRowLength * 4);
                    GL.PixelStore(PixelStoreParameter.UnpackRowLength, dataRowLength);
                    GL.TexImage2D(TextureTarget.Texture2D, level, PixelInternalFormat.Rgba, tex.Width >> level, tex.Height >> level,
                                  0, PixelFormat.Rgba, PixelType.UnsignedByte, data);
                }
                textureIdToGlTextureHandle[textureId] = texIdx;
            }
        }