Open LeeMcQueen opened 3 years ago
■Grasses.cpp Grasses::update 函数里面加上了 ShadowMap 传值给fragmentShader uniform sampler2D shadowMap
■main.cpp
参数・buffer声明部分 unsigned int lightCameraUniformBuffer = 0; glGenBuffers(1, &lightCameraUniformBuffer); glBindBuffer(GL_UNIFORM_BUFFER, lightCameraUniformBuffer); glBufferData(GL_UNIFORM_BUFFER, 128, nullptr, GL_STATIC_DRAW); glBindBufferBase(GL_UNIFORM_BUFFER, 4, lightCameraUniformBuffer);
mainLoop 里面的草地部分 需要的光线相机的参数 观察矩阵 glm::mat4 lightViewMatrix = shadowMapCamera.getViewMatrix(); 正交投影矩阵 (模拟的是平行光所以我们不需要透视投影) glm::mat4 lightProjectionMatrix = masterRenderer.getProjectionMatrix(false); glBindBufferBase(GL_UNIFORM_BUFFER, 4, lightCameraUniform); glBindBuffer(GL_UNIFORM_BUFFER, lightCameraUniformBuffer); glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, &lightViewMatrix); glBufferSubData(GL_UNIFORM_BUFFER, 64, 64, &lightProjectionMatrix);
■teseShader 加上光线相机相关参数 layout(set = 0, binding = 0) uniform lightCameraBufferObject { mat4 view; mat4 proj; }lightCamera;
out TESE_OUT { vec4 fragPosLightSpace; vec3 normal; vec2 uv; } tese_out
mat4 grassesLightVPMatrix = lightCamera.view lightCamera.proj; tese_out.fragPosLightSpace = grassesLightVPMatrix vec4(p, 1.0);
■fragmentShader in TESE_OUT { vec4 fragPosLightSpace; vec3 normal; vec2 uv; } frag_in;
float calculateShadow(Vec4 FragPosLightSpace) { vec3 ndcPos = FragPosLightSpace.xyz / FragPosLightSpace.w; vec3 winPos = ndcPos * 0.5 + 0.5; float closestDepth = texture(shadowMap, winPos.xy).r; flaot currentDepth = winPos.z; return currentDepth > closestDepth ? 1.0 : 0.0; }
float shadow = 0.0f; shadow = calculateShadow(frag_in.fragPosLightSpace); vec3 result = ((1.0 - shadow) + 0.3) * mixedColor;
outColor = vec4(result, 1.0) vec4(mixedColor NoL, 1.0);
看了一下好像和是不是computerShader并没有直接的关系,所以只要在FBO里面加上草地的渲染好像就可以了
■Grasses.cpp Grasses::update 函数里面加上了 ShadowMap 传值给fragmentShader uniform sampler2D shadowMap
■main.cpp
参数・buffer声明部分 unsigned int lightCameraUniformBuffer = 0; glGenBuffers(1, &lightCameraUniformBuffer); glBindBuffer(GL_UNIFORM_BUFFER, lightCameraUniformBuffer); glBufferData(GL_UNIFORM_BUFFER, 128, nullptr, GL_STATIC_DRAW); glBindBufferBase(GL_UNIFORM_BUFFER, 4, lightCameraUniformBuffer);
mainLoop 里面的草地部分 需要的光线相机的参数 观察矩阵 glm::mat4 lightViewMatrix = shadowMapCamera.getViewMatrix(); 正交投影矩阵 (模拟的是平行光所以我们不需要透视投影) glm::mat4 lightProjectionMatrix = masterRenderer.getProjectionMatrix(false); glBindBufferBase(GL_UNIFORM_BUFFER, 4, lightCameraUniform); glBindBuffer(GL_UNIFORM_BUFFER, lightCameraUniformBuffer); glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, &lightViewMatrix); glBufferSubData(GL_UNIFORM_BUFFER, 64, 64, &lightProjectionMatrix);
■teseShader 加上光线相机相关参数 layout(set = 0, binding = 0) uniform lightCameraBufferObject { mat4 view; mat4 proj; }lightCamera;
out TESE_OUT { vec4 fragPosLightSpace; vec3 normal; vec2 uv; } tese_out
mat4 grassesLightVPMatrix = lightCamera.view lightCamera.proj; tese_out.fragPosLightSpace = grassesLightVPMatrix vec4(p, 1.0);
■fragmentShader in TESE_OUT { vec4 fragPosLightSpace; vec3 normal; vec2 uv; } frag_in;
float calculateShadow(Vec4 FragPosLightSpace) { vec3 ndcPos = FragPosLightSpace.xyz / FragPosLightSpace.w; vec3 winPos = ndcPos * 0.5 + 0.5; float closestDepth = texture(shadowMap, winPos.xy).r; flaot currentDepth = winPos.z; return currentDepth > closestDepth ? 1.0 : 0.0; }
float shadow = 0.0f; shadow = calculateShadow(frag_in.fragPosLightSpace); vec3 result = ((1.0 - shadow) + 0.3) * mixedColor;
outColor = vec4(result, 1.0) vec4(mixedColor NoL, 1.0);