OpenGL Skybox渲染错误

时间:2016-01-23 21:34:15

标签: c++ opengl

我试图实现一个天空盒,然后使用天空盒的立方体贴图在我的场景中使用我发现编辑的一些代码来制作反射球体以适应我的项目。天空盒显示但是图像看起来几乎已损坏,有一个基本的山脉轮廓,但没有颜色,你可以在图像中看到。 http://i.imgur.com/kJT4aCV.jpg 我一直在尝试确保活动纹理和我的着色器是正确的,但我无法弄清楚。

顶点和片段着色器

#version 330 core
layout (location = 0) in vec3 position;
out vec3 TexCoords;

uniform mat4 projection;
uniform mat4 view;


void main()
{
   vec4 pos = projection * view * vec4(position, 1.0);
   gl_Position = pos.xyww;
   TexCoords = position;
}  
#version 330 core
in vec3 TexCoords;
out vec4 color;

uniform samplerCube skybox;

void main()
{    
    color = texture(skybox, TexCoords);
}

加载立方体贴图

GLuint loadCubemap(std::vector<const GLchar*> faces)
{
    GLuint textureID;
    glGenTextures(1, &textureID);

    int width, height, numComponents;
    unsigned char* image;

    glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
    for (GLuint i = 0; i < faces.size(); i++)
    {
        image = stbi_load(faces[i], &width, &height, &numComponents, 4);
        glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
        stbi_image_free(image);
    }
    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_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);
    glBindTexture(GL_TEXTURE_CUBE_MAP, 0);

    return textureID;
}

绘制天空盒

            glDepthFunc(GL_LEQUAL);  
            skyShader.Bind();
            skyShader.Update(transform, camera);
            view = glm::mat4(glm::mat3(camera.GetViewProjection()));
            glUniformMatrix4fv(glGetUniformLocation(skyShader.getProg(), "view"), 1, GL_FALSE, glm::value_ptr(view));
            glUniformMatrix4fv(glGetUniformLocation(skyShader.getProg(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));

            glBindVertexArray(skyboxVAO);
            glActiveTexture(GL_TEXTURE0);
            glUniform1i(glGetUniformLocation(skyShader.getProg(), "skybox"), 0);
            glBindTexture(GL_TEXTURE_CUBE_MAP, skyboxTexture);
            glDrawArrays(GL_TRIANGLES, 0, 36);
            glBindVertexArray(0);
            glDepthFunc(GL_LESS);

让我知道是否有任何其他部分可用于查看

1 个答案:

答案 0 :(得分:0)

读入纹理,并将其送入OpenGL,每个像素的元件数量不一致

image = stbi_load(faces[i], &width, &height, &numComponents, 4);使用4个组件读取图像

但是当你把它送到OpenGL时,glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);你把它作为一个每个像素有3个分量的纹理,它会导致每4个字节被解释为红色,而它应该是其他东西