我试图让一个太阳着色器工作,但我不能让它工作。
我目前得到的是我屏幕左下角四分之一的圆圈 / elipsis,那真的卡在我的屏幕上了 (如果我移动相机,它也会移动)。
我所做的就是渲染两个三角形,形成一个屏幕覆盖四边形,屏幕宽度和高度统一。
顶点着色器
#version 430 core
void main(void) {
const vec4 vertices[6] = vec4[](
vec4(-1.0, -1.0, 1.0, 1.0),
vec4(-1.0, 1.0, 1.0, 1.0),
vec4(1.0, 1.0, 1.0, 1.0),
vec4(1.0, 1.0, 1.0, 1.0),
vec4(1.0, -1.0, 1.0, 1.0),
vec4(-1.0, -1.0, 1.0, 1.0)
);
gl_Position = vertices[gl_VertexID];
}
片段着色器
#version 430 core
layout(location = 7) uniform int screen_width;
layout(location = 8) uniform int screen_height;
layout(location = 1) uniform mat4 view_matrix;
layout(location = 2) uniform mat4 proj_matrix;
out vec4 color;
uniform vec3 light_pos = vec3(-20.0, 7.5, -20.0);
void main(void) {
//calculate light position in screen space and get x, y components
vec2 screen_space_light_pos = (proj_matrix * view_matrix * vec4(light_pos, 1.0)).xy;
//calculate fragment position in screen space
vec2 screen_space_fragment_pos = vec2(gl_FragCoord.x / screen_width, gl_FragCoord.y / screen_height);
//check if it is in the radius of the sun
if (length(screen_space_light_pos - screen_space_fragment_pos) < 0.1) {
color = vec4(1.0, 1.0, 0.0, 1.0);
}
else {
discard;
}
}
我认为它的作用:
获取屏幕空间中太阳的位置(light_pos
)。
在屏幕空间中获取片段位置。
如果它们之间的距离低于某个值,则用绘制片段;
否则丢弃。
2
screen_space_light_pos
尚未在剪辑空间中。您错过了划分:
vec3 before_division = (proj_matrix * view_matrix * vec4(light_pos, 1.0)).xyw;
vec2 screen_space_light_pos = before_division.xy / before_division.z;
对于常见的proj_matrix
配置,screen_space_light_pos
将位于[-1,1]x[-1,1]
。要匹配screen_space_fragment_pos
范围,您可能需要调整screen_space_light_pos
:
screen_space_light_pos = screen_space_light_pos * 0.5 + 0.5;
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(5条)