static void build_image_lookup(Main *bmain, Object *ob, BakeImages *bake_images) { const int tot_mat = ob->totcol; int i, j; int tot_images = 0; /* error handling and tag (in case multiple materials share the same image) */ BKE_main_id_tag_idcode(bmain, ID_IM, false); for (i = 0; i < tot_mat; i++) { Image *image; ED_object_get_active_image(ob, i + 1, &image, NULL, NULL, NULL); if ((image->id.tag & LIB_TAG_DOIT)) { for (j = 0; j < i; j++) { if (bake_images->data[j].image == image) { bake_images->lookup[i] = j; break; } } } else { bake_images->lookup[i] = tot_images; bake_images->data[tot_images].image = image; image->id.tag |= LIB_TAG_DOIT; tot_images++; } } bake_images->size = tot_images; }
static bool build_image_lookup(Main *bmain, Object *ob, BakeImages *bake_images, ReportList *reports) { const int tot_mat = ob->totcol; int i, j; int tot_images = 0; /* error handling and tag (in case multiple materials share the same image) */ BKE_main_id_tag_idcode(bmain, ID_IM, false); for (i = 0; i < tot_mat; i++) { Image *image; ED_object_get_active_image(ob, i + 1, &image, NULL, NULL); if (!image) { if (ob->mat[i]) { BKE_reportf(reports, RPT_ERROR, "No active image found in material %d (%s)", i, ob->mat[i]->id.name + 2); } else if (((Mesh *) ob->data)->mat[i]) { BKE_reportf(reports, RPT_ERROR, "No active image found in material %d (%s)", i, ((Mesh *) ob->data)->mat[i]->id.name + 2); } else { BKE_reportf(reports, RPT_ERROR, "No active image found in material %d", i); } return false; } if ((image->id.flag & LIB_DOIT)) { for (j = 0; j < i; j++) { if (bake_images->data[j].image == image) { bake_images->lookup[i] = j; break; } } } else { bake_images->lookup[i] = tot_images; bake_images->data[tot_images].image = image; image->id.flag |= LIB_DOIT; tot_images++; } } bake_images->size = tot_images; return true; }
/* if all is good tag image and return true */ static bool bake_object_check(Object *ob, ReportList *reports) { Image *image; void *lock; int i; if (ob->type != OB_MESH) { BKE_reportf(reports, RPT_ERROR, "Object \"%s\" is not a mesh", ob->id.name + 2); return false; } else { Mesh *me = (Mesh *)ob->data; if (CustomData_get_active_layer_index(&me->ldata, CD_MLOOPUV) == -1) { BKE_reportf(reports, RPT_ERROR, "No active UV layer found in the object \"%s\"", ob->id.name + 2); return false; } } for (i = 0; i < ob->totcol; i++) { bNodeTree *ntree = NULL; bNode *node = NULL; ED_object_get_active_image(ob, i + 1, &image, NULL, &node, &ntree); if (image) { ImBuf *ibuf; if (node) { if (BKE_node_is_connected_to_output(ntree, node)) { /* we don't return false since this may be a false positive * this can't be RPT_ERROR though, otherwise it prevents * multiple highpoly objects to be baked at once */ BKE_reportf(reports, RPT_INFO, "Circular dependency for image \"%s\" from object \"%s\"", image->id.name + 2, ob->id.name + 2); } } ibuf = BKE_image_acquire_ibuf(image, NULL, &lock); if (ibuf) { BKE_image_release_ibuf(image, ibuf, lock); } else { BKE_reportf(reports, RPT_ERROR, "Uninitialized image \"%s\" from object \"%s\"", image->id.name + 2, ob->id.name + 2); BKE_image_release_ibuf(image, ibuf, lock); return false; } } else { if (ob->mat[i]) { BKE_reportf(reports, RPT_ERROR, "No active image found in material \"%s\" (%d) for object \"%s\"", ob->mat[i]->id.name + 2, i, ob->id.name + 2); } else if (((Mesh *) ob->data)->mat[i]) { BKE_reportf(reports, RPT_ERROR, "No active image found in material \"%s\" (%d) for object \"%s\"", ((Mesh *) ob->data)->mat[i]->id.name + 2, i, ob->id.name + 2); } else { BKE_reportf(reports, RPT_ERROR, "No active image found in material (%d) for object \"%s\"", i, ob->id.name + 2); } return false; } image->id.tag |= LIB_TAG_DOIT; } return true; }
static void tex_mat_set_texture_cb(void *userData, int mat_nr, void *attribs) { /* texture draw mode without GLSL */ TexMatCallback *data = (TexMatCallback *)userData; GPUVertexAttribs *gattribs = attribs; Image *ima; ImageUser *iuser; bNode *node; int texture_set = 0; /* draw image texture if we find one */ if (ED_object_get_active_image(data->ob, mat_nr, &ima, &iuser, &node)) { /* get openl texture */ int mipmap = 1; int bindcode = (ima) ? GPU_verify_image(ima, iuser, 0, 0, mipmap, false) : 0; float zero[4] = {0.0f, 0.0f, 0.0f, 0.0f}; if (bindcode) { NodeTexBase *texbase = node->storage; /* disable existing material */ GPU_disable_material(); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, zero); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, zero); glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 0); /* bind texture */ glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); glEnable(GL_COLOR_MATERIAL); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, ima->bindcode); glColor3f(1.0f, 1.0f, 1.0f); glMatrixMode(GL_TEXTURE); glLoadMatrixf(texbase->tex_mapping.mat); glMatrixMode(GL_MODELVIEW); /* use active UV texture layer */ memset(gattribs, 0, sizeof(*gattribs)); gattribs->layer[0].type = CD_MTFACE; gattribs->layer[0].name[0] = '\0'; gattribs->layer[0].gltexco = 1; gattribs->totlayer = 1; texture_set = 1; } } if (!texture_set) { glMatrixMode(GL_TEXTURE); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); /* disable texture */ glDisable(GL_TEXTURE_2D); glDisable(GL_COLOR_MATERIAL); /* draw single color */ GPU_enable_material(mat_nr, attribs); } }
static void tex_mat_set_texture_cb(void *userData, int mat_nr, void *attribs) { /* texture draw mode without GLSL */ TexMatCallback *data = (TexMatCallback *)userData; GPUVertexAttribs *gattribs = attribs; Image *ima; ImageUser *iuser; bNode *node; /* draw image texture if we find one */ if (ED_object_get_active_image(data->ob, mat_nr, &ima, &iuser, &node, NULL)) { /* get openl texture */ int mipmap = 1; int bindcode = (ima) ? GPU_verify_image(ima, iuser, GL_TEXTURE_2D, 0, 0, mipmap, false) : 0; if (bindcode) { NodeTexBase *texbase = node->storage; /* disable existing material */ GPU_object_material_unbind(); /* bind texture */ glBindTexture(GL_TEXTURE_2D, ima->bindcode[TEXTARGET_TEXTURE_2D]); glMatrixMode(GL_TEXTURE); glLoadMatrixf(texbase->tex_mapping.mat); glMatrixMode(GL_MODELVIEW); /* use active UV texture layer */ memset(gattribs, 0, sizeof(*gattribs)); gattribs->layer[0].type = CD_MTFACE; gattribs->layer[0].name[0] = '\0'; gattribs->layer[0].gltexco = 1; gattribs->totlayer = 1; /* bind material */ float diffuse[3] = {1.0f, 1.0f, 1.0f}; int options = GPU_SHADER_TEXTURE_2D; if (!data->shadeless) options |= GPU_SHADER_LIGHTING; if (data->two_sided_lighting) options |= GPU_SHADER_TWO_SIDED; GPU_basic_shader_colors(diffuse, NULL, 0, 1.0f); GPU_basic_shader_bind(options); return; } } /* disable texture material */ GPU_basic_shader_bind(GPU_SHADER_USE_COLOR); if (data->shadeless) { glColor3f(1.0f, 1.0f, 1.0f); memset(gattribs, 0, sizeof(*gattribs)); } else { glMatrixMode(GL_TEXTURE); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); /* enable solid material */ GPU_object_material_bind(mat_nr, attribs); } }