Removed the internal_select parameter from ntreeCopyTree. This was used just in one...
[blender-staging.git] / source / blender / blenkernel / intern / texture.c
index 4ced858ff17da4f84af025c93d491c40cfefe177..57c629d5544372e552ff30eb8eb7356f15dc6374 100644 (file)
 
 #include "PIL_dynlib.h"
 
-
-
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
-#include "BLI_rand.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;
        }
@@ -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(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,7 +830,7 @@ void make_local_texture(Tex *tex)
                }
                wrld= wrld->id.next;
        }
-       br= G.main->brush.first;
+       br= bmain->brush.first;
        while(br) {
                if(br->mtex.tex==tex) {
                        if(br->id.lib) lib= 1;
@@ -719,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) {
@@ -732,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) {
@@ -745,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) {
@@ -758,7 +887,7 @@ void make_local_texture(Tex *tex)
                        }
                        wrld= wrld->id.next;
                }
-               br= G.main->brush.first;
+               br= bmain->brush.first;
                while(br) {
                        if(br->mtex.tex==tex) {
                                if(br->id.lib==0) {
@@ -776,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", "", "", "", ""};
@@ -784,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) {
@@ -794,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]);
        }
 }
 
@@ -884,9 +1014,15 @@ 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) {
@@ -1039,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;
 } 
@@ -1107,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 = 0;
+       pd->psys_cache_space= TEX_PD_WORLDSPACE;
        return pd;
 } 
 
@@ -1172,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;
  }
@@ -1197,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;
 }