gpt4 book ai didi

OpenGL - GLSL Geometry Shader - gl_Position to gl_FragCoord(OpenGL-GLSL几何图形着色器-GL_位置到GL_FragCoord)

转载 作者:bug小助手 更新时间:2023-10-26 20:37:25 26 4
gpt4 key购买 nike



How can I convert gl_Position to gl_FragCoord in order to check if the gl_FragCoord is between 2 points?

如何将gl_Position转换为gl_FragCoord以检查gl_FragCoord是否在两个点之间?


See what I have done so far below "it glitch, there is a bit of red". I think I am badly converting gl_Position to gl_FragCoord.

请看我到目前为止所做的“It Gitch,There a bit of red”一文。我认为我正在严重地将gl_Position转换为gl_FragCoord。


Can someone help me with my problem? FYI: I am new with GLSL.

有人能帮我解决我的问题吗?仅供参考:我是GLSL的新手。


Geometry Shader:

几何体着色器:


#version 330 core

layout (triangles) in;
layout (triangle_strip, max_vertices = 3) out;

out vec3 color;

out vec2 v1;
out vec2 v2;
out vec2 v3;

vec2 halfScreenSize;

in DATA
{
vec3 color;
vec2 screenSize;
mat4 projection;
} data_in[];

vec2 ToFragCoord(vec2 position)
{
return position * halfScreenSize;
}

void main()
{
halfScreenSize = vec2(data_in[0].screenSize.x / 2, data_in[0].screenSize.y / 2);

v1 = ToFragCoord((data_in[0].projection * gl_in[0].gl_Position).xy);
v2 = ToFragCoord((data_in[1].projection * gl_in[1].gl_Position).xy);
v3 = ToFragCoord((data_in[2].projection * gl_in[2].gl_Position).xy);

gl_Position = data_in[0].projection * gl_in[0].gl_Position;
color = data_in[0].color;
EmitVertex();

gl_Position = data_in[1].projection * gl_in[1].gl_Position;
color = data_in[1].color;
EmitVertex();

gl_Position = data_in[2].projection * gl_in[2].gl_Position;
color = data_in[2].color;
EmitVertex();

EndPrimitive();
}

Fragment:

片段:


#version 330 core

in vec3 color;

in vec2 v1;
in vec2 v2;
in vec2 v3;

bool Check(vec2 a, vec2 b, vec2 c)
{
float len = distance(a,c) + distance(b,c);

if(len <= distance(a,b) + 1.0f
&& len >= distance(a,b) - 1.0f)
{
return true;
}

return false;
}

void main()
{
if(Check(v1, v2, gl_FragCoord.xy)
|| Check(v2, v3, gl_FragCoord.xy))
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
else
{
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
}

更多回答
优秀答案推荐

The clip space coordinate is a Homogeneous coordinate. You must do a perspective derivation to get a Cartesian normalized device space coordinate.

剪裁空间坐标是齐次坐标。必须进行透视推导才能获得笛卡尔归一化设备空间坐标。


Normalized device coordinates are in the range [-1.0, 1.0]. The conversion of a normalized device coordinate into the range [0.0, 1.0] is therefore x' = (x + 1) / 2.

归一化设备坐标在[-1.0,1.0]范围内。因此,归一化器件坐标到范围[0.0,1.0]的转换为x‘=(x+1)/2。


gl_FragCoord contains window relative x and y coordinate and the depth in the z coordinate. So you have to multiply the x and y component of the coordinate in the range [0.0, 1.0] with the resolution of the window.

GL_FragCoord包含窗口相对x和y坐标以及z坐标中的深度。因此,您必须将范围为[0.0,1.0]的坐标的x和y分量与窗口的分辨率相乘。


vec4 vh1 = data_in[0].projection * gl_in[0].gl_Position;
v1 = (vh1.xy / vh1.w + 1.0) / 2.0 * screenSize.xy;

Note: vh1 = data_in[0].projection * gl_in[0].gl_Position is only necessary if gl_in[0].gl_Position is not already transformed with the projection matrix in the vertex shader. If it is already transformed, it is simply vh1 = gl_in[0].gl_position;.

注意:仅当gl_in[0].gl_position尚未与顶点着色器中的投影矩阵一起变换时,才需要vh1=data_in[0].project*gl_in[0].gl_Position。如果它已经被转换,则它只需vh1=gl_in[0].gl_Position;。


更多回答

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