gpt4 book ai didi

c++ - bmp 纹理未使用 Open GL 正确加载

转载 作者:行者123 更新时间:2023-11-30 04:41:29 25 4
gpt4 key购买 nike

大家好,我正在尝试使用 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

最佳答案

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

SDL_Surface* tmpSurface = SDL_LoadBMP(fileName);
SDL_Surface* formattedSurface = SDL_ConvertSurfaceFormat(
tmpSurface, SDL_PIXELFORMAT_ABGR8888, 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);

当然你可以评估SDL_Surfaceformat属性并在glTexImage2D指定二维纹理图像时设置适当的格式属性.

关于c++ - bmp 纹理未使用 Open GL 正确加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59126334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com