Skinned mesh code part 3 - mesh and controller memory pools

posted in Gamedev info
Published June 30, 2015
Advertisement
Skinned mesh code part 3 - mesh and controller memory pools
// ----------------------------------- skinned mesh pool API ------------------------------------------- struct skinned_mesh_master{LPD3DXFRAME root;ID3DXAnimationController *controller;LPD3DXFRAME headbone;D3DXMESHCONTAINER_DERIVED *body, *bra, *loincloth;}; #define MAX_SKINNED_MESHES 100 skinned_mesh_master skinned_mesh_pool[MAX_SKINNED_MESHES]; int num_skinned_meshes=0; void load_skinned_mesh_master(char *fname){int result;LPD3DXMESHCONTAINER p;result=load_skinned_mesh(fname,&skinned_mesh_pool[num_skinned_meshes].controller,&skinned_mesh_pool[num_skinned_meshes].root);switch (result) { case 1: msg3("load skinned mesh master: invalid call!"); break; case 2: msg3("load skinned mesh master: out of memory!"); break; case 3: msg3("load skinned mesh master: setup bone matrix pointers error"); break; }skinned_mesh_pool[num_skinned_meshes].headbone=get_bone(skinned_mesh_pool[num_skinned_meshes].root,"metarig_head");if (skinned_mesh_pool[num_skinned_meshes].headbone == NULL) { msg3("load skinned mesh master: metarig_head not found in root!"); }// get pointer to body mesh to demonstrate interchangeable pooled textures for each mesh of a skinned meshp=get_mesh_container2(skinned_mesh_pool[num_skinned_meshes].root,"body");if (p == NULL) { msg3("load skinned mesh master: body frame meshcontainer is null!"); }skinned_mesh_pool[num_skinned_meshes].body=(D3DXMESHCONTAINER_DERIVED*)p;p=get_mesh_container2(skinned_mesh_pool[num_skinned_meshes].root,"bra");if (p == NULL) {// its a male skinned mesh, no bra mesh. skinned_mesh_pool[num_skinned_meshes].bra=NULL; }else { skinned_mesh_pool[num_skinned_meshes].bra=(D3DXMESHCONTAINER_DERIVED*)p; }p=get_mesh_container2(skinned_mesh_pool[num_skinned_meshes].root,"loincloth");if (p == NULL) { msg3("load skinned mesh master: loincloth frame meshcontainer is null!"); }skinned_mesh_pool[num_skinned_meshes].loincloth=(D3DXMESHCONTAINER_DERIVED*)p;num_skinned_meshes++;} void load_all_skinned_meshes(){FILE *f;int quit;char s[100];if (filefound("skinned_meshes.dat") == 0) { msg3("error: meshes.dat not found!"); return; }num_skinned_meshes=0;f=infile("skinned_meshes.dat");quit=0;while (!quit) { readfile(f,s); if (strcmp(s,"endfile") == 0) { quit=1; } else if (s[0] == '/') {// do nothing, its a comment } else {// c msg2 s load_skinned_mesh_master(s); } }fclose(f);} void unload_all_skinned_meshes(){int i;for (i=0; i.controller,skinned_mesh_pool.root); }} // -------------------------------------- controller pool API ------------------------------------------ struct skinned_mesh_instance{// curani: -1 = noneint active,mesh_index,curani;ID3DXAnimationController *controller;}; #define MAX_SKINNED_MESH_INSTANCES 200 skinned_mesh_instance controller_pool[MAX_SKINNED_MESH_INSTANCES]; void init_controller_pool(){int i;for (i=0; i.active=0; }} int get_free_controller(){int i;for (i=0; i.active == 0) { return(i); } }return(-1);} // activates a controller. // mesh_index is the index of the skinned mesh the controller should use.// returns the index of the controller. int activate_controller(int mesh_index){int j;j=get_free_controller();if (j == -1) { msg3("error: all animation controllers are in use!"); return(-1); }controller_pool[j].active=1;clone_controller(skinned_mesh_pool[mesh_index].controller,&controller_pool[j].controller);controller_pool[j].mesh_index=mesh_index;controller_pool[j].curani=-1;return(j);} void draw_skinned_mesh_instance(int i,double seconds,LPD3DXMATRIX mWorld){// i is the index of the controller in the controller pool.int j;// j is the index of the skinned mesh in the skinned mesh poolj=controller_pool.mesh_index;update_and_draw_skinned_mesh(controller_pool.controller,seconds,skinned_mesh_pool[j].root,mWorld);} void deactivate_controller(int c){// c is controller #if (controller_pool[c].active == 0) { return; }if (controller_pool[c].controller == NULL) { return; }controller_pool[c].controller->Release();controller_pool[c].controller=NULL;controller_pool[c].active=0;} void deactivate_all_controllers(){int i;for (i=0; i.active == 1) { deactivate_controller(i); } }} void controller_pool_setani(int cindex,int aniID){// sets the animation of a controller in the controller pool.// does not set if its already the current ani.// cIndex is the controller index.// aniID is the aniID.if (controller_pool[cindex].curani == aniID) { return; }set_ani(controller_pool[cindex].controller,aniID);controller_pool[cindex].curani=aniID;} // the controller should track which ani its playing// it does, via controller_pool.curani /* usage: // initinit_controller_poolload_all_skinned_meshesactivate_controller// drawdraw_skinned_mesh_instance// shutdowndeactivate_controller -or- deactivate_all_controllersunload_all_skinned_meshes for drawing in caveman:a cm or animal rec will hold the controller index, similar to an aniplayerID.the mWorld for the cm or animal will be used.seconds will usually be 0.066.there will be a draw skinned mesh model routine that will set textures, then call draw_skinned_mesh_instance, thendraw attached stuff (eyes, hair, gear, weapons etc). texture IDs will be stored in the cm rec or the npc rec. */ // -------------------------- end mesh pool and controller pool APIs --------------------------
0 likes 1 comments
Advertisement