10

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).

WesDec
  • 2,098
  • 4
  • 19
  • 23

2 Answers2

9

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)
Nicolas Lefebvre
  • 4,247
  • 1
  • 25
  • 29
  • does glProvokingVertex allow me to only store one normal per triangle? – Arne Feb 08 '12 at 03:33
  • 3
    @Arne: I am no expert, so others can correct me if I am wrong, but I dont think its possible. There is no such thing as triangle attributes. You have to instead derive it from the vertex attributes in the geometry shader. – WesDec Feb 08 '12 at 11:04
0

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...

Calvin1602
  • 9,413
  • 2
  • 44
  • 55