Add animation loader and animate with given values
This commit is contained in:
parent
71edbc6f51
commit
b4bec44770
2 changed files with 46 additions and 46 deletions
|
@ -33,12 +33,33 @@ SkeletonViewer::SkeletonViewer(DataStore *data)
|
|||
colors[i][j] = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
|
||||
}
|
||||
}
|
||||
|
||||
frame = 0;
|
||||
}
|
||||
|
||||
//draws a part of a skeleton, represented by the given root node
|
||||
void SkeletonViewer::draw_skeleton_subtree(Bone *node, const Mat4 &global_to_parent_local, context &c, int depth)
|
||||
{
|
||||
//Task: Draw skeleton
|
||||
if (frame < animations.size())
|
||||
{
|
||||
auto ¤t_step = animations[frame];
|
||||
auto it = current_step.find(node->get_name());
|
||||
|
||||
if (it != current_step.end())
|
||||
{
|
||||
std::vector<float> &values = it->second;
|
||||
|
||||
if (values.size() == node->dof_count())
|
||||
{
|
||||
for (int i = 0; i < values.size(); i++)
|
||||
{
|
||||
node->get_dof(i)->set_value(values[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Mat4 local_transform = node->calculate_transform_prev_to_current_with_dofs() * global_to_parent_local;
|
||||
|
||||
Vec4 zero = Vec4(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
|
@ -49,8 +70,6 @@ void SkeletonViewer::draw_skeleton_subtree(Bone *node, const Mat4 &global_to_par
|
|||
Vec3 p1_v3 = Vec3(p1.x(), p1.y(), p1.z());
|
||||
Vec3 p2_v3 = Vec3(p2.x(), p2.y(), p2.z());
|
||||
|
||||
std::cout << node->get_name() << " start: " << from_Vec3(p1_v3) << ", end: " << from_Vec3(p2_v3) << std::endl;
|
||||
|
||||
glColor3f(colors[depth][0], colors[depth][1], colors[depth][2]);
|
||||
|
||||
c.tesselate_arrow(p1_v3, p2_v3);
|
||||
|
@ -80,8 +99,6 @@ void SkeletonViewer::stop_animation()
|
|||
|
||||
void SkeletonViewer::skeleton_changed(std::shared_ptr<Skeleton> s)
|
||||
{
|
||||
std::cout << "skeleton changed" << std::endl;
|
||||
|
||||
// This function is called whenever the according signal of the
|
||||
// data store has been called.
|
||||
|
||||
|
@ -120,8 +137,6 @@ void SkeletonViewer::recursive_connect_signals(Bone *b)
|
|||
|
||||
void SkeletonViewer::dof_changed(double)
|
||||
{
|
||||
std::cout << "dof changed" << std::endl;
|
||||
|
||||
if (!data->dof_changed_by_ik)
|
||||
data->set_endeffector(nullptr);
|
||||
|
||||
|
@ -245,12 +260,14 @@ void SkeletonViewer::load_animation()
|
|||
std::string filename = cgv::gui::file_open_dialog("Open", "Animation File (*.amc):*.amc");
|
||||
if (!filename.empty())
|
||||
{
|
||||
animations.clear();
|
||||
|
||||
std::ifstream file(filename);
|
||||
|
||||
std::string line;
|
||||
int current_index = -1;
|
||||
|
||||
while(std::getline(file, line))
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
if (line[0] == '#' || line[0] == ':')
|
||||
{
|
||||
|
@ -266,7 +283,9 @@ void SkeletonViewer::load_animation()
|
|||
{
|
||||
current_index = number - 1;
|
||||
|
||||
std::cout << "current_index: " << current_index << std::endl;
|
||||
//std::map<std::string, std::vector<AtomicTransform *>> tmp;
|
||||
std::map<std::string, std::vector<float>> tmp;
|
||||
animations.emplace_back(tmp);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -274,53 +293,28 @@ void SkeletonViewer::load_animation()
|
|||
abort();
|
||||
|
||||
std::string bone_name;
|
||||
float bla;
|
||||
std::vector<float> values;
|
||||
|
||||
// sexy c-code for gettin string
|
||||
while(!line.empty()) {
|
||||
int first_blank = line.find_first_of(" ");
|
||||
string fnumbers = line.substr(first_blank + 1);
|
||||
string bonename = line.substr(0, first_blank);
|
||||
}
|
||||
int first_blank = line.find_first_of(" ");
|
||||
bone_name = line.substr(0, first_blank);
|
||||
|
||||
if (!(sline >> bone_name))
|
||||
{
|
||||
std::cout << "index(" << current_index << ") could not get bone name from: " << line << std::endl;
|
||||
auto ¤t_map = animations[current_index];
|
||||
|
||||
if (!(sline >> bla))
|
||||
{
|
||||
std::cout << "index(" << current_index << ") could not get value from: " << line << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<AtomicTransform> transforms;
|
||||
std::string rest = line.substr(first_blank + 1, line.size());
|
||||
std::stringstream sstream(rest);
|
||||
float value;
|
||||
|
||||
if (name == "root" )
|
||||
while (sstream >> value)
|
||||
{
|
||||
for(int i = 0; i < 6; i++)
|
||||
{
|
||||
if (!(sline >> value))
|
||||
{
|
||||
std::cout << "could not get value from: " << line << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (!name.empty())
|
||||
{
|
||||
while (sline >> value)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
values.push_back(value);
|
||||
}
|
||||
|
||||
abort();
|
||||
// test if values alone are enough
|
||||
current_map[bone_name] = values;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "successfully loaded anination " << animations.size() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -389,4 +383,8 @@ void SkeletonViewer::draw(context &c)
|
|||
|
||||
if (data->get_skeleton() != nullptr)
|
||||
draw_skeleton_subtree(data->get_skeleton()->get_root(), data->get_skeleton()->get_origin(), c, 0);
|
||||
|
||||
frame++;
|
||||
if (frame >= animations.size())
|
||||
frame = 0;
|
||||
}
|
|
@ -33,12 +33,14 @@ class SkeletonViewer : public node, public drawable, public provider
|
|||
gui_group_ptr tree_view;
|
||||
gui_group_ptr bone_group;
|
||||
|
||||
std::vector<std::map<std::string, std::vector<AtomicTransform* >>> animations;
|
||||
std::vector<std::map<std::string, std::vector<float>>> animations;
|
||||
float colors[SKELETON_DEPTH][3];
|
||||
|
||||
// Maps gui elements in the tree view to a specific bone
|
||||
std::map<base_ptr, Bone *> gui_to_bone;
|
||||
|
||||
int frame;
|
||||
|
||||
// slot for the signal
|
||||
void timer_event(double, double dt);
|
||||
void skeleton_changed(std::shared_ptr<Skeleton>);
|
||||
|
|
Loading…
Reference in a new issue