Opengl 着色器为正方形边缘着色

发布于 2025-01-13 07:14:50 字数 1898 浏览 2 评论 0原文

我有一个基本的 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
enter image description here

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.
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

八巷 2025-01-20 07:14:51

gl_FragCoord.xy包含窗口坐标,单位是像素(水平和垂直片段)。左下坐标为(0.5, 0.5),右上角坐标为(width-0.5, height-0.5)。
如果要获取立方体上某个片段的顶点坐标,需要将 pos 从顶点传递给片段着色器:

顶点着色器:

layout (location = 0) in vec3 pos;
out vec3 vertPos;

void main()
{
    vertPos = pos;

    // [...]
}

片段着色器:

in vec3 vertPos;

// [...]

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:

layout (location = 0) in vec3 pos;
out vec3 vertPos;

void main()
{
    vertPos = pos;

    // [...]
}

Fragment shader:

in vec3 vertPos;

// [...]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文