C# OpenGL 纹理不显示
在这个例子中,我已尽力简化问题,希望有人能为我解决它。
我找不到用于显示纹理的 C# OpenGL 代码的简单工作示例。 所以我尝试在这里做一个。 不幸的是,这段代码对我不起作用。 我没有收到任何错误,但也没有纹理。
也许我设置不正确。 也许有人可以粘贴此代码并查看它是否运行。
using System;
using System.Drawing;
using Tao.OpenGl;
using Tao.DevIl;
using System.Windows.Forms;
namespace TextureTest
{
public partial class Form1 : Form
{
int imageID = 1;
int texture = 1;
public Form1()
{
InitializeComponent();
OpenGlControl.InitializeContexts();
Il.ilInit();
Il.ilBindImage(imageID);
bool success = Il.ilLoadImage(@"test.bmp");
Il.ilConvertImage(Il.IL_RGBA, Il.IL_UNSIGNED_BYTE);
Gl.glEnable(Gl.GL_TEXTURE_2D);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Il.ilGetInteger(Il.IL_IMAGE_BPP),
Il.ilGetInteger(Il.IL_IMAGE_WIDTH), Il.ilGetInteger(Il.IL_IMAGE_HEIGHT), 0,
Il.ilGetInteger(Il.IL_IMAGE_FORMAT), Gl.GL_UNSIGNED_BYTE, Il.ilGetData());
Il.ilDeleteImage(imageID);
Gl.glBegin(Gl.GL_QUADS);
Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex2f(-1.0f, -1.0f);
Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex2f(1.0f, -1.0f);
Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex2f(1.0f, 1.0f);
Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex2f(-1.0f, 1.0f);
Gl.glEnd();
Gl.glFlush();
Gl.glDeleteTextures(1, ref texture);
}
}
}
In this example I've tried as much as I can to simplify the problem in the hope that someone can solve it for me.
I could not find a simple working example of C# OpenGL code to display a texture.
So I've tried to make one here.
Unfortunatley, this code does not work for me.
I get no errors, but no texture either.
Maybe I set it up incorrectly.
Perhaps someone could paste this code and see if it runs.
using System;
using System.Drawing;
using Tao.OpenGl;
using Tao.DevIl;
using System.Windows.Forms;
namespace TextureTest
{
public partial class Form1 : Form
{
int imageID = 1;
int texture = 1;
public Form1()
{
InitializeComponent();
OpenGlControl.InitializeContexts();
Il.ilInit();
Il.ilBindImage(imageID);
bool success = Il.ilLoadImage(@"test.bmp");
Il.ilConvertImage(Il.IL_RGBA, Il.IL_UNSIGNED_BYTE);
Gl.glEnable(Gl.GL_TEXTURE_2D);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Il.ilGetInteger(Il.IL_IMAGE_BPP),
Il.ilGetInteger(Il.IL_IMAGE_WIDTH), Il.ilGetInteger(Il.IL_IMAGE_HEIGHT), 0,
Il.ilGetInteger(Il.IL_IMAGE_FORMAT), Gl.GL_UNSIGNED_BYTE, Il.ilGetData());
Il.ilDeleteImage(imageID);
Gl.glBegin(Gl.GL_QUADS);
Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex2f(-1.0f, -1.0f);
Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex2f(1.0f, -1.0f);
Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex2f(1.0f, 1.0f);
Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex2f(-1.0f, 1.0f);
Gl.glEnd();
Gl.glFlush();
Gl.glDeleteTextures(1, ref texture);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试加载纹理而不干扰
glEnable
。然后,当您准备好绘制时,调用glEnable(GL_TEXTURE_2D);
并再次绑定纹理。另外,我看不到您是否配置为单缓冲或双缓冲使用。您可能需要调用
SwapBuffers
。Try loading the texture without messing with
glEnable
. Then when you're ready to draw, callglEnable(GL_TEXTURE_2D);
and bind the texture again.Also, I can't see whether you're configured for single-buffered or double-buffered usage. You might need a call to
SwapBuffers
.我偶然发现了这个问题的一个非常简单的解决方案。
使用 Ulut 函数“Ilut.ilutGLLoadImage”几乎可以处理所有事情。
至少在这里给出的简单示例中是这样。
希望其他人发现这段代码有用。
I stumbled upon a very simple solution to this problem.
Using the Ulut function "Ilut.ilutGLLoadImage" pretty much takes care af everything.
At least in the simple example given here.
Hopefully someone else finds this code useful.