c++ - 立方图上的 OpenGL 奇怪的红绿蓝线重复三次

标签 c++ opengl

请检查下图:

enter image description here

我不明白为什么会发生这种情况,它只是没有意义,我一遍又一遍地检查它,它一直显示相同的东西,天空盒和红色的每一侧都有三个相同的图像,绿色和蓝色条纹向下延伸。

我做错了什么?

顶点着色器:

#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 以加载具有 4 个组件的纹理 - 所需的组件数量是 stbi_load 的最后一个参数。您还向 OpenGL 指定纹理是 GL_RGB,但事实并非如此。解决此问题的方法是将纹理指定为 OpenGL 作为 GL_RGBA 或将纹理解码为 3 个组件。

关于c++ - 立方图上的 OpenGL 奇怪的红绿蓝线重复三次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39730613/

相关文章:

windows - wglCreateContext 抛出 INVALID_OPERATION 异常

c++ - 使用 C++ MATLAB API 定义二维数组

c - Opengl 在 3d 场景问题上绘制 2d 叠加

opengl - 访问片段着色器中的重心坐标

c - 根据点旋转一个物体

c++ - 为什么有时在类内部定义 C++ 方法?

c++ - 允许访问 C++ 中的容器对象

c++ - 如何使用GYP?

c++ - 将 uint32_t 与 uint64_t 连接和拆分的最佳方法

c++ - opengl中的freetype 2用法