Removed the internal_select parameter from ntreeCopyTree. This was used just in one...
[blender-staging.git] / source / blender / blenkernel / intern / texture.c
index 33dfcfca76ea63578e6781c9823068460e19c8aa..57c629d5544372e552ff30eb8eb7356f15dc6374 100644 (file)
@@ -17,7 +17,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  * All rights reserved.
 
 #include "PIL_dynlib.h"
 
-
-
 #include "BLI_blenlib.h"
-#include "BLI_arithb.h"
-#include "BLI_rand.h"
+#include "BLI_math.h"
 #include "BLI_kdopbvh.h"
+#include "BLI_utildefines.h"
 
-#include "DNA_texture_types.h"
 #include "DNA_key_types.h"
 #include "DNA_object_types.h"
 #include "DNA_lamp_types.h"
 #include "DNA_material_types.h"
-#include "DNA_image_types.h"
 #include "DNA_world_types.h"
 #include "DNA_brush_types.h"
 #include "DNA_node_types.h"
 #include "DNA_color_types.h"
-#include "DNA_scene_types.h"
 
-#include "IMB_imbuf_types.h"
 #include "IMB_imbuf.h"
 
 #include "BKE_plugin_types.h"
-
 #include "BKE_utildefines.h"
-
 #include "BKE_global.h"
 #include "BKE_main.h"
 
@@ -73,7 +65,6 @@
 #include "BKE_texture.h"
 #include "BKE_key.h"
 #include "BKE_icons.h"
-#include "BKE_brush.h"
 #include "BKE_node.h"
 #include "BKE_animsys.h"
 
@@ -177,8 +168,8 @@ PluginTex *add_plugin_tex(char *str)
        open_plugin_tex(pit);
        
        if(pit->doit==0) {
-               if(pit->handle==0); //XXX error("no plugin: %s", str);
-               else ; //XXX error("in plugin: %s", str);
+               if(pit->handle==0) {;} //XXX error("no plugin: %s", str);
+               else {;} //XXX error("in plugin: %s", str);
                MEM_freeN(pit);
                return NULL;
        }
@@ -215,7 +206,7 @@ TexMapping *add_mapping(void)
        
        texmap->size[0]= texmap->size[1]= texmap->size[2]= 1.0f;
        texmap->max[0]= texmap->max[1]= texmap->max[2]= 1.0f;
-       Mat4One(texmap->mat);
+       unit_m4(texmap->mat);
        
        return texmap;
 }
@@ -224,16 +215,16 @@ void init_mapping(TexMapping *texmap)
 {
        float eul[3], smat[3][3], rmat[3][3], mat[3][3];
        
-       SizeToMat3(texmap->size, smat);
+       size_to_mat3( smat,texmap->size);
        
        eul[0]= (M_PI/180.0f)*texmap->rot[0];
        eul[1]= (M_PI/180.0f)*texmap->rot[1];
        eul[2]= (M_PI/180.0f)*texmap->rot[2];
-       EulToMat3(eul, rmat);
+       eul_to_mat3( rmat,eul);
        
-       Mat3MulMat3(mat, rmat, smat);
+       mul_m3_m3m3(mat, rmat, smat);
        
-       Mat4CpyMat3(texmap->mat, mat);
+       copy_m4_m3(texmap->mat, mat);
        VECCOPY(texmap->mat[3], texmap->loc);
 
 }
@@ -253,7 +244,7 @@ void init_colorband(ColorBand *coba, int rangetype)
                coba->data[0].b= 0.0;
                coba->data[0].a= 0.0;
                
-               coba->data[1].r= 0.0;
+               coba->data[1].r= 1.0;
                coba->data[1].g= 1.0;
                coba->data[1].b= 1.0;
                coba->data[1].a= 1.0;
@@ -411,6 +402,73 @@ void colorband_table_RGBA(ColorBand *coba, float **array, int *size)
                do_colorband(coba, (float)a/(float)CM_TABLE, &(*array)[a*4]);
 }
 
+int vergcband(const void *a1, const void *a2)
+{
+       const CBData *x1=a1, *x2=a2;
+
+       if( x1->pos > x2->pos ) return 1;
+       else if( x1->pos < x2->pos) return -1;
+       return 0;
+}
+
+CBData *colorband_element_add(struct ColorBand *coba, float position)
+{
+       int a;
+
+       if(coba->tot==MAXCOLORBAND) {
+               return NULL;
+       }
+       else if(coba->tot > 0) {
+               CBData *xnew;
+               float col[4];
+
+               do_colorband(coba, position, col);
+
+               xnew = &coba->data[coba->tot];
+               xnew->pos = position;
+
+               xnew->r = col[0];
+               xnew->g = col[1];
+               xnew->b = col[2];
+               xnew->a = col[3];
+       }
+
+       coba->tot++;
+       coba->cur = coba->tot-1;
+
+       for(a = 0; a < coba->tot; a++)
+               coba->data[a].cur = a;
+
+       qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
+
+       for(a = 0; a < coba->tot; a++) {
+               if(coba->data[a].cur == coba->cur) {
+                       coba->cur = a;
+                       break;
+               }
+       }
+
+       return coba->data + coba->cur;
+}
+
+int colorband_element_remove(struct ColorBand *coba, int index)
+{
+       int a;
+
+       if(coba->tot < 2)
+               return 0;
+
+       if(index < 0 || index >= coba->tot)
+               return 0;
+
+       for(a = index; a < coba->tot; a++) {
+               coba->data[a] = coba->data[a + 1];
+       }
+       if(coba->cur) coba->cur--;
+       coba->tot--;
+       return 1;
+}
+
 /* ******************* TEX ************************ */
 
 void free_texture(Tex *tex)
@@ -420,6 +478,7 @@ void free_texture(Tex *tex)
        if(tex->env) BKE_free_envmap(tex->env);
        if(tex->pd) BKE_free_pointdensity(tex->pd);
        if(tex->vd) BKE_free_voxeldata(tex->vd);
+       BKE_free_animdata((struct ID *)tex);
        BKE_previewimg_free(&tex->preview);
        BKE_icon_delete((struct ID*)tex);
        tex->id.icon_id = 0;
@@ -458,6 +517,7 @@ void default_tex(Tex *tex)
        tex->nabla= 0.025;      // also in do_versions
        tex->bright= 1.0;
        tex->contrast= 1.0;
+       tex->saturation= 1.0;
        tex->filtersize= 1.0;
        tex->rfac= 1.0;
        tex->gfac= 1.0;
@@ -482,10 +542,10 @@ void default_tex(Tex *tex)
        tex->vn_coltype = 0;
 
        if (tex->env) {
-               tex->env->stype=ENV_STATIC;
+               tex->env->stype=ENV_ANIM;
                tex->env->clipsta=0.1;
                tex->env->clipend=100;
-               tex->env->cuberes=100;
+               tex->env->cuberes=600;
                tex->env->depth=0;
        }
 
@@ -512,17 +572,40 @@ void default_tex(Tex *tex)
        tex->iuser.fie_ima= 2;
        tex->iuser.ok= 1;
        tex->iuser.frames= 100;
+       tex->iuser.sfra= 1;
        
        tex->preview = NULL;
 }
 
+void tex_set_type(Tex *tex, int type)
+{
+       switch(type) {
+                       
+               case TEX_VOXELDATA:
+                       if (tex->vd == NULL)
+                               tex->vd = BKE_add_voxeldata();
+                       break;
+               case TEX_POINTDENSITY:
+                       if (tex->pd == NULL)
+                               tex->pd = BKE_add_pointdensity();
+                       break;
+               case TEX_ENVMAP:
+                       if (tex->env == NULL)
+                               tex->env = BKE_add_envmap();
+                       break;
+       }
+       
+       tex->type = type;
+}
+
 /* ------------------------------------------------------------------------- */
 
-Tex *add_texture(char *name)
+Tex *add_texture(const char *name)
 {
+       Main *bmain= G.main;
        Tex *tex;
 
-       tex= alloc_libblock(&G.main->tex, ID_TE, name);
+       tex= alloc_libblock(&bmain->tex, ID_TE, name);
        
        default_tex(tex);
        
@@ -547,7 +630,7 @@ void default_mtex(MTex *mtex)
        mtex->size[1]= 1.0;
        mtex->size[2]= 1.0;
        mtex->tex= 0;
-       mtex->texflag= MTEX_NEW_BUMP;
+       mtex->texflag= MTEX_3TAP_BUMP | MTEX_BUMP_OBJECTSPACE;
        mtex->colormodel= 0;
        mtex->r= 1.0;
        mtex->g= 0.0;
@@ -606,6 +689,49 @@ MTex *add_mtex()
        return mtex;
 }
 
+/* slot -1 for first free ID */
+MTex *add_mtex_id(ID *id, int slot)
+{
+       MTex **mtex_ar;
+       short act;
+
+       give_active_mtex(id, &mtex_ar, &act);
+
+       if(mtex_ar==NULL) {
+               return NULL;
+       }
+       
+       if(slot==-1) {
+               /* find first free */
+               int i;          
+               for (i= 0; i < MAX_MTEX; i++) {
+                       if (!mtex_ar[i]) {
+                               slot= i;
+                               break;
+                       }
+               }
+               if(slot == -1) {
+                       return NULL;
+               }
+       }
+       else {
+               /* make sure slot is valid */
+               if(slot < 0 || slot >= MAX_MTEX) {
+                       return NULL;
+               }
+       }
+
+       if (mtex_ar[slot]) {
+               id_us_min((ID *)mtex_ar[slot]->tex);
+               MEM_freeN(mtex_ar[slot]);
+               mtex_ar[slot]= NULL;
+       }
+
+       mtex_ar[slot]= add_mtex();
+
+       return mtex_ar[slot];
+}
+
 /* ------------------------------------------------------------------------- */
 
 Tex *copy_texture(Tex *tex)
@@ -627,12 +753,14 @@ Tex *copy_texture(Tex *tex)
        
        if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
        if(texn->env) texn->env= BKE_copy_envmap(texn->env);
