简体   繁体   中英

Setting GL_TEXTURE_CUBE_MAP cause GL_INVALID_OPERATION in glUseProgram

I am using the following function to load my cube map. Without loading, there is no error. Whenever I set me cube map texture next glUseProgram() function causes GL_INVALID_OPERATION error;

unsigned int Model::loadCubemap(std::vector<std::string> faces){
    GLuint CubeMapOrder[] = {
            GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
            GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
            GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
    };
    unsigned int textureID;
    glGenTextures(1, &textureID);
    glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);

    int width, height, nrComponents;
    for (unsigned int i = 0; i < faces.size(); i++){
        unsigned char *data = stbi_load(faces[i].c_str(), &width, &height, &nrComponents, 0);
        if (data){
            GLenum format;
            if (nrComponents == 1){
                format = GL_RED;
            }else if (nrComponents == 3){
                format = GL_RGB;
            }else if (nrComponents == 4){
                format = GL_RGBA;
            }
            glTexImage2D(CubeMapOrder[i], 0, format, width, height, 0, format, 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_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);

    return textureID;
}

I am setting my cube map for cube map shader like this;

    GLuint TextureID  = glGetUniformLocation(ShaderProgram::shaders[shadername]->programID, "skybox");
    glActiveTexture(GL_TEXTURE8);
    glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
    glUniform1i(TextureID, 8);

for the first cube map shader, it works. I see the skybox. but another shader that doesn't use skybox texture gives an error.

Both shaders are using the same vertex array object. But the skybox is texture 8. I think the problem is textures parameters. because

if I don't set these

    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);

there is no problem.

I made some digging and couldn't find my problem.

Why setting glTexParameteri gives an error and there is no render.

I am just setting a parameter for GL_TEXTURE_CUBE_MAP. Even if it is wrong why the other textures are affected?

If you have something like

uniform samplerCube PointShadowMapTexture[MAX_POINT_LIGHT];

you have to uniform all elements in samplerCube array. Otherwise, there will be a black screen. Or your clear color.Also following draw calls may cause invalid operation.

Better to uniform all unused sampler types with blank 1x1 textures.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM