How to use samplerCube as array

亡梦爱人 提交于 2020-06-29 04:45:23

问题


I am using samplerCube for a point light shadow map. For multiple lights, I implemented samplerCube as an array in the following.

uniform samplerCube pointShadowMapTexture[MAX_LIGHT];

But somehow I can't index this samplerCube. Shader compiles and there is no problem. This is working for sampler2D arrays.

I tried indexing it with [0], [1] .. in the shader but always the same image. I am sending different cube textures for each light but somehow shader doesn't index it or doesn't accept it.

I am doing the same for directional lights as sampler2D array. But when it comes to samplerCubes it doesn't work.

The code sending sampler cubes to the shader

void ShaderProgram::bindTexture(GLenum target , const char * name , int id){
    GLuint TextureID  = glGetUniformLocation(programID, name);
    glActiveTexture(GL_TEXTURE0 + textureOrder);
    glBindTexture(target , id);
    glUniform1i(TextureID, textureOrder);
    textureOrder++;
    App::checkErrors(__FILE__,__LINE__,name);
}
//depthMapTexture is member of Light class
std::string PointShadowMapTexture = "pointShadowMapTexture[" + std::to_string(LightNumber) + "]";
ShaderProgram::shaders["deferred"]->bindTexture(GL_TEXTURE_CUBE_MAP, PointShadowMapTexture.data(), depthMapTexture );
float SoftPointShadowCalculation(int index , vec3 fragPos ,vec3 lightPos){
    vec3 fragToLight = fragPos - lightPos;
    float currentDepth = length(fragToLight);
    float shadow = 0.0;
    float bias = 0.0;
    int samples = 20;
    float viewDistance = length(viewPos - fragPos);
    float diskRadius = (1.0 + (viewDistance / farPlane)) / 25.0;
    for(int i = 0; i < samples; ++i){
        float closestDepth = texture(pointShadowMapTexture[index], fragToLight + gridSamplingDisk[i] * diskRadius).r;
        closestDepth *= farPlane;//farplane
        if(currentDepth - bias > closestDepth){
            shadow += 0.5;
        }
    }
    shadow /= float(samples);
    return shadow;
}

Is this valid for samplerCube type? If not what should I do to have an array of samplerCubes?


回答1:


I realized that all lights are showing the what last light sees. So when I render model I was using the last light projection*view matrix for each light. :) It took hours to realize.

Now each light rendered with its own matrix.

this might help someone if encounters the same problem

 glEnable(GL_CULL_FACE);
    glCullFace(GL_FRONT);
    for(Light * light : Light::shadowCasterLights){
        glBindFramebuffer(GL_FRAMEBUFFER, light->depthMapFrameBuffer);App::checkFrameBufferError(__FILE__,__LINE__);
        glViewport(0,0,light->depthMapTextureSize,light->depthMapTextureSize);
        ShaderProgram::shaders[light->depthMapShader]->use("ShadowCaster Model");
        ShaderProgram::shaders[light->depthMapShader]->uniformMatrix4("ModelMatrix", &scene->ModelMatrix[0][0]);
        ShaderProgram::shaders[light->depthMapShader]->attributeBuffer("ModelSpaceVertexPosition", mesh->vertexBufferID, 3);

        switch (light->lightType) {
            case LightType::DIRECTIONAL:
                ShaderProgram::shaders[light->depthMapShader]->use("Light");
                ShaderProgram::shaders[light->depthMapShader]->uniformMatrix4("LightSpaceMatrix",&light->lightSpaceMatrix[0][0]);
                break;
            case LightType::POINT:
                ShaderProgram::shaders[light->depthMapShader]->use("Light");
                ShaderProgram::shaders[light->depthMapShader]->uniform3f("LightPosition" , &positionVector[0]);
                ShaderProgram::shaders[light->depthMapShader]->uniform1f("FarPlane" , light->farPlane);
                ShaderProgram::shaders[light->depthMapShader]->uniformMatrix4("LightSpaceMatrix[0]",&light->lightSpaceMatrixCube[0][0][0]);
                ShaderProgram::shaders[light->depthMapShader]->uniformMatrix4("LightSpaceMatrix[1]",&light->lightSpaceMatrixCube[1][0][0]);
                ShaderProgram::shaders[light->depthMapShader]->uniformMatrix4("LightSpaceMatrix[2]",&light->lightSpaceMatrixCube[2][0][0]);
                ShaderProgram::shaders[light->depthMapShader]->uniformMatrix4("LightSpaceMatrix[3]",&light->lightSpaceMatrixCube[3][0][0]);
                ShaderProgram::shaders[light->depthMapShader]->uniformMatrix4("LightSpaceMatrix[4]",&light->lightSpaceMatrixCube[4][0][0]);
                ShaderProgram::shaders[light->depthMapShader]->uniformMatrix4("LightSpaceMatrix[5]",&light->lightSpaceMatrixCube[5][0][0]);
                break;
        }
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->elementBufferID);
        glDrawElements(
                GL_TRIANGLES,      // mode
                mesh->indices.size(),    // count
                GL_UNSIGNED_SHORT,   // type
                (void *) 0           // element array buffer offset
        );
        ShaderProgram::shaders[light->depthMapShader]->reset();
    }
    glCullFace(GL_BACK);
    glEnable(GL_CULL_FACE);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glViewport(0,0,1920,1080);

Upon the power of shadows



来源:https://stackoverflow.com/questions/62499395/how-to-use-samplercube-as-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!