edits ontop of Alex's patch from r41292.
[blender.git] / source / blender / blenkernel / intern / object.c
index 862d583..819a982 100644 (file)
@@ -1,8 +1,4 @@
-/* object.c
- *
- * 
- * $Id$
- *
+/*
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
@@ -54,6 +50,7 @@
 #include "DNA_scene_types.h"
 #include "DNA_screen_types.h"
 #include "DNA_sequence_types.h"
+#include "DNA_sound_types.h"
 #include "DNA_space_types.h"
 #include "DNA_view3d_types.h"
 #include "DNA_world_types.h"
@@ -96,6 +93,7 @@
 #include "BKE_sca.h"
 #include "BKE_scene.h"
 #include "BKE_sequencer.h"
+#include "BKE_speaker.h"
 #include "BKE_softbody.h"
 #include "BKE_material.h"
 
@@ -233,6 +231,17 @@ void object_free_display(Object *ob)
        freedisplist(&ob->disp);
 }
 
+void free_sculptsession_deformMats(SculptSession *ss)
+{
+       if(ss->orig_cos) MEM_freeN(ss->orig_cos);
+       if(ss->deform_cos) MEM_freeN(ss->deform_cos);
+       if(ss->deform_imats) MEM_freeN(ss->deform_imats);
+
+       ss->orig_cos = NULL;
+       ss->deform_cos = NULL;
+       ss->deform_imats = NULL;
+}
+
 void free_sculptsession(Object *ob)
 {
        if(ob && ob->sculpt) {
@@ -263,6 +272,7 @@ void free_sculptsession(Object *ob)
        }
 }
 
+
 /* do not free object itself */
 void free_object(Object *ob)
 {
@@ -404,7 +414,7 @@ void unlink_object(Object *ob)
                                                for (ct= targets.first; ct; ct= ct->next) {
                                                        if (ct->tar == ob) {
                                                                ct->tar = NULL;
-                                                               strcpy(ct->subtarget, "");
+                                                               ct->subtarget[0]= '\0';
                                                                obt->recalc |= OB_RECALC_DATA;
                                                        }
                                                }
@@ -434,7 +444,7 @@ void unlink_object(Object *ob)
                                for (ct= targets.first; ct; ct= ct->next) {
                                        if (ct->tar == ob) {
                                                ct->tar = NULL;
-                                               strcpy(ct->subtarget, "");
+                                               ct->subtarget[0]= '\0';
                                                obt->recalc |= OB_RECALC_DATA;
                                        }
                                }
@@ -746,9 +756,7 @@ void make_local_camera(Camera *cam)
        
        if(cam->id.lib==NULL) return;
        if(cam->id.us==1) {
-               cam->id.lib= NULL;
-               cam->id.flag= LIB_LOCAL;
-               new_id(&bmain->camera, (ID *)cam, NULL);
+               id_clear_lib_data(bmain, (ID *)cam);
                return;
        }
        
@@ -760,9 +768,7 @@ void make_local_camera(Camera *cam)
        }
        
        if(local && lib==0) {
-               cam->id.lib= NULL;
-               cam->id.flag= LIB_LOCAL;
-               new_id(&bmain->camera, (ID *)cam, NULL);
+               id_clear_lib_data(bmain, (ID *)cam);
        }
        else if(local && lib) {
                Camera *camn= copy_camera(cam);
@@ -911,9 +917,7 @@ void make_local_lamp(Lamp *la)
        
        if(la->id.lib==NULL) return;
        if(la->id.us==1) {
-               la->id.lib= NULL;
-               la->id.flag= LIB_LOCAL;
-               new_id(&bmain->lamp, (ID *)la, NULL);
+               id_clear_lib_data(bmain, (ID *)la);
                return;
        }
        
@@ -927,9 +931,7 @@ void make_local_lamp(Lamp *la)
        }
        
        if(local && lib==0) {
-               la->id.lib= NULL;
-               la->id.flag= LIB_LOCAL;
-               new_id(&bmain->lamp, (ID *)la, NULL);
+               id_clear_lib_data(bmain, (ID *)la);
        }
        else if(local && lib) {
                lan= copy_lamp(la);
@@ -989,6 +991,7 @@ static void *add_obdata_from_type(int type)
        case OB_LAMP: return add_lamp("Lamp");
        case OB_LATTICE: return add_lattice("Lattice");
        case OB_ARMATURE: return add_armature("Armature");
+       case OB_SPEAKER: return add_speaker("Speaker");
        case OB_EMPTY: return NULL;
        default:
                printf("add_obdata_from_type: Internal error, bad type: %d\n", type);
@@ -1008,6 +1011,7 @@ static const char *get_obdata_defname(int type)
        case OB_LAMP: return "Lamp";
        case OB_LATTICE: return "Lattice";
        case OB_ARMATURE: return "Armature";
+       case OB_SPEAKER: return "Speaker";
        case OB_EMPTY: return "Empty";
        default:
                printf("get_obdata_defname: Internal error, bad type: %d\n", type);
@@ -1051,7 +1055,7 @@ Object *add_only_object(int type, const char *name)
        ob->empty_drawtype= OB_PLAINAXES;
        ob->empty_drawsize= 1.0;
 
-       if(type==OB_CAMERA || type==OB_LAMP) {
+       if(type==OB_CAMERA || type==OB_LAMP || type==OB_SPEAKER) {
                ob->trackflag= OB_NEGZ;
                ob->upflag= OB_POSY;
        }
@@ -1078,6 +1082,7 @@ Object *add_only_object(int type, const char *name)
        ob->state=1;
        /* ob->pad3 == Contact Processing Threshold */
        ob->m_contactProcessingThreshold = 1.;
+       ob->obstacleRad = 1.;
        
        /* NT fluid sim defaults */
        ob->fluidsimFlag = 0;
@@ -1293,6 +1298,37 @@ static void copy_object_pose(Object *obn, Object *ob)
        }
 }
 
