Developer Documentation
Loading...
Searching...
No Matches
Skeleton Tutorial SourceCode
#include <ACG/Math/GLMatrixT.hh>
#include <ObjectTypes/Skeleton/BaseSkin.hh>
void skeletonTutorial2() {
int objectId = -1;
emit addEmptyObject( DATA_SKELETON, objectId );
SkeletonObject* skeletonObject;
if ( !PluginFunctions::getObject(objectId, skeletonObject) ) {
emit log(LOGERR,"Unable to create new Skeleton");
return;
}
Skeleton* skeleton = PluginFunctions::skeleton(skeletonObject);
//setup the topology
Skeleton::Joint* currentParent = 0;
int jointCount = 3;
for (int i=0; i < jointCount; i++){
std::string jointName = QString("Joint" + QString::number(i)).toStdString();
Skeleton::Joint* newJoint = new Skeleton::Joint(currentParent, jointName );
skeleton->addJoint(currentParent, newJoint);
currentParent = newJoint;
}
//set positions for the reference pose
Skeleton::Pose* refPose = skeleton->referencePose();
double xOffset = 0.0;
for (Skeleton::Iterator it = skeleton->begin(); it != skeleton->end(); ++it){
refPose->setGlobalTranslation( (*it)->id(), ACG::Vec3d(xOffset, 0.0, 0.0) );
xOffset += 1.0;
}
//add an animation
int frameCount = 100;
FrameAnimationT<ACG::Vec3d>* animation = new FrameAnimationT<ACG::Vec3d>(skeleton, frameCount);
AnimationHandle animHandle = skeleton->addAnimation("Rotation", animation);
skeleton->animation(animHandle)->setFps(25);
//set position in every pose/frame of the animation
Skeleton::Joint* middleJoint = skeleton->joint(1);
ACG::GLMatrixd rotationMatrix;
rotationMatrix.identity();
double maxAngle = 90.0;
ACG::Vec3d axis(0.0, 0.0, 1.0);
for (int i=0; i < frameCount; i++){
animHandle.setFrame(i);
Skeleton::Pose* pose = skeleton->pose(animHandle);
for (Skeleton::Iterator it = skeleton->begin(); it != skeleton->end(); ++it)
pose->setGlobalTranslation( (*it)->id(), refPose->globalTranslation( (*it)->id() ) );
rotationMatrix.rotate( maxAngle / frameCount, axis);
pose->setGlobalMatrix( middleJoint->id(), rotationMatrix, false );
pose->setGlobalTranslation(middleJoint->id(), refPose->globalTranslation( middleJoint->id() ), false );
}
//add a mesh
objectId = -1;
emit addEmptyObject( DATA_TRIANGLE_MESH, objectId );
TriMeshObject* meshObject;
if ( !PluginFunctions::getObject(objectId,meshObject) ) {
emit log(LOGERR,"Unable to create new Mesh");
return;
}
TriMesh* mesh = PluginFunctions::triMesh(meshObject);
//add skin-weights property on the mesh
mesh->add_property(propWeights, SKIN_WEIGHTS_PROP);
int steps = 50;
for (int i=0; i < jointCount; i++)
for (int j=0; j < steps; j++){
double xPos = (j / double(steps-1));
TriMesh::VertexHandle vh1 = mesh->add_vertex( ACG::Vec3d(i + xPos, 0.1, 0.0) );
TriMesh::VertexHandle vh2 = mesh->add_vertex( ACG::Vec3d(i + xPos, -0.1, 0.0) );
mesh->property(propWeights, vh1)[ i ] = 1.0 - xPos;
mesh->property(propWeights, vh2)[ i ] = 1.0 - xPos;
if ( i != 2 ){
mesh->property(propWeights, vh1)[ i+1 ] = xPos;
mesh->property(propWeights, vh2)[ i+1 ] = xPos;
} else
break;
}
RPC::callFunctionValue<bool, int, int>("skeletalanimation", "attachSkin", skeletonObject->id(), meshObject->id());
emit updatedObject(skeletonObject->id(), UPDATE_ALL);
emit updatedObject(meshObject->id(), UPDATE_ALL);
}
@ LOGERR
#define DATA_SKELETON
Definition Skeleton.hh:64
#define DATA_TRIANGLE_MESH
void rotate(Scalar angle, Scalar x, Scalar y, Scalar z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
void identity()
setup an identity matrix
A handle used to refer to an animation or to a specific frame in an animation.
void setFrame(size_t _iFrame)
Sets the current animation frame (not failsafe)
int id() const
Represents a single joint in the skeleton.
Definition JointT.hh:61
size_t id() const
returns the joint id
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition PolyMeshT.hh:136
SmartVertexHandle add_vertex(const Point _p)
Definition PolyMeshT.hh:255
A general pose, used to store the frames of the animation.
Definition PoseT.hh:59
void setGlobalTranslation(unsigned int _joint, const Vector &_position, bool _keepGlobalChildPositions=true)
Sets the global translation vector.
void setGlobalMatrix(unsigned int _joint, const Matrix &_global, bool _keepGlobalChildPositions=true)
Sets the global coordinate system.
Vector globalTranslation(unsigned int _joint)
Returns the global translation vector.
Iterator class for the skeleton.
Definition SkeletonT.hh:83
Pose * referencePose()
Returns a pointer to the reference pose.
Iterator begin()
Iterator over joints of the skeletal tree in TOP-DOWN order (from root to leafs)
void addJoint(typename SkeletonT< PointT >::Joint *_pParent, typename SkeletonT< PointT >::Joint *_pJoint)
Adds a joint as child of a given parent joint.
AnimationHandle addAnimation(std::string _name, Animation *_animation)
Adds a new animation to the list.
Animation * animation(std::string _name)
Returns a pointer to the animation to the given name.
Pose * pose(const AnimationHandle &_hAni)
Returns a pointer to the pose with the given animation handle.
Joint * joint(const size_t &_index)
Returns the joint with the given index.
Iterator end()
Compare an iterator with the return value of this method to test if it is done.
Type for a MeshObject containing a triangle mesh.
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
DrawMode POINTS
draw unlighted points using the default base color
Definition DrawModes.cc:73
VectorT< double, 3 > Vec3d
Definition VectorT.hh:121
SkeletonObject * skeletonObject(BaseObjectData *_object)
Cast an BaseObject to a SkeletonObject if possible.
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
TriMesh * triMesh(BaseObjectData *_object)
Get a triangle mesh from an object.
Skeleton * skeleton(BaseObjectData *_object)
Get a skeleton from an object.
void setDrawMode(const ACG::SceneGraph::DrawModes::DrawMode &_mode, int _viewer)
Set the draw Mode of a Viewer. .
void viewAll(int _viewer)
View the whole scene.