OpenTk texture not display the image

本秂侑毒 提交于 2020-01-16 15:47:14

问题


OpenTk texture not display the image

It shows only a white triangle, but not with the image i try to load.

Here is my code:

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);
        }
    }
}
}

There isn't any errors or warning. I just don't know what is missing.


回答1:


You don't seem to be binding your texture at all.

You load it from the disk, and send it to openGL, and you get back an ID for it, but you don't tell opengl that you want to use the texture.

try this:

//"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();


来源:https://stackoverflow.com/questions/19169452/opentk-texture-not-display-the-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!