bmp纹理无法使用Open GL正确加载

问题描述 投票:0回答:1

大家好,我正在尝试使用OpenGL加载纹理(法线贴图)。

GLuint loadTexture(const char* fileName) {
    GLuint textureID;
    glGenTextures(1, &textureID);

    // load file - using core SDL library
    SDL_Surface* tmpSurface;
    tmpSurface = SDL_LoadBMP(fileName);
    if (tmpSurface == nullptr) {
        std::cout << "Error loading bitmap" << std::endl;
    }

    // bind texture and set parameters
    glBindTexture(GL_TEXTURE_2D, textureID);
    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_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tmpSurface->w, tmpSurface->h, 0,
        GL_BGR, GL_UNSIGNED_BYTE, tmpSurface->pixels);
    glGenerateMipmap(GL_TEXTURE_2D);

    SDL_FreeSurface(tmpSurface);
    return textureID;
}

然后,如果我尝试渲染它,它将为我提供:enter image description here

而不是:

enter image description here

但是我可以正常地呈现:enter image description here

你有个主意吗?较深的颜色表示不起作用的颜色为32,有效的颜色为24]

c++ visual-studio opengl textures
1个回答
0
投票

使用SDL_ConvertSurfaceFormat转换表面格式并加载转换后的曲面:

SDL_ConvertSurfaceFormat

当然,当用SDL_Surface* tmpSurface = SDL_LoadBMP(fileName); SDL_Surface* formattedSurface = SDL_ConvertSurfaceFormat( tmpSurface, SDL_PIXELFORMAT_RGBA8888, 0); SDL_FreeSurface(tmpSurface); // [...] glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tmpSurface->w, tmpSurface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, formattedSurface->pixels); SDL_FreeSurface(formattedSurface); 指定二维纹理图像时,您可以评估formatSDL_Surface属性并设置适当的格式属性。

© www.soinside.com 2019 - 2024. All rights reserved.