+       if(texn->pd) texn->pd= MEM_dupallocN(texn->pd);
+       if(texn->vd) texn->vd= MEM_dupallocN(texn->vd);
        
        if(tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
 
        if(tex->nodetree) {
                ntreeEndExecTree(tex->nodetree);
-               texn->nodetree= ntreeCopyTree(tex->nodetree, 0); /* 0 == full new tree */
+               texn->nodetree= ntreeCopyTree(tex->nodetree); /* 0 == full new tree */
        }
        
        return texn;
@@ -642,6 +770,7 @@ Tex *copy_texture(Tex *tex)
 
 void make_local_texture(Tex *tex)
 {
+       Main *bmain= G.main;
        Tex *texn;
        Material *ma;
        World *wrld;
@@ -650,9 +779,9 @@ void make_local_texture(Tex *tex)
        int a, local=0, lib=0;
 
        /* - only lib users: do nothing
-           * - only local users: set flag
-           * - mixed: make copy
-           */
+               * - only local users: set flag
+               * - mixed: make copy
+               */
        
        if(tex->id.lib==0) return;
 
@@ -671,7 +800,7 @@ void make_local_texture(Tex *tex)
                return;
        }
        
-       ma= G.main->mat.first;
+       ma= bmain->mat.first;
        while(ma) {
                for(a=0; a<MAX_MTEX; a++) {
                        if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
@@ -681,7 +810,7 @@ void make_local_texture(Tex *tex)
                }
                ma= ma->id.next;
        }
-       la= G.main->lamp.first;
+       la= bmain->lamp.first;
        while(la) {
                for(a=0; a<MAX_MTEX; a++) {
                        if(la->mtex[a] && la->mtex[a]->tex==tex) {
@@ -691,7 +820,7 @@ void make_local_texture(Tex *tex)
                }
                la= la->id.next;
        }
-       wrld= G.main->world.first;
+       wrld= bmain->world.first;
        while(wrld) {
                for(a=0; a<MAX_MTEX; a++) {
                        if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
@@ -701,13 +830,11 @@ void make_local_texture(Tex *tex)
                }
                wrld= wrld->id.next;
        }
-       br= G.main->brush.first;
+       br= bmain->brush.first;
        while(br) {
-               for(a=0; a<MAX_MTEX; a++) {
-                       if(br->mtex[a] && br->mtex[a]->tex==tex) {
-                               if(br->id.lib) lib= 1;
-                               else local= 1;
-                       }
+               if(br->mtex.tex==tex) {
+                       if(br->id.lib) lib= 1;
+                       else local= 1;
                }
                br= br->id.next;
        }
@@ -721,7 +848,7 @@ void make_local_texture(Tex *tex)
                texn= copy_texture(tex);
                texn->id.us= 0;
                
-               ma= G.main->mat.first;
+               ma= bmain->mat.first;
                while(ma) {
                        for(a=0; a<MAX_MTEX; a++) {
                                if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
@@ -734,7 +861,7 @@ void make_local_texture(Tex *tex)
                        }
                        ma= ma->id.next;
                }
-               la= G.main->lamp.first;
+               la= bmain->lamp.first;
                while(la) {
                        for(a=0; a<MAX_MTEX; a++) {
                                if(la->mtex[a] && la->mtex[a]->tex==tex) {
@@ -747,7 +874,7 @@ void make_local_texture(Tex *tex)
                        }
                        la= la->id.next;
                }
-               wrld= G.main->world.first;
+               wrld= bmain->world.first;
                while(wrld) {
                        for(a=0; a<MAX_MTEX; a++) {
                                if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
@@ -760,15 +887,13 @@ void make_local_texture(Tex *tex)
                        }
                        wrld= wrld->id.next;
                }
-               br= G.main->brush.first;
+               br= bmain->brush.first;
                while(br) {
-                       for(a=0; a<MAX_MTEX; a++) {
-                               if(br->mtex[a] && br->mtex[a]->tex==tex) {
-                                       if(br->id.lib==0) {
-                                               br->mtex[a]->tex= texn;
-                                               texn->id.us++;
-                                               tex->id.us--;
-                                       }
+                       if(br->mtex.tex==tex) {
+                               if(br->id.lib==0) {
+                                       br->mtex.tex= texn;
+                                       texn->id.us++;
+                                       tex->id.us--;
                                }
                        }
                        br= br->id.next;
@@ -780,6 +905,7 @@ void make_local_texture(Tex *tex)
 
 void autotexname(Tex *tex)
 {
+       Main *bmain= G.main;
        char texstr[20][15]= {"None"  , "Clouds" , "Wood", "Marble", "Magic"  , "Blend",
                "Stucci", "Noise"  , "Image", "Plugin", "EnvMap" , "Musgrave",
                "Voronoi", "DistNoise", "Point Density", "Voxel Data", "", "", "", ""};
@@ -788,7 +914,7 @@ void autotexname(Tex *tex)
        
        if(tex) {
                if(tex->use_nodes) {
-                       new_id(&G.main->tex, (ID *)tex, "Noddy");
+                       new_id(&bmain->tex, (ID *)tex, "Noddy");
                }
                else
                if(tex->type==TEX_IMAGE) {
@@ -798,12 +924,12 @@ void autotexname(Tex *tex)
                                BLI_splitdirstring(di, fi);
                                strcpy(di, "I.");
                                strcat(di, fi);
-                               new_id(&G.main->tex, (ID *)tex, di);
+                               new_id(&bmain->tex, (ID *)tex, di);
                        }
-                       else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
+                       else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
                }
-               else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&G.main->tex, (ID *)tex, tex->plugin->pname);
-               else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
+               else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&bmain->tex, (ID *)tex, tex->plugin->pname);
+               else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
        }
 }
 
@@ -862,6 +988,14 @@ void set_current_lamp_texture(Lamp *la, Tex *newtex)
        }
 }
 
+bNode *give_current_material_texture_node(Material *ma)
+{
+       if(ma && ma->use_nodes && ma->nodetree)
+               return nodeGetActiveID(ma->nodetree, ID_TE);
+       
+       return NULL;
+}
+
 Tex *give_current_material_texture(Material *ma)
 {
        MTex *mtex= NULL;
@@ -869,6 +1003,9 @@ Tex *give_current_material_texture(Material *ma)
        bNode *node;
        
        if(ma && ma->use_nodes && ma->nodetree) {
+               /* first check texture, then material, this works together
+                  with a hack that clears the active ID flag for textures on
+                  making a material node active */
                node= nodeGetActiveID(ma->nodetree, ID_TE);
 
                if(node) {
@@ -877,10 +1014,17 @@ Tex *give_current_material_texture(Material *ma)
                }
                else {
                        node= nodeGetActiveID(ma->nodetree, ID_MA);
-                       if(node)
+                       if(node) {
                                ma= (Material*)node->id;
+                               if(ma) {
+                                       mtex= ma->mtex[(int)(ma->texact)];
+                                       if(mtex) tex= mtex->tex;
+                               }
+                       }
                }
+               return tex;
        }
+
        if(ma) {
                mtex= ma->mtex[(int)(ma->texact)];
                if(mtex) tex= mtex->tex;
@@ -904,10 +1048,6 @@ int give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
                *mtex_ar=               ((Lamp *)id)->mtex;
                if(act) *act=   (((Lamp *)id)->texact);
                break;
-       case ID_BR:
-               *mtex_ar=               ((Brush *)id)->mtex;
-               if(act) *act=   (((Brush *)id)->texact);
-               break;
        default:
                *mtex_ar = NULL;
                if(act) *act=   0;
@@ -932,9 +1072,6 @@ void set_active_mtex(ID *id, short act)
        case ID_LA:
                ((Lamp *)id)->texact= act;
                break;
-       case ID_BR:
-               ((Brush *)id)->texact= act;
-               break;
        }
 }
 
@@ -1016,35 +1153,18 @@ void set_current_world_texture(World *wo, Tex *newtex)
 
 Tex *give_current_brush_texture(Brush *br)
 {
-       MTex *mtex= NULL;
-       Tex *tex= NULL;
-
-       if(br) {
-               mtex= br->mtex[(int)(br->texact)];
-               if(mtex) tex= mtex->tex;
-       }
-
-       return tex;
+       return br->mtex.tex;
 }
 
 void set_current_brush_texture(Brush *br, Tex *newtex)
 {
-       int act= br->texact;
-
-       if(br->mtex[act] && br->mtex[act]->tex)
-               id_us_min(&br->mtex[act]->tex->id);
+       if(br->mtex.tex)
+               id_us_min(&br->mtex.tex->id);
 
        if(newtex) {
-               if(!br->mtex[act])
-                       br->mtex[act]= add_mtex();
-               
-               br->mtex[act]->tex= newtex;
+               br->mtex.tex= newtex;
                id_us_plus(&newtex->id);
        }
-       else if(br->mtex[act]) {
-               MEM_freeN(br->mtex[act]);
-               br->mtex[act]= NULL;
-       }
 }
 
 /* ------------------------------------------------------------------------- */
@@ -1055,10 +1175,11 @@ EnvMap *BKE_add_envmap(void)
        
        env= MEM_callocN(sizeof(EnvMap), "envmap");
        env->type= ENV_CUBE;
-       env->stype= ENV_STATIC;
+       env->stype= ENV_ANIM;
        env->clipsta= 0.1;
        env->clipend= 100.0;
-       env->cuberes= 100;
+       env->cuberes= 600;
+       env->viewscale = 0.5;
        
        return env;
 } 
@@ -1123,9 +1244,9 @@ PointDensity *BKE_add_pointdensity(void)
        pd->coba = add_colorband(1);
        pd->speed_scale = 1.0f;
        pd->totpoints = 0;
-       pd->coba = add_colorband(1);
        pd->object = NULL;
-       pd->psys = NULL;
+       pd->psys = 0;
+       pd->psys_cache_space= TEX_PD_WORLDSPACE;
        return pd;
 } 
 
@@ -1188,6 +1309,8 @@ struct VoxelData *BKE_add_voxeldata(void)
        vd->int_multiplier = 1.0;
        vd->extend = TEX_CLIP;
        vd->object = NULL;
+       vd->cachedframe = -1;
+       vd->ok = 0;
        
        return vd;
  }
@@ -1213,13 +1336,11 @@ int BKE_texture_dependsOnTime(const struct Tex *texture)
        else if(        texture->ima && 
                        ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
                return 1;
-       } 
-#if 0 // XXX old animation system
-       else if(texture->ipo) {
-               // assume any ipo means the texture is animated
+       }
+       else if(texture->adt) {
+               // assume anything in adt means the texture is animated
                return 1;
        }
-#endif // XXX old animation system
        return 0;
 }