Cube with triangle strips and vector normals in openGL

偶尔善良 提交于 2019-12-06 14:21:33

问题


I'm trying to create a cube using one series of vertices such as attempted in this example (which I believe is wrong) and also talked about on this forum. The answer I'm looking for should look something like this:

 1,  1, -1
-1, -1, -1,
 1,  1,  1

... and so on. I'm hoping to get this down to a minimum of 13 vertices (6 sided cube = 12 triangles).

Is this possible?


回答1:


There's little benefit in minimizing the vertices of a triangle / quad strip. One advantage of having each face has its own vertex (for a sharp-edged mesh like the cube) is the ability to specify different normal to each vertex, which might might be important for you if you want to have correct per-pixel lighting with a specular reflection.

(Anyway, if you're not concerned about normals or anything: The best you can do in terms of sheer efficiency is specify just the 8 vertices and use an index array. This enables the use of vertex cache so your vertex shader is likely to run only once per each vertex, even though it is used by several faces).




回答2:


It requires 14 vertices, as the first 3 make a triangle, and then every additional triangle uses an extra vertex (2+num_triangles). This is how I managed it:

v = ((-w, -h,  d),  # front-bottom-left     0
     ( w, -h,  d),  # front-bottom-right    1
     (-w,  h,  d),  # front-top-left        2
     ( w,  h,  d),  # front-top-right       3
     (-w, -h, -d),  # back-bottom-left      4
     ( w, -h, -d),  # back-bottom-right     5
     (-w,  h, -d),  # back-top-left         6
     ( w,  h, -d))  # back-top-right        7


strip_vertices = (v[7] + v[6] + v[3] + v[2] + v[0] + v[6] + v[4] + 
                  v[7] + v[5] + v[3] + v[1] + v[0] + v[5] + v[4])

As mentioned already, this causes problems with normals, so I guess I'll be rewriting this in a non-strip format.




回答3:


I can get it as low as 17 if I've understood it correctly

 1: -1,  1,  1
 2:  1,  1,  1
 3: -1, -1,  1
 4:  1, -1,  1
 5: -1, -1, -1
 6:  1, -1, -1
 7: -1,  1, -1
 8:  1,  1, -1
 9: -1,  1,  1
10:  1,  1,  1
11:  1, -1,  1
12:  1,  1, -1
13:  1, -1, -1
14: -1, -1, -1
15: -1,  1, -1
16: -1, -1,  1
17: -1,  1,  1


来源:https://stackoverflow.com/questions/4157998/cube-with-triangle-strips-and-vector-normals-in-opengl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!