Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glDrawElements and flat shading

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

like image 725
WesDec Avatar asked May 19 '11 09:05

WesDec


People also ask

What is flat shading OpenGL?

Flat shading is the simplest shading model which calculates the illumination at a single point for each polygon (or polygon vertices in OpenGL) which means that it yields the same value for all the pixels across the polygon. Advantages. Fast, a single computation per polygon (or one per polygon vertex in OpenGL)

What is glShadeModel?

The glShadeModel function selects flat or smooth shading.


1 Answers

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)
like image 66
Nicolas Lefebvre Avatar answered Oct 03 '22 09:10

Nicolas Lefebvre