我正在尝试让太阳着色器起作用,但我无法让它起作用。
目前我得到的是屏幕左下角的四分之一圆形/椭圆形,它真的粘在我的屏幕上(如果我移动相机,它也会移动)。
我所做的就是渲染两个三角形以形成覆盖整个屏幕的四边形,并使用uniforms中的屏幕宽度和高度。
顶点着色器
#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
)。 - 获取片段在屏幕空间中的位置。
- 如果它们之间的距离低于某个值,则用黄色颜色绘制片段;
- 否则丢弃。
screen_space_light_pos
在x
和y
组件上都返回isnan()
。然而,在 vec3 形式中,before_division
在x
,y
和z
组件上都给出了正确的值。 - skiwibefore_division.z == 0
,不过我仍然需要找出原因。 - skiwiproj_matrix
和view_matrix
的统一变量,不知道为什么 GLSL/OpenGL 没有报错。 - skiwi