opengltexturesskybox

OpenGL: skybox/cubemap is displayed partially, triangles of it are missing


I tried to create a textured skybox, following this tutorial: cubemaps. Some triangles of the box (it is triangulated) are not shown at all, but weirdly enough not every second triangle of a square is missing (1 square's texturing is correct). My problem can be seen in the pictures below. The grayish color is "general" color of the scene, it has nothing to do with the skybox.

enter image description here

enter image description here

I believe the problem is caused by one of the shaders, as I get an

invalid operation

error, while trying to use the shaders of the skybox. What might be the problem?

Here the code of vertex shader:

#version 330 core
layout (location = 0) in vec3 aPos;

out vec3 TexCoords;

uniform mat4 projection;
uniform mat4 view;

void main() {
    TexCoords = aPos;
    vec4 pos = projection * view * vec4(aPos, 1.0);
    gl_Position = pos.xyww;
}  

and the fragment shader:

#version 330 core
out vec4 FragColor;

in vec3 TexCoords;

uniform samplerCube skybox;

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

and finally the procedure which loads the cubemap:

GLuint loadCubemap(std::vector<std::string> faces) {
  GLuint textureID;
  glGenTextures(1, &textureID);
  glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);

  int width, height, nrChannels;
  for (GLuint i = 0; i < faces.size(); i++) {
    unsigned char *data = stbi_load(faces[i].c_str(), &width, &height, &nrChannels, 0);
    if (data) {
      glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
               0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
      stbi_image_free(data);
    } else {
      std::cout << "Cubemap texture failed to load at path: " << faces[i] << std::endl;
      stbi_image_free(data);
    }
  }
  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_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);

  return textureID;
}

The requested geometry (it's a .obj file):

o Cube
v 300.000000 -300.000000 -300.000000
v 300.000000 -300.000000 300.000000
v -300.00000 -300.000000 300.000000
v -300.00000 -300.000000 -300.00000
v 300.000000 300.000000 -300.000000
v 300.000000 300.000000 300.00000
v -300.000000 300.000000 300.000000
v -300.000000 300.000000 -300.000000
vn 0.0000 -1.0000 0.0000
vn 0.0000 1.0000 0.0000
vn 1.0000 -0.0000 0.0000
vn -0.0000 -0.0000 1.0000
vn -1.0000 -0.0000 -0.0000
vn 0.0000 0.0000 -1.0000
s off
f 2//1 4//1 1//1
f 8//2 6//2 5//2
f 5//3 2//3 1//3
f 6//4 3//4 2//4
f 3//5 8//5 4//5
f 1//6 8//6 5//6
f 2//1 3//1 4//1
f 8//2 7//2 6//2
f 5//3 6//3 2//3
f 6//4 7//4 3//4
f 3//5 7//5 8//5
f 1//6 4//6 8//6

Solution

  • I have found the problem! It was not caused by the shaders but by my own stupidity. In the main rendering loop, I forgot to remove an extra binding VAO-buffer + drawing triangles pair after the newly inserted one.