1
请检查下面的图片:OpenGL的怪异红,绿和蓝线的立方体贴图和Repeting三次
我想不通为什么发生这种情况,它只是没有任何意义,我一遍又一遍地检查了它,并且它一直显示相同的东西,天空盒两侧有三个相同的图像,红色,绿色和蓝色条纹都沿着它们向下。
我在做什么错?
顶点着色器:
#version 400
in vec3 position;
uniform mat4 mvp;
out vec3 tex;
void main(void) {
gl_Position = mvp * vec4(position, 1.0);
tex = position;
}
Fragmant着色器:
#version 400
uniform samplerCube defuse;
in vec3 tex;
out vec4 out_Color;
void main(void) {
out_Color = texture(defuse, tex);
}
立方体贴图装载机
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_load' 4作为最后一个参数,这意味着图像将被转换为4个组件(如果我理解正确),但是您告诉openGL您的图像只有RGB(3个组件)。如果你能告诉我们你使用的OpenGL版本会更好吗? (或达到您允许使用的值) – tambre
将其更改为GL_RGBA修复了它!感谢您的帮助:) –
我已经发布它作为答案。 – tambre