On 30/08/2014 at 11:49, xxxxxxxx wrote:
User Information:
Cinema 4D Version: 13
Platform: Windows ;
Language(s) : C++ ;
---------
Hi,
I'm trying to build a matrix from a polygon. And these are the requirements:
- The Y axis needs to point out outwards (away from the polygon)
- The X&Z axis need to be squared with the polygon
- This has to work if the object the polygon is on is not at world center 0,0,0
- This has to work if the object the polygon is on is rotated
- This has to work if the object the polygon is on is either a parent or a child object
As an example. C4D already does this when we go into polygon mode and select a polygon.
But it's Z Axis is pointing outwards instead of the Y axis.
I need to know how they wrote that code so that I can write one that points in the Y direction.
I am 99% there. But I just can't get the axis to meet all of the above conditions.
I've manged to get the Y axis to point outwards. But the other two axis I can't figure out how to make squared to the polygon. They keep fighting me.
And it doesn't work if the object is rotated.
My code:
Bool SimplePlugin::Execute(BaseDocument *doc)
{
PolygonObject *obj = (PolygonObject* ) doc->GetActiveObject();
if(!obj)return FALSE;
Vector *points = obj->GetPointW();
Matrix objMtx = obj->GetMg();
//The first polygon in the object
CPolygon poly = obj->GetPolygonW()[0];
//The global positions of each point in the polygon
Vector a = obj->GetPointW()[poly.a] * objMtx;
Vector b = obj->GetPointW()[poly.b] * objMtx;
Vector c = obj->GetPointW()[poly.c] * objMtx;
Vector d = obj->GetPointW()[poly.d] * objMtx;
Vector midpoint;
if(c == d) midpoint = (a+b+c) / 3; //The global position to the "midpoint" variable
else midpoint = (a+b+c+d) / 4; //The global position to the "midpoint" variable
//The normals direction the polygon is facing
Vector faceNormal = CalcFaceNormal(points, poly);
//Create a matrix for the polygon
Matrix polyMtx;
polyMtx.off = midpoint * objMtx;
polyMtx.v1 = !((a - midpoint)%faceNormal);
polyMtx.v2 = !(faceNormal); //<---These matrix values are wrong!!
polyMtx.v3 = !(polyMtx.v1 % faceNormal); //How do I make the three Axis squared(alined) with the polygon?
//With the Y axis pointing outwards from the polygon?
BaseObject *null = BaseObject::Alloc(Onull);
null->SetMg(polyMtx);
doc->InsertObject(null, NULL, NULL);
null->Message(MSG_UPDATE);
EventAdd();
return TRUE;
}
Can anyone tell me how to tame these matrix axis handles please?
-ScottA