OpenGL为什么有的图像可以读取有的读取不了

hrshjy 2017-08-14 07:59:04
我要用OpenGL画一些图,其中需要加载一些纹理图片,但是为什么我的函数有的图片能读取并正常显示(比如Nehe教程第六课的示例图片),有的图片就没法用啊(比如NeHe教程第七课图片),是因为图片格式的关系么?

以下为加载图片的函数:




#define BMP_Header_Length 54
GLuint MyGLWidget::load_texture(const char* file_name)
{
GLint width, height, total_bytes;
GLubyte* pixels = 0;
GLuint texture_ID = 0;
FILE* pFile = fopen(file_name, "rb");
if( pFile == 0 )
return 0;
fseek(pFile, 0x0012, SEEK_SET);
fread(&width, sizeof(width), 1, pFile);
fread(&height, sizeof(height), 1, pFile);
fseek(pFile, BMP_Header_Length, SEEK_SET);
{
GLint line_bytes = width * 3;
while( line_bytes % 4 != 0 )
++line_bytes;
total_bytes = line_bytes * height;
}
pixels = NULL;
pixels=(GLubyte*)malloc(total_bytes);

if( pixels == 0 )
{
fclose(pFile);
return 0;
}



GLint max;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
if( !power_of_two(width)
|| !power_of_two(height)
|| width > max
|| height > max )
{
const GLint new_width = 256;
const GLint new_height = 256;
GLint new_line_bytes, new_total_bytes;
GLubyte* new_pixels = 0;
new_line_bytes = new_width * 3;
while( new_line_bytes % 4 != 0 )
++new_line_bytes;
new_total_bytes = new_line_bytes * new_height;
new_pixels = (GLubyte*)malloc(new_total_bytes);
if( new_pixels == 0 )
{
free(pixels);
fclose(pFile);
return 0;
}
gluScaleImage(GL_RGB,width, height, GL_UNSIGNED_BYTE, pixels,new_width, new_height, GL_UNSIGNED_BYTE, new_pixels);
free(pixels);
pixels = new_pixels;
width = new_width;
height = new_height;
}
if( fread(pixels, total_bytes, 1, pFile) <= 0 )
{
free(pixels);
fclose(pFile);
return 0;
}

glGenTextures(1, &texture_ID);

if( texture_ID == 0 )
{
free(pixels);
fclose(pFile);
return 0;
}

glBindTexture(GL_TEXTURE_2D, texture_ID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, pixels);
free(pixels);
fclose(pFile);
return texture_ID;}

...全文
352 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hrshjy 2017-08-17
  • 打赏
  • 举报
回复
终于解决了,谢谢各位,果然提前预处理才是王道
赵4老师 2017-08-16
  • 打赏
  • 举报
回复
请检查每个函数调用的返回值。 请用try捕获该捕获的每个异常。 请用下列函数显示每个字节值:
void HexDump(char *buf,int len,int addr) {
    int i,j,k;
    char binstr[80];

    for (i=0;i<len;i++) {
        if (0==(i%16)) {
            sprintf(binstr,"%08x -",i+addr);
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        } else if (15==(i%16)) {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
            sprintf(binstr,"%s  ",binstr);
            for (j=i-15;j<=i;j++) {
                sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
            }
            printf("%s\n",binstr);
        } else {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        }
    }
    if (0!=(i%16)) {
        k=16-(i%16);
        for (j=0;j<k;j++) {
            sprintf(binstr,"%s   ",binstr);
        }
        sprintf(binstr,"%s  ",binstr);
        k=16-k;
        for (j=i-k;j<i;j++) {
            sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
        }
        printf("%s\n",binstr);
    }
}
赵4老师 2017-08-15
  • 打赏
  • 举报
回复
为什么不在外部先用比如PhotoShop将图片转换为代码需要的格式呢?
hrshjy 2017-08-15
  • 打赏
  • 举报
回复
那么该怎么判断怎么写呢,我初学者不太明白,能否赐教
hrshjy 2017-08-15
  • 打赏
  • 举报
回复
现在我想问的是,我的函数为什么有的文件不能读取呢
trytry1992 2017-08-15
  • 打赏
  • 举报
回复
引用 2 楼 hrshjy 的回复:
那么请问如果修改我的读取函数可以实现加载不同对的图片么
那你可能还需要一个判断图片格式的函数
hrshjy 2017-08-15
  • 打赏
  • 举报
回复
那么请问如果修改我的读取函数可以实现加载不同对的图片么
赵4老师 2017-08-15
  • 打赏
  • 举报
回复
引用 6 楼 hrshjy 的回复:
也不是我不想改好啊,可是我都不知道需要什么样的格式,何况Photoshop也没咋用过,而且如果这样那每张照片岂不是还要预先进行处理
就算每张照片都要预先进行处理,也不会象你想象的那么难。 比如使用Image Magick的话:
WinExec("cmd /c "\"C:\\Program Files\\ImageMagick-6.6.9-Q16\\convert.exe\“ old.bmp new.bmp",SW_HIDE);
hrshjy 2017-08-15
  • 打赏
  • 举报
回复
也不是我不想改好啊,可是我都不知道需要什么样的格式,何况Photoshop也没咋用过,而且如果这样那每张照片岂不是还要预先进行处理
ID870177103 2017-08-14
  • 打赏
  • 举报
回复
图片的格式可能不一样,比如说保留的偏移,调色板,压缩算法,和通道数等等 推荐使用freeimage,opengl程序的好伙伴

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