I have a mesh data structure with polygons that can be either triangles and quads. What is the fastest way to draw this using OpenGL?
我有一个带有多边形的网格数据结构,可以是三角形和四边形。使用OpenGL绘制它的最快方法是什么?
The slow way is to iterate over the structure and for each polygon to make the appropriate glBegin() .. glEnd()
with either GL_QUADS
or GL_TRIANGLES
. I would like to avoid having to do glBegin() .. glEnd()
for each and every polygon.
缓慢的方法是迭代结构和每个多边形,以使用GL_QUADS或GL_TRIANGLES生成适当的glBegin().. glEnd()。我想避免为每个多边形做glBegin().. glEnd()。
Another option is to divide the structure into two structures, one containing the triangles and one containing the quads and then go over them separately. This is also something I'd like to avoid since I really want to keep them all in a single structure.
另一种选择是将结构划分为两个结构,一个包含三角形,一个包含四边形,然后分别进行处理。这也是我想要避免的,因为我真的想把它们全部保存在一个结构中。
Unfortunately triangulating the quads into triangles is not an option at this time.
不幸的是,将四边形三角化为三角形不是一种选择。
Is there a good solution to this?
有一个很好的解决方案吗?
3 个解决方案
#1
You may want to create two indice table, one for quads one for triangles and then write all the Quad, followed by triangles (or the inverse). To draw using an array of indices, use glDrawElements with a set of glVertexPointer and glEnableClientState to make it works.
您可能想要创建两个indice表,一个用于四边形,一个用于三角形,然后写入所有Quad,后跟三角形(或反向)。要使用索引数组进行绘制,请使用glDrawElements和一组glVertexPointer和glEnableClientState来使其工作。
Moreover if you really want to gain speed, you may place all of your vertices in a VBO, and your indice. This way you get all your vertices and indices on the GPU's RAM.
此外,如果你真的想要获得速度,你可以将所有顶点放在一个VBO和你的indice中。这样您就可以在GPU的RAM上获得所有顶点和索引。
#2
You can always convert them all to triangles, quite easily, too.
您也可以很容易地将它们全部转换为三角形。
Basically the pseudocode would look something like this:
基本上伪代码看起来像这样:
glBegin(GL_TRIANGLES);
for each (object *a in objectList)
if (a->type == TRIANGLE)
glVertex3fv(a->vertices);
if (a->type == QUAD)
{
glVertex3f(a->vertices[0]);
glVertex3f(a->vertices[1]);
glVertex3f(a->vertices[2]);
glVertex3f(a->vertices[2]);
glVertex3f(a->vertices[1]);
glVertex3f(a->vertices[3]);
}
glEnd();
You'd have to be careful about keeping them oriented the same way (clockwise or counterclockwise), but it should work!
你必须小心保持它们的方向相同(顺时针或逆时针),但它应该工作!
#3
As far as glPolygonMode(GL_LINE) and decomposed GL_QUADS go, glEdgeFlagPointer() should be the functionality you're looking for. GL_TRUE for the perimeter segments, GL_FALSE on the quad diagonals.
就glPolygonMode(GL_LINE)和分解的GL_QUADS而言,glEdgeFlagPointer()应该是您正在寻找的功能。周边区段的GL_TRUE,四边形对角线上的GL_FALSE。
Don't forget to glEnableClientState(GL_EDGE_FLAG_ARRAY).
不要忘记glEnableClientState(GL_EDGE_FLAG_ARRAY)。
#1
You may want to create two indice table, one for quads one for triangles and then write all the Quad, followed by triangles (or the inverse). To draw using an array of indices, use glDrawElements with a set of glVertexPointer and glEnableClientState to make it works.
您可能想要创建两个indice表,一个用于四边形,一个用于三角形,然后写入所有Quad,后跟三角形(或反向)。要使用索引数组进行绘制,请使用glDrawElements和一组glVertexPointer和glEnableClientState来使其工作。
Moreover if you really want to gain speed, you may place all of your vertices in a VBO, and your indice. This way you get all your vertices and indices on the GPU's RAM.
此外,如果你真的想要获得速度,你可以将所有顶点放在一个VBO和你的indice中。这样您就可以在GPU的RAM上获得所有顶点和索引。
#2
You can always convert them all to triangles, quite easily, too.
您也可以很容易地将它们全部转换为三角形。
Basically the pseudocode would look something like this:
基本上伪代码看起来像这样:
glBegin(GL_TRIANGLES);
for each (object *a in objectList)
if (a->type == TRIANGLE)
glVertex3fv(a->vertices);
if (a->type == QUAD)
{
glVertex3f(a->vertices[0]);
glVertex3f(a->vertices[1]);
glVertex3f(a->vertices[2]);
glVertex3f(a->vertices[2]);
glVertex3f(a->vertices[1]);
glVertex3f(a->vertices[3]);
}
glEnd();
You'd have to be careful about keeping them oriented the same way (clockwise or counterclockwise), but it should work!
你必须小心保持它们的方向相同(顺时针或逆时针),但它应该工作!
#3
As far as glPolygonMode(GL_LINE) and decomposed GL_QUADS go, glEdgeFlagPointer() should be the functionality you're looking for. GL_TRUE for the perimeter segments, GL_FALSE on the quad diagonals.
就glPolygonMode(GL_LINE)和分解的GL_QUADS而言,glEdgeFlagPointer()应该是您正在寻找的功能。周边区段的GL_TRUE,四边形对角线上的GL_FALSE。
Don't forget to glEnableClientState(GL_EDGE_FLAG_ARRAY).
不要忘记glEnableClientState(GL_EDGE_FLAG_ARRAY)。