I've been learning about cube map and I implemented one into my program. It seemed to work well until I started moving the camera around the scene and zooming out. As you can see in the attached gif, there seems to be a problem with the cube map since this weird behavior is occurring:
https://gyazo.com/70ad4ce027d1e032bc19258e28def66f
Main program:
unsigned int cubemapTexture = texo.loadCubeMap(faces);
glDepthFunc(GL_EQUAL);
bg.bind();
skyBox.use();
glm::mat4 projection = glm::perspective(glm::radians(fov), (float)scr_width / (float)scr_height, 0.1f, 100.0f);
glm::mat4 view = camera.GetViewMatrix();
skyBox.setUniformMat4("projection", projection);
skyBox.setUniformMat4("view", view);
glActiveTexture(GL_TEXTURE20);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
GLCall(glDrawArrays(GL_TRIANGLES, 0, 36));
loadCubeMap:
unsigned int Textures::loadCubeMap(std::vector<std::string> faces)
{
unsigned int textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
stbi_set_flip_vertically_on_load(false);
int width, height, nrChannels;
for (unsigned int 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 tex 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;
}
Vertex Shader:
#version 330 core
layout (location = 0) in vec3 aPos;
out vec3 TexCoords;
uniform mat4 view;
uniform mat4 projection;
void main()
{
vec4 pos = projection * view * vec4(aPos, 1.0);
TexCoords = aPos;
//Setting z value to w (1.0) so that the cube map is always in the background
gl_Position = pos.xyww;
}
Fragment shader:
#version 330 core
out vec4 FragColor;
in vec3 TexCoords;
uniform samplerCube skybox;
void main()
{
FragColor = texture(skybox, TexCoords);
}
The problem probably originates from one of these snippets, but if you have any other ideas I can provide more info.