Search code examples
c++opengltexturesshaderskybox

Cube map black using shaders c++


I have been stuck trying to figure this thing out. I am trying to create a skybox but for some reason no matter what I do my cube map is always black. In the fragment shader if I put in a output color value like vec4(1.0f) then I do see my cube turn white. Which made me think it might be me loading in the image but I checked that and it is loading the image fine. I'm sure I am just being stupid and missing something.

Creating Cube map

    glGenTextures(1, &cube_texture);
    glBindTexture(GL_TEXTURE_CUBE_MAP, cube_texture);
    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);

     LoadCubeMapFace(posX, GL_TEXTURE_CUBE_MAP_POSITIVE_X);
     LoadCubeMapFace(negX, GL_TEXTURE_CUBE_MAP_NEGATIVE_X);
     LoadCubeMapFace(posY, GL_TEXTURE_CUBE_MAP_POSITIVE_Y);
     LoadCubeMapFace(negY, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y);
     LoadCubeMapFace(posZ, GL_TEXTURE_CUBE_MAP_POSITIVE_Z);
     LoadCubeMapFace(negZ, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z);

Loading the images using SDL_image

        void LoadCubeMapFace(const std::string& filename, GLenum face)
        {
            SDL_Surface *imageSurface = IMG_Load((TEXTURE_PATH + filename).c_str());
            if (!imageSurface){

                std::cout << "Can't Load    image " << filename << " " << IMG_GetError();
                return;
            }

            GLint nOfColors = imageSurface->format->BytesPerPixel;

            GLenum  textureFormat = GL_RGB;
            GLenum  internalFormat = GL_RGB8;

            if (nOfColors == 4)                 //  contains    an  alpha   channel
            {
                if (imageSurface->format->Rmask == 0x000000ff){
                    textureFormat = GL_RGBA;
                    internalFormat = GL_RGBA8;
                }
                else
                {
                    textureFormat = GL_BGRA;
                    internalFormat = GL_RGBA8;
                }
            }
            else if (nOfColors == 3)                    //  no  alpha   channel
            {
                if (imageSurface->format->Rmask == 0x000000ff){
                    textureFormat = GL_RGB;
                    internalFormat = GL_RGB8;
                }
                else
                {
                    textureFormat = GL_BGR;
                    internalFormat = GL_RGB8;
                }
            }
            else
            {
                std::cout << "ERROR [SDL_Surface -> Texture]: Image is not True Color" << std::endl;
                return;
            }
            glTexImage2D(face, 0, internalFormat, imageSurface->w, imageSurface->h, 0, textureFormat, GL_UNSIGNED_BYTE, imageSurface->pixels);

            SDL_FreeSurface(imageSurface);
        }

Rendering cube map

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_CUBE_MAP, texture->GetTextureMap());
    GLint texture_uniform = glGetUniformLocation(Skybox::shader->GetShaderProgram(), "cube_map");
    glUniform1i(texture_uniform, 0);

VS & FS Shaders

    #version 150

    in vec3 vertex_position_model;

    out vec3 the_uv;

    uniform mat4 projection_matrix;
    uniform mat4 view_matrix;

    void main()
    {
        the_uv = vertex_position_model;

        vec4 v = vec4(vertex_position_model, 1.0f);
        gl_Position = projection_matrix * view_matrix * v;
    }


    #version 150

    out vec4 FragColor;

    in vec3 the_uv;

    uniform samplerCube cube_map;

    void main()
    {
        FragColor = texture(cube_map, the_uv);
    }

Solution

  • I am not sure what the issue was but apparently the images I was using were not working properly. Opening them up with paint.NET and then saving them as a 32-bit depth image seems to have solve the issue.