struct material_st *mtr_new_material(int num) { struct material_st *tmp; tmp = (struct material_st *)malloc(sizeof(struct material_st) * num); if (tmp == NULL) { perror("Cannot allocate memory for std_material.\n"); exit(EXIT_FAILURE); } copy_material(black_mat, tmp); tmp->id = id++; tmp->name = NULL; return tmp; }
int id_copy(ID *id, ID **newid, int test) { if(!test) *newid= NULL; /* conventions: * - make shallow copy, only this ID block * - id.us of the new ID is set to 1 */ switch(GS(id->name)) { case ID_SCE: return 0; /* can't be copied from here */ case ID_LI: return 0; /* can't be copied from here */ case ID_OB: if(!test) *newid= (ID*)copy_object((Object*)id); return 1; case ID_ME: if(!test) *newid= (ID*)copy_mesh((Mesh*)id); return 1; case ID_CU: if(!test) *newid= (ID*)copy_curve((Curve*)id); return 1; case ID_MB: if(!test) *newid= (ID*)copy_mball((MetaBall*)id); return 1; case ID_MA: if(!test) *newid= (ID*)copy_material((Material*)id); return 1; case ID_TE: if(!test) *newid= (ID*)copy_texture((Tex*)id); return 1; case ID_IM: if(!test) *newid= (ID*)copy_image((Image*)id); return 1; case ID_LT: if(!test) *newid= (ID*)copy_lattice((Lattice*)id); return 1; case ID_LA: if(!test) *newid= (ID*)copy_lamp((Lamp*)id); return 1; case ID_SPK: if(!test) *newid= (ID*)copy_speaker((Speaker*)id); return 1; case ID_CA: if(!test) *newid= (ID*)copy_camera((Camera*)id); return 1; case ID_IP: return 0; /* deprecated */ case ID_KE: if(!test) *newid= (ID*)copy_key((Key*)id); return 1; case ID_WO: if(!test) *newid= (ID*)copy_world((World*)id); return 1; case ID_SCR: return 0; /* can't be copied from here */ case ID_VF: return 0; /* not implemented */ case ID_TXT: if(!test) *newid= (ID*)copy_text((Text*)id); return 1; case ID_SCRIPT: return 0; /* deprecated */ case ID_SO: return 0; /* not implemented */ case ID_GR: if(!test) *newid= (ID*)copy_group((Group*)id); return 1; case ID_AR: if(!test) *newid= (ID*)copy_armature((bArmature*)id); return 1; case ID_AC: if(!test) *newid= (ID*)copy_action((bAction*)id); return 1; case ID_NT: if(!test) *newid= (ID*)ntreeCopyTree((bNodeTree*)id); return 1; case ID_BR: if(!test) *newid= (ID*)copy_brush((Brush*)id); return 1; case ID_PA: if(!test) *newid= (ID*)psys_copy_settings((ParticleSettings*)id); return 1; case ID_WM: return 0; /* can't be copied from here */ case ID_GD: return 0; /* not implemented */ } return 0; }
/* This function merges a mesh from the current scene into another main * it does not convert */ RAS_MeshObject *KX_BlenderSceneConverter::ConvertMeshSpecial(KX_Scene* kx_scene, Main *maggie, const char *name) { /* Find a mesh in the current main */ ID *me= static_cast<ID *>(BLI_findstring(&m_maggie->mesh, name, offsetof(ID, name) + 2)); if(me==NULL) { printf("Could not be found \"%s\"\n", name); return NULL; } /* Watch this!, if its used in the original scene can cause big troubles */ if(me->us > 0) { printf("Mesh has a user \"%s\"\n", name); me = (ID*)copy_mesh((Mesh*)me); me->us--; } BLI_remlink(&m_maggie->mesh, me); /* even if we made the copy it needs to be removed */ BLI_addtail(&maggie->mesh, me); /* Must copy the materials this uses else we cant free them */ { Mesh *mesh= (Mesh *)me; /* ensure all materials are tagged */ for(int i=0; i<mesh->totcol; i++) if(mesh->mat[i]) mesh->mat[i]->id.flag &= ~LIB_DOIT; for(int i=0; i<mesh->totcol; i++) { Material *mat_old= mesh->mat[i]; /* if its tagged its a replaced material */ if(mat_old && (mat_old->id.flag & LIB_DOIT)==0) { Material *mat_old= mesh->mat[i]; Material *mat_new= copy_material( mat_old ); mat_new->id.flag |= LIB_DOIT; mat_old->id.us--; BLI_remlink(&m_maggie->mat, mat_new); BLI_addtail(&maggie->mat, mat_new); mesh->mat[i]= mat_new; /* the same material may be used twice */ for(int j=i+1; j<mesh->totcol; j++) { if(mesh->mat[j]==mat_old) { mesh->mat[j]= mat_new; mat_new->id.us++; mat_old->id.us--; } } } } } RAS_MeshObject *meshobj = BL_ConvertMesh((Mesh *)me, NULL, kx_scene, this); kx_scene->GetLogicManager()->RegisterMeshName(meshobj->GetName(),meshobj); m_map_mesh_to_gamemesh.clear(); /* This is at runtime so no need to keep this, BL_ConvertMesh adds */ return meshobj; }
void mtr_init(void) { num_std_materials = MATERIAL_MAX; color_init(); std_material = (struct material_st *)malloc(sizeof(struct material_st) * num_std_materials); if (std_material == NULL) { perror("Cannot allocate memory for std_material.\n"); exit(EXIT_FAILURE); } int mat = 0; GLfloat color[4]; mat = MATERIAL_NONE; mtr_get_std_color(COLOR_NONE, color); copy_material(black_mat, &(std_material[mat])); copy_vec4(color, std_material[mat].matblk.ambient); copy_vec4(color, std_material[mat].matblk.diffuse); std_material[mat].id = mat; std_material[mat].name = strdup("none"); mat = MATERIAL_WHITE; mtr_get_std_color(COLOR_WHITE, color); copy_material(black_mat, &std_material[mat]); copy_vec4(color, std_material[mat].matblk.ambient); copy_vec4(color, std_material[mat].matblk.diffuse); std_material[mat].id = mat; std_material[mat].name = strdup("white"); mat = MATERIAL_BLACK; mtr_get_std_color(COLOR_BLACK, color); copy_material(black_mat, &std_material[mat]); copy_vec4(color, std_material[mat].matblk.ambient); copy_vec4(color, std_material[mat].matblk.diffuse); std_material[mat].id = mat; std_material[mat].name = strdup("black"); mat = MATERIAL_RED; mtr_get_std_color(COLOR_RED, color); copy_material(black_mat, &std_material[mat]); copy_vec4(color, std_material[mat].matblk.ambient); copy_vec4(color, std_material[mat].matblk.diffuse); std_material[mat].id = mat; std_material[mat].name = strdup("red"); mat = MATERIAL_BLUE; mtr_get_std_color(COLOR_BLUE, color); copy_material(black_mat, &std_material[mat]); copy_vec4(color, std_material[mat].matblk.ambient); copy_vec4(color, std_material[mat].matblk.diffuse); std_material[mat].id = mat; std_material[mat].name = strdup("blue"); mat = MATERIAL_GREEN; mtr_get_std_color(COLOR_GREEN, color); copy_material(black_mat, &std_material[mat]); copy_vec4(color, std_material[mat].matblk.ambient); copy_vec4(color, std_material[mat].matblk.diffuse); std_material[mat].id = mat; std_material[mat].name = strdup("green"); mat = MATERIAL_YELLOW; mtr_get_std_color(COLOR_YELLOW, color); copy_material(black_mat, &std_material[mat]); copy_vec4(color, std_material[mat].matblk.ambient); copy_vec4(color, std_material[mat].matblk.diffuse); std_material[mat].id = mat; std_material[mat].name = strdup("yellow"); mat = MATERIAL_MAGENTA; mtr_get_std_color(COLOR_MAGENTA, color); copy_material(black_mat, &std_material[mat]); copy_vec4(color, std_material[mat].matblk.ambient); copy_vec4(color, std_material[mat].matblk.diffuse); std_material[mat].id = mat; std_material[mat].name = strdup("magenta"); mat = MATERIAL_CYAN; mtr_get_std_color(COLOR_CYAN, color); copy_material(black_mat, &std_material[mat]); copy_vec4(color, std_material[mat].matblk.ambient); copy_vec4(color, std_material[mat].matblk.diffuse); std_material[mat].id = mat; std_material[mat].name = strdup("cyan"); mat = MATERIAL_1000; mtr_get_std_color(COLOR_1000, color); copy_material(black_mat, &std_material[mat]); copy_vec4(color, std_material[mat].matblk.ambient); copy_vec4(color, std_material[mat].matblk.diffuse); std_material[mat].id = mat; std_material[mat].name = strdup("mat1000"); mat = MATERIAL_0100; mtr_get_std_color(COLOR_0100, color); copy_material(black_mat, &std_material[mat]); copy_vec4(color, std_material[mat].matblk.ambient); copy_vec4(color, std_material[mat].matblk.diffuse); std_material[mat].id = mat; std_material[mat].name = strdup("mat0100"); mat = MATERIAL_0010; mtr_get_std_color(COLOR_0010, color); copy_material(black_mat, &std_material[mat]); copy_vec4(color, std_material[mat].matblk.ambient); copy_vec4(color, std_material[mat].matblk.diffuse); std_material[mat].id = mat; std_material[mat].name = strdup("mat0010"); mat = MATERIAL_0001; mtr_get_std_color(COLOR_0001, color); copy_material(black_mat, &std_material[mat]); copy_vec4(color, std_material[mat].matblk.ambient); copy_vec4(color, std_material[mat].matblk.diffuse); std_material[mat].id = mat; std_material[mat].name = strdup("mat0001"); id = MATERIAL_MAX; }
void make_local_material(Material *ma) { Main *bmain= G.main; Object *ob; Mesh *me; Curve *cu; MetaBall *mb; Material *man; int a, local=0, lib=0; /* - only lib users: do nothing * - only local users: set flag * - mixed: make copy */ if(ma->id.lib==NULL) return; if(ma->id.us==1) { ma->id.lib= NULL; ma->id.flag= LIB_LOCAL; new_id(&bmain->mat, (ID *)ma, NULL); extern_local_material(ma); return; } /* test objects */ ob= bmain->object.first; while(ob) { if(ob->mat) { for(a=0; a<ob->totcol; a++) { if(ob->mat[a]==ma) { if(ob->id.lib) lib= 1; else local= 1; } } } ob= ob->id.next; } /* test meshes */ me= bmain->mesh.first; while(me) { if(me->mat) { for(a=0; a<me->totcol; a++) { if(me->mat[a]==ma) { if(me->id.lib) lib= 1; else local= 1; } } } me= me->id.next; } /* test curves */ cu= bmain->curve.first; while(cu) { if(cu->mat) { for(a=0; a<cu->totcol; a++) { if(cu->mat[a]==ma) { if(cu->id.lib) lib= 1; else local= 1; } } } cu= cu->id.next; } /* test mballs */ mb= bmain->mball.first; while(mb) { if(mb->mat) { for(a=0; a<mb->totcol; a++) { if(mb->mat[a]==ma) { if(mb->id.lib) lib= 1; else local= 1; } } } mb= mb->id.next; } if(local && lib==0) { ma->id.lib= NULL; ma->id.flag= LIB_LOCAL; new_id(&bmain->mat, (ID *)ma, NULL); extern_local_material(ma); } else if(local && lib) { man= copy_material(ma); man->id.us= 0; /* do objects */ ob= bmain->object.first; while(ob) { if(ob->mat) { for(a=0; a<ob->totcol; a++) { if(ob->mat[a]==ma) { if(ob->id.lib==NULL) { ob->mat[a]= man; man->id.us++; ma->id.us--; } } } } ob= ob->id.next; } /* do meshes */ me= bmain->mesh.first; while(me) { if(me->mat) { for(a=0; a<me->totcol; a++) { if(me->mat[a]==ma) { if(me->id.lib==NULL) { me->mat[a]= man; man->id.us++; ma->id.us--; } } } } me= me->id.next; } /* do curves */ cu= bmain->curve.first; while(cu) { if(cu->mat) { for(a=0; a<cu->totcol; a++) { if(cu->mat[a]==ma) { if(cu->id.lib==NULL) { cu->mat[a]= man; man->id.us++; ma->id.us--; } } } } cu= cu->id.next; } /* do mballs */ mb= bmain->mball.first; while(mb) { if(mb->mat) { for(a=0; a<mb->totcol; a++) { if(mb->mat[a]==ma) { if(mb->id.lib==NULL) { mb->mat[a]= man; man->id.us++; ma->id.us--; } } } } mb= mb->id.next; } } }