iluFlipImage导致图形问题

时间:2012-02-07 23:48:48

标签: c# devil tao-framework

我现在读了一本书。它关于OpenGl。有一个例子用魔鬼加载纹理。 我已经实现了书1:1中的代码。但是iluFlipImage函数会导致图形问题。 我使用.Net Framework 4.0和toa-framework 2.1


翻转

flipped


没有翻转

not flipped


    public void LoadTexture(string textureName, string path)
    {
        int devIlId;
        Il.ilGenImages(1, out devIlId);
        Gl.glBindTexture(Gl.GL_TEXTURE_2D, devIlId);

        if (!Il.ilLoadImage(path))
        {
            System.Diagnostics.Debug.Assert(false,
            "Could not open file, [" + path + "].");
        }

        Ilu.iluFlipImage();

        var width = Il.ilGetInteger(Il.IL_IMAGE_WIDTH);
        var height = Il.ilGetInteger(Il.IL_IMAGE_HEIGHT);
        var textureId = Ilut.ilutGLBindTexImage();

        System.Diagnostics.Debug.Assert(textureId != 0);
        Il.ilDeleteImages(1, ref devIlId);
        _textureStorage.Add(textureName, new Texture(textureId, path, width, height));
    }

1 个答案:

答案 0 :(得分:0)

    public void LoadTexture(string name, string path)
    {
        var bitmap = new Bitmap(path);
        var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                                         ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
        int textureId;
        Gl.glGenTextures(1, out textureId);
        Gl.glBindTexture(Gl.GL_TEXTURE_2D, textureId);
        Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA8, bitmap.Width, bitmap.Height, 0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, bitmapData.Scan0);
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
        _textureStorage.Add(name, new Texture(textureId, path, bitmap.Width, bitmap.Height));

        bitmap.UnlockBits(bitmapData);
        bitmap.Dispose();
    }