gpt4 book ai didi

android - OpenGL 阴影在对象上映射不正确的阴影

转载 作者:搜寻专家 更新时间:2023-11-01 08:24:12 24 4
gpt4 key购买 nike

我在 Android Native OpenGL C++ 应用程序中使用定向光实现了阴影贴图,阴影渲染正确,但我的代码中存在一些错误,导致结果如附图所示:

enter image description here

如图所示,模型上有奇怪的三角阴影,实现如下:

首先创建深度缓冲区和深度图如下:

if (sun->castShadow) {
glCullFace(GL_FRONT);
glGenFramebuffers(1, &depthMapFBO);
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
glGenTextures(1, &depthMap);
glBindTexture(GL_TEXTURE_2D, depthMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
glDrawBuffers(1, GL_NONE);
glReadBuffer(GL_NONE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glCullFace(GL_BACK);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
LOGI("framebuffer incomplete");
}
}

glTexImage2D 的内部格式不适用于 GL_Float,所以我使用了 GL_UNSIGNED_SHORT。

然后我从光 View 渲染场景,首先光 vector 是 (50,100,50),我对直射光方向有冲突,它是朝向光的方向还是来自光的方向,但该值创建了阴影和使用不同的光组件可以很好地渲染场景。代码是:

    if (sun->castShadow) {
double delta = GetCurrentTime() - firstFrame;
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
glClear(GL_DEPTH_BUFFER_BIT);
glm::vec3 pos = player->getPosition();
glm::vec3 tar = pos + (-sun->direction);
glm::mat4 lightSpaceProjection = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, -20.0f, 500.0f);
glm::mat4 lightSpaceView = glm::lookAt(pos, tar, glm::vec3(0, 1, 0));
lightSpaceMatrix = lightSpaceProjection * lightSpaceView;
shaders.setDepthLightSpaceUniform(lightSpaceMatrix);
terrain->renderDepth(delta);
for (item_it it = engineItems.begin(); it != engineItems.end(); ++it) {
if (it->get()->castShadow)
it->get()->renderDepth(delta);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

最终正常渲染场景:

shaders.setLightsUniforms(directlights, pointlights, spotlights);
shaders.setViewUniforms(camera);
double first = GetCurrentTime() - firstFrame;
terrain->render(first);
for (item_it it = engineItems.begin(); it != engineItems.end(); ++it) {
it->get()->render(first);
}

直接光计算的 fragment 着色器部分是:

    vec3 calcDirectLight(DirectLight light,vec3 norm,vec3 tLightPos,vec3 diffColor,vec3 specColor){ 
vec3 lightDir ;
if(Has_normal_map==1)
lightDir= normalize(tLightPos-tFragPos);
else
lightDir = normalize(light.direction - tFragPos);

float diff = max(dot(lightDir,norm), 0.0);
vec3 diffuse = light.color * diff * diffColor;

vec3 viewDir = normalize(tViewPos - tFragPos);
vec3 halfwayDir = normalize(lightDir + viewDir);
float spec = pow(max(dot(norm, halfwayDir), 0.0), 32.0);
vec3 specular = shininess * spec *specColor* light.color;
vec3 result;
if(light.castShadow==1){
vec3 projCoords = FragPosLightSpace.xyz / FragPosLightSpace.w;
projCoords = projCoords * 0.5 + 0.5;
float shadow = 1.0;
for (int i=0;i<4;i++){
shadow -= 0.2*(1.0-texture( shadowMap, projCoords.xy + poissonDisk[i]/700.0).r);
}
if(projCoords.z > 1.0)
shadow = 0.0;
result =light.intensity* shadow * (diffuse + specular);
}
else
result =light.intensity* (diffuse + specular);
return result;
}

还有一些其他的错误,但这是主要的

最佳答案

我认为问题出在这里:

if(Has_normal_map==1)
lightDir= normalize(tLightPos-tFragPos);
else
lightDir = normalize(light.direction - tFragPos);

我看到您正在使用定向光。计算定向光的方向很简单:

lightDir = light.direction

此外,我不明白为什么您根据 Has_normal_map 是否使用不同的公式来计算光的方向。我相信光的方向不依赖于法线贴图。我希望这能解决您的错误。

关于android - OpenGL 阴影在对象上映射不正确的阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46914953/

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