+static int object_pose_context(Object *ob)
+{
+       if(     (ob) &&
+               (ob->type == OB_ARMATURE) &&
+               (ob->pose) &&
+               (ob->mode & OB_MODE_POSE)
+       ) {
+               return 1;
+       }
+       else {
+               return 0;
+       }
+}
+
+//Object *object_pose_armature_get(Object *ob)
+Object *object_pose_armature_get(struct Object *ob)
+{
+       if(ob==NULL)
+               return NULL;
+
+       if(object_pose_context(ob))
+               return ob;
+
+       ob= modifiers_isDeformedByArmature(ob);
+
+       if(object_pose_context(ob))
+               return ob;
+
+       return NULL;
+}
+
 static void copy_object_transform(Object *ob_tar, Object *ob_src)
 {
        copy_v3_v3(ob_tar->loc, ob_src->loc);
@@ -1424,9 +1460,8 @@ void make_local_object(Object *ob)
        ob->proxy= ob->proxy_from= NULL;
        
        if(ob->id.us==1) {
-               ob->id.lib= NULL;
-               ob->id.flag= LIB_LOCAL;
-               new_id(&bmain->object, (ID *)ob, NULL);
+               id_clear_lib_data(bmain, (ID *)ob);
+               extern_local_object(ob);
        }
        else {
                for(sce= bmain->scene.first; sce && ELEM(0, lib, local); sce= sce->id.next) {
@@ -1437,9 +1472,8 @@ void make_local_object(Object *ob)
                }
 
                if(local && lib==0) {
-                       ob->id.lib= NULL;
-                       ob->id.flag= LIB_LOCAL;
-                       new_id(&bmain->object, (ID *)ob, NULL);
+                       id_clear_lib_data(bmain, (ID *)ob);
+                       extern_local_object(ob);
                }
                else if(local && lib) {
                        Object *obn= copy_object(ob);
@@ -1462,8 +1496,6 @@ void make_local_object(Object *ob)
                        }
                }
        }
