2

I would like to create a wire frame effect using a shader program written in AGAL for Stage3D.

I have been Googling and I understand that I can determine how close a pixel is to the edge of a triangle using barycentric coordinates (BC) passed into the fragment program via the vertex program, then colour it accordingly if it is close enough.

My confusion is in what method I would use to pass this information into the shader program. I have a simple example set up with a cube, 8 vertices and an index buffer to draw triangles between using them.

If I was to place the BC's into the vertex buffer then that wouldn't make sense as they would need to be different depending on which triangle was being rendered; e.g. Vetex1 might need (1,0,0) when rendered with Vetex2 and Vetex3, but another value when rendered with Vetex5 and Vetex6. Perhaps I am not understanding the method completely.

Do I need to duplicate vertex positions and add the aditional data into the vertex buffer, essentially making 3 vertices per triangle and tripling my vertex count? Do I always give the vertex a (1,0,0), (0,1,0) or (0,0,1) value or is this just an example? Am I over complicating this and is there an easier way to do wire-frame with shaders and Stage3d?

Hope that fully explains my problems. Answers are much appreciated, thanks!

4

2 回答 2

3

这完全取决于您的几何图形,而这个问题实际上是图形顶点着色的问题:您需要几何图形是 3 色的。好的起点是Wikipedia 文章

例如,假设(1, 0, 0)基向量是红色、(0, 1, 0)绿色和(0, 0, 1)蓝色。很明显,如果您使用以下基本元素构建几何图形

基本图形元素

那么您可以避免重复顶点,因为这样的图将是 3-colorable (即每条边,因此每个三角形,将有不同颜色的顶点)。您可以在任何方向平铺此基本元素,并且图形将保持 3 色:

平铺示例

于 2013-02-06T16:27:01.983 回答
1

您偶然发现了让我对 AGAL/Stage3D 感到疯狂的事情。API 中的限制阻止您在许多情况下使用共享顶点。线框渲染是一个问题的例子……但简单的平面着色也是另一个例子。您需要做的是为网格中的每个三角形创建三个唯一的顶点。对于每个顶点,添加一个额外的参数(或设计您的引擎以接受顶点法线并重用它们,因为您可能不会对线框进行着色)。

分别为每个三角形分配一个单位向量 A[1,0,0]、B[0,1,0] 或 C[0,0,1]。这会让你开始。请注意,显而易见的解决方案(在片段着色器中设置阈值并有条件地绘制像素)会产生非常难看的混叠结果。查看此页面,了解对片段程序渲染的线框进行抗锯齿技术的一些见解:

http://cgg-journal.com/2008-2/06/index.html

正如我所提到的,如果您希望实现平面着色,您需要采用类似的技术(每个三角形的唯一顶点)。由于没有等效于 GL_FLAT 并且无法使变化的寄存器返回平均值,因此实现平面着色的唯一方法是为给定三角形的每个顶点通道计算相同的光照......这意味着每个顶点都需要相同的顶点法线。

于 2013-01-19T09:56:03.933 回答