narrowPolyViewer.cpp
#include "narrowPolyViewer.h"
#include <maya/MItDag.h>
#include <maya/MStringArray.h>
#include <maya/MGlobal.h>
#include <maya/MPoint.h>
#include <maya/MSelectionList.h>
#include <maya/MItSelectionList.h>
#include <maya/MFnMesh.h>
#include <maya/MItMeshVertex.h>
#include <maya/MItMeshPolygon.h>
#include <maya/MPointArray.h>
#include <maya/MIOStream.h>
#if defined(OSMac_MachO_)
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif
#include <math.h>
narrowPolyViewer::narrowPolyViewer()
: fDrawManips(true)
, fLightTest(false)
{
setMultipleDrawEnable(true);
tol = 10;
}
narrowPolyViewer::~narrowPolyViewer()
{
}
MStatus narrowPolyViewer::setCameraList(const MDagPathArray &cameraList)
{
setMultipleDrawEnable(true);
fCameraList.clear();
unsigned ii = 0;
unsigned nCameras = cameraList.length();
for (ii = 0; ii < nCameras; ++ii) {
fCameraList.append(cameraList[ii]);
}
refresh();
return MS::kSuccess;
}
MStatus narrowPolyViewer::getCameraList(MDagPathArray &cameraList) const
{
cameraList.clear();
unsigned ii = 0;
unsigned nCameras = fCameraList.length();
for (ii = 0; ii < nCameras; ++ii) {
cameraList.append(fCameraList[ii]);
}
return MS::kSuccess;
}
MStatus narrowPolyViewer::getCameraList(MStringArray &cameraList) const
{
cameraList.clear();
unsigned ii = 0;
unsigned nCameras = fCameraList.length();
for (ii = 0; ii < nCameras; ++ii) {
cameraList.append(fCameraList[ii].partialPathName());
}
return MS::kSuccess;
}
MStatus narrowPolyViewer::appendCamera(const MDagPath &camera)
{
MStatus ReturnStatus = fCameraList.append(camera);
refresh(true);
return ReturnStatus;
}
MStatus narrowPolyViewer::removeCamera(const MDagPath &camera)
{
int ii = 0;
bool cameraRemoved = false;
int nCameras = (int)fCameraList.length();
for (ii = nCameras - 1; ii >= 0; ii--) {
if (camera == fCameraList[ii]) {
fCameraList.remove(ii);
cameraRemoved = true;
}
}
if (cameraRemoved) {
refresh();
}
return MS::kSuccess;
}
MStatus narrowPolyViewer::removeAllCameras()
{
MStatus ReturnStatus = fCameraList.clear();
refresh();
return ReturnStatus;
}
MString narrowPolyViewer::getCameraHUDName()
{
MString hudName("narrowPolyViewer: ");
MDagPath cameraPath;
getCamera(cameraPath);
cameraPath.pop();
hudName += cameraPath.partialPathName();
return hudName;
}
MStatus narrowPolyViewer::setIsolateSelect(MSelectionList &list)
{
setViewSelected(true);
return setObjectsToView(list);
}
MStatus narrowPolyViewer::setIsolateSelectOff()
{
return setViewSelected(false);
}
MStatus narrowPolyViewer::setLightTest(MSelectionList &list)
{
MStatus ReturnStatus = MS::kSuccess;
MItSelectionList it(list, MFn::kLight, &ReturnStatus);
if (MS::kSuccess != ReturnStatus) {
return ReturnStatus;
}
for (; !it.isDone(); it.next()) {
MDagPath lightPath;
if (MS::kSuccess != it.getDagPath(lightPath)) {
continue;
}
fLightList.append(lightPath);
}
if (fLightList.length() > 0) {
fLightTest = true;
}
return ReturnStatus;
}
void narrowPolyViewer::preMultipleDraw()
{
fCurrentPass = 0;
fDrawManips = false;
MDagPath oldCamera;
MStatus status = getCamera(oldCamera);
if (MS::kSuccess != status) {
status.perror("M3dView::getCamera");
}
fOldCamera = oldCamera;
displayHUD(false);
fTestCameraList.clear();
MStatus stat;
MSelectionList sList;
MGlobal::getActiveSelectionList(sList);
MDagPath dagPath;
stat = sList.getDagPath(0, dagPath);
if (stat != MS::kSuccess) return;
MItMeshPolygon itMeshPolygon(dagPath, MObject::kNullObj, &stat);
if (stat != MS::kSuccess) return;
beginGL();
for (; !itMeshPolygon.isDone(); itMeshPolygon.next())
{
MPointArray points;
itMeshPolygon.getPoints(points, MSpace::kWorld, &stat);
int length = points.length();
if (length == 3)
{
for (int i=0; i<length; i++)
{
MPoint p = points[i];
MPoint p1 = points[(i+1)%length];
MPoint p2 = points[(i+2)%length];
MVector v1(p1 - p);
MVector v2(p2 - p);
double angle = v1.angle(v2) * 180 / 3.14159;
#if 0
MString astr = "The angle is : ";
astr += angle;
astr += " tolerance : ";
astr += tol;
MGlobal::displayInfo( astr );
#endif
if ( (fabs( angle - tol ) < .0001) || angle < tol )
{
glBegin(GL_POLYGON);
glVertex3f((float)points[0].x, (float)points[0].y, (float)points[0].z);
glVertex3f((float)points[1].x, (float)points[1].y, (float)points[1].z);
glVertex3f((float)points[2].x, (float)points[2].y, (float)points[2].z);
glEnd();
break;
}
}
}
}
endGL();
}
void narrowPolyViewer::postMultipleDraw()
{
MStatus status = setCamera(fOldCamera);
fDrawManips = true;
updateViewingParameters();
if (MS::kSuccess != status) {
status.perror("M3dView::setCamera");
}
}
void narrowPolyViewer::preMultipleDrawPass(unsigned index)
{
fCurrentPass = index;
MStatus status;
if (MS::kSuccess != (status = setDisplayAxis(false))) {
status.perror("M3dView::setDisplayAxis");
}
if (MS::kSuccess != (status = setDisplayAxisAtOrigin(false))) {
status.perror("M3dView::setDisplayAxisAtOrigin");
}
if (MS::kSuccess != (status = setDisplayCameraAnnotation(false))) {
status.perror("M3dView::setDisplayCameraAnnotation");
}
MDagPath dagPath;
if (fCurrentPass == 0) {
getCamera(dagPath);
} else {
unsigned nCameras = fCameraList.length();
if (fCurrentPass <= nCameras) {
dagPath = fCameraList[fCurrentPass-1];
} else {
cerr << "Error ... too many passes specified: "
<< fCurrentPass << endl;
return;
}
}
if (MS::kSuccess != (status = setCameraInDraw(dagPath))) {
status.perror("M3dView::setCamera");
return;
}
MString thisCameraPath = dagPath.partialPathName();
fTestCameraList.append(thisCameraPath);
setObjectDisplay(M3dView::kDisplayEverything, true);
if (dagPath == fOldCamera) {
fDrawManips = true;
setObjectDisplay(M3dView::kDisplayGrid, true);
if (!objectDisplay(M3dView::kDisplayGrid)) {
MGlobal::displayError("objectDisplay kDisplayGrid should be true!");
}
if (MS::kSuccess != (status = setFogEnabled(true))) {
status.perror("M3dView::setFogEnabled");
}
if (!isFogEnabled()) {
MGlobal::displayError("setFogEnabled did not work!");
}
setBackgroundFogEnabled(false);
setObjectDisplay(M3dView::kDisplayLights, true);
setObjectDisplay(M3dView::kDisplayCameras, true);
setObjectDisplay(M3dView::kDisplayIkHandles, true);
setObjectDisplay(M3dView::kDisplayDimensions, true);
setObjectDisplay(M3dView::kDisplaySelectHandles, true);
MPoint textPos(0, 0, 0);
MString str("Main View");
drawText(str, textPos, M3dView::kLeft);
} else {
fDrawManips = false;
setObjectDisplay(M3dView::kDisplayGrid, false);
if (objectDisplay(M3dView::kDisplayGrid)) {
MGlobal::displayError(
"objectDisplay kDisplayGrid should be false!");
}
if (MS::kSuccess != (status = setFogEnabled(false))) {
status.perror("M3dView::setFogEnabled");
}
if (isFogEnabled()) {
MGlobal::displayError("setFogEnabled did not work!");
}
setObjectDisplay(M3dView::kDisplayLights, false);
setObjectDisplay(M3dView::kDisplayCameras, false);
setObjectDisplay(M3dView::kDisplayIkHandles, false);
setObjectDisplay(M3dView::kDisplayDimensions, false);
setObjectDisplay(M3dView::kDisplaySelectHandles, false);
}
if (fLightTest) {
unsigned step = 4;
unsigned min = 0 + (fCurrentPass * step);
unsigned max = (fCurrentPass * step);
if (max > 0) {
max--;
}
unsigned nLights = fLightList.length();
if (nLights < min) {
setLightingMode(kLightDefault);
} else {
if (nLights < max) {
max = nLights;
}
MSelectionList list;
unsigned ii = 0;
for (ii = min; ii < max; ii++) {
list.add(fLightList[ii].node());
}
MGlobal::setActiveSelectionList(list);
setLightingMode(kLightSelected);
}
} else {
setLightingMode(kLightDefault);
}
if (fCurrentPass % 2 == 0) {
setObjectDisplay(M3dView::kDisplayNurbsSurfaces, true);
setObjectDisplay(M3dView::kDisplayNurbsCurves, true);
}
updateViewingParameters();
}
void narrowPolyViewer::postMultipleDrawPass(unsigned index)
{
setObjectDisplay(M3dView::kDisplayEverything, true);
}
bool narrowPolyViewer::okForMultipleDraw(const MDagPath &dagPath)
{
if (!fDrawManips && dagPath.hasFn(MFn::kManipulator3D)) {
return false;
}
return true;
}
unsigned narrowPolyViewer::multipleDrawPassCount()
{
return fCameraList.length() + 1;
}
void narrowPolyViewer::removingCamera(MDagPath &cameraPath)
{
int ii = 0;
int nCameras = (int)fCameraList.length();
for (ii = nCameras - 1; ii >= 0; ii--) {
if (cameraPath == fCameraList[ii]) {
fCameraList.remove(ii);
}
}
}
void * narrowPolyViewer::creator()
{
return new narrowPolyViewer();
}
MStatus narrowPolyViewer::copy(MPx3dModelView &src)
{
return MS::kSuccess;
}
MStatus narrowPolyViewer::swap(MPx3dModelView &src)
{
return MS::kSuccess;
}
MString narrowPolyViewer::viewType() const
{
return MString("narrowPolyViewer");
}