glDrawElements and flat shading

会有一股神秘感。 提交于 2019-12-19 05:53:59

问题


Is it possible to achieve flat shading in OpenGL when using glDrawElements to draw objects, and if so how? The ideal way would be to calculate a normal for each triangle only once, if possible.

The solution must only use the programmable pipeline (core profile).


回答1:


There are indeed ways around this without duplicating vertices, with some limitations for each one (at least those I can think of with my limited OpenGL experience).

I can see two solutions that would give you a constant value for the normal over each triangle :

  • declare the input as flat in your shader and pick which vertex gives its value via glProvokingVertex; fast but you'll get the normal for one vertex as the normal for the whole triangle, which might not look right
  • use a geometry shader taking triangles and outputing triangles to calculate a single normal per face. This is the most flexible way, allowing you to control the resulting effect, but it might be slow (and required geometry shader capable hardware, obviously)



回答2:


Sadly, the only way to do that is to duplicate all your vertices, since attributes are per-vertex and not per-triangle

When you think about it, this is what we did in immediate mode...



来源:https://stackoverflow.com/questions/6056679/gldrawelements-and-flat-shading

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