-       
-       extern_local_object(ob);
 }
 
 /*
@@ -1601,7 +1633,7 @@ void object_make_proxy(Object *ob, Object *target, Object *gob)
        if(ob->matbits) MEM_freeN(ob->matbits);
        ob->mat = NULL;
        ob->matbits= NULL;
-       if ((target->totcol) && (target->mat) && ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL)) { //XXX OB_SUPPORT_MATERIAL
+       if ((target->totcol) && (target->mat) && OB_TYPE_SUPPORT_MATERIAL(ob->type)) {
                int i;
                ob->colbits = target->colbits;
                
@@ -2302,7 +2334,7 @@ BoundBox *unit_boundbox(void)
        BoundBox *bb;
        float min[3] = {-1.0f,-1.0f,-1.0f}, max[3] = {-1.0f,-1.0f,-1.0f};
 
-       bb= MEM_callocN(sizeof(BoundBox), "bb");
+       bb= MEM_callocN(sizeof(BoundBox), "OB-BoundBox");
        boundbox_set_from_min_max(bb, min, max);
        
        return bb;
@@ -2754,7 +2786,35 @@ void object_handle_update(Scene *scene, Object *ob)
        }
 }
 
-float give_timeoffset(Object *ob) {
+void object_sculpt_modifiers_changed(Object *ob)
+{
+       SculptSession *ss= ob->sculpt;
+
+       if(!ss->cache) {
+               /* we free pbvh on changes, except during sculpt since it can't deal with
+                  changing PVBH node organization, we hope topology does not change in
+                  the meantime .. weak */
+               if(ss->pbvh) {
+                               BLI_pbvh_free(ss->pbvh);
+                               ss->pbvh= NULL;
+               }
+
+               free_sculptsession_deformMats(ob->sculpt);
+       } else {
+               PBVHNode **nodes;
+               int n, totnode;
+
+               BLI_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
+
+               for(n = 0; n < totnode; n++)
+                       BLI_pbvh_node_mark_update(nodes[n]);
+
+               MEM_freeN(nodes);
+       }
+}
+
+float give_timeoffset(Object *ob)
+{
        if ((ob->ipoflag & OB_OFFS_PARENTADD) && ob->parent) {
                return ob->sf + give_timeoffset(ob->parent);
        } else {
@@ -2762,7 +2822,8 @@ float give_timeoffset(Object *ob) {
        }
 }
 
-int give_obdata_texspace(Object *ob, short **texflag, float **loc, float **size, float **rot) {
+int give_obdata_texspace(Object *ob, short **texflag, float **loc, float **size, float **rot)
+{
        
        if (ob->data==NULL)
                return 0;
@@ -2980,6 +3041,79 @@ void object_camera_matrix(
 
 }
 
+void camera_view_frame_ex(Scene *scene, Camera *camera, float drawsize, const short do_clip, const float scale[3],
+                          float r_asp[2], float r_shift[2], float *r_drawsize, float r_vec[4][3])
+{
+       float facx, facy;
+       float depth;
+
+       /* aspect correcton */
+       if (scene) {
+               float aspx= (float) scene->r.xsch*scene->r.xasp;
+               float aspy= (float) scene->r.ysch*scene->r.yasp;
+
+               if(aspx < aspy) {
+                       r_asp[0]= aspx / aspy;
+                       r_asp[1]= 1.0;
+               }
+               else {
+                       r_asp[0]= 1.0;
+                       r_asp[1]= aspy / aspx;
+               }
+       }
+       else {
+               r_asp[0]= 1.0f;
+               r_asp[1]= 1.0f;
+       }
+
+       if(camera->type==CAM_ORTHO) {
+               facx= 0.5f * camera->ortho_scale * r_asp[0] * scale[0];
+               facy= 0.5f * camera->ortho_scale * r_asp[1] * scale[1];
+               r_shift[0]= camera->shiftx * camera->ortho_scale * scale[0];
+               r_shift[1]= camera->shifty * camera->ortho_scale * scale[1];
+               depth= do_clip ? -((camera->clipsta * scale[2]) + 0.1f) : - drawsize * camera->ortho_scale * scale[2];
+
+               *r_drawsize= 0.5f * camera->ortho_scale;
+       }
+       else {
+               /* that way it's always visible - clipsta+0.1 */
+               float fac;
+               *r_drawsize= drawsize / ((scale[0] + scale[1] + scale[2]) / 3.0f);
+
+               if(do_clip) {
+                       /* fixed depth, variable size (avoids exceeding clipping range) */
+                       depth = -(camera->clipsta + 0.1f);
+                       fac = depth / (camera->lens/-16.0f * scale[2]);
+               }
+               else {
+                       /* fixed size, variable depth (stays a reasonable size in the 3D view) */
+                       depth= *r_drawsize * camera->lens/-16.0f * scale[2];
+                       fac= *r_drawsize;
+               }
+
+               facx= fac * r_asp[0] * scale[0];
+               facy= fac * r_asp[1] * scale[1];
+               r_shift[0]= camera->shiftx*fac*2 * scale[0];
+               r_shift[1]= camera->shifty*fac*2 * scale[1];
+       }
+
+       r_vec[0][0]= r_shift[0] + facx; r_vec[0][1]= r_shift[1] + facy; r_vec[0][2]= depth;
+       r_vec[1][0]= r_shift[0] + facx; r_vec[1][1]= r_shift[1] - facy; r_vec[1][2]= depth;
+       r_vec[2][0]= r_shift[0] - facx; r_vec[2][1]= r_shift[1] - facy; r_vec[2][2]= depth;
+       r_vec[3][0]= r_shift[0] - facx; r_vec[3][1]= r_shift[1] + facy; r_vec[3][2]= depth;
+}
+
+void camera_view_frame(Scene *scene, Camera *camera, float r_vec[4][3])
+{
+       float dummy_asp[2];
+       float dummy_shift[2];
+       float dummy_drawsize;
+       const float dummy_scale[3]= {1.0f, 1.0f, 1.0f};
+
+       camera_view_frame_ex(scene, camera, FALSE, 1.0, dummy_scale,
+                            dummy_asp, dummy_shift, &dummy_drawsize, r_vec);
+}
+
 #if 0
 static int pc_findindex(ListBase *listbase, int index)
 {
@@ -3134,7 +3268,7 @@ int object_is_modified(Scene *scene, Object *ob)
        int flag= 0;
 
        if(ob_get_key(ob)) {
-               flag |= eModifierMode_Render | eModifierMode_Render;
+               flag |= eModifierMode_Render;
        }
        else {
                ModifierData *md;