5 public enum FrustumPlacement { outside, inside, intersecting };
7 public class ObjectFormat
9 class_data const char * extension;
10 class_property const char * extension
12 set { class_data(extension) = value; }
13 get { return class_data(extension); }
16 virtual bool ::Load(Object object, const char * fileName, DisplaySystem displaySystem);
19 // TODO: Review these:
20 public class ObjectFlags
23 bool root:1, viewSpace:1, ownMesh:1, translucent:1, flipWindings:1, keysLoaded:1, transform:1, mesh:1, light:1, camera:1;
27 public struct Transform
30 Quaternion orientation;
34 /*static float ease(float t, float a, float b)
39 if (s == 0.0f) return t;
46 if (t < a) return (k/a)*t*t;
47 if (t < 1.0f - b) return k*(2.0f * t - a);
49 return 1.0f - (k/b)*t*t;
52 public enum FrameTrackType : uint16 { position = 1, rotation, scaling, fov, roll, colorChange, morph, hotSpot, fallOff, hide };
54 public class FrameTrackBits
60 public struct FrameKey
63 float tension, continuity, bias;
64 float easeFrom, easeTo;
68 Quaternion orientation;
78 enum SplinePart { splinePoint, splineA, splineB };
80 public class FrameTrack : struct
82 FrameTrack prev, next;
97 float GetFloat(SplinePart what, unsigned int n)
100 FrameKey *kn_1, *kn, *kn1;
107 if(what == splinePoint)
119 value *= 1.0f - kn->tension;
124 kn_1 = &keys[numKeys-2];
125 d1 = keys[numKeys-1].frame - kn_1->frame;
126 d2 = kn1->frame - kn->frame;
134 a1 = GetFloat(splineA, 1);
138 value *= 1.0f - kn->tension;
142 else if(n == numKeys-1)
150 value *= 1.0f - kn->tension;
156 d1 = kn->frame - kn_1->frame;
157 d2 = kn1->frame - keys[0].frame;
165 bn_1 = GetFloat(splineB, n-1);
169 value *= 1.0f - kn->tension;
177 d1 = kn->frame - kn_1->frame;
178 d2 = kn1->frame - kn->frame;
198 adjust = 0.5f + (1.0f - Abs(C))*(adjust - 0.5f);
201 part1 *= (1.0f + kn->bias)*(1.0f - C);
204 part2 *= (1.0f - kn->bias)*(1.0f + C);
206 value = part1 + part2;
207 value *= (1.0f - kn->tension)*adjust;
213 void GetVector(Vector3Df vector, SplinePart what, unsigned int n)
215 FrameKey *kn_1, *kn, *kn1;
216 Vector3Df *pn_1, *pn, *pn1;
222 if(what == splinePoint)
229 pn1 = &kn1->position;
233 vector.Subtract(pn1, pn);
234 vector.Scale(vector, 1.0f - kn->tension);
239 kn_1 = &keys[numKeys-2];
240 d1 = keys[numKeys-1].frame - kn_1->frame;
241 d2 = kn1->frame - kn->frame;
246 vector.Subtract(pn1, pn);
247 vector.Scale(vector, 1.5f);
249 GetVector(a1, splineA, 1);
252 vector.Subtract(vector, a1);
253 vector.Scale(vector, 1.0f - kn->tension);
257 else if(n == numKeys-1)
260 pn_1 = &kn_1->position;
264 vector.Subtract(pn, pn_1);
265 vector.Scale(vector, 1.0f - kn->tension);
271 d1 = kn->frame - kn_1->frame;
272 d2 = kn1->frame - keys[0].frame;
277 vector.Subtract(pn, pn_1);
278 vector.Scale(vector, 1.5f);
280 GetVector(&bn_1, splineB, n-1);
281 bn_1.Scale(bn_1, 0.5f);
283 vector.Subtract(vector, bn_1);
284 vector.Scale(vector, 1.0f - kn->tension);
292 d1 = kn->frame - kn_1->frame;
293 d2 = kn1->frame - kn->frame;
297 Vector3Df part1, part2;
299 pn_1 = &kn_1->position;
300 pn1 = &kn1->position;
313 adjust = 0.5f + (1.0f - Abs(C))*(adjust - 0.5f);
315 part1.Subtract(pn, pn_1);
316 part1.Scale(part1, (1.0f + kn->bias)*(1.0f - C));
318 part2.Subtract(pn1, pn);
319 part2.Scale(part2, (1.0f - kn->bias)*(1.0f + C));
321 vector.Add(part1, part2);
322 vector.Scale(vector, (1.0f - kn->tension)*adjust);
327 void GetQuaternion(Quaternion quat, SplinePart what, unsigned int n)
329 FrameKey *kn_1, *kn, *kn1;
330 Quaternion *qn_1, *qn, *qn1;
334 qn = &kn->orientation;
336 if (what == splinePoint)
344 if (!(type.loop) || numKeys <= 2)
346 qn1 = &kn1->orientation;
347 quat.Slerp(qn, qn1, (1.0f - kn->tension)*(1.0f + kn->continuity*kn->bias)/3.0f);
352 kn_1= &keys[numKeys-2];
353 d1 = keys[numKeys-1].frame - kn_1->frame;
354 d2 = kn1->frame - kn->frame;
357 else if (n == numKeys-1)
361 if (!(type.loop) || numKeys <= 2)
363 qn_1 = &kn_1->orientation;
364 quat.Slerp(qn, qn_1, (1.0f - kn->tension)*(1.0f - kn->continuity*kn->bias)/3.0f);
370 d1 = kn->frame - kn_1->frame;
371 d2 = kn1->frame - keys[0].frame;
378 d1 = kn->frame - kn_1->frame;
379 d2 = kn1->frame - kn->frame;
383 Quaternion g1, g2, tmp;
385 qn_1 = &kn_1->orientation;
386 qn1 = &kn1->orientation;
399 adjust = 0.5f + (1.0f - Abs(kn->continuity))*(adjust - 0.5f);
401 g1.Slerp(qn, qn_1,-(1.0f + kn->bias)/3.0f);
402 g2.Slerp(qn, qn1 , (1.0f - kn->bias)/3.0f);
403 tmp.Slerp(g1, g2, 0.5f + f*0.5f*kn->continuity);
404 quat.Slerp(qn, &tmp, f*(kn->tension - 1.0f)*adjust*2.0f);
409 void Interpolate(Vector3Df vector, Vector3Df prevVector, Vector3Df nextVector, int prev, int next, float t)
415 Vector3Df p1 = prevVector, p2 = nextVector;
418 GetVector(r1, splineB, prev);
419 GetVector(r2, splineA, next);
421 p1.Scale(p1, 2*t*t*t - 3*t*t + 1);
422 p2.Scale(p2,-2*t*t*t + 3*t*t);
424 r1.Scale(r1, t*t*t - 2*t*t + t);
425 r2.Scale(r2, t*t*t - t*t);
428 vector.Add(vector, r1);
429 vector.Add(vector, p2);
430 vector.Add(vector, r2);
434 void InterpolateQuat(Quaternion quat, Quaternion prevQuat, Quaternion nextQuat, int prev, int next, float t)
441 Quaternion q0, q1, q2;
443 GetQuaternion(b, splineB, prev);
444 GetQuaternion(a, splineA, next);
446 q0.Slerp(prevQuat, b, t);
448 q2.Slerp(a, nextQuat, t);
453 quat.Slerp(q0, q1, t);
457 float InterpolateFloat(float prevValue, float nextValue, int prev, int next, float t)
464 float p1 = prevValue, p2 = nextValue;
466 float r1 = GetFloat(splineB, prev);
467 float r2 = GetFloat(splineA, next);
469 p1 *= 2*t*t*t - 3*t*t + 1;
470 p2 *= -2*t*t*t + 3*t*t;
472 r1 *= t*t*t - 2*t*t + t;
475 value = p1 + r1 + p2 + r2;
481 static bool FindMaterialAndType(Mesh mesh, Material material, PrimitiveGroupType type)
483 PrimitiveGroup group;
484 for(group = mesh.groups.first; group; group = group.next)
485 if(group.material == material && group.type == type)
493 void SetMinMaxRadius(bool processMesh)
497 if(flags.mesh && mesh)
500 mesh.SetMinMaxRadius();
503 volume = (max.x >= min.x && max.y >= min.y && max.z >= min.z);
507 min = { MAXFLOAT, MAXFLOAT, MAXFLOAT };
508 max = { -MAXFLOAT, -MAXFLOAT, -MAXFLOAT };
511 for(child = children.first; child; child = child.next)
513 child.SetMinMaxRadius(processMesh);
517 // Child Local + Child Object Transform
518 Vector3Df points[8] =
520 { child.min.x, child.min.y, child.min.z },
521 { child.min.x, child.min.y, child.max.z },
522 { child.min.x, child.max.y, child.min.z },
523 { child.min.x, child.max.y, child.max.z },
524 { child.max.x, child.min.y, child.min.z },
525 { child.max.x, child.min.y, child.max.z },
526 { child.max.x, child.max.y, child.min.z },
527 { child.max.x, child.max.y, child.max.z }
533 point.MultMatrix(points[c], child.localMatrix);
535 if(point.x < min.x) min.x = point.x;
536 if(point.y < min.y) min.y = point.y;
537 if(point.z < min.z) min.z = point.z;
539 if(point.x > max.x) max.x = point.x;
540 if(point.y > max.y) max.y = point.y;
541 if(point.z > max.z) max.z = point.z;
549 Vector3Df points[8] =
551 { min.x, min.y, min.z },
552 { min.x, min.y, max.z },
553 { min.x, max.y, min.z },
554 { min.x, max.y, max.z },
555 { max.x, min.y, min.z },
556 { max.x, min.y, max.z },
557 { max.x, max.y, min.z },
558 { max.x, max.y, max.z }
560 Vector3Df halfExtent;
561 Vector3D halfExtentd;
564 center.Add(min, max);
565 center.Scale(center, 0.5f);
566 halfExtent.Subtract(max, min);
567 halfExtent.Scale(halfExtent, 0.5f);
568 radius = halfExtent.length;
572 Vector3D min { MAXFLOAT, MAXFLOAT, MAXFLOAT };
573 Vector3D max { -MAXFLOAT, -MAXFLOAT, -MAXFLOAT };
578 point.MultMatrixf(points[c], matrix);
580 if(point.x < min.x) min.x = point.x;
581 if(point.y < min.y) min.y = point.y;
582 if(point.z < min.z) min.z = point.z;
584 if(point.x > max.x) max.x = point.x;
585 if(point.y > max.y) max.y = point.y;
586 if(point.z > max.z) max.z = point.z;
591 wcenter.Add(wmin, wmax);
592 wcenter.Scale(wcenter, 0.5f);
594 halfExtentd.Subtract(wmax, wmin);
595 halfExtentd.Scale(halfExtentd, 0.5);
596 wradius = halfExtentd.length;
600 void Duplicate(Object model)
606 name = CopyString(model.name);
608 flags.ownMesh = false;
613 radius = model.radius;
615 transform = model.transform;
617 for(modelChild = model.children.first; modelChild; modelChild = modelChild.next)
619 Object child { parent = this };
620 child.Duplicate(modelChild);
621 children.AddName(child);
626 void Free(DisplaySystem displaySystem)
632 while((child = children.first))
634 children.Remove(child);
635 child.Free(displaySystem);
637 // We did not do this before so as to keep transform on reloading for new DisplaySystem
638 // However since children are removed, it seems that purpose has been gone, we'd need
639 // a new mechanism to handle lost resources reloading...
642 if(flags.ownMesh && mesh)
644 DisplaySystem meshDisplaySystem = mesh.displaySystem;
646 if(meshDisplaySystem)
647 meshDisplaySystem.RemoveMesh(mesh);
651 tracks.Free(FrameTrack::Free);
657 bool Load(const char * fileName, const char * type, DisplaySystem displaySystem)
659 char ext[MAX_EXTENSION];
660 subclass(ObjectFormat) format;
664 if(!type && fileName)
665 type = strlwr(GetExtension(fileName, ext));
667 for(link = class(ObjectFormat).derivatives.first; link; link = link.next)
670 if(format.extension && !strcmp(format.extension, type))
673 if(!link) format = null;
677 if((format.Load(this, fileName, displaySystem)))
681 ErrorLogCode(GERR_LOAD_OBJECT_FAILED, fileName);*/
685 void FreeMesh(DisplaySystem displaySystem)
691 for(child = children.first; child; child = child.next)
692 child.FreeMesh(displaySystem);
696 Object Find(const char * name)
702 if(this.name && !strcmp(this.name, name))
706 for(child = children.first; child; child = child.next)
708 Object result = child.Find(name);
717 void Initialize(void)
721 transform.scaling = { 1, 1, 1 };
722 transform.orientation = { 1,0,0,0 };
724 flags.transform = true;
729 Mesh InitializeMesh(DisplaySystem displaySystem)
737 flags.ownMesh = true;
741 FillBytes(mesh, 0, sizeof(class Mesh));
742 displaySystem.AddMesh(mesh);
750 bool AddName(Object object, const char * name)
755 char * newName = CopyString(name);
756 object.name = newName;
757 result = children.AddName(object);
759 object.parent = this;
760 object.flags.transform = true;
765 // TODO: Add support to Merge Vertex Colors mesh feature
766 bool Merge(DisplaySystem displaySystem)
770 if(!this.flags.mesh || (!children.first && this.flags.ownMesh))
774 Object child, nextChild;
776 MeshFeatures flags = 0;
777 Mesh objectMesh = mesh;
778 bool freeMesh = this.flags.ownMesh;
781 this.flags.ownMesh = true;
782 this.flags.mesh = true;
783 displaySystem.AddMesh(mesh);
785 // Count total number of vertices
788 flags |= objectMesh.flags;
789 nVertices += objectMesh.nVertices;
792 for(child = children.first; child; child = child.next)
794 child.Merge(displaySystem);
797 nVertices += child.mesh.nVertices;
798 flags |= child.mesh.flags;
802 if(mesh.Allocate(flags, nVertices, displaySystem))
806 int vertexOffset = 0;
807 PrimitiveGroup group = null;
814 for(c = 0; c<objectMesh.nVertices; c++)
816 mesh.vertices[nVertices] = objectMesh.vertices[c];
818 if(objectMesh.normals)
819 mesh.normals[nVertices] = objectMesh.normals[c];
820 if(objectMesh.texCoords)
821 mesh.texCoords[nVertices] = objectMesh.texCoords[c];
827 for(child = children.first; child; child = child.next)
829 Matrix matrix, normalMatrix;
832 matrix.Scale(child.transform.scaling.x, child.transform.scaling.y, child.transform.scaling.z);
834 matrix.Rotate(child.transform.orientation);
836 normalMatrix = matrix;
838 matrix.Translate(child.transform.position.x, child.transform.position.y, child.transform.position.z);
841 for(c = 0; c<child.mesh.nVertices; c++)
843 mesh.vertices[nVertices].MultMatrix(child.mesh.vertices[c], matrix);
844 if(child.mesh.normals)
845 mesh.normals[nVertices].MultMatrix(child.mesh.normals[c], normalMatrix);
846 if(child.mesh.texCoords)
847 mesh.texCoords[nVertices] = child.mesh.texCoords[c];
853 // Merge Indexed Primitive Groups
857 PrimitiveGroupType type = (PrimitiveGroupType)-1;
858 Material material = null;
859 bool foundGroup = false;
861 // Find first group type/material to process and determine how many indices are required
864 for(group = objectMesh.groups.first; group; group = group.next)
866 if(!foundGroup && !(group.type.vertexRange))
868 if(!FindMaterialAndType(mesh, group.material, group.type))
870 material = group.material;
872 nIndices += group.nIndices;
876 else if(material == group.material && type == group.type)
877 nIndices += group.nIndices;
881 for(child = children.first; child; child = child.next)
885 for(group = child.mesh.groups.first; group; group = group.next)
887 if(!foundGroup && !(group.type.vertexRange))
889 if(!FindMaterialAndType(mesh, group.material ? group.material : child.material, group.type))
891 material = group.material ? group.material : child.material;
893 nIndices += group.nIndices;
897 else if(material == (group.material ? group.material : child.material) && type == group.type)
898 nIndices += group.nIndices;
903 // Merge with all similar groups
906 PrimitiveGroup newGroup = mesh.AddPrimitiveGroup(type, nIndices);
909 newGroup.material = material;
916 for(group = objectMesh.groups.first; group; group = group.next)
918 if(newGroup.material == group.material && newGroup.type == group.type)
921 if(group.type.indices32bit)
922 for(c = 0; c<group.nIndices; c++)
923 newGroup.indices32[nIndices++] = group.indices32[c] + vertexOffset;
925 for(c = 0; c<group.nIndices; c++)
926 newGroup.indices[nIndices++] = (uint16)(group.indices[c] + vertexOffset);
929 vertexOffset += objectMesh.nVertices;
932 for(child = children.first; child; child = child.next)
936 for(group = child.mesh.groups.first; group; group = group.next)
938 if(newGroup.material == (group.material ? group.material : child.material) &&
939 newGroup.type == group.type)
942 if(group.type.indices32bit)
943 for(c = 0; c<group.nIndices; c++)
944 newGroup.indices32[nIndices++] = group.indices32[c] + vertexOffset;
946 for(c = 0; c<group.nIndices; c++)
947 newGroup.indices[nIndices++] = (uint16)(group.indices[c] + vertexOffset);
950 vertexOffset += child.mesh.nVertices;
953 mesh.UnlockPrimitiveGroup(newGroup);
960 // Merge Non-Indexed Primitive Groups
965 for(group = objectMesh.groups.first; group; group = group.next)
967 if(group.type.vertexRange)
969 PrimitiveGroup newGroup = mesh.AddPrimitiveGroup(group.type, 0);
972 newGroup.material = group.material;
973 newGroup.nVertices = group.nVertices;
974 newGroup.first = group.first + vertexOffset;
978 vertexOffset += objectMesh.nVertices;
981 for(child = children.first; child; child = child.next)
985 for(group = child.mesh.groups.first; group; group = group.next)
987 if(group.type.vertexRange)
989 PrimitiveGroup newGroup = mesh.AddPrimitiveGroup(group.type, 0);
992 newGroup.material = group.material ? group.material : child.material;
993 newGroup.nVertices = group.nVertices;
994 newGroup.first = group.first + vertexOffset;
998 vertexOffset += child.mesh.nVertices;
1004 nTriangles = objectMesh.nPrimitives;
1006 for(child = children.first; child; child = child.next)
1009 nTriangles += child.mesh.nPrimitives;
1011 mesh.primitives = new PrimitiveSingle[nTriangles];
1012 mesh.nPrimitives = 0;
1016 for(c = 0; c<objectMesh.nPrimitives; c++)
1019 PrimitiveSingle * triangle = &mesh.primitives[mesh.nPrimitives++];
1021 mesh.AllocatePrimitive(triangle, objectMesh.primitives[c].type, objectMesh.primitives[c].nIndices);
1022 triangle->material = objectMesh.primitives[c].material;
1023 triangle->middle = objectMesh.primitives[c].middle;
1024 triangle->plane = objectMesh.primitives[c].plane;
1026 memcpy(triangle->indices, objectMesh.primitives[c].indices, objectMesh.primitives[c].nIndices * sizeof(uint16));
1029 *triangle = objectMesh.primitives[c];
1030 objectMesh.primitives[c].indices = null;
1031 objectMesh.primitives[c].data = null;
1034 if(triangle->type.indices32bit)
1035 for(i = 0; i<triangle->nIndices; i++)
1036 triangle->indices32[i] += vertexOffset;
1038 for(i = 0; i<triangle->nIndices; i++)
1039 triangle->indices[i] += (uint16)vertexOffset;
1040 mesh.UnlockPrimitive(triangle);
1042 vertexOffset += objectMesh.nVertices;
1044 for(child = children.first; child; child = child.next)
1048 for(c = 0; c<child.mesh.nPrimitives; c++)
1051 PrimitiveSingle * triangle = &mesh.primitives[mesh.nPrimitives++];
1053 mesh.AllocatePrimitive(triangle, child.mesh.primitives[c].type, child.mesh.primitives[c].nIndices);
1054 triangle->material = child.mesh.primitives[c].material ? child.mesh.primitives[c].material : child.material;
1055 triangle->middle = child.mesh.primitives[c].middle;
1056 triangle->plane = child.mesh.primitives[c].plane;
1057 memcpy(triangle->indices, child.mesh.primitives[c].indices, child.mesh.primitives[c].nIndices * sizeof(uint16));
1060 *triangle = child.mesh.primitives[c];
1061 child.mesh.primitives[c].indices = null;
1062 child.mesh.primitives[c].data = null;
1065 if(triangle->type.indices32bit)
1066 for(i = 0; i<triangle->nIndices; i++)
1067 triangle->indices[i] += (uint16)vertexOffset;
1069 for(i = 0; i<triangle->nIndices; i++)
1070 triangle->indices32[i] += vertexOffset;
1071 mesh.UnlockPrimitive(triangle);
1073 vertexOffset += child.mesh.nVertices;
1078 for(child = children.first; child; child = nextChild)
1080 nextChild = child.next;
1081 children.Remove(child);
1082 child.Free(displaySystem);
1086 mesh.ApplyTranslucency(this);
1087 // this.flags.translucent = true;
1093 if(freeMesh && objectMesh)
1095 if(objectMesh.displaySystem)
1096 objectMesh.displaySystem.RemoveMesh(objectMesh);
1099 SetMinMaxRadius(true);
1104 void RotateEuler(Euler rotation, Euler min, Euler max)
1106 // WARNING: 'eulerOrientation' is only updated by this function
1107 Euler euler = eulerOrientation;//transform.orientation;
1108 euler.Add(euler, rotation);
1112 if(min.pitch && max.pitch)
1113 euler.pitch = Min(Max(euler.pitch, min.pitch), max.pitch);
1114 if(min.yaw && max.yaw)
1115 euler.yaw = Min(Max(euler.yaw, min.yaw), max.yaw);
1116 if(min.roll && max.roll)
1117 euler.roll = Min(Max(euler.roll, min.roll), max.roll);
1120 eulerOrientation = euler;
1121 transform.orientation = euler;
1125 void Move(Vector3D direction)
1130 matrix.RotationQuaternion(transform.orientation);
1131 offset.MultMatrix(direction, matrix);
1132 transform.position.Add(transform.position, offset);
1136 void UpdateTransform(void)
1138 SetTransformDirty();
1140 SetMinMaxRadius(false);
1143 void Animate(unsigned int frame)
1145 if(this && startFrame != endFrame)
1147 while(frame < startFrame) frame += (endFrame - startFrame + 1);
1148 while(frame > endFrame) frame -= (endFrame - startFrame + 1);
1153 SetMinMaxRadius(false);
1157 void DoubleSided(bool flag)
1162 mesh.DoubleSided(flag);
1163 for(child = children.first; child; child = child.next)
1164 child.DoubleSided(flag);
1168 bool IntersectsGroundPolygon(int count, Pointf * pointfs)
1170 bool result = false;
1174 double minX = wmin.x, maxX = wmax.x;
1175 double minY = wmin.z, maxY = wmax.z;
1176 double delta = (maxX - minX)/2;
1177 double x = (maxX + minX)/2, y = (maxY + minY)/2;
1179 for(c = 0; c<count; c++)
1183 p2 = &pointfs[(c == count-1) ? 0 : (c+1)];
1185 if( (p1->x < minX) && (p2->x < minX) )
1187 if((p1->y <= y) && (p2->y > y) )
1189 else if( (p1->y > y) && (p2->y <= y) )
1192 else if(!((p1->x > maxX && p2->x > maxX) || (p1->y < minY && p2->y < minY) || (p1->y > maxY && p2->y > maxY)))
1198 if (d < delta) return true;
1200 else if(p1->x == p2->x)
1204 if(d < delta) return true;
1205 else if(p1->x > x) ;
1206 else if( (p1->y <= y) && (p2->y > y) )
1208 else if( (p1->y > y) && (p2->y <= y) )
1219 d = a * x + b * y + (p2->x * p1->y) - (p2->y * p1->x);
1226 else if (d < b * delta)
1228 else if( ( (p1->y <= y) && (p2->y > y) ) || ( (p1->y > y) && (p2->y <= y) ) )
1232 xdy = (dx * (y - p1->y)) + (dy * p1->x);
1238 else if(xdy < x * dy)
1247 property Transform transform { set { transform = value; eulerOrientation = transform.orientation; } get { value = transform; } };
1248 property Material material { set { material = value; } get { return material; } };
1249 property Vector3Df max { get { value = max; } };
1250 property Vector3Df min { get { value = min; } };
1251 property Vector3Df center { get { value = center; } };
1252 property float radius { get { return radius; } };
1254 property Vector3D wmax { get { value = wmax; } };
1255 property Vector3D wmin { get { value = wmin; } };
1256 property Vector3D wcenter { get { value = wcenter; } };
1257 property double wradius { get { return wradius; } };
1259 property void * tag { set { tag = value; } get { return tag; } };
1260 property int frame { set { Animate(value); } get { return frame; } };
1261 property int startFrame { set { startFrame = value; } get { return startFrame; } };
1262 property int endFrame { set { endFrame = value; } get { return endFrame; } };
1264 property Mesh mesh { set { mesh = value; } get { return mesh; } };
1265 property Camera camera { get { return camera; } }; // Fix this with inheritance? camera inherit from Object?
1266 property Object firstChild { get { return children.first; } };
1267 property Object next { get { return next; } };
1268 property const char * name { get { return name; } };
1269 property Matrix matrix { get { value = matrix; } };
1270 property Object cameraTarget { set { cameraTarget = value; } get { return cameraTarget; } };
1271 property OldList * tracks { /* set { tracks = value; } */ get { return &tracks; } };
1272 property ObjectFlags flags { set { flags = value; } get { return flags; } };
1277 children.offset = (uint)(uintptr)&((Object)0).prev;
1278 transform.scaling = { 1, 1, 1 };
1279 transform.orientation = { 1,0,0,0 };
1280 flags.transform = true;
1288 void SetTransformDirty()
1291 flags.transform = true;
1292 for(child = children.first; child; child = child.next)
1293 child.SetTransformDirty();
1296 void _UpdateTransform()
1303 // Cameras / Spot Lights must update their target first
1304 if(flags.camera && cameraTarget && cameraTarget.flags.transform)
1305 cameraTarget.UpdateTransform();
1306 else if(flags.light && light.flags.spot && light.target.flags.transform)
1307 light.target._UpdateTransform();
1309 if(flags.camera && cameraTarget)
1311 // Determine angle to look at target
1312 Vector3D position, direction;
1313 if(flags.root || !parent)
1314 position = transform.position;
1316 position.MultMatrix(transform.position, parent.matrix);
1318 direction.Subtract((Vector3D *)cameraTarget.matrix.m[3], position);
1319 transform.orientation.RotationDirection(direction);
1322 transform.orientation.RotateRoll(roll);
1325 if(flags.light && light.flags.spot)
1327 // DeterMine angle to look at target
1329 if(flags.root || !parent)
1330 position = transform.position;
1332 position.MultMatrix(transform.position, parent.matrix);
1334 light.direction.Subtract((Vector3D *) light.target.matrix.m[3], position);
1335 light.direction.Normalize(light.direction);
1336 transform.orientation.RotationDirection(light.direction);
1340 matrix.Scale(transform.scaling.x, transform.scaling.y, transform.scaling.z);
1341 matrix.Rotate(transform.orientation);
1342 matrix.Translate(transform.position.x, transform.position.y, transform.position.z);
1344 localMatrix = matrix;
1346 // Compute transform (with ancestors)
1347 if(flags.root || !parent)
1348 this.matrix = matrix;
1350 this.matrix.Multiply(matrix, parent.matrix);
1352 flags.transform = false;
1354 for(child = children.first; child; child = child.next)
1356 if(child.flags.transform)
1357 child._UpdateTransform();
1362 void _Animate(unsigned int frame)
1367 for(track = tracks.first; track; track = track.next)
1373 unsigned int prev = 0, next = track.numKeys - 1;
1374 FrameKey * prevKey = &track.keys[prev], * nextKey = &track.keys[next];
1377 for(c = 0; c<track.numKeys; c++)
1379 FrameKey * key = track.keys + c;
1380 if(key->frame <= frame) { prevKey = key; prev = c; }
1381 if(key->frame >= frame) { nextKey = key; next = c; break; }
1384 if(nextKey->frame != prevKey->frame)
1385 t = ease((float) (frame - prevKey->frame) / (nextKey->frame - prevKey->frame), prevKey->easeFrom, nextKey->easeTo);
1387 switch(track.type.type)
1392 track.Interpolate(position, prevKey->position, nextKey->position, prev, next, t);
1393 transform.position = { (double)position.x, (double)position.y, (double)position.z };
1397 track.Interpolate(transform.scaling, prevKey->scaling, &nextKey->scaling, prev, next, t);
1400 track.InterpolateQuat(transform.orientation, prevKey->orientation, nextKey->orientation, prev, next, t);
1404 roll = track.InterpolateFloat(prevKey->roll, nextKey->roll, prev, next, t);
1408 camera.fov = track.InterpolateFloat(prevKey->fov, nextKey->fov, prev, next, t);
1410 double mm = (camera.fov - 5.05659508373109) / 1.13613250717301;
1411 camera.fov = 1248.58921609766 * pow(mm, -0.895625414990581);
1413 //camera.Setup(camera.width, camera.height, camera.origin);
1419 track.Interpolate((Vector3Df *)&light.diffuse,
1420 (Vector3Df *)&prevKey->color, (Vector3Df *)&nextKey->color, prev, next, t);
1421 light.specular = light.diffuse;
1426 light.fallOff = track.InterpolateFloat(prevKey->fallOff, nextKey->fallOff, prev, next, t);
1431 light.hotSpot = track.InterpolateFloat(prevKey->hotSpot, nextKey->hotSpot, prev, next, t);
1438 for(child = children.first; child; child = child.next)
1439 child._Animate(frame);
1441 flags.transform = true;
1445 FrustumPlacement InsideFrustum(Plane * planes)
1447 FrustumPlacement result = inside;
1450 // First test: Sphere
1451 for(p = 0; p<6; p++)
1453 Plane * plane = &planes[p];
1454 double dot = plane->normal.DotProduct(wcenter);
1455 double distance = dot + plane->d;
1456 if(distance < -wradius)
1461 if(Abs(distance) < wradius)
1462 result = intersecting;
1465 if(result == intersecting)
1467 // Second test: Bounding Box
1470 { wmin.x, wmin.y, wmin.z },
1471 { wmin.x, wmin.y, wmax.z },
1472 { wmin.x, wmax.y, wmin.z },
1473 { wmin.x, wmax.y, wmax.z },
1474 { wmax.x, wmin.y, wmin.z },
1475 { wmax.x, wmin.y, wmax.z },
1476 { wmax.x, wmax.y, wmin.z },
1477 { wmax.x, wmax.y, wmax.z }
1479 int numPlanesAllIn = 0;
1480 for(p = 0; p < 6; p++)
1482 Plane * plane = &planes[p];
1484 int numGoodPoints = 0;
1485 for(i = 0; i < 8; ++i)
1487 double dot = plane->normal.DotProduct(box[i]);
1488 double distance = dot + plane->d;
1497 if(numGoodPoints == 8)
1500 if(numPlanesAllIn == 6)
1514 unsigned startFrame, endFrame;
1518 public Transform transform;
1519 public Matrix matrix;
1522 Vector3Df min, max, center;
1523 Vector3D wmin, wmax, wcenter;
1546 Object cameraTarget;
1551 public property Light light
1563 Euler eulerOrientation;