0

OpenTk 纹理不显示图像

它只显示一个白色三角形,但不显示我尝试加载的图像。

这是我的代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;

namespace StarterKit{
class Game : GameWindow
{
    uint Texture;


    public Game()
        : base(800, 600, GraphicsMode.Default, "OpenTK Quick Start Sample")
    {
        VSync = VSyncMode.On;
    }

    static int LoadTexture(string filename)
    {
        if (String.IsNullOrEmpty(filename))
            throw new ArgumentException(filename);

        int id = GL.GenTexture();
        GL.BindTexture(TextureTarget.Texture2D, id);

        Bitmap bmp = new Bitmap(filename);
        BitmapData bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp_data.Width, bmp_data.Height, 0,
            OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmp_data.Scan0);

        bmp.UnlockBits(bmp_data);


        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

        return id;
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        GL.ClearColor(0.1f, 0.2f, 0.5f, 0.0f);
        GL.Enable(EnableCap.DepthTest);
        GL.Enable(EnableCap.Texture2D);


        Texture = (uint)LoadTexture("unknown.png");

        return;
    }


    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);

        GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);

        Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 64.0f);
        GL.MatrixMode(MatrixMode.Projection);
        GL.LoadMatrix(ref projection);
    }


    protected override void OnUpdateFrame(FrameEventArgs e)
    {
        base.OnUpdateFrame(e);

        if (Keyboard[Key.Escape])
            Exit();
    }


    protected override void OnRenderFrame(FrameEventArgs e)
    {
        base.OnRenderFrame(e);

        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

        Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadMatrix(ref modelview);

        GL.Begin(BeginMode.Triangles);

        GL.TexCoord2(0, 0);
        GL.Vertex3(-1.0f, -1.0f, 4.0f);
        GL.TexCoord2(1, 0);
        GL.Vertex3(1.0f, -1.0f, 4.0f);
        GL.TexCoord2(0.5, 1);
        GL.Vertex3(0.0f, 1.0f, 4.0f);

        GL.End();

        SwapBuffers();
    }


    [STAThread]
    static void Main()
    {

        using (Game game = new Game())
        {
            game.Run(30.0);
        }
    }
}
}

没有任何错误或警告。我只是不知道缺少什么。

4

1 回答 1

2

你似乎根本没有绑定你的纹理。

你从磁盘加载它,然后将它发送到 openGL,然后你会得到它的 ID,但你没有告诉 opengl 你想使用纹理。

尝试这个:

//"Texture" is the uint you got back from openTK when you loaded the texture

GL.Begin(BeginMode.Triangles);
GL.BindTexture(TextureTarget.Texture2D, Texture);
GL.TexCoord2(0, 0);
GL.Vertex3(-1.0f, -1.0f, 4.0f);
GL.TexCoord2(1, 0);
GL.Vertex3(1.0f, -1.0f, 4.0f);
GL.TexCoord2(0.5, 1);
GL.Vertex3(0.0f, 1.0f, 4.0f);

GL.End();

SwapBuffers();
于 2013-12-06T16:01:41.410 回答