Opengl 着色器为正方形边缘着色
我有一个基本的 opengl 应用程序,它显示一个正方形,正方形的颜色是使用插值确定的。在我的片段着色器中,我试图检测像素是否沿着正方形的边缘之一运行,如果是,则将其着色为黑色。
这是我的顶点着色器。
#version 330
layout (location = 0) in vec3 pos;
out vec4 vertColour;
uniform mat4 model;
uniform mat4 projection;
void main()
{
gl_Position = projection * model * vec4(pos.x, pos.y, pos.z, 1.0);
vertColour = vec4(clamp(pos, 0.0f, 1.0f), 1.0f);
}
我的片段着色器
#version 330
out vec4 colour;
in vec4 vertColour;
void main()
{
float x = gl_FragCoord.x;
float y = gl_FragCoord.y;
float z = gl_FragCoord.z;
if((x == 1 && y == 1) ||
(x == -1 && y == -1) ||
(x == 1 && y == -1) ||
(x == -1 && y == 1) ||
(x == 1 && z == 1) ||
(x == -1 && z == -1) ||
(x == -1 && z == 1) ||
(x == 1 && z == -1) ||
(y == 1 && z == 1) ||
(y == -1 && z == -1) ||
(y == 1 && z == -1) ||
(y == -1 && z == 1))
{
colour = vertColour;
} else {
colour = vertColour;
}
}
这是我形成正方形的索引和顶点。
int[] indices = {
0,1,4,
4,5,1,
1,0,3,
3,2,1,
1,5,6,
6,1,2,
2,6,7,
7,6,5,
5,4,7,
7,2,3
};
float[] vertices = {
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
};
I have a basic opengl app which is displaying a square, the squares colour is determined using interpolation. In my fragment shader I am trying to detect if the pixel runs along one of the edges of the square and if so to colour it black.
This is my vertex shader.
#version 330
layout (location = 0) in vec3 pos;
out vec4 vertColour;
uniform mat4 model;
uniform mat4 projection;
void main()
{
gl_Position = projection * model * vec4(pos.x, pos.y, pos.z, 1.0);
vertColour = vec4(clamp(pos, 0.0f, 1.0f), 1.0f);
}
And my fragment shader
#version 330
out vec4 colour;
in vec4 vertColour;
void main()
{
float x = gl_FragCoord.x;
float y = gl_FragCoord.y;
float z = gl_FragCoord.z;
if((x == 1 && y == 1) ||
(x == -1 && y == -1) ||
(x == 1 && y == -1) ||
(x == -1 && y == 1) ||
(x == 1 && z == 1) ||
(x == -1 && z == -1) ||
(x == -1 && z == 1) ||
(x == 1 && z == -1) ||
(y == 1 && z == 1) ||
(y == -1 && z == -1) ||
(y == 1 && z == -1) ||
(y == -1 && z == 1))
{
colour = vertColour;
} else {
colour = vertColour;
}
}
Here are my indices and vertices to form the square.
int[] indices = {
0,1,4,
4,5,1,
1,0,3,
3,2,1,
1,5,6,
6,1,2,
2,6,7,
7,6,5,
5,4,7,
7,2,3
};
float[] vertices = {
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
};
In effect I'm trying to determine if the location of the current fragment lies at either the max / minimum value of each edge. Here is what I see prior to trying the outline
And when I try to apply the outline the whole cube turns black. I assume there is something wrong in my logic in the fragment shader but have not been able to spot the issue. Any help is welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
gl_FragCoord.xy
包含窗口坐标,单位是像素(水平和垂直片段)。左下坐标为(0.5, 0.5),右上角坐标为(width-0.5, height-0.5)。如果要获取立方体上某个片段的顶点坐标,需要将 pos 从顶点传递给片段着色器:
顶点着色器:
片段着色器:
gl_FragCoord.xy
contains the window coordinates, the units are pixels (horizontal and vertical fragments). The bottom left coordinate is (0.5, 0.5) and the top right is (width-0.5, height-0.5).If you want to get the vertex coordinates of a fragment on the cube, you need to pass pos from the vertex to the fragment shader:
Vertex shader:
Fragment shader: