bool animation::load_nan(shared_animation &res,resource_data &data,const char* name) { nya_memory::memory_reader reader(data.get_data(),data.get_size()); const char nan_sign[]={'n','y','a',' ','a','n','i','m'}; if(!reader.test(nan_sign,sizeof(nan_sign))) return false; typedef unsigned int uint; const uint version=reader.read<uint>(); if(version!=1) return false; enum curve_type //ToDo { pos_vec3_linear=10, //pos_vec3_bezier=11, rot_quat_linear=20, //rot_quat_bezier=21, //scale_vec3_linear=30, curve_float_linear=70 }; const int bones_count=reader.read<int>(); for(int i=0;i<bones_count;++i) { const std::string bone_name=reader.read_string(); if(bone_name.empty()) return false; unsigned char type=reader.read<unsigned char>(); uint frames_count=reader.read<uint>(); switch(type) { case pos_vec3_linear: { const int bone_idx=res.anim.add_bone(bone_name.c_str()); for(uint j=0;j<frames_count;++j) { const uint time=reader.read<uint>(); const nya_math::vec3 pos=reader.read<nya_math::vec3>(); res.anim.add_bone_pos_frame(bone_idx,time,pos); } } break; case rot_quat_linear: { const int bone_idx=res.anim.add_bone(bone_name.c_str()); for(uint j=0;j<frames_count;++j) { const uint time=reader.read<uint>(); const nya_math::quat rot=reader.read<nya_math::quat>(); res.anim.add_bone_rot_frame(bone_idx,time,rot); } } break; case curve_float_linear: { const int bone_idx=res.anim.add_curve(bone_name.c_str()); for(uint j=0;j<frames_count;++j) { const uint time=reader.read<uint>(); const float value=reader.read<float>(); res.anim.add_curve_frame(bone_idx,time,value); } } break; default: return false; } } return true; }
bool material::load_text(shared_material &res,resource_data &data,const char* name) { nya_formats::text_parser parser; parser.load_from_data((const char *)data.get_data(),data.get_size()); for(int section_idx=0;section_idx<parser.get_sections_count();++section_idx) { const char *section_type=parser.get_section_type(section_idx); if(strcmp(section_type,"@pass")==0) { int pass_idx = res.add_pass(parser.get_section_name(section_idx)); pass &p = res.get_pass(pass_idx); for(int subsection_idx=0;subsection_idx<parser.get_subsections_count(section_idx);++subsection_idx) { const char *subsection_type = parser.get_subsection_type(section_idx,subsection_idx); const char *subsection_value = parser.get_subsection_value(section_idx,subsection_idx); if(!subsection_type || !subsection_value) continue; if(strcmp(subsection_type,"shader") == 0) { if(!p.m_shader.load(subsection_value)) nya_log::log()<<"can't load shader when loding material '"<<name<<"'\n"; } else if(strcmp(subsection_type,"blend")==0) p.get_state().blend=nya_formats::blend_mode_from_string(subsection_value,p.get_state().blend_src,p.get_state().blend_dst); else if(strcmp(subsection_type,"zwrite")==0) p.get_state().zwrite=nya_formats::bool_from_string(subsection_value); else if(strcmp(subsection_type,"cull")==0) p.get_state().cull_face=nya_formats::cull_face_from_string(subsection_value,p.get_state().cull_order); else p.set_pass_param(subsection_type,param(nya_formats::vec4_from_string(subsection_value))); } } else if(strcmp(section_type,"@texture")==0) { texture_proxy tex; tex.create(); if(tex->load(parser.get_section_value(section_idx))) { const int texture_idx=res.get_texture_idx(parser.get_section_name(section_idx)); if(texture_idx<0) { material_internal::material_texture mat; mat.semantics=parser.get_section_name(section_idx); mat.proxy=tex; res.m_textures.push_back(mat); } else res.m_textures[texture_idx].proxy=tex; } else nya_log::log()<<"can't load texture when loading material "<<name<<"'\n"; } else if(strcmp(section_type,"@param")==0) { material_internal::param_holder ph; ph.name=parser.get_section_name(section_idx); ph.p=param_proxy(material_internal::param(parser.get_section_value_vector(section_idx))); const int param_idx=res.get_param_idx(parser.get_section_name(section_idx)); if(param_idx>=0) res.m_params[param_idx]=ph; else res.m_params.push_back(ph); } else nya_log::log()<<"unknown section when loading material '"<<name<<"'\n"; } res.m_should_rebuild_passes_maps=true; return true; }
bool animation::load_vmd(shared_animation &res,resource_data &data,const char* name) { nya_memory::memory_reader reader(data.get_data(),data.get_size()); if(!reader.test("Vocaloid Motion Data 0002",25)) return false; reader.skip(5); reader.skip(20);//name typedef unsigned int uint; const uint frames_count=reader.read<uint>(); if(!reader.check_remained(frames_count*(15+sizeof(uint)+sizeof(float)*7+16*4))) return false; for(uint i=0;i<frames_count;++i) { struct { std::string name; uint frame; nya_math::vec3 pos; nya_math::quat rot; char bezier_x[16]; char bezier_y[16]; char bezier_z[16]; char bezier_rot[16]; } bone_frame; bone_frame.name=std::string((const char*)reader.get_data(),15); bone_frame.name.resize(strlen(bone_frame.name.c_str())); reader.skip(15); bone_frame.frame=reader.read<uint>(); bone_frame.pos.x=reader.read<float>(); bone_frame.pos.y=reader.read<float>(); bone_frame.pos.z= -reader.read<float>(); bone_frame.rot.v.x= -reader.read<float>(); bone_frame.rot.v.y= -reader.read<float>(); bone_frame.rot.v.z=reader.read<float>(); bone_frame.rot.w=reader.read<float>(); memcpy(bone_frame.bezier_x,reader.get_data(),sizeof(bone_frame.bezier_x)); reader.skip(sizeof(bone_frame.bezier_x)); memcpy(bone_frame.bezier_y,reader.get_data(),sizeof(bone_frame.bezier_y)); reader.skip(sizeof(bone_frame.bezier_y)); memcpy(bone_frame.bezier_z,reader.get_data(),sizeof(bone_frame.bezier_z)); reader.skip(sizeof(bone_frame.bezier_z)); memcpy(bone_frame.bezier_rot,reader.get_data(),sizeof(bone_frame.bezier_rot)); reader.skip(sizeof(bone_frame.bezier_rot)); const float c2f=1.0f/128.0f; const int bone_idx=res.anim.add_bone(bone_frame.name.c_str()); if(bone_idx<0) continue; const uint time=bone_frame.frame*33; //33.6 nya_render::animation::pos_interpolation pos_inter; pos_inter.x=nya_math::bezier(bone_frame.bezier_x[0]*c2f,bone_frame.bezier_x[4]*c2f, bone_frame.bezier_x[8]*c2f,bone_frame.bezier_x[12]*c2f); pos_inter.y=nya_math::bezier(bone_frame.bezier_y[0]*c2f,bone_frame.bezier_y[4]*c2f, bone_frame.bezier_y[8]*c2f,bone_frame.bezier_y[12]*c2f); pos_inter.z=nya_math::bezier(bone_frame.bezier_z[0]*c2f,bone_frame.bezier_z[4]*c2f, bone_frame.bezier_z[8]*c2f,bone_frame.bezier_z[12]*c2f); res.anim.add_bone_pos_frame(bone_idx,time,bone_frame.pos,pos_inter); const nya_math::bezier rot_inter=nya_math::bezier(bone_frame.bezier_rot[0]*c2f,bone_frame.bezier_rot[4]*c2f, bone_frame.bezier_rot[8]*c2f,bone_frame.bezier_rot[12]*c2f); res.anim.add_bone_rot_frame(bone_idx,time,bone_frame.rot,rot_inter); } return true; }