Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenGL阴影映射-阴影位于错误的位置_C++_Linux_Opengl_Render_Shadow - Fatal编程技术网

C++ OpenGL阴影映射-阴影位于错误的位置

C++ OpenGL阴影映射-阴影位于错误的位置,c++,linux,opengl,render,shadow,C++,Linux,Opengl,Render,Shadow,我有一个阴影的问题,我没有看到我的代码中有任何错误。 这一切都可以在视频中看到 我的光源在没有纹理的球里面,它在移动。 阴影被替换 基本上: 在阴影过程中,我使用与渲染过程相同的程序 我的阴影纹理与视口大小相同(1920 x 1200) 我的光源是点光源 我只使用片段和顶点着色器(没有几何体和镶嵌) 只有一个光源 我的深度mvp矩阵是LightProjection*LightCamera*MeshModelMatrix 我的预测是: 视野90 近平面0.1 远平面5000000.0 视窗比率

我有一个阴影的问题,我没有看到我的代码中有任何错误。 这一切都可以在视频中看到

我的光源在没有纹理的球里面,它在移动。 阴影被替换

基本上:

  • 在阴影过程中,我使用与渲染过程相同的程序
  • 我的阴影纹理与视口大小相同(1920 x 1200)
  • 我的光源是点光源
  • 我只使用片段和顶点着色器(没有几何体和镶嵌)
  • 只有一个光源
  • 我的深度mvp矩阵是LightProjection*LightCamera*MeshModelMatrix
  • 我的预测是: 视野90 近平面0.1 远平面5000000.0
  • 视窗比率1
  • 摄像头(0.0,1.0,0.0)
  • 我正确地使用了偏差矩阵
  • 图形nVidia NVM310M驱动程序版本。304.117 -我显示了额外的视口以从灯光角度查看场景
我所有的代码都在抽象背后,所以我从中选择了所有(我认为)会影响阴影的东西。点光源工作正常,但阴影位于错误的位置

我的片段着色器:

#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;
}
Light class构造函数中的我的相机设置:

   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
     }
}

我终于成功了。窗口的分辨率设置为1920X1200,纹理大小也等于1920X1200。我的屏幕是1440x900,因此我正在将屏幕1440x900渲染为纹理1920x1200。这就是影子被移动的原因。纹理部分填充


更新了视频,更新了代码。添加了额外的调试窗口以从摄影机的角度查看场景。我终于让它工作了。窗口的分辨率设置为1920x1200,纹理大小也等于1920x1200。我的屏幕是1440x900,因此我正在将screenv 1440x900屏幕渲染为纹理1920x1200。这就是影子被移动的原因。纹理部分填充。
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
     }
}