Removed the internal_select parameter from ntreeCopyTree. This was used just in one...
[blender-staging.git] / source / blender / blenkernel / intern / texture.c
index 993706104791f20a8934f029d979e24657fbcf3f..57c629d5544372e552ff30eb8eb7356f15dc6374 100644 (file)
 
 #include "PIL_dynlib.h"
 
-
-
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
 #include "BLI_kdopbvh.h"
+#include "BLI_utildefines.h"
 
 #include "DNA_key_types.h"
 #include "DNA_object_types.h"
@@ -169,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;
        }
@@ -245,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;
@@ -403,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)
@@ -451,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;
@@ -505,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);
        
@@ -540,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;
@@ -599,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,7 +760,7 @@ Tex *copy_texture(Tex *tex)
 
        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;
@@ -637,6 +770,7 @@ Tex *copy_texture(Tex *tex)
 
 void make_local_texture(Tex *tex)
 {
+       Main *bmain= G.main;
        Tex *texn;
        Material *ma;
        World *wrld;
@@ -666,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) {
@@ -676,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) {
@@ -686,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) {
@@ -696,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;
@@ -714,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) {
@@ -727,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) {
@@ -740,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) {
@@ -753,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) {
@@ -771,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", "", "", "", ""};
@@ -779,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) {
@@ -789,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]);
        }
 }
 
@@ -1111,6 +1246,7 @@ PointDensity *BKE_add_pointdensity(void)
        pd->totpoints = 0;
        pd->object = NULL;
        pd->psys = 0;
+       pd->psys_cache_space= TEX_PD_WORLDSPACE;
        return pd;
 } 
 
@@ -1173,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;
  }
@@ -1198,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;
 }