C# OpenTk纹理不显示图像
OpenTk纹理不显示图像 它只显示了一个白色三角形,但没有显示我试图加载的图像 这是我的密码:C# OpenTk纹理不显示图像,c#,opentk,texture2d,C#,Opentk,Texture2d,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 Op
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);
}
}
}
}
没有任何错误或警告。我只是不知道缺少了什么。您似乎根本没有绑定您的纹理 您从磁盘加载它,并将其发送到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();
他确实绑定了它:
GL.BindTexture(TextureTarget.Texture2D,id)代码>是,但仅在onLoad
期间。然后,他在即时模式下继续绘制,而不告诉openGL何时需要他的纹理。