FilterDataPtr TransformFilter::apply(FilterDataPtr input) { // To apply the transform, we just add a new node and put all root nodes as // children of it. for(FilterData::const_iterator md_it = input->begin(); md_it != input->end(); md_it++) { VisualPtr vis = *md_it; MeshdataPtr md( std::tr1::dynamic_pointer_cast<Meshdata>(vis) ); if (!md) { SILOG(transform-filter, warning, "Can't transform this type of visual: " << vis->type()); continue; } Node new_root(NullNodeIndex, mTransform); new_root.children = md->rootNodes; NodeIndex new_root_index = md->nodes.size(); md->nodes.push_back(new_root); md->rootNodes.clear(); md->rootNodes.push_back(new_root_index); } return input; }
FilterDataPtr SaveFilter::apply(FilterDataPtr input) { assert(input->single()); ModelsSystem* parser = ModelsSystemFactory::getSingleton().getConstructor("any")(""); VisualPtr vis = input->get(); bool success = parser->convertVisual(vis, mFormat, mFilename); if (!success) { std::cout << "Error saving mesh." << std::endl; return FilterDataPtr(); } return input; }
FilterDataPtr SquashMaterialsFilter::apply(FilterDataPtr input) { using namespace Sirikata::Transfer; assert(input->single()); MeshdataPtr md = input->get(); // This keeps track of our new set of unique materials and will replace // Meshdata::materials. MaterialEffectInfoList new_materials; // And this keeps track of how the indices are remapped. This is used to // update all material indices to use indices for new_materials. typedef std::map<uint32, uint32> MaterialReindexMap; MaterialReindexMap reindexes; // First, scan through and generate unique materials and the map necessary // to map old material indices to new material indices for(uint32 mat_idx = 0; mat_idx < md->materials.size(); mat_idx++) { MaterialEffectInfo& orig_mat = md->materials[mat_idx]; // First, try to find an identical material bool found_match = false; for(uint32 nmi = 0; nmi < new_materials.size(); nmi++) { if (orig_mat == new_materials[nmi]) { reindexes[mat_idx] = nmi; found_match = true; break; } } if (found_match) continue; // If we can't find one, use this as a new material in the new list uint32 new_mat_idx = new_materials.size(); new_materials.push_back( orig_mat ); reindexes[mat_idx] = new_mat_idx; } // Now, replace materials and run through all the data referencing materials // and replace indices md->materials = new_materials; // Materials are referenced in the SubMeshGeometry::Primitive, but those are // then bound to actual materials by the // GeometryInstance::MaterialBindingMap. To fix up bindings, we just need to // remap all the values in all MaterialBindingMaps. for(GeometryInstanceList::iterator geo_inst_it = md->instances.begin(); geo_inst_it != md->instances.end(); geo_inst_it++) { GeometryInstance& geo_inst = *geo_inst_it; for(GeometryInstance::MaterialBindingMap::iterator mat_binding_it = geo_inst.materialBindingMap.begin(); mat_binding_it != geo_inst.materialBindingMap.end(); mat_binding_it++) { mat_binding_it->second = reindexes[ mat_binding_it->second ]; } } // FIXME we could also try to run through and squash references to materials // into a single reference, i.e. so that MaterialBindingMaps don't refer to // the same one twice. This is more complicated because of the level of // indirection and the fact that multiple GeometryInstances could refer to // the same SubMeshGeometry -- we would need to make sure that all // GeometryInstances referring to each SubMeshGeometry could squash in the // same way. // We munged input directly, return same FilterDataPtr return input; }
FilterDataPtr PrintFilter::apply(FilterDataPtr input) { assert(input->single()); MeshdataPtr md = input->get(); if(mTexturesOnly) { for(TextureList::const_iterator it = md->textures.begin(); it != md->textures.end(); it++) { printf("%s\n", it->c_str()); } return input; } printf("URI: %s\n", md->uri.c_str()); printf("ID: %ld\n", md->id); printf("Hash: %s\n", md->hash.toString().c_str()); printf("Texture List:\n"); for(TextureList::const_iterator it = md->textures.begin(); it != md->textures.end(); it++) { printf(" %s\n", it->c_str()); } printf("Submesh Geometry List:\n"); for(SubMeshGeometryList::const_iterator it = md->geometry.begin(); it != md->geometry.end(); it++) { printf(" Name: %s, Positions: %d Normals: %d Primitives: %d, UVs: %d (sets) x %d (stride) x %d (count)\n", it->name.c_str(), (int)it->positions.size(), (int)it->normals.size(), (int)it->primitives.size(), (int)it->texUVs.size(), (int)( it->texUVs.size() ? it->texUVs[0].stride : 0), (int)( it->texUVs.size() ? it->texUVs[0].uvs.size() : 0)); for(std::vector<SubMeshGeometry::Primitive>::const_iterator p = it->primitives.begin(); p != it->primitives.end(); p++) { printf(" Primitive: material: %d, indices: %d, type: %s\n", (int)p->materialId, (int)p->indices.size(), PrimitiveTypeToString(p->primitiveType)); } } printf("Lights:\n"); for(LightInfoList::const_iterator it = md->lights.begin(); it != md->lights.end(); it++) { printf(" Type: %d Power: %f\n", it->mType, it->mPower); } printf("Material Effects:\n"); for(MaterialEffectInfoList::const_iterator it = md->materials.begin(); it != md->materials.end(); it++) { printf(" Textures: %d Shininess: %f Reflectivity: %f\n", (int)it->textures.size(), it->shininess, it->reflectivity); for(MaterialEffectInfo::TextureList::const_iterator t_it = it->textures.begin(); t_it != it->textures.end(); t_it++) printf(" Texture: %s, color = %s, affects %s, %s, min: %s, mag: %s, wrap = (%s, %s, %s), max_mip = %d, mip_bias = %f\n", t_it->uri.c_str(), t_it->color.toString().c_str(), AffectingToString(t_it->affecting), SamplerTypeToString(t_it->samplerType), SamplerFilterToString(t_it->minFilter), SamplerFilterToString(t_it->magFilter), WrapModeToString(t_it->wrapS), WrapModeToString(t_it->wrapT), WrapModeToString(t_it->wrapU), t_it->maxMipLevel, t_it->mipBias ); } printf("Geometry Instances: (%d in list, %d instanced)\n", (int)md->instances.size(), (int)md->getInstancedGeometryCount()); for(GeometryInstanceList::const_iterator it = md->instances.begin(); it != md->instances.end(); it++) { printf(" Index: %d MapSize: %d\n", it->geometryIndex, (int)it->materialBindingMap.size()); for(GeometryInstance::MaterialBindingMap::const_iterator m = it->materialBindingMap.begin(); m != it->materialBindingMap.end(); m++) { printf(" map from: %d to: %d\n", (int)m->first, (int)m->second); } } printf("Light Instances: (%d in list, %d instanced)\n", (int)md->lightInstances.size(), (int)md->getInstancedLightCount()); for(LightInstanceList::const_iterator it = md->lightInstances.begin(); it != md->lightInstances.end(); it++) { printf(" Index: %d Matrix: %s\n", it->lightIndex, md->getTransform(it->parentNode).toString().c_str()); } printf("Material Effect size: %d\n", (int)md->materials.size()); printf("Nodes size: %d (%d roots)\n", (int)md->nodes.size(), (int)md->rootNodes.size()); for(uint32 ri = 0; ri < md->rootNodes.size(); ri++) { std::stack<NodeState> node_stack; node_stack.push( NodeState(md->rootNodes[ri]) ); String indent = ""; while(!node_stack.empty()) { NodeState& curnode = node_stack.top(); if (curnode.curChild == -1) { // First time we've seen this node, print info and move it // forward to start procesing children printf("%s %d\n", indent.c_str(), curnode.node); curnode.curChild++; indent += " "; } else if (curnode.curChild >= (int)md->nodes[curnode.node].children.size()) { // We finished with this node node_stack.pop(); indent = indent.substr(1); // reduce indent } else { // Normal iteration, process next child int32 childindex = curnode.curChild; curnode.curChild++; node_stack.push( NodeState(md->nodes[curnode.node].children[childindex]) ); } } } printf("Joints: %d instanced (%d in list)\n", md->getJointCount(), md->joints.size()); // Compute the expected number of draw calls assuming no smart // transformation is occuring. This should be: // Number of instances * number of primitives in instance // This really should trace from the root to make sure that all instances // are actually drawn... uint32 draw_calls = 0; Meshdata::GeometryInstanceIterator geoinst_it = md->getGeometryInstanceIterator(); uint32 geoinst_idx; Matrix4x4f pos_xform; while( geoinst_it.next(&geoinst_idx, &pos_xform) ) { draw_calls += md->geometry[ md->instances[geoinst_idx].geometryIndex ].primitives.size(); } printf("Estimated draw calls: %d\n", draw_calls); return input; }