2
$\begingroup$

Following up on my recent question on ComputerGraphics.SE — how is flat shading of non-planar polygons implemented in Blender?

If polygons are rendered using GL_TRIANGLE_FAN in OpenGL, one approach would be to upload a single normal vector for every individual polygon that's rendered. This might not be very efficient though.

On the other hand, if polygons are split into triangles before everything is rendered, then these triangles somehow have to share a normal vector. This sounds more like glDrawArrays() than glDrawElements(), which again might not be the best solution.

$\endgroup$
2
  • 1
    $\begingroup$ Exact OpenGL use is outside the scope of this site, for that just read Blender's source code. However basic method can be answered. $\endgroup$
    – ideasman42
    Feb 24 '16 at 22:06
  • $\begingroup$ @ideasman42 Just the general workflow is Ok, converting this into OpenGL myself should not be a problem. $\endgroup$
    – Ailurus
    Feb 24 '16 at 22:07
3
$\begingroup$

Blender's face normals are calculated as follows:

  • For quads, we use the cross product of 2 edge vectors which don't share a vertex.
  • For polygons, we use Newell's Method.

To draw with these normals, we store the tessellated triangles and simply copy the normals calculated from the original faces, into each of the tessellation triangles normals (which is sent as a buffer to the GPU).

See: gpu_buffers.c for details (though you will probably want to see how this API is used by other files to see how it works).

$\endgroup$
4
  • $\begingroup$ Could you perhaps point me to the relevant part(s) of the code (if possible on git.blender.org/gitweb)? $\endgroup$
    – Ailurus
    Feb 24 '16 at 22:35
  • $\begingroup$ @Ailurus relevant to what exactly? (normal calculation? storing tessellation, uploading to GPU?) $\endgroup$
    – ideasman42
    Feb 24 '16 at 22:36
  • $\begingroup$ To visualising/rendering the triangles. I'm just curious whether something like glDrawArrays() is used or not. I don't have a local checkout at the moment so I can't grep it. Normal calculation is clear, I use something similar in my code. $\endgroup$
    – Ailurus
    Feb 24 '16 at 22:38
  • 1
    $\begingroup$ Added link to source. $\endgroup$
    – ideasman42
    Feb 24 '16 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.