When creating and rendering a skybox with a cube map texture, the skybox is black.
Here is how I create the cube map texture:
GLuint loadCubemap(std::vector<std::string> faces)
{
GLuint textureID;
glGenTextures(1, &textureID);
glActiveTexture(GL_TEXTURE0);
int width,height;
unsigned char* image;
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
for(GLuint i = 0; i < faces.size(); i++)
{
image = SOIL_load_image(faces[i].c_str(), &width, &height, 0, SOIL_LOAD_RGBA);
glTexImage2D(
GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0,
GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 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;
}
Here is how I draw the cube map:
void Skybox::update()
{
//the sky box should be in the background
glDepthMask(GL_FALSE);
//set the position of the skybox around the camera
pos = Camera::getCurrentlyBound()->getPos();
//faces of the cube map
std::vector<std::string> faces = {
"/home/a/Programming/GameEngine/Game/Assets/skybox_px.png", //+x
"/home/a/Programming/GameEngine/Game/Assets/skybox_nx.png", //-x
"/home/a/Programming/GameEngine/Game/Assets/skybox_py.png", //+y
"/home/a/Programming/GameEngine/Game/Assets/skybox_ny.png", //-y
"/home/a/Programming/GameEngine/Game/Assets/skybox_pz.png", //+z
"/home/a/Programming/GameEngine/Game/Assets/skybox_nz.png", //-z
};
//create the texture object
GLuint cubeMap = loadCubemap(faces);
//set up some matrices
glm::mat4 modelMatrix = glm::translate(glm::mat4(), pos)*glm::mat4_cast(orien);
glm::mat4 mvpMatrix = Camera::getCurrentlyBound()->getVP()*modelMatrix;
//you can ignore this part
Program* prog = ProgramManager::getProgram(msh.getProgramPath());
//bind the program
prog->bind();
//set uniform data
prog->setUniformData(mvpMatrix, "MVP");
//bind the VAO
msh.bind();
//bind the cube map texture
glBindTexture(GL_TEXTURE_CUBE_MAP, cubeMap);
glDrawElements(GL_TRIANGLES, msh.getNumVerts(), GL_UNSIGNED_INT, 0); //draw
msh.unbind(); //unbind the VAO
prog->unbind();
glDepthMask(GL_TRUE);
}
And here are the shaders:
Vertex Shader:
#version 330 core
in vec3 pos;
uniform mat4 MVP;
out vec3 fragTexVector;
void main()
{
gl_Position = MVP*vec4(pos, 1);
fragTexVector = pos;
}
Fragment Shader:
#version 330 core
out vec4 finalColor;
in vec3 fragTexVector;
uniform samplerCube skybox;
void main()
{
//finalColor = vec4(1,1,1,1);
finalColor = texture(skybox, fragTexVector);
}
I have scanned over this piece of code and over my entire project many times to find what was wrong and could not find anything. The samplerCube seems to always get black pixels.
Extra Information (might be helpful, I'm not sure):
I have tried debugging the program with VOGL (Valve's opengl debugger) but when I try to capture a snapshot of a frame it crashes with an error:
Internal error: KTX texture failed internal consistency check, texture 7 target GL_TEXTURE_CUBE_MAP. This should not happen!
I'm very new to OpenGL and if anybody with OpenGL experience could help me with issue it would be greatly appreciated.
Thanks.