View Full Version : Maya API "point to Normal"

03 March 2004, 05:21 AM
:) Hello iam working on a shader in maya, iam using the raytraceFirstGeometeryIntersect to trace a ray to find the intersection of a point from the ray to the object. Now after that I need to be able to get the normal at the point. But there realy is no function to this Correct in the API, at least i don't if there is. But i have tried many methods and still with poor results. So if there is any information to help out or any locations on the net that could help out that would be Great.


03 March 2004, 11:02 AM

However, if you've tried this and found that it doesn't work (maya's closest point functions are notoriously buggy), then it's really not that hard to roll your own. You'll prolly want to set up a kd or octree for your object if you're doing multiple intersections to save time, but you should be able to get the main function working pretty quickly.

Things to watch out for with MfnMesh: make sure you intialise your Fn properly with a full DAG path, or make sure to connect the worldMatrix or world space geometry to whatever node you're using, and just work in world space.

03 March 2004, 03:57 PM
:) Hey playmesumch00ns at one point i did try to use the MfnMesh class's and MItMeshPolygon to have the information of my mesh. But it seems that i couldn't get it to intialize properly so when i would compile it and try to run it maya would crash, And when it wasn't crashing i wasn't getting any information back from the mesh. So iam sure it was just something i was doing wrong with it. Have you ever gotten the MfnMesh to run correctly for you ?

03 March 2004, 05:17 PM
Yeah, generally works fine. There are some issues with the closestPointOnMesh function but I've never had it crash on me. All you need to do is

MFnMesh meshFn( dagPathToMeshShape )

and you're away

03 March 2004, 11:14 PM
Hmmmmmmm, Thats usually how i set it, I'll probably go and give it another just and just try different things out. It would be much easier if i could just get the information from the Object.

03 March 2004, 02:13 PM
This is just getting the information from the object. It has to work this way to stop you accidentally screwing up Maya's internal database.

Can you post a code snippet? It might be easier to see what's going on that way.

03 March 2004, 05:38 PM
Ok here is the code snippet for the finding a Point,

for ( int i = 0; i < samples; i+=1)
// rayTrace appends
MFloatVectorArray ptArray(1, P);
MFloatVectorArray dirArray(1, -L);
MFloatVectorArray resultIntersection(1, MFloatVector::zero);
MIntArray resultIntersected(1);
// rayTrace intersection to find Point
status = MRenderUtil::raytraceFirstGeometryIntersections(
ptArray, // origin
dirArray, // direction
objId, // object id
samplerPtr, // sampler info
resultIntersection, // result
resultIntersected );

if ((resultIntersected[0] == 1))
MFloatVector PointNew = resultIntersection[0];
// here i also need to find the normal at PointNew
// do rest of shader calculations in here

03 March 2004, 05:49 PM
Here is how i used the MfnMesh before

// defining the attribute info so i can plug a path into my shader
// to retrieve the surface mesh info

static MObject aInMesh;

MObject testCode::aInMesh;

aInMesh = tAttr.create("inMesh", "im", MFnData::kMesh);


attributeAffects(aInMesh, aOutColor);

// from This point on is where the computations start to
// take place in the shader.

// get the InMesh attribute from previous blocks
MObject inMesh = inMeshDataHandle.asMesh();

// here is how i would use the MfnMesh type before
MFnMesh polyMesh(inMesh); <---- mesh Input to access Info

// then from here i should be able to compute and
//access information from the mesh that is inputed
// into my shader

polyMesh.numVertices() <--------accessing number vertices
MFloatPointArray Ppoints;
polyMesh.getPoints(Ppoints,MSpace::kWorld); <---getting points

03 March 2004, 07:36 PM
hmmmm. Not sure why that might crash. what attribute of your mesh shape are you attaching to the inMesh attribute on your shader?

You'll need to attach the .worldMesh attribute or getting points in world space will fail, and any attempt to access those points will cause an error.

Also, you didn't say if you've left out error-checking for brevity here. If you've not got any error-checking code you really need to add some. getting the status of everything you do in Maya makes debugging a million times easier.

03 March 2004, 09:03 PM
Well iam gonna go back and give it a try again with the method of using the MfnMesh, Iam sure it works maybe before i was just doing something wrong thats all. But before i used the world Mesh to input it in. Usually it would just show up with nothing but again iam sure it was something that i was not doing right. As far as error checking the only that i know of is the one that is Explained in the Maya 5.0 shader source codes.

03 March 2004, 11:27 AM
Yep I'd go back and try again. Often just doing a re-write will fix a problem you couldn't see before. Hardly a satisfactory debugging method, but whatever works ;)

By error checking I mean getting the status return code whenever you instance any Maya object that you're planning to use.


MStatus stat;
MObject inMesh = inMeshDataHandle.asMesh(&stat);

if (!stat)
stat.perror( "Could not get inMesh" );
return stat;

MFnMesh polyMesh(inMesh, &stat);
if ( !stat )
stat.perror( "Could not get polyMesh" );
return stat;

// Obvioulsy that's a lot of typing, but you can make it easier on yourself with a macro like

#define PERR( errmsg ) if (!stat) {stat.perr( errmsg); return stat;}

int numv = polyMesh.numVertices(&stat)
PERR( "Could not get numVertices" )

03 March 2004, 05:45 PM
:) Yea thats the only kind of error checking, that i know of. I usually don't use it cause mostly iam dealing with only shader networks. But it seems that if iam going to deal with more than just shading, and start to access actual geometery info i better start using IT. :thumbsup:

CGTalk Moderation
01 January 2006, 03:00 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.