OpenGL阴影贴图 - 阴影位置错误

时间:2014-07-31 12:23:28

标签: c++ linux opengl render shadow

我遇到阴影问题,我的代码中没有任何错误。 它在视频中都可见。

https://www.youtube.com/watch?v=dZXEPLIYGNM&feature=youtu.be

我的光源位于没有纹理的球内部并且正在移动。 阴影被替换

基本上:

  • 在阴影传递期间,我使用与渲染传递中相同的程序
  • 我的阴影纹理与我的视口(1920 x 1200)
  • 的大小相同
  • 我的光源是点光源
  • 我只使用片段和顶点着色器(没有几何和细分)
  • 只有一个光源
  • 我的深度mvp矩阵是LightProjection * LightCamera * MeshModelMatrix
  • 我的预测是: 视野90 在0.1附近 远飞机5000000.0
  • ViewportRatio 1
  • 摄像头(0.0,1.0,0.0)
  • 我以正确的方式使用偏差矩阵
  • 图形nVidia nvm3100m驱动程序版本。 304.117 - 我已经显示了额外的视口,以便从灯光的角度看场景

我的所有代码都在抽象背后,因此我从中选择了所有能够影响阴影的东西(我认为)。点光源工作正常,但阴影位置错误。

https://www.youtube.com/watch?v=dZXEPLIYGNM&feature=youtu.be

我的片段着色器:

#version 330

struct Light {
    vec4 position;
    vec4 intensity;
    mat4 mvp;
};

...
out vec4 fColor;
in vec4 vVertexWorldSpace;
...
uniform texture2D uShadowTexUnit;
...
uniform Light uLight;
...
vec4 ambient = vec4(0.1,0.1,0.1,0.0);
...
float calculateShadow(vec4 lightPosition, sampler2D shadowMap) {
    vec3 shadowMapUV = lightPosition.xyz / lightPosition.w;
    float shadowMapDepth = texture(shadowMap, shadowMapUV.xy).x;

    if ((shadowMapDepth < shadowMapUV.z - 0.00001))                                                                 
        return 0.1;                                                                         
    else                                                                                    
        return 1.0;     
}


void main() {
   ...
   lightDepth = uLight.mvp * vVertexWorldSpace;
   shadow = calculateShadow(lightDepth, uShadowMaps);
   ...
   fColor = vec4(shadow * colorDiffuse.xyz,1.0) + colorAmbient;
}

我的顶点着色器:

#version 330
...
layout(location = 0) in vec4 aPosition;
...
out vec4 vVertexWorldSpace;

uniform mat4 uModelMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uProjectionMatrix;

void main() {
   vec4 vertexWorldspace = uModelMatrix * aPosition;
   vVertexWorldSpace = vertexWorldSpace;

   gl_Position = uProjectionMatrix * uViewMatrix * vertexWorldspace;
}

我在Light类构造函数中的相机设置:

   Light::Light(glm::vec3 position,
                glm::vec4 intensity):mIntensity(intensity),
                                     mShadowTextureUnit(31){
   mShadowCamera = new Camera(glm::vec3(position), //position
                              glm::vec3(0.0,0.0,0.0), // lookAt point - always 0
                              glm::vec3(0.0,1.0,0.0), //head
                              glm::radians(90.0), //field of view angle
                              1.0, // viewport ratio
                              0.1, // near plane
                              2000.0); //far plane
   }

我在网格中的渲染代码:

class Mesh {

     ...

     glm::mat4 modelMatrix;
     glm::mat4 cameraMatrix;
     glm::mat4 projectionMatrix;

     ...

     void Mesh::render () {

        glActiveTexture(GL_TEXTURE0 + fillwave_texture_unit);
        mTextureRegion->getTexture()->bind();
        mProgram->uniformPush("uDiffuseTextureUnit", fillwave_texture_unit);

        glm::mat4 eye = mLight->getShadowCamera()->getEye();
        glm::mat4 projection = mLight->getShadowCamera()->getProjection();
        glm::vec4 lightTranslation = mLight->getShadowCamera()->getTranslation();
        glm::vec4 lightIntensity = mLight->getIntensity()();

        //MVP model
        shader->uniformPush ("uModelMatrix", modelMatrix);
        shader->uniformPush ("uCameraMatrix", cameraMatrix);
        shader->uniformPush ("uProjectionMatrix", projectionMatrix);

        //MVP light
        shader->uniformPush ("uLight.position", lightTranslation);
        shader->uniformPush ("uLight.intensity", lightIntensity);
        shader->uniformPush ("uShadowTexUnit", mLight->getTextureUnit());

        glm::mat4 biasMatrix(
        0.5, 0.0, 0.0, 0.0,
        0.0, 0.5, 0.0, 0.0,
        0.0, 0.0, 0.5, 0.0,
        0.5, 0.5, 0.5, 1.0
        );

        shader->uniformPush ("uLight.mvp",biasMatrix *
                                           projection * // projection
                                           eye * // camera matrix
                                           m); // model matrix of this mesh
     }
}

1 个答案:

答案 0 :(得分:0)

我终于搞定了。窗口的分辨率设置为1920X1200,纹理大小也等于1920x1200。我的屏幕是1440x900所以我将屏幕1440x900呈现为纹理1920x1200。这就是阴影被移动的原因。纹理部分填充。

bottom left - camera view, bottom right - depth from camera view