Maya API How-To #07

Back · Previous · Next Maya

How do I use MPxLocatorNode::draw() to draw in static world-space independent of the parent locator's transformation?

When the MPxLocator::draw() method is initiated the OpenGL MODELVIEW describes the matrix for the offset from the camera's eyepoint to your node. Rendering with this matrix will draw using the node's object space.

To draw in world-space, find the Maya Camera represented by the current view and determine the inverse of this Camera's transformation matrix. Load this inverted matrix into the OpenGL MODELVIEW. Rendering with this matrix will draw using Maya's world-space, ignoring the transformations applied to your MPxLocator node.

void myMPxLocator::draw( M3dView & view,
                         const MDagPath & dagPath,
                         M3dView::DisplayStyle style,
                         M3dView::DisplayStatus displayStatus )
{
  MStatus                     status;

  MDagPath                    thisCameraView;       // the dagPath for this modelPanel's Camera
  MMatrix                     cameraInverseMatrix;  // inverse matrix for the modelPanel's Camera
  double                      dMatrix[4][4];        // the 4Ч4 array for the OpenGL matrix

  // Get the Maya Camera associated with this view
  view.getCamera( thisCameraView );

  // Load inverse of the Camera's Matrix
  cameraInverseMatrix = (thisCameraView.inclusiveMatrix()).inverse();

  // Get 4Ч4 array to represent matrix (required for glLoadMatrix)
  cameraInverseMatrix.get( dMatrix );

  // Initialize Maya's GL
  view.beginGL();

  glMatrixMode( GL_MODELVIEW );
  glPushMatrix();

  // Load the inverse of the Camera's matrix into the Model View.
  // This places the render origin at Maya's world center.
  glLoadMatrixd( (GLdouble *)dMatrix );

  // Now draw your OpenGL using world-coordinates for the vertices
  glBegin( GL_LINES )
  /* ... render here ... */
  glEnd();

  glPopMatrix();

  view.endGL();
}

Monday, January 08, 2001