3

只是一个关于绘制四边形的快速问题。我目前正在使用:

GraphicsDevice.DrawPrimitives(PrimitiveType primitiveType,
                        int startVertex, int primitiveCount);

这可以完美地绘制我的四边形,但我可以让它工作的唯一方法是为我的四边形使用六 (6) 个顶点(将它们绘制为两个三角形)。我只是想知道是否可以为 GPU 提供四 (4) 个顶点并仍然保留我的动态顶点缓冲区。

我知道我可以用 DrawUserIndexedPrimitives() 做到这一点,但我想要我的缓冲区!;)

编辑:我需要吗,如果需要,我在哪里告诉我的 GPU 我每两个三角形给它四个顶点?我目前将四边形存储为顶点缓冲区中的 nQuads * 6 个顶点,而我的 GPU 将每三个顶点用作一个三角形。因此,只需切换到四个顶点就意味着:

Quads: {v1,v2,v3,v4} {v5,v6,v7,v8} ...
Triangles: {v1,v2,v3} {v4,v5,v6} {v7,v8 ...}

这不是一件好事,因为第二个三角形使用第一个四边形的一个顶点,第三个使用第二个四边形的两个顶点,依此类推。

编辑 2:对不起,我实际上使用的是动态顶点缓冲区。

发布一些关于我如何用六个顶点制作四边形的代码:

        // ## CONSTRUCTION
        // Setting up buffer and vertex holder.
        particleVertexBuffer = new DynamicVertexBuffer(graphicsDevice,
            ParticleQuad.VerticesSizeInBytes * nMaxParticles,
            BufferUsage.WriteOnly);
        particleVertices = new ParticleVertex[nMaxParticles * 6];

        // ## ADD VERTICES
        particleVertices[i].Set();
        particleVertices[i+1].Set();
        particleVertices[i+2].Set();
        particleVertices[i+3].Set();
        particleVertices[i+4].Set();
        particleVertices[i+5].Set();


        // ## SET BUFFER
        particleVertexBuffer.SetData(particleVertices, 0, nMaxParticles * 6, SetDataOptions.NoOverwrite);
        graphicsDevice.Vertices[0].SetSource(particleVertexBuffer, 0, ParticleVertex.SizeInBytes);

        // ## DRAW
        graphicsDevice.DrawPrimitives(PrimitiveType.TriangleList,
                    FirstUsedParticle * 6, ((LastUsedParticle - FirstUsedParticle)+1)* 2);

现在,还有更多内容,因为我使用了循环队列和其他一些东西,但这足以理解。

4

1 回答 1

2

是的,这是可能的。您需要使用 TriangleStrip 作为 PrimitiveType。在您的 VertexBuffer 中,您按以下顺序定义您的顶点:

1---3
|\  |
| \ |
|  \|
0---2
编辑以解决您修改后的问题

我不知道您的代码是什么样的,但通常有关原始计数的信息用于以下位置:

  • 缓冲区创建:public VertexBuffer ( GraphicsDevice graphicsDevice, int sizeInBytes, BufferUsage usage )//sizeInBytes 取决于原始计数。
  • 绘制调用:public void DrawPrimitives ( PrimitiveType primitiveType, int startVertex, int primitiveCount )

请注意,在更多地方需要有关您的顶点格式大小的信息,但不应该成为问题,因为大小不应该改变。

更新

谢谢你的澄清,现在我知道了,你想要实现什么。处理您的问题的正确方法是将顶点缓冲区与索引缓冲区结合使用。顶点缓冲区保存顶点,索引缓冲区保存连接顶点以形成三角形的顺序。想象一下,您想在屏幕上绘制以下四边形:

 (0,1)________(1,1)
     |        |
     |        |
     |________|
 (0,0)        (1,0)

你之前做的是这样的:

Vector3 v0, v1, v2, v3;
v0 = new Vector3(0, 0, 0);
v1 = new Vector3(1, 0, 0);
v2 = new Vector3(0, 1, 0);
v3 = new Vector3(1, 1, 0);

List vertices = new List();
//first triangle
vertices.add(v0);
vertices.add(v2);
vertices.add(v1);

//second triangle
vertices.add(v2);
vertices.add(v1);
vertices.add(v3);

VertexBuffer vb = new VertexBuffer(...);
vb.setData(verticesToDraw);

//draw the scene using PrimitiveType.TriangleList and primitive count 2

你现在要做的是:

//vertices stay the same as in the example above!
List vertices = new List();
vertices.add(v0);
vertices.add(v1);
vertices.add(v2);
vertices.add(v3);

int[] indices = new int[6];
//first triangle
indices[0] = 0;
indices[1] = 2; 
indices[2] = 1;

//second triangle
indices[3] = 2;
indices[4] = 1;
indices[5] = 3;

VertexBuffer vb = new VertexBuffer(...);
IndexBuffer ib = new IndexBuffer(...);

vb.setData(vertices);
ib.setData(indices);

/*draw using the DrawIndexedPrimitives() method rather than the
DrawPrimitives() method and use PrimitiveType.TriangleList and
primitive count 2.*/

您会看到,每个四边形保存 2 个顶点,但必须使用 6 个索引来指定从顶点构建三角形的顺序。因此,此方法仅在您的顶点很大(包含许多信息,如法线、纹理坐标等)并且由许多三角形共享时才有用。

Riemers.net有一个关于顶点和索引缓冲区的非常好的、简短且易于理解的教程。

于 2010-07-07T15:09:55.403 回答