【问题标题】:Opening bmp image to texture in OpenGL在OpenGL中打开bmp图像到纹理
【发布时间】:2015-05-28 03:04:07
【问题描述】:

我在使用 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;

}

函数总是返回0,但没有错误(我也看了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();

有什么想法吗?

【问题讨论】:

    标签: c++ opengl textures texture2d


    【解决方案1】:

    glutCreateWindow() 成功返回之前,您不会使用 GLUT 获得当前的 GL 上下文。 glutInitDisplayMode() 是不够的。

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

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

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

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

    // 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;
    }
    

    【讨论】:

    • 我取消了订单,现在是 glutCreateWindow("Sample"); texture = loadTexture(); 并且仍然返回 0..
    • 您是否也移动了printf()?还是在glutCreateWindow() 之前?还是您使用调试器检查texture
    • 好的!是的,那很愚蠢..纹理为 1,但我仍然没有在屏幕上看到图像..
    • 您是否将glutInitDisplayMode()glutCreateWindow() 之间的其他总帐调用移到glutCreateWindow() 之后?相同的“GL 调用在没有上下文的情况下不起作用”逻辑也适用于它们。如果那是你唯一的 glEnable(GL_TEXTURE_2D) 那么是的,你没有纹理 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多