【发布时间】:2016-09-27 17:10:30
【问题描述】:
请查看以下图片:
我不知道为什么会这样,只是没有意义,我一遍又一遍地检查它,它一直显示相同的东西,天空盒的每一侧都有三个相同的图像和红色,绿色和蓝色的条纹沿着它们向下延伸。
我做错了什么?
顶点着色器:
#version 400
in vec3 position;
uniform mat4 mvp;
out vec3 tex;
void main(void) {
gl_Position = mvp * vec4(position, 1.0);
tex = position;
}
片段着色器:
#version 400
uniform samplerCube defuse;
in vec3 tex;
out vec4 out_Color;
void main(void) {
out_Color = texture(defuse, tex);
}
CubeMap 加载器
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
int width, height, numComponents;
unsigned char* imageData = stbi_load((path.getURL() + "posx.png").c_str(), &width, &height, &numComponents, 4);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
stbi_image_free(imageData);
imageData = stbi_load((path.getURL() + "posy.png").c_str(), &width, &height, &numComponents, 4);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
stbi_image_free(imageData);
imageData = stbi_load((path.getURL() + "posz.png").c_str(), &width, &height, &numComponents, 4);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
stbi_image_free(imageData);
imageData = stbi_load((path.getURL() + "negx.png").c_str(), &width, &height, &numComponents, 4);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
stbi_image_free(imageData);
imageData = stbi_load((path.getURL() + "negy.png").c_str(), &width, &height, &numComponents, 4);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
stbi_image_free(imageData);
imageData = stbi_load((path.getURL() + "negz.png").c_str(), &width, &height, &numComponents, 4);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
stbi_image_free(imageData);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, 0);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
return new GLTexture(texture);
【问题讨论】:
-
您将
stbi_load4 作为最后一个参数传递,这意味着图像将被转换为具有 4 个组件(如果我理解正确的话),但您告诉 openGL 您的图像只有 RGB(3 个组件)。如果您能告诉我们您使用的是什么 OpenGL 版本,那会更好吗? (或直到你被允许使用) -
将其更改为 GL_RGBA 已修复!感谢您的帮助:)
-
我已将其发布为答案。