gpt4 book ai didi

c# - OpenGL C# (OpenTK) 加载和绘制图像功能不起作用

转载 作者:太空狗 更新时间:2023-10-29 21:40:39 34 4
gpt4 key购买 nike

我正在尝试在屏幕上加载和显示纹理,但是在应该显示纹理的地方收到了一个黑框。

两个主要方法是“LoadTexture”和“Draw Image”,我假设其中一个有错误。

using System;
using System.Diagnostics;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using System.Drawing;
using System.Drawing.Imaging;

namespace FailRender
{
public class FailRender: GameWindow
{
public FailRender() : base(800, 600, GraphicsMode.Default, "Hoard of Upgrades")
{
GL.ClearColor(0, 0.1f, 0.4f, 1);

texture = LoadTexture("sand.jpg");
}

private int texture;

public int LoadTexture(string file)
{
Bitmap bitmap = new Bitmap(file);

int tex;
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);

GL.GenTextures(1, out tex);
GL.BindTexture(TextureTarget.Texture2D, tex);

BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

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

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

return tex;
}

public static void DrawImage(int image)
{
GL.MatrixMode(MatrixMode.Projection);
GL.PushMatrix();
GL.LoadIdentity();

GL.Ortho(0, 800, 0, 600, -1, 1);

GL.MatrixMode(MatrixMode.Modelview);
GL.PushMatrix();
GL.LoadIdentity();

GL.Disable(EnableCap.Lighting);

GL.Enable(EnableCap.Texture2D);

GL.Color4(1, 0, 0, 1);

GL.BindTexture(TextureTarget.Texture2D, image);

GL.Begin(BeginMode.Quads);

GL.TexCoord2(0, 0);
GL.Vertex3(0, 0, 0);

GL.TexCoord2(1, 0);
GL.Vertex3(256, 0, 0);

GL.TexCoord2(1, 1);
GL.Vertex3(256, 256, 0);

GL.TexCoord2(0, 1);
GL.Vertex3(0, 256, 0);

GL.End();

GL.Disable(EnableCap.Texture2D);
GL.PopMatrix();

GL.MatrixMode(MatrixMode.Projection);
GL.PopMatrix();

GL.MatrixMode(MatrixMode.Modelview);
}

protected override void OnRenderFrame( FrameEventArgs e )
{
GL.Clear( ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit );

DrawImage(texture);

SwapBuffers();
}
}

public class Program
{
[STAThread]
public static void Main()
{
using (FailRender win = new FailRender())
{
win.Run();
}
}
}
}

最佳答案

删除行“GL.Color4(1, 0, 0, 1);”

并将加载纹理函数更改为:

public int LoadTexture(string file)
{
Bitmap bitmap = new Bitmap(file);

int tex;
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);

GL.GenTextures(1, out tex);
GL.BindTexture(TextureTarget.Texture2D, tex);

BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

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


GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) TextureWrapMode.Repeat);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) TextureWrapMode.Repeat);

return tex;
}

解决了这个问题。 System.Drawing.PixelFormat.Argb 使用 OpenGL 格式 Bgra

关于c# - OpenGL C# (OpenTK) 加载和绘制图像功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11645368/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com