Negative Scale

From K-3D

Jump to: navigation, search

Problem Statement

Under certain circumstances, a transform matrix can turn a mesh "inside-out", typically when the matrix includes negative scale factors. This can make OpenGL mesh rendering somewhat tricky, since the transform matrix affects both the geometry of the mesh (including normal vectors), and the way in which OpenGL decides which polygons are "front" and "back" polys. Without handling this special-case, polygons that are rendered with negative scale will be rendered incorrectly, typically appearing unlit (dark).

Problem Resolution

To handle this case, painter authors need to adjust their definition of "front" and "back" polygons based on the transform matrix used while rendering a mesh. There is a flag in k3d::gl::painter_render_state which can be used for this purpose, inside_out:

void on_paint_mesh(const k3d::mesh& Mesh, const k3d::gl::painter_render_state& RenderState)
{
  glFrontFace(RenderState.inside_out ? GL_CCW : GL_CW);
  
  /* Remaining implementation here */
}

Note that the choice of orientation demonstrated here is arbitrary and dependent on the painter implementation. Polyhedron faces in K-3D are defined in clockwise order and typically rendered that way, but other algorithms or geometric types (such as patches) may require the opposite orientation to render correctly.

Personal tools