示例#1
0
        public void ResetCamera()
        {
            //<PerspectiveCamera UpDirection="0,1,0" Position="1,1,1" LookDirection="-1,-1,-1" FieldOfView="45" />
            MDagPath cameraPath;

            try {
                // Try with a Maya host first
                cameraPath = M3dView.active3dView.Camera;
            } catch {
                // We are in standalone mode (WPF application)
                MSelectionList list = new MSelectionList();
                list.add("persp");
                cameraPath = new MDagPath();
                list.getDagPath(0, cameraPath);
            }

            MFnCamera fnCamera = new MFnCamera(cameraPath);
            MPoint    eyePoint = fnCamera.eyePoint(MSpace.Space.kWorld);
            MPoint    centerOfInterestPoint = fnCamera.centerOfInterestPoint(MSpace.Space.kWorld);
            MVector   direction             = centerOfInterestPoint.minus(eyePoint);
            MVector   upDirection           = fnCamera.upDirection(MSpace.Space.kWorld);

            camera.Position      = new Point3D(eyePoint.x, eyePoint.y, eyePoint.z);
            camera.LookDirection = new Vector3D(direction.x, direction.y, direction.z);
            MAngle fieldOfView = new MAngle(fnCamera.verticalFieldOfView);              //verticalFieldOfView / horizontalFieldOfView

            camera.FieldOfView       = fieldOfView.asDegrees;
            camera.UpDirection       = new Vector3D(upDirection.x, upDirection.y, upDirection.z);
            camera.NearPlaneDistance = fnCamera.nearClippingPlane;
            camera.FarPlaneDistance  = fnCamera.farClippingPlane;
            camera.Transform         = new Transform3DGroup();
            (camera.Transform as Transform3DGroup).Children.Add(new TranslateTransform3D(new Vector3D()));
        }
示例#2
0
        public void updateDragInformation()
        {
            // Find the mouse point in local space
            MPoint  localMousePoint     = new MPoint();
            MVector localMouseDirection = new MVector();

            try
            {
                mouseRay(localMousePoint, localMouseDirection);
            }
            catch (System.Exception)
            {
                return;
            }

            // Find the intersection of the mouse point with the
            // manip plane
            MPoint mouseIntersectionWithManipPlane = new MPoint();

            if (!plane.intersect(localMousePoint, localMouseDirection, ref mouseIntersectionWithManipPlane))
            {
                return;
            }

            mousePointGlName.assign(mouseIntersectionWithManipPlane);

            uint active = 0;

            try
            {
                glActiveName(ref active);
            }
            catch (System.Exception)
            {
                return;
            }

            if (active == lineName && active != 0)
            {
                lineMath line = new lineMath();
                // Find a vector on the plane
                MPoint  a   = lineGeometry.topPoint();
                MPoint  b   = lineGeometry.bottomPoint();
                MVector vab = a.minus(b);
                // Define line with a point and a vector on the plane
                line.setLine(a, vab);
                MPoint cpt = new MPoint();
                // Find the closest point so that we can get the
                // delta change of the mouse in local space
                if (line.closestPoint(mousePointGlName, ref cpt))
                {
                    mousePointGlName.x -= cpt.x;
                    mousePointGlName.y -= cpt.y;
                    mousePointGlName.z -= cpt.z;
                }
            }
        }
示例#3
0
        //
        // Description
        //
        //    Create circles of vertices starting with
        //    the top pole ending with the bottom pole
        //
        public void buildSphere(double rad,
                                int div,
                                MPointArray vertices,
                                MIntArray counts,
                                MIntArray connects,
                                MVectorArray normals,
                                apiMeshGeomUV uvs)
        {
            double u       = -Math.PI / 2.0;
            double v       = -Math.PI;
            double u_delta = Math.PI / ((double)div);
            double v_delta = 2 * Math.PI / ((double)div);

            MPoint topPole = new MPoint(0.0, rad, 0.0);
            MPoint botPole = new MPoint(0.0, -rad, 0.0);

            // Build the vertex and normal table
            //
            vertices.append(botPole);
            normals.append(botPole.minus(MPoint.origin));
            int i;

            for (i = 0; i < (div - 1); i++)
            {
                u += u_delta;
                v  = -Math.PI;

                for (int j = 0; j < div; j++)
                {
                    double x   = rad * Math.Cos(u) * Math.Cos(v);
                    double y   = rad * Math.Sin(u);
                    double z   = rad * Math.Cos(u) * Math.Sin(v);
                    MPoint pnt = new MPoint(x, y, z);
                    vertices.append(pnt);
                    normals.append(pnt.minus(MPoint.origin));
                    v += v_delta;
                }
            }
            vertices.append(topPole);
            normals.append(topPole.minus(MPoint.origin));

            // Create the connectivity lists
            //
            int vid  = 1;
            int numV = 0;

            for (i = 0; i < div; i++)
            {
                for (int j = 0; j < div; j++)
                {
                    if (i == 0)
                    {
                        counts.append(3);
                        connects.append(0);
                        connects.append(j + vid);
                        connects.append((j == (div - 1)) ? vid : j + vid + 1);
                    }
                    else if (i == (div - 1))
                    {
                        counts.append(3);
                        connects.append(j + vid + 1 - div);
                        connects.append(vid + 1);
                        connects.append(j == (div - 1) ? vid + 1 - div : j + vid + 2 - div);
                    }
                    else
                    {
                        counts.append(4);
                        connects.append(j + vid + 1 - div);
                        connects.append(j + vid + 1);
                        connects.append(j == (div - 1) ? vid + 1 : j + vid + 2);
                        connects.append(j == (div - 1) ? vid + 1 - div : j + vid + 2 - div);
                    }
                    numV++;
                }
                vid = numV;
            }

            // TODO: Define UVs for sphere ...
            //
        }
示例#4
0
		//
		// Description
		//
		//    Create circles of vertices starting with 
		//    the top pole ending with the bottom pole
		//
		public void buildSphere(double rad,
								int div,
								MPointArray vertices,
								MIntArray counts,
								MIntArray connects,
								MVectorArray normals,
								apiMeshGeomUV uvs)
		{
			double u = -Math.PI / 2.0;
			double v = -Math.PI;
			double u_delta = Math.PI / ((double)div); 
			double v_delta = 2 * Math.PI / ((double)div); 

			MPoint topPole = new MPoint( 0.0, rad, 0.0 );
			MPoint botPole = new MPoint( 0.0, -rad, 0.0 );

			// Build the vertex and normal table
			//
			vertices.append( botPole );
			normals.append( botPole.minus(MPoint.origin) );
			int i;
			for ( i=0; i<(div-1); i++ )
			{
				u += u_delta;
				v = -Math.PI;

				for ( int j=0; j<div; j++ )
				{
					double x = rad * Math.Cos(u) * Math.Cos(v);
					double y = rad * Math.Sin(u);
					double z = rad * Math.Cos(u) * Math.Sin(v) ;
					MPoint pnt = new MPoint( x, y, z );
					vertices.append( pnt );
					normals.append( pnt.minus(MPoint.origin) );
					v += v_delta;
				}
			}
			vertices.append( topPole );
			normals.append( topPole.minus(MPoint.origin) );

			// Create the connectivity lists
			//
			int vid = 1;
			int numV = 0;
			for ( i=0; i<div; i++ )
			{
				for ( int j=0; j<div; j++ )
				{
					if ( i==0 )
					{
						counts.append( 3 );
						connects.append( 0 );
						connects.append( j+vid );
						connects.append( (j==(div-1)) ? vid : j+vid+1 );
					}
					else if ( i==(div-1) )
					{
						counts.append( 3 );
						connects.append( j+vid+1-div );
						connects.append( vid+1 );
						connects.append( j==(div-1) ? vid+1-div : j+vid+2-div );
					}
					else
					{
						counts.append( 4 );
						connects.append( j + vid+1-div );
						connects.append( j + vid+1 );
						connects.append( j == (div-1) ? vid+1 : j+vid+2 );
						connects.append( j == (div-1) ? vid+1-div : j+vid+2-div );
					}
					numV++;
				}
				vid = numV;
			}

			// TODO: Define UVs for sphere ...
			//
		}
示例#5
0
        private void doSimpleSolver()
        //
        // Solve single joint in the x-y plane
        //
        // - first it calculates the angle between the handle and the end-effector.
        // - then it determines which way to rotate the joint.
        //
        {
            // Get the handle and create a function set for it
            //
            MIkHandleGroup handle_group = handleGroup;

            if (null == handle_group)
            {
                throw new System.InvalidOperationException("invalid handle group.");
            }

            MObject     handle     = handle_group.handle(0);
            MDagPath    handlePath = MDagPath.getAPathTo(handle);
            MFnIkHandle fnHandle   = new MFnIkHandle(handlePath);

            // Get the position of the end_effector
            //
            MDagPath end_effector = new MDagPath();

            fnHandle.getEffector(end_effector);
            MFnTransform tran = new MFnTransform(end_effector);
            MPoint       effector_position = tran.rotatePivot(MSpace.Space.kWorld);

            // Get the position of the handle
            //
            MPoint handle_position = fnHandle.rotatePivot(MSpace.Space.kWorld);

            // Get the start joint position
            //
            MDagPath start_joint = new MDagPath();

            fnHandle.getStartJoint(start_joint);
            MFnTransform start_transform = new MFnTransform(start_joint);
            MPoint       start_position  = start_transform.rotatePivot(MSpace.Space.kWorld);

            // Calculate the rotation angle
            //
            MVector v1    = start_position.minus(effector_position);
            MVector v2    = start_position.minus(handle_position);
            double  angle = v1.angle(v2);

            // -------- Figure out which way to rotate --------
            //
            //  define two vectors U and V as follows
            //  U   =   EndEffector(E) - StartJoint(S)
            //  N   =   Normal to U passing through EndEffector
            //
            //  Clip handle_position to half-plane U to determine the region it
            //  lies in. Use the region to determine  the rotation direction.
            //
            //             U
            //             ^              Region      Rotation
            //             |  B
            //            (E)---N            A          C-C-W
            //         A   |                 B           C-W
            //             |  B
            //             |
            //            (S)
            //
            double rot = 0.0;                   // Rotation about Z-axis

            // U and N define a half-plane to clip the handle against
            //
            MVector U = effector_position.minus(start_position);

            U.normalize();

            // Get a normal to U
            //
            MVector zAxis = new MVector(0.0, 0.0, 1.0);
            MVector N     = U.crossProduct(zAxis);              // Cross product

            N.normalize();

            // P is the handle position vector
            //
            MVector P = handle_position.minus(effector_position);

            // Determine the rotation direction
            //
            double PdotN = P[0] * N[0] + P[1] * N[1];

            if (PdotN < 0)
            {
                // counter-clockwise
                rot = angle;
            }
            else
            {
                // clockwise
                rot = -1.0 * angle;
            }

            // get and set the Joint Angles
            //
            MDoubleArray jointAngles = new MDoubleArray();

            getJointAngles(jointAngles);
            jointAngles.set(jointAngles[0] + rot, 0);
            setJointAngles(jointAngles);
        }