无法在c# OpenGL中加载纹理
本文关键字:加载 纹理 OpenGL | 更新日期: 2023-09-27 17:53:32
我正在通过c#游戏编程进行严肃游戏创作,我似乎已经碰壁了。
下面的代码似乎不起作用。即openGLId返回0。
纹理似乎可以从磁盘加载。但是没有附加Id。
public void LoadTexture(string textureId, string path)
{
int devilId = 0;
Il.ilGenImages(1, out devilId);
Il.ilBindImage(devilId); // set as the active texture.
if (!Il.ilLoadImage(path))
{
System.Diagnostics.Debug.Assert(false,"Could not open file, [" + path + "].");
}
Ilu.iluFlipImage();
int width = Il.ilGetInteger(Il.IL_IMAGE_WIDTH);
int height = Il.ilGetInteger(Il.IL_IMAGE_HEIGHT);
int openGLId = Ilut.ilutGLBindTexImage();
System.Diagnostics.Debug.Assert(openGLId != 0);
Il.ilDeleteImages(1, ref devilId);
_textureDatabase.Add(textureId, new Texture(openGLId, width, height, path));
}
是否有更好的方法来加载OpenGL纹理?
Thanks in advance
我可能有答案。
这是我用devIL库加载纹理的类。
public class TextureLoader
{
int texid;
bool success;
int image;
int width;
int height;
string path;
public TextureLoader(string path)
{
this.path = path;
}
public void display()
{
Il.ilInit();
Il.ilGenImages(1, out texid);
Il.ilBindImage(texid);
success = Il.ilLoadImage(path);
if (success)
{
success = Il.ilConvertImage(Il.IL_RGBA, Il.IL_UNSIGNED_BYTE);
if (!success)
Console.WriteLine("ERROR");
width = Il.ilGetInteger(Il.IL_IMAGE_WIDTH);
height = Il.ilGetInteger(Il.IL_IMAGE_HEIGHT);
Gl.glGenTextures(1, out image);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, image);
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(texid);
Gl.glBegin(Gl.GL_QUADS);
Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex2f(-width / 2.0f, height / 2.0f);
Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex2f(-width / 2.0f, height / 2.0f);
Gl.glTexCoord2f(1, 1); Gl.glVertex2f(1, -1);
Gl.glTexCoord2f(1, 0); Gl.glVertex2f(1, 1);
Gl.glEnd();
Gl.glFlush();
Gl.glDeleteTextures(1, ref image);
}
}
代码没有注释,但并不复杂。有了这个类,我可以加载png和jpg图像。