如何使用FreeImage库将纹理加载到OpenGL? [英] How do I load textures to OpenGL using FreeImage library?

查看:1289
本文介绍了如何使用FreeImage库将纹理加载到OpenGL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NeHe教程在C + +中学习OpenGL,但我想使用FreeImage库,而不是Glaux或SOIL。我在使用FreeImage时看到的好处是它的最后一次更新是在去年十月,而SOIL没有在5年内更新。

I'm learning OpenGL in C++ using NeHe tutorials, but I'm trying to do them with FreeImage library, instead of Glaux or SOIL. The good point I see in using FreeImage is thar it's last update was in October last year, while SOIL hasn't been updated in 5 years. The problem I have is that I'm not able to load textures correctly.

这是我的代码:

static GLuint texture = 0;
if (texture == 0)
{
    FIBITMAP* bitmap = FreeImage_Load(
        FreeImage_GetFileType("textures/test/nehe_06.png", 0),
        "textures/test/nehe_06.png");

    glGenTextures(1, &texture);

    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, 3, FreeImage_GetWidth(bitmap), FreeImage_GetHeight(bitmap),
        0, GL_RGB, GL_UNSIGNED_BYTE, FreeImage_ConvertTo32Bits(bitmap));

    FreeImage_Unload(bitmap);
}

使用这个代码,我得到的纹理是错误的:点和条,并改变图像给出相同的结果,所以我假设我没有正确加载纹理。

Using this code, the texture I get is wrong: it's black, with coloured dots and strips, and changing the image gives the same result, so I assume I'm not loading the texture correctly.

我试图交换红色和蓝色位(读取FreeImage在BGR中加载),没有更好的结果。最后,如果我改变* FreeImage_ConvertTo32Bits(bitmap)* * FreeImage_GetBits(bitmap)*我得到一个segfault。

I've tried to swap the red and blue bits (read that FreeImage loads in BGR) with no better result. Finally, if I change the *FreeImage_ConvertTo32Bits(bitmap)* by *FreeImage_GetBits(bitmap)* I get a segfault.

我做错了什么?

What I'm doing wrong?

推荐答案

你的代码有两个问题 - 一个是你将图像转换为32位,但你指定一个24位的纹理格式为openGL ( GL_RGB )。第二个是你将FreeImage对象本身传递给纹理而不是转换的位。

There are two problems with your code - one is that you are converting to image to 32 bits but you specify a 24 bit texture format to openGL (GL_RGB). The second is that you are passing the FreeImage object itself to the texture rather than the converted bits.

你需要这样做,而不是

FIBITMAP *pImage = FreeImage_ConvertTo32Bits(bitmap);
int nWidth = FreeImage_GetWidth(pImage);
int nHeight = FreeImage_GetHeight(pImage);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, nWidth, nHeight,
    0, GL_BGRA, GL_UNSIGNED_BYTE, (void*)FreeImage_GetBits(pImage));

FreeImage_Unload(pImage);

这篇关于如何使用FreeImage库将纹理加载到OpenGL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