Challenge: Object under cursor

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

REPLY TO THREAD
 
Thread Tools Search this Thread Display Modes
  04 April 2014
if you change point_on_obj function in MOUSTRAK.CPP in the mxsagni project (\maxsdk\samples\maxscript\mxsagni) to the following it works with editable poly just fine.

 BOOL
 TrackMouseCallBack::point_on_obj(ViewExp *vpt, IPoint2 m, Point3& pt, Point3 &norm)
 {
 	// computes the normal ray at the point of intersection
 	Ray ray, world_ray;
 	float at, best_dist = 0.0f;
 	TimeValue t = MAXScript_time();	
 	Object *obj = NULL;
 	Matrix3 obtm, iobtm;
 	Point3 testNorm;
 
 	BOOL found_hit = FALSE;
 	
 	vl->face_num_val = vl->face_bary = &undefined;
 	hit_node = NULL;
 
 	// Calculate a ray from the mouse point
 	vpt->MapScreenToWorldRay(float(m.x), float(m.y), world_ray);
 
 	for( int i=(nodeTab.Count()-1); i>=0; i-- ) {
 		// Get the object from the node
 		INode* node = nodeTab[i];
 		ObjectState os = node->EvalWorldState(t);
 		obj = os.obj;	
 
 		// Back transform the ray into object space.
 		obtm	= node->GetObjectTM(t);
 		iobtm	= Inverse(obtm);
 		ray.p   = iobtm * world_ray.p;
 		ray.dir = VectorTransform(iobtm, world_ray.dir);
 		
 		// See if we hit the object
 		if (obj->IsSubClassOf(triObjectClassID))
 		{
 			TriObject*  tobj = (TriObject*)obj;
 			DWORD		fi;
 			Point3		bary;
 			if (tobj->mesh.IntersectRay(ray, at, testNorm, fi, bary)  &&
 			   ((!found_hit) || (at<=best_dist)) )
 			{
 				// Calculate the hit point and transform everything back into world space.
 				// record the face index and bary coord
 				best_dist = at;
 				pt = ray.p + ray.dir * at;
 				pt = pt * obtm;
 				norm = Normalize(VectorTransform(obtm, testNorm));
 				vl->face_num_val = Integer::intern(fi + 1);
 				vl->face_bary = new Point3Value(bary);
 				hit_node = node;
 				found_hit = TRUE;
 			}
 		}
 		else if (obj->IsSubClassOf(polyObjectClassID))
 		{
 			PolyObject* pobj = (PolyObject*)obj; 
 			int		fi;
 			//Point3		bary;
 			Tab<float> bary;
 			if (pobj->GetMesh().IntersectRay(ray, at, testNorm, fi, bary)  && ((!found_hit) || (at<=best_dist)) )
 			{
 				// Calculate the hit point and transform everything back into world space.
 				// record the face index and bary coord
 				best_dist = at;
 				pt = ray.p + ray.dir * at;
 				pt = pt * obtm;
 				norm = Normalize(VectorTransform(obtm, testNorm));
 				vl->face_num_val = Integer::intern(fi + 1);
 				//vl->face_bary = new Point3Value(bary);
 				hit_node = node;
 				found_hit = TRUE;
 			}
 		}
 		else if (obj->IntersectRay(t, ray, at, testNorm)  &&
 				((!found_hit) || (at<=best_dist)) )
 		{
 			// Calculate the hit point and transform everything back into world space.
 			best_dist = at;
 			pt = ray.p + ray.dir * at;
 			pt = pt * obtm;
 			norm = Normalize(VectorTransform(obtm, testNorm));
 			hit_node = node;
 			found_hit = TRUE;
 		}
 	}
 	if( found_hit ) return TRUE;
 
 	// Failed to find a hit on any node, look at the Normal Align Vector for the first node
 	if ((obj!=NULL) && obj->NormalAlignVector(t, pt, testNorm)) // See if a default NA vector is provided
 	{
 		pt   = pt * obtm;
 		norm = Normalize(VectorTransform(obtm, testNorm));
 		return TRUE;
 	}
 	else
 		return FALSE;
 }
 


obviously you then have to recompile the project and replace MXSAgni.dlx in stdplugs with the newer version.

Last edited by Klvnk : 04 April 2014 at 09:34 AM.
 
  04 April 2014
Thanks a lot Klvnk! I'll definitely look into that.
I'm using some hack-y workarounds now for performance, but will work at proper solutions.
Here's something I'm using the code for:

__________________
http://www.per128.com - freelance 3d artist
 
  04 April 2014
very cool !
 
reply share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 01:24 PM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.