gpt4 book ai didi

c++ - OpenGL 阴影映射 - 阴影在错误的位置

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:32:17 29 4
gpt4 key购买 nike

我的阴影有问题,而且我的代码中没有发现任何错误。这一切都在视频中可见。

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

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

基本上:

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

我所有的代码都是抽象的,所以我从中选择了所有可以影响阴影的东西(我认为)。点光源效果很好,但阴影出现在错误的位置。

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

最佳答案

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

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

关于c++ - OpenGL 阴影映射 - 阴影在错误的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25058777/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com