使用顶点着色器时如何获得平坦的、非插值的颜色

发布于 2024-08-29 22:08:46 字数 439 浏览 7 评论 0原文

有没有办法实现这一点(OpenGL 2.1)?如果我像这样画线,

glShadeModel(GL_FLAT);
glBegin(GL_LINES);
  glColor3f(1.0, 1.0, 0.0);
  glVertex3fv(bottomLeft);
  glVertex3fv(topRight);

  glColor3f(1.0, 0.0, 0.0);
  glVertex3fv(topRight);
  glVertex3fv(topLeft);
  .
  .
  (draw a square)
  .
  .
glEnd();

我会得到所需的结果(每个边缘有不同的颜色),但我希望能够计算着色器中的片段值。如果我在设置着色器程序后执行相同的操作,我总是会在顶点之间获得插值颜色。有办法解决这个问题吗? (如果我能使用四边形得到相同的结果,那就更好了)

谢谢

Is there a way to achieve this (OpenGL 2.1)? If I draw lines like this

glShadeModel(GL_FLAT);
glBegin(GL_LINES);
  glColor3f(1.0, 1.0, 0.0);
  glVertex3fv(bottomLeft);
  glVertex3fv(topRight);

  glColor3f(1.0, 0.0, 0.0);
  glVertex3fv(topRight);
  glVertex3fv(topLeft);
  .
  .
  (draw a square)
  .
  .
glEnd();

I get the desired result (a different colour for each edge) but I want to be able to calculate the fragment values in a shader. If I do the same after setting up my shader program I always get interpolated colors between vertices. Is there a way around this? (would be even better if I could get the same results using quads)

Thanks

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

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

发布评论

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

评论(1

不乱于心 2024-09-05 22:08:46

如果您不希望基元内的顶点属性之间进行插值(例如,您的情况下线段中的颜色),您将需要传递相同的颜色两次,因此最终会复制您的几何图形:

 v0             v1          v2
 x--------------x-----------x    

(v0 由结构体 p0 组成以及 p1 和 c1 的 c0、v1 等..)

对于使用颜色插值绘制线条:

glBegin(GL_LINES);
//draw first segment
glColor3fv(&c0); glVertex3fv(&p0);
glColor3fv(&c1); glVertex3fv(&p1);
//draw second segment
glColor3fv(&c1); glVertex3fv(&p1);
glColor3fv(&c2); glVertex3fv(&p2);
glEnd();

对于不使用插值绘制:

glBegin(GL_LINES);
//draw first segment
glColor3fv(&c0); glVertex3fv(&p0);
glColor3fv(&c0); glVertex3fv(&p1);
//draw second segment
glColor3fv(&c1); glVertex3fv(&v1);
glColor3fv(&c1); glVertex3fv(&v2);
glEnd();

请注意,这意味着您不能再使用 GL_x_STRIP 拓扑,因为您不想共享基元内的属性。

If you don't want the interpolation between vertice attributes inside a primitive (e.g. color in a line segment in your case) you'll need to pass the same color twice, so end up duplicating your geometry:

 v0             v1          v2
 x--------------x-----------x    

(v0 is made of structs p0 and c0, v1 of p1 and c1 etc..)

For drawing the line with color interpolation:

glBegin(GL_LINES);
//draw first segment
glColor3fv(&c0); glVertex3fv(&p0);
glColor3fv(&c1); glVertex3fv(&p1);
//draw second segment
glColor3fv(&c1); glVertex3fv(&p1);
glColor3fv(&c2); glVertex3fv(&p2);
glEnd();

For drawing without interpolation:

glBegin(GL_LINES);
//draw first segment
glColor3fv(&c0); glVertex3fv(&p0);
glColor3fv(&c0); glVertex3fv(&p1);
//draw second segment
glColor3fv(&c1); glVertex3fv(&v1);
glColor3fv(&c1); glVertex3fv(&v2);
glEnd();

Note this mean you can no longer use GL_x_STRIP topology, since you don't want to share attributes inside a primitive.

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