Example #1
0
void cvColor::draw( M3dView & view, const MDagPath & path, 
							 M3dView::DisplayStyle style,
							 M3dView::DisplayStatus status )
{ 
	// cout << "cvColor::draw\n";

	MStatus		stat;
	MObject		thisNode = thisMObject();

	MPlug enPlug( thisNode, drawingEnabled );
	bool doDrawing; 
	stat = enPlug.getValue ( doDrawing );
	if (!stat) {
		stat.perror("cvColor::draw get drawingEnabled");
		return;
	}

	if (!doDrawing)
		return;

	MPlug szPlug( thisNode, pointSize );
	float ptSize; 
	stat = szPlug.getValue ( ptSize );
	if (!stat) {
		stat.perror("cvColor::draw get pointSize");
		ptSize = 4.0;
	}

	MPlug cvPlug( thisNode, cvLocations );
	MObject cvObject;
	stat = cvPlug.getValue(cvObject);
	if (!stat) {
		stat.perror("cvColor::draw get cvObject");
		return;
	}

	MFnPointArrayData cvData(cvObject, &stat);
	if (!stat) {
		stat.perror("cvColor::draw get point array data");
		return;
	}

	MPointArray cvs = cvData.array( &stat );
	if (!stat) {
		stat.perror("cvColor::draw get point array");
		return;
	}

	// Extract the 'worldMatrix' attribute that is inherited from 'dagNode'
	//
	MFnDependencyNode fnThisNode( thisNode );
	MObject worldSpaceAttribute = fnThisNode.attribute( "worldMatrix" );
	MPlug matrixPlug( thisNode, worldSpaceAttribute);

	// 'worldMatrix' is an array so we must specify which element the plug
	// refers to
	matrixPlug = matrixPlug.elementByLogicalIndex (0);

	// Get the value of the 'worldMatrix' attribute
	//
	MObject matObject;
	stat = matrixPlug.getValue(matObject);
	if (!stat) {
		stat.perror("cvColor::draw get matObject");
		return;
	}

	MFnMatrixData matrixData(matObject, &stat);
	if (!stat) {
		stat.perror("cvColor::draw get world matrix data");
		return;
	}

	MMatrix worldSpace = matrixData.matrix( &stat );
	if (!stat) {
		stat.perror("cvColor::draw get world matrix");
		return;
	}

	if ( view.isColorIndexMode() ) {
		cerr << "Can't update cv colors in color index mode\n";
		return;
	}

	view.beginGL(); 

	// Push the color settings
	// 
	glPushAttrib( GL_CURRENT_BIT | GL_POINT_BIT );
	glPointSize( ptSize );
	glDisable ( GL_POINT_SMOOTH );  // Draw square "points"

	glBegin( GL_POINTS );

		int numCVs = cvs.length();
		for (int i = 0; i < numCVs; i++) {
			// cout << "cv[" << i << "]: " << cvs[i] << ": ";
			MPoint		cv( cvs[i] );
			MColor		cvColor;

			cv *= worldSpace;

			if (cv.x < 0 && cv.y < 0) {
				// cout << "Red";
				cvColor.r = 1.0;
				cvColor.g = 0.0;
				cvColor.b = 0.0;
			} else if (cv.x < 0 && cv.y >= 0) {
				// cout << "Cyan";
				cvColor.r = 0.0;
				cvColor.g = 1.0;
				cvColor.b = 1.0;
			} else if (cv.x >= 0 && cv.y < 0) {
				// cout << "Blue";
				cvColor.r = 0.0;
				cvColor.g = 0.0;
				cvColor.b = 1.0;
			} else if (cv.x >= 0 && cv.y >= 0) {
				// cout << "Yellow";
				cvColor.r = 1.0;
				cvColor.g = 1.0;
				cvColor.b = 0.0;
			}
			// cout << endl;

			view.setDrawColor ( cvColor );
			glVertex3f( (float)cvs[i].x, (float)cvs[i].y, (float)cvs[i].z);
		} 
	glEnd();

	glPopAttrib();

	view.endGL();
}