Newer
Older
Gundalf
committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
static Class_ID SCUBA_CLASS_ID(0x6d3d77ac, 0x79c939a9);
enum
{
CAPSULE_RADIUS = 0,
CAPSULE_HEIGHT = 1,
};
/*
To mimic the "Reset Transform" and "Reset Scale" behavior, the following code snippet should help:
Interface *ip = theResetScale.ip;
TimeValue t = ip->GetTime();
Control *tmControl = node->GetTMController();
BOOL lookAt = tmControl->GetRollController() ? TRUE : FALSE;
Matrix3 ntm = node->GetNodeTM(t);
Matrix3 ptm = node->GetParentTM(t);
Matrix3 rtm = ntm * Inverse(ptm);
Matrix3 otm(1);
Quat rot;
// Grab the trans, and then set it to 0
Point3 trans = rtm.GetTrans();
rtm.NoTrans();
// We're only doing scale - save out the
// rotation so we can put it back
AffineParts parts;
decomp_affine(rtm, &parts);
rot = parts.q;
// Build the offset tm
otm.PreTranslate(node->GetObjOffsetPos());
if (node->GetObjOffsetRot()!=IdentQuat()) {
PreRotateMatrix(otm,node->GetObjOffsetRot());
}
Point3 tS(1,1,1);
if ( node->GetObjOffsetScale().s != tS ) {
ApplyScaling(otm,node->GetObjOffsetScale());
}
// Apply the relative tm to the offset
otm = otm * rtm;
decomp_affine(otm, &parts);
node->SetObjOffsetPos(parts.t);
node->SetObjOffsetScale(ScaleValue(parts.k*parts.f,parts.u));
// Now set the transform controller with a matrix
// that has no rotation or scale
rtm.IdentityMatrix();
rtm.SetTrans(trans);
if (!lookAt) {
PreRotateMatrix(rtm,rot);
}
// But first, want to keep children stationary.
Matrix3 ttm = rtm*ptm;
for (int i=0; iNumberOfChildren(); i++) {
Control *tmc = node->GetChildNode(i)->GetTMController();
Matrix3 oldtm = node->GetChildNode(i)->GetNodeTM(t);
SetXFormPacket pk(oldtm,ttm);
tmc->SetValue(t,&pk);
}
SetXFormPacket pckt(rtm);
tmControl->SetValue(t,&pckt);
To mimic the "Align to world" behavior, the following code snippet should help:
AffineParts parts;
TimeValue currtime = m_pInterface->GetTime();
Matrix3 m = pNode->GetNodeTM(currtime);
decomp_affine(m, &parts);
if (rotobj) {
// if "affect obj only" we move it simply thus:
pNode->SetObjOffsetRot(Inverse(parts.q));
} else {
// otherwise, "affect pivot only" we would do:
IdentityTM ident;
Matrix3 wax = ident;
wax.SetTrans(m.GetTrans()); // world aligned axis, centered at pivot point
pNode->Rotate(currtime, wax, Inverse(parts.q),TRUE,FALSE, PIV_PIVOT_ONLY);
}
m_pInterface->RedrawViews(m_pInterface->GetTime(),REDRAW_NORMAL,NULL);
*/
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
int Exporter::addVertex(vector<Vector3> &verts, vector<Vector3> &vnorms, const Point3 &pt, const Point3 &norm)
{
for (int i=0; i<verts.size(); i++)
{
if (equal(verts[i], pt, mWeldThresh) &&
equal(vnorms[i], norm, 0))
return i;
}
verts.push_back(Vector3(pt.x, pt.y, pt.z));
vnorms.push_back(Vector3(norm.x, norm.y, norm.z));
return verts.size()-1;
}
void Exporter::addFace(Triangles &tris, vector<Vector3> &verts, vector<Vector3> &vnorms,
int face, const int vi[3], Mesh *mesh)
{
Triangle tri;
for (int i=0; i<3; i++)
{
tri[i] = addVertex(verts, vnorms,
mesh->verts[ mesh->faces[ face ].v[ vi[i] ] ],
getVertexNormal(mesh, face, mesh->getRVertPtr(mesh->faces[ face ].v[ vi[i] ])));
}
tris.push_back(tri);
}
Gundalf
committed
/*
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
bool Exporter::makeCollisionHierarchy(NiNodeRef &parent, INode *node, TimeValue t)
{
Matrix3 tm = node->GetObjTMAfterWSM(t);
// Order of the vertices. Get 'em counter clockwise if the objects is
// negatively scaled.
int vi[3];
if (TMNegParity(tm))
{
vi[0] = 2;
vi[1] = 1;
vi[2] = 0;
} else
{
vi[0] = 0;
vi[1] = 1;
vi[2] = 2;
}
ObjectState os = node->EvalWorldState(t);
if (!os.obj || os.obj->SuperClassID()!=GEOMOBJECT_CLASS_ID)
return Error;
Object *obj = os.obj;
if (!obj->CanConvertToType(Class_ID(TRIOBJ_CLASS_ID, 0)))
return Error;
TriObject *tri = (TriObject *)obj->ConvertToType(t, Class_ID(TRIOBJ_CLASS_ID, 0));
if (!tri)
return false;
Mesh *mesh = &tri->GetMesh();
mesh->buildNormals();
// setup shape data
vector<Vector3> verts;
vector<Vector3> vnorms;
Triangles tris;
for (int i=0; i<mesh->getNumFaces(); i++)
addFace(tris, verts, vnorms, i, vi, mesh);
TriStrips strips;
strippify(strips, verts, vnorms, tris);
NiTriStripsDataRef data = makeTriStripsData(strips);
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
data->SetVertices(verts);
data->SetNormals(vnorms);
// setup shape
bhkNiTriStripsShapeRef shape = DynamicCast<bhkNiTriStripsShape>(CreateBlock("bhkNiTriStripsShape"));
shape->SetNumStripsData(1);
shape->SetStripsData(0, data);
shape->SetMaterial(mtl);
array<float, 2> unknownFloats1;
uint i1 = 0x3DCCCCCD;
uint i2 = 0x004ABE60;
unknownFloats1[0] = *((float*)&i1);
unknownFloats1[1] = *((float*)&i2);
shape->SetUnknownFloats1(unknownFloats1);
array<float, 3> unknownFloats2;
unknownFloats2[0] = 1;
unknownFloats2[1] = 1;
unknownFloats2[2] = 1;
shape->SetUnknownFloats2(unknownFloats2);
array<uint, 5> unknownInts1;
unknownInts1[4] = 1;
shape->SetUnknownInts1(unknownInts1);
vector<uint> unknownInts3;
unknownInts3.resize(1);
shape->SetUnknownInts3(unknownInts3);
// setup collision object
bhkCollisionObjectRef co = DynamicCast<bhkCollisionObject>(CreateBlock("bhkCollisionObject"));
// setup body
bhkRigidBodyTRef body = DynamicCast<bhkRigidBodyT>(CreateBlock("bhkRigidBodyT"));
Vector3 trans;
QuaternionXYZW q;
nodeTransform(q, trans, node, t, false);
body->SetRotation(q);
body->SetTranslation(Vector3(trans.x/7, trans.y/7, trans.z/7));
body->SetLayer(lyr);
body->SetLayerCopy(lyr);
body->SetMotionSystem(msys);
body->SetQualityType(qtype);
body->SetMass(mass);
body->SetLinearDamping(lindamp);
body->SetAngularDamping(angdamp);
body->SetFriction(frict);
body->SetRestitution(resti);
body->SetMaxLinearVelocity(maxlinvel);
body->SetMaxAngularVelocity(maxangvel);
body->SetPenetrationDepth(pendepth);
body->SetCenter(center);
// link
parent->SetCollisionObject(DynamicCast<NiCollisionObject>(co));
co->SetParent(parent);
co->SetBody(DynamicCast<NiObject>(body));
body->SetShape(DynamicCast<bhkShape>(shape));
if (obj != tri)
tri->DeleteMe();
return true;
}
Gundalf
committed
*/
Exporter::Result Exporter::exportCollision(NiNodeRef &parent, INode *node)
{
// marked as collision?
Gundalf
committed
NiNodeRef newParent;
Gundalf
committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
{
/* NiNodeRef n = DynamicCast<NiNode>(CreateBlock("NiNode"));
parent->AddChild(DynamicCast<NiAVObject>(n));
Matrix33 rot;
Vector3 trans;
TimeValue t = 0;
nodeTransform(rot, trans, node, t);
n->SetLocalRotation(rot);
n->SetLocalTranslation(trans);
string name = (char*)node->GetName();
n->SetName(name);
/* Vector3 trans;
QuaternionXYZW q;
TimeValue t = 0;
nodeTransform(q, trans, node, t, false);
body->SetRotation(q);
body->SetTranslation(Vector3(trans.x/7, trans.y/7, trans.z/7));
*/
newParent = makeNode(parent, node);
bhkSphereRepShapeRef shape = makeCollisionShape(node);
bhkRigidBodyRef body = makeCollisionBody(node);
body->SetShape(DynamicCast<bhkShape>(shape));
bhkCollisionObjectRef co = DynamicCast<bhkCollisionObject>(CreateBlock("bhkCollisionObject"));
co->SetBody(DynamicCast<NiObject>(body));
co->SetParent(newParent);
// link
newParent->SetCollisionObject(DynamicCast<NiCollisionObject>(co));
} else
Gundalf
committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
{
newParent = makeNode(parent, node);
} else
newParent = parent;
for (int i=0; i<node->NumberOfChildren(); i++)
{
Result result = exportCollision(newParent, node->GetChildNode(i));
if (result!=Ok && result!=Skip)
return result;
}
return Ok;
}
bhkRigidBodyRef Exporter::makeCollisionBody(INode *node)
{
// get data from node
int lyr, mtl, msys, qtype;
float mass, lindamp, angdamp, frict, maxlinvel, maxangvel, resti, pendepth;
Vector3 center;
npGetProp(node, NP_HVK_LAYER, lyr, NP_DEFAULT_HVK_LAYER);
npGetProp(node, NP_HVK_MATERIAL, mtl, NP_DEFAULT_HVK_MATERIAL);
npGetProp(node, NP_HVK_MOTION_SYSTEM, msys, NP_DEFAULT_HVK_MOTION_SYSTEM);
npGetProp(node, NP_HVK_QUALITY_TYPE, qtype, NP_DEFAULT_HVK_QUALITY_TYPE);
npGetProp(node, NP_HVK_MASS, mass, NP_DEFAULT_HVK_MASS);
npGetProp(node, NP_HVK_LINEAR_DAMPING, lindamp, NP_DEFAULT_HVK_LINEAR_DAMPING);
npGetProp(node, NP_HVK_ANGULAR_DAMPING, angdamp, NP_DEFAULT_HVK_ANGULAR_DAMPING);
npGetProp(node, NP_HVK_FRICTION, frict, NP_DEFAULT_HVK_FRICTION);
npGetProp(node, NP_HVK_RESTITUTION, resti, NP_DEFAULT_HVK_RESTITUTION);
npGetProp(node, NP_HVK_MAX_LINEAR_VELOCITY, maxlinvel, NP_DEFAULT_HVK_MAX_LINEAR_VELOCITY);
npGetProp(node, NP_HVK_MAX_ANGULAR_VELOCITY, maxangvel, NP_DEFAULT_HVK_MAX_ANGULAR_VELOCITY);
npGetProp(node, NP_HVK_PENETRATION_DEPTH, pendepth, NP_DEFAULT_HVK_PENETRATION_DEPTH);
npGetProp(node, NP_HVK_CENTER, center);
// setup body
bhkRigidBodyRef body = DynamicCast<bhkRigidBody>(CreateBlock("bhkRigidBody"));
body->SetLayer(lyr);
body->SetLayerCopy(lyr);
body->SetMotionSystem(msys);
body->SetQualityType(qtype);
body->SetMass(mass);
body->SetLinearDamping(lindamp);
body->SetAngularDamping(angdamp);
body->SetFriction(frict);
body->SetRestitution(resti);
body->SetMaxLinearVelocity(maxlinvel);
body->SetMaxAngularVelocity(maxangvel);
body->SetPenetrationDepth(pendepth);
body->SetCenter(center);
return body;
}
bhkSphereRepShapeRef Exporter::makeCollisionShape(INode *node)
{
bhkSphereRepShapeRef shape;
TimeValue t = 0;
ObjectState os = node->EvalWorldState(t);
if (os.obj->ClassID() == SCUBA_CLASS_ID)
shape = makeCapsuleShape(os.obj);
else
if (os.obj->ClassID() == Class_ID(BOXOBJ_CLASS_ID, 0))
shape = makeBoxShape(os.obj);
else
if (os.obj->ClassID() == Class_ID(SPHERE_CLASS_ID, 0))
shape = makeSphereShape(os.obj);
else
if (os.obj->SuperClassID() == GEOMOBJECT_CLASS_ID)
shape = makeTriStripsShape(node);
if (shape)
{
int mtl;
npGetProp(node, NP_HVK_MATERIAL, mtl, NP_DEFAULT_HVK_MATERIAL);
shape->SetMaterial(mtl);
}
return shape;
}
bhkSphereRepShapeRef Exporter::makeBoxShape(Object *obj)
{
float length = 0;
float height = 0;
float width = 0;
IParamArray *params = obj->GetParamBlock();
params->GetValue(obj->GetParamBlockIndex(BOXOBJ_LENGTH), 0, length, FOREVER);
params->GetValue(obj->GetParamBlockIndex(BOXOBJ_HEIGHT), 0, height, FOREVER);
params->GetValue(obj->GetParamBlockIndex(BOXOBJ_WIDTH), 0, width, FOREVER);
bhkBoxShapeRef box = DynamicCast<bhkBoxShape>(CreateBlock("bhkBoxShape"));
box->SetDimensions(Vector3(width, height, length));
return bhkSphereRepShapeRef(DynamicCast<bhkSphereRepShape>(box));
}
bhkSphereRepShapeRef Exporter::makeSphereShape(Object *obj)
{
float radius = 0;
IParamArray *params = obj->GetParamBlock();
params->GetValue(obj->GetParamBlockIndex(SPHERE_RADIUS), 0, radius, FOREVER);
bhkSphereShapeRef sphere = DynamicCast<bhkSphereShape>(CreateBlock("bhkSphereShape"));
sphere->SetRadius(radius);
return bhkSphereRepShapeRef(DynamicCast<bhkSphereRepShape>(sphere));
}
bhkSphereRepShapeRef Exporter::makeCapsuleShape(Object *obj)
{
float radius = 0;
float height = 0;
IParamArray *params = obj->GetParamBlock();
params->GetValue(obj->GetParamBlockIndex(CAPSULE_RADIUS), 0, radius, FOREVER);
params->GetValue(obj->GetParamBlockIndex(CAPSULE_HEIGHT), 0, height, FOREVER);
bhkCapsuleShapeRef capsule = DynamicCast<bhkCapsuleShape>(CreateBlock("bhkCapsuleShape"));
return bhkSphereRepShapeRef(DynamicCast<bhkSphereRepShape>(capsule));
}
bhkSphereRepShapeRef Exporter::makeTriStripsShape(INode *node)
{
TimeValue t = 0;
Matrix3 tm = node->GetObjTMAfterWSM(t);
// Order of the vertices. Get 'em counter clockwise if the objects is
// negatively scaled.
int vi[3];
if (TMNegParity(tm))
{
vi[0] = 2;
vi[1] = 1;
vi[2] = 0;
} else
{
vi[0] = 0;
vi[1] = 1;
vi[2] = 2;
}
ObjectState os = node->EvalWorldState(t);
TriObject *tri = (TriObject *)os.obj->ConvertToType(t, Class_ID(TRIOBJ_CLASS_ID, 0));
if (!tri)
return false;
Mesh *mesh = &tri->GetMesh();
mesh->buildNormals();
// setup shape data
vector<Vector3> verts;
vector<Vector3> vnorms;
Triangles tris;
for (int i=0; i<mesh->getNumFaces(); i++)
addFace(tris, verts, vnorms, i, vi, mesh);
TriStrips strips;
strippify(strips, verts, vnorms, tris);
NiTriStripsDataRef data = makeTriStripsData(strips);
data->SetVertices(verts);
data->SetNormals(vnorms);
// setup shape
bhkNiTriStripsShapeRef shape = DynamicCast<bhkNiTriStripsShape>(CreateBlock("bhkNiTriStripsShape"));
shape->SetNumStripsData(1);
shape->SetStripsData(0, data);
/*
array<float, 2> unknownFloats1;
uint i1 = 0x3DCCCCCD;
uint i2 = 0x004ABE60;
unknownFloats1[0] = *((float*)&i1);
unknownFloats1[1] = *((float*)&i2);
shape->SetUnknownFloats1(unknownFloats1);
array<float, 3> unknownFloats2;
unknownFloats2[0] = 1;
unknownFloats2[1] = 1;
unknownFloats2[2] = 1;
shape->SetUnknownFloats2(unknownFloats2);
*/
/* array<uint, 5> unknownInts1;
unknownInts1[4] = 1;
shape->SetUnknownInts1(unknownInts1);
*/
vector<uint> unknownInts2;
unknownInts2.resize(1);
shape->SetUnknownInts2(unknownInts2);
if (tri != os.obj)
tri->DeleteMe();
return bhkSphereRepShapeRef(DynamicCast<bhkSphereRepShape>(shape));
}