在 OpenGL 中打开 bmp 图像到纹理

Opening bmp image to texture in OpenGL

我在使用 OpenGl 将 bmp 图像打开为纹理时遇到问题 这是 loadTexture 函数:

GLuint loadTexture() {
FILE *f;
int imageSize,rd;
f = fopen(filename, "rb");
if(f == 0){
    printf("Couldn't open file\n");
    exit(-1);
}
GLubyte header[54];
fread(header, 54,1,f);
if(header[0] != 'B' || header[1] != 'M'){
    printf("File not bitmap\n");
    exit(1);
}
dataPos    = *(int*)&(header[0x0A]);
imageSize  = *(int*)&(header[0x22]);
width      = *(int*)&(header[0x12]);
height     = *(int*)&(header[0x16]);
if (imageSize==0)    imageSize=width*height*3;
if (dataPos==0)      dataPos=54;
data = new unsigned char [imageSize];
fread(data,1,imageSize,f);
fclose(f);
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
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_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);  return textureID;

}

函数总是returns0,但是没有报错(我也看了glGetError()

尝试加载纹理时:

    glClear(GL_COLOR_BUFFER_BIT);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_REPLACE);
//BOTTOM LEFT - red
glBindTexture(GL_TEXTURE_2D,texture);
glViewport(0,0,256,256);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(-1,1);
glTexCoord2f(1,0);
glVertex2f(-1,-1);
glTexCoord2f(0,1);
glVertex2f(1,-1);
glTexCoord2f(1,1);
glVertex2f(1,1);
glEnd();

我得到的是一个白色方块,而不是图片.. 这是我的初始化函数:

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(512, 512);
glutCreateWindow("Sample");
glEnable(GL_TEXTURE_2D);
glOrtho(-1.0,1.0,-1.0,1.0,2.0,-2.0);
glClearColor(0,0,0,0);
texture = loadTexture();
printf("Texture: %d\n",texture);
glutDisplayFunc(mydisplay);
glutMainLoop();

有什么想法吗?

glutCreateWindow() 成功 returns 之前,您不会获得带有 GLUT 的当前 GL 上下文。 glutInitDisplayMode() 不够。

您在 loadTexture() 中调用的所有 GL 函数都需要当前的 GL 上下文才能运行。

texture = loadTexture(); 移动到 glutCreateWindow() 之后和 glutMainLoop(); 之前的某个位置。

此外,如果您要使用 3 分量 BGR/RGB,请确保使用 glPixelStorei()GL_UNPACK_ALIGNMENT 设置为 1(而不是默认值4) 在你的 glTexImage2D() 电话之前。

这是在我的系统上运行的最简单的东西:

// http://glew.sourceforge.net/
#include <GL/glew.h>
#include <GL/glut.h>

GLuint loadTexture()
{
    const unsigned char data[] =
    {
        255,   0,   0,    0, 255,   0,
          0,   0, 255,  255, 255, 255,
    };
    const GLsizei width = 2;
    const GLsizei height = 2;

    GLuint textureID = 0;
    glGenTextures(1, &textureID);
    glBindTexture(GL_TEXTURE_2D, textureID);
    glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
    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_CLAMP);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
    return textureID;
}

GLuint texture = 0;
void display()
{
    glClearColor( 0, 0, 0, 1 );
    glClear( GL_COLOR_BUFFER_BIT );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( -2, 2, -2, 2, 2, -2 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,texture);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_REPLACE);

    glBegin(GL_QUADS);
    glTexCoord2f( 0, 0 );
    glVertex2i( -1, -1 );
    glTexCoord2f( 1, 0 );
    glVertex2i(  1, -1 );
    glTexCoord2f( 1, 1 );
    glVertex2i(  1,  1 );
    glTexCoord2f( 0, 1 );
    glVertex2i( -1,  1 );
    glEnd();

    glutSwapBuffers();
}

int main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
    glutInitWindowSize( 640, 480 );
    glutCreateWindow( "GLUT" );
    glewInit();
    texture = loadTexture();
    glutDisplayFunc( display );
    glutMainLoop();
    return 0;
}