committing working copy
[blender.git] / source / blender / editors / render / render_shading.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2009 Blender Foundation.
20  * All rights reserved.
21  *
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "MEM_guardedalloc.h"
31
32 #include "DNA_curve_types.h"
33 #include "DNA_lamp_types.h"
34 #include "DNA_material_types.h"
35 #include "DNA_node_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_space_types.h"
39 #include "DNA_world_types.h"
40
41 #include "BKE_animsys.h"
42 #include "BKE_context.h"
43 #include "BKE_depsgraph.h"
44 #include "BKE_font.h"
45 #include "BKE_global.h"
46 #include "BKE_icons.h"
47 #include "BKE_image.h"
48 #include "BKE_library.h"
49 #include "BKE_main.h"
50 #include "BKE_material.h"
51 #include "BKE_node.h"
52 #include "BKE_report.h"
53 #include "BKE_scene.h"
54 #include "BKE_texture.h"
55 #include "BKE_utildefines.h"
56 #include "BKE_world.h"
57 #include "BKE_tessmesh.h"
58
59 #include "IMB_imbuf.h"
60 #include "IMB_imbuf_types.h"
61
62 #include "BLI_blenlib.h"
63 #include "BLI_math.h"
64 #include "BLI_editVert.h"
65 #include "BLI_listbase.h"
66
67 #include "GPU_material.h"
68
69 #include "RNA_access.h"
70
71 #include "WM_api.h"
72 #include "WM_types.h"
73
74 #include "ED_curve.h"
75 #include "ED_mesh.h"
76
77 #include "RNA_define.h"
78
79 #include "UI_interface.h"
80
81 #include "render_intern.h"      // own include
82
83 /***************************** Updates ***********************************
84  * ED_render_id_flush_update gets called from DAG_id_flush_update, to do *
85  * editor level updates when the ID changes. when these ID blocks are in *
86  * the dependency graph, we can get rid of the manual dependency checks  */
87
88 static int mtex_use_tex(MTex **mtex, int tot, Tex *tex)
89 {
90         int a;
91
92         if(!mtex)
93                 return 0;
94
95         for(a=0; a<tot; a++)
96                 if(mtex[a] && mtex[a]->tex == tex)
97                         return 1;
98         
99         return 0;
100 }
101
102 static int nodes_use_tex(bNodeTree *ntree, Tex *tex)
103 {
104         bNode *node;
105
106         for(node=ntree->nodes.first; node; node= node->next) {
107                 if(node->id) {
108                         if(node->id == (ID*)tex) {
109                                 return 1;
110                         }
111                         else if(node->type==NODE_GROUP) {
112                                 if(nodes_use_tex((bNodeTree *)node->id, tex))
113                                         return 1;
114                         }
115                 }
116         }
117
118         return 0;
119 }
120
121 static void material_changed(Main *bmain, Material *ma)
122 {
123         /* icons */
124         BKE_icon_changed(BKE_icon_getid(&ma->id));
125
126         /* glsl */
127         if(ma->gpumaterial.first)
128                 GPU_material_free(ma);
129 }
130
131 static void texture_changed(Main *bmain, Tex *tex)
132 {
133         Material *ma;
134         Lamp *la;
135         World *wo;
136
137         /* icons */
138         BKE_icon_changed(BKE_icon_getid(&tex->id));
139
140         /* find materials */
141         for(ma=bmain->mat.first; ma; ma=ma->id.next) {
142                 if(mtex_use_tex(ma->mtex, MAX_MTEX, tex));
143                 else if(ma->use_nodes && ma->nodetree && nodes_use_tex(ma->nodetree, tex));
144                 else continue;
145
146                 BKE_icon_changed(BKE_icon_getid(&ma->id));
147
148                 if(ma->gpumaterial.first)
149                         GPU_material_free(ma);
150         }
151
152         /* find lamps */
153         for(la=bmain->lamp.first; la; la=la->id.next) {
154                 if(mtex_use_tex(la->mtex, MAX_MTEX, tex));
155                 else continue;
156
157                 BKE_icon_changed(BKE_icon_getid(&la->id));
158         }
159
160         /* find worlds */
161         for(wo=bmain->world.first; wo; wo=wo->id.next) {
162                 if(mtex_use_tex(wo->mtex, MAX_MTEX, tex));
163                 else continue;
164
165                 BKE_icon_changed(BKE_icon_getid(&wo->id));
166         }
167 }
168
169 static void lamp_changed(Main *bmain, Lamp *la)
170 {
171         Object *ob;
172         Material *ma;
173
174         /* icons */
175         BKE_icon_changed(BKE_icon_getid(&la->id));
176
177         /* glsl */
178         for(ob=bmain->object.first; ob; ob=ob->id.next)
179                 if(ob->data == la && ob->gpulamp.first)
180                         GPU_lamp_free(ob);
181
182         for(ma=bmain->mat.first; ma; ma=ma->id.next)
183                 if(ma->gpumaterial.first)
184                         GPU_material_free(ma);
185 }
186
187 static void world_changed(Main *bmain, World *wo)
188 {
189         Material *ma;
190
191         /* icons */
192         BKE_icon_changed(BKE_icon_getid(&wo->id));
193
194         /* glsl */
195         for(ma=bmain->mat.first; ma; ma=ma->id.next)
196                 if(ma->gpumaterial.first)
197                         GPU_material_free(ma);
198 }
199
200 static void image_changed(Main *bmain, Image *ima)
201 {
202         Tex *tex;
203
204         /* icons */
205         BKE_icon_changed(BKE_icon_getid(&ima->id));
206
207         /* textures */
208         for(tex=bmain->tex.first; tex; tex=tex->id.next)
209                 if(tex->ima == ima)
210                         texture_changed(bmain, tex);
211 }
212
213 void ED_render_id_flush_update(Main *bmain, ID *id)
214 {
215         if(!id)
216                 return;
217
218         switch(GS(id->name)) {
219                 case ID_MA:
220                         material_changed(bmain, (Material*)id);
221                         break;
222                 case ID_TE:
223                         texture_changed(bmain, (Tex*)id);
224                         break;
225                 case ID_WO:
226                         world_changed(bmain, (World*)id);
227                         break;
228                 case ID_LA:
229                         lamp_changed(bmain, (Lamp*)id);
230                         break;
231                 case ID_IM:
232                         image_changed(bmain, (Image*)id);
233                         break;
234                 default:
235                         break;
236         }
237 }
238
239 /********************** material slot operators *********************/
240
241 static int material_slot_add_exec(bContext *C, wmOperator *op)
242 {
243         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
244
245         if(!ob)
246                 return OPERATOR_CANCELLED;
247
248         object_add_material_slot(ob);
249         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
250         
251         return OPERATOR_FINISHED;
252 }
253
254 void OBJECT_OT_material_slot_add(wmOperatorType *ot)
255 {
256         /* identifiers */
257         ot->name= "Add Material Slot";
258         ot->idname= "OBJECT_OT_material_slot_add";
259         ot->description="Add a new material slot or duplicate the selected one";
260         
261         /* api callbacks */
262         ot->exec= material_slot_add_exec;
263
264         /* flags */
265         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
266 }
267
268 static int material_slot_remove_exec(bContext *C, wmOperator *op)
269 {
270         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
271
272         if(!ob)
273                 return OPERATOR_CANCELLED;
274
275         object_remove_material_slot(ob);
276         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
277         
278         return OPERATOR_FINISHED;
279 }
280
281 void OBJECT_OT_material_slot_remove(wmOperatorType *ot)
282 {
283         /* identifiers */
284         ot->name= "Remove Material Slot";
285         ot->idname= "OBJECT_OT_material_slot_remove";
286         ot->description="Remove the selected material slot";
287         
288         /* api callbacks */
289         ot->exec= material_slot_remove_exec;
290
291         /* flags */
292         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
293 }
294
295 static int material_slot_assign_exec(bContext *C, wmOperator *op)
296 {
297         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
298
299         if(!ob)
300                 return OPERATOR_CANCELLED;
301
302         if(ob && ob->actcol>0) {
303                 if(ob->type == OB_MESH) {
304                         BMEditMesh *em= ((Mesh*)ob->data)->edit_btmesh;
305                         BMFace *efa;
306                         BMIter iter;
307
308                         if(em) {
309                                 BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
310                                         if(BM_TestHFlag(efa, BM_SELECT))
311                                                 efa->mat_nr= ob->actcol-1;
312                                 }
313                         }
314                 }
315                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
316                         ListBase *editnurb= ((Curve*)ob->data)->editnurb;
317                         Nurb *nu;
318
319                         if(editnurb) {
320                                 for(nu= editnurb->first; nu; nu= nu->next)
321                                         if(isNurbsel(nu))
322                                                 nu->mat_nr= nu->charidx= ob->actcol-1;
323                         }
324                 }
325                 else if(ob->type == OB_FONT) {
326                         EditFont *ef= ((Curve*)ob->data)->editfont;
327                         int i, selstart, selend;
328
329                         if(ef && BKE_font_getselection(ob, &selstart, &selend)) {
330                                 for(i=selstart; i<=selend; i++)
331                                         ef->textbufinfo[i].mat_nr = ob->actcol-1;
332                         }
333                 }
334         }
335
336         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
337         WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
338         
339         return OPERATOR_FINISHED;
340 }
341
342 void OBJECT_OT_material_slot_assign(wmOperatorType *ot)
343 {
344         /* identifiers */
345         ot->name= "Assign Material Slot";
346         ot->idname= "OBJECT_OT_material_slot_assign";
347         ot->description="Assign the material in the selected material slot to the selected vertices";
348         
349         /* api callbacks */
350         ot->exec= material_slot_assign_exec;
351
352         /* flags */
353         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
354 }
355
356 static int material_slot_de_select(bContext *C, int select)
357 {
358         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
359
360         if(!ob)
361                 return OPERATOR_CANCELLED;
362
363         if(ob->type == OB_MESH) {
364                 BMEditMesh *em= ((Mesh*)ob->data)->edit_btmesh;
365 /*BMESH_TODO
366                 if(em) {
367                         if(select)
368                                 EM_select_by_material(em, ob->actcol-1);
369                         else
370                                 EM_deselect_by_material(em, ob->actcol-1);
371                 }
372 */
373         }
374         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
375                 ListBase *editnurb= ((Curve*)ob->data)->editnurb;
376                 Nurb *nu;
377                 BPoint *bp;
378                 BezTriple *bezt;
379                 int a;
380
381                 for(nu= editnurb->first; nu; nu=nu->next) {
382                         if(nu->mat_nr==ob->actcol-1) {
383                                 if(nu->bezt) {
384                                         a= nu->pntsu;
385                                         bezt= nu->bezt;
386                                         while(a--) {
387                                                 if(bezt->hide==0) {
388                                                         if(select) {
389                                                                 bezt->f1 |= SELECT;
390                                                                 bezt->f2 |= SELECT;
391                                                                 bezt->f3 |= SELECT;
392                                                         }
393                                                         else {
394                                                                 bezt->f1 &= ~SELECT;
395                                                                 bezt->f2 &= ~SELECT;
396                                                                 bezt->f3 &= ~SELECT;
397                                                         }
398                                                 }
399                                                 bezt++;
400                                         }
401                                 }
402                                 else if(nu->bp) {
403                                         a= nu->pntsu*nu->pntsv;
404                                         bp= nu->bp;
405                                         while(a--) {
406                                                 if(bp->hide==0) {
407                                                         if(select) bp->f1 |= SELECT;
408                                                         else bp->f1 &= ~SELECT;
409                                                 }
410                                                 bp++;
411                                         }
412                                 }
413                         }
414                 }
415         }
416
417         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, ob->data);
418
419         return OPERATOR_FINISHED;
420 }
421
422 static int material_slot_select_exec(bContext *C, wmOperator *op)
423 {
424         return material_slot_de_select(C, 1);
425 }
426
427 void OBJECT_OT_material_slot_select(wmOperatorType *ot)
428 {
429         /* identifiers */
430         ot->name= "Select Material Slot";
431         ot->idname= "OBJECT_OT_material_slot_select";
432         ot->description="Select vertices assigned to the selected material slot";
433         
434         /* api callbacks */
435         ot->exec= material_slot_select_exec;
436
437         /* flags */
438         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
439 }
440
441 static int material_slot_deselect_exec(bContext *C, wmOperator *op)
442 {
443         return material_slot_de_select(C, 0);
444 }
445
446 void OBJECT_OT_material_slot_deselect(wmOperatorType *ot)
447 {
448         /* identifiers */
449         ot->name= "Deselect Material Slot";
450         ot->idname= "OBJECT_OT_material_slot_deselect";
451         ot->description="Deselect vertices assigned to the selected material slot";
452         
453         /* api callbacks */
454         ot->exec= material_slot_deselect_exec;
455
456         /* flags */
457         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
458 }
459
460
461 static int material_slot_copy_exec(bContext *C, wmOperator *op)
462 {
463         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
464         Material ***matar;
465
466         if(!ob || !(matar= give_matarar(ob)))
467                 return OPERATOR_CANCELLED;
468
469         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
470                 if(ob != ob_iter && give_matarar(ob_iter)) {
471                         if (ob->data != ob_iter->data)
472                                 assign_matarar(ob_iter, matar, ob->totcol);
473                         
474                         if(ob_iter->totcol==ob->totcol) {
475                                 ob_iter->actcol= ob->actcol;
476                                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob_iter);
477                         }
478                 }
479         }
480         CTX_DATA_END;
481
482         return OPERATOR_FINISHED;
483 }
484
485
486 void OBJECT_OT_material_slot_copy(wmOperatorType *ot)
487 {
488         /* identifiers */
489         ot->name= "Copy Material to Others";
490         ot->idname= "OBJECT_OT_material_slot_copy";
491         ot->description="Copies materials to other selected objects";
492
493         /* api callbacks */
494         ot->exec= material_slot_copy_exec;
495
496         /* flags */
497         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
498 }
499
500 /********************** new material operator *********************/
501
502 static int new_material_exec(bContext *C, wmOperator *op)
503 {
504         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
505         PointerRNA ptr, idptr;
506         PropertyRNA *prop;
507
508         /* add or copy material */
509         if(ma)
510                 ma= copy_material(ma);
511         else
512                 ma= add_material("Material");
513
514         /* hook into UI */
515         uiIDContextProperty(C, &ptr, &prop);
516
517         if(prop) {
518                 /* when creating new ID blocks, use is already 1, but RNA
519                  * pointer se also increases user, so this compensates it */
520                 ma->id.us--;
521
522                 RNA_id_pointer_create(&ma->id, &idptr);
523                 RNA_property_pointer_set(&ptr, prop, idptr);
524                 RNA_property_update(C, &ptr, prop);
525         }
526
527         WM_event_add_notifier(C, NC_MATERIAL|NA_ADDED, ma);
528         
529         return OPERATOR_FINISHED;
530 }
531
532 void MATERIAL_OT_new(wmOperatorType *ot)
533 {
534         /* identifiers */
535         ot->name= "New Material";
536         ot->idname= "MATERIAL_OT_new";
537         ot->description="Add a new material";
538         
539         /* api callbacks */
540         ot->exec= new_material_exec;
541
542         /* flags */
543         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
544 }
545
546 /********************** new texture operator *********************/
547
548 static int new_texture_exec(bContext *C, wmOperator *op)
549 {
550         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
551         PointerRNA ptr, idptr;
552         PropertyRNA *prop;
553
554         /* add or copy texture */
555         if(tex)
556                 tex= copy_texture(tex);
557         else
558                 tex= add_texture("Texture");
559
560         /* hook into UI */
561         uiIDContextProperty(C, &ptr, &prop);
562
563         if(prop) {
564                 /* when creating new ID blocks, use is already 1, but RNA
565                  * pointer se also increases user, so this compensates it */
566                 tex->id.us--;
567
568                 RNA_id_pointer_create(&tex->id, &idptr);
569                 RNA_property_pointer_set(&ptr, prop, idptr);
570                 RNA_property_update(C, &ptr, prop);
571         }
572
573         WM_event_add_notifier(C, NC_TEXTURE|NA_ADDED, tex);
574         
575         return OPERATOR_FINISHED;
576 }
577
578 void TEXTURE_OT_new(wmOperatorType *ot)
579 {
580         /* identifiers */
581         ot->name= "New Texture";
582         ot->idname= "TEXTURE_OT_new";
583         ot->description="Add a new texture";
584         
585         /* api callbacks */
586         ot->exec= new_texture_exec;
587
588         /* flags */
589         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
590 }
591
592 /********************** new world operator *********************/
593
594 static int new_world_exec(bContext *C, wmOperator *op)
595 {
596         World *wo= CTX_data_pointer_get_type(C, "world", &RNA_World).data;
597         PointerRNA ptr, idptr;
598         PropertyRNA *prop;
599
600         /* add or copy world */
601         if(wo)
602                 wo= copy_world(wo);
603         else
604                 wo= add_world("World");
605
606         /* hook into UI */
607         uiIDContextProperty(C, &ptr, &prop);
608
609         if(prop) {
610                 /* when creating new ID blocks, use is already 1, but RNA
611                  * pointer se also increases user, so this compensates it */
612                 wo->id.us--;
613
614                 RNA_id_pointer_create(&wo->id, &idptr);
615                 RNA_property_pointer_set(&ptr, prop, idptr);
616                 RNA_property_update(C, &ptr, prop);
617         }
618
619         WM_event_add_notifier(C, NC_WORLD|NA_ADDED, wo);
620         
621         return OPERATOR_FINISHED;
622 }
623
624 void WORLD_OT_new(wmOperatorType *ot)
625 {
626         /* identifiers */
627         ot->name= "New World";
628         ot->idname= "WORLD_OT_new";
629         ot->description= "Add a new world";
630         
631         /* api callbacks */
632         ot->exec= new_world_exec;
633
634         /* flags */
635         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
636 }
637
638 /********************** render layer operators *********************/
639
640 static int render_layer_add_exec(bContext *C, wmOperator *op)
641 {
642         Scene *scene= CTX_data_scene(C);
643
644         scene_add_render_layer(scene);
645         scene->r.actlay= BLI_countlist(&scene->r.layers) - 1;
646
647         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
648         
649         return OPERATOR_FINISHED;
650 }
651
652 void SCENE_OT_render_layer_add(wmOperatorType *ot)
653 {
654         /* identifiers */
655         ot->name= "Add Render Layer";
656         ot->idname= "SCENE_OT_render_layer_add";
657         ot->description="Add a render layer";
658         
659         /* api callbacks */
660         ot->exec= render_layer_add_exec;
661
662         /* flags */
663         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
664 }
665
666 static int render_layer_remove_exec(bContext *C, wmOperator *op)
667 {
668         Scene *scene= CTX_data_scene(C);
669         SceneRenderLayer *rl;
670         int act= scene->r.actlay;
671
672         if(BLI_countlist(&scene->r.layers) <= 1)
673                 return OPERATOR_CANCELLED;
674         
675         rl= BLI_findlink(&scene->r.layers, scene->r.actlay);
676         BLI_remlink(&scene->r.layers, rl);
677         MEM_freeN(rl);
678
679         scene->r.actlay= 0;
680         
681         if(scene->nodetree) {
682                 bNode *node;
683                 for(node= scene->nodetree->nodes.first; node; node= node->next) {
684                         if(node->type==CMP_NODE_R_LAYERS && node->id==NULL) {
685                                 if(node->custom1==act)
686                                         node->custom1= 0;
687                                 else if(node->custom1>act)
688                                         node->custom1--;
689                         }
690                 }
691         }
692
693         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
694         
695         return OPERATOR_FINISHED;
696 }
697
698 void SCENE_OT_render_layer_remove(wmOperatorType *ot)
699 {
700         /* identifiers */
701         ot->name= "Remove Render Layer";
702         ot->idname= "SCENE_OT_render_layer_remove";
703         ot->description="Remove the selected render layer";
704         
705         /* api callbacks */
706         ot->exec= render_layer_remove_exec;
707
708         /* flags */
709         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
710 }
711
712 static int texture_slot_move(bContext *C, wmOperator *op)
713 {
714         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
715         Material *ma = (Material *)id;
716
717         if(id) {
718                 MTex **mtex_ar, *mtexswap;
719                 short act;
720                 int type= RNA_enum_get(op->ptr, "type");
721
722                 give_active_mtex(id, &mtex_ar, &act);
723
724                 if(type == -1) { /* Up */
725                         if(act > 0) {
726                                 mtexswap = mtex_ar[act];
727                                 mtex_ar[act] = mtex_ar[act-1];
728                                 mtex_ar[act-1] = mtexswap;
729                                 
730                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, act-1, -1, 0);
731                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, act, act-1, 0);
732                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, -1, act, 0);
733
734                                 if(GS(id->name)==ID_MA) {
735                                         Material *ma= (Material *)id;
736                                         int mtexuse = ma->septex & (1<<act);
737                                         ma->septex &= ~(1<<act);
738                                         ma->septex |= (ma->septex & (1<<(act-1))) << 1;
739                                         ma->septex &= ~(1<<(act-1));
740                                         ma->septex |= mtexuse >> 1;
741                                 }
742                                 
743                                 set_active_mtex(id, act-1);
744                         }
745                 }
746                 else { /* Down */
747                         if(act < MAX_MTEX-1) {
748                                 mtexswap = mtex_ar[act];
749                                 mtex_ar[act] = mtex_ar[act+1];
750                                 mtex_ar[act+1] = mtexswap;
751                                 
752                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, act+1, -1, 0);
753                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, act, act+1, 0);
754                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, -1, act, 0);
755
756                                 if(GS(id->name)==ID_MA) {
757                                         Material *ma= (Material *)id;
758                                         int mtexuse = ma->septex & (1<<act);
759                                         ma->septex &= ~(1<<act);
760                                         ma->septex |= (ma->septex & (1<<(act+1))) >> 1;
761                                         ma->septex &= ~(1<<(act+1));
762                                         ma->septex |= mtexuse << 1;
763                                 }
764                                 
765                                 set_active_mtex(id, act+1);
766                         }
767                 }
768
769                 WM_event_add_notifier(C, NC_TEXTURE, CTX_data_scene(C));
770         }
771
772         return OPERATOR_FINISHED;
773 }
774
775 void TEXTURE_OT_slot_move(wmOperatorType *ot)
776 {
777         static EnumPropertyItem slot_move[] = {
778                 {-1, "UP", 0, "Up", ""},
779                 {1, "DOWN", 0, "Down", ""},
780                 {0, NULL, 0, NULL, NULL}
781         };
782
783         /* identifiers */
784         ot->name= "Move Texture Slot";
785         ot->idname= "TEXTURE_OT_slot_move";
786         ot->description="Move texture slots up and down";
787
788         /* api callbacks */
789         ot->exec= texture_slot_move;
790
791         /* flags */
792         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
793
794         RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
795 }
796
797
798
799 /********************** environment map operators *********************/
800
801 static int save_envmap(wmOperator *op, Scene *scene, EnvMap *env, char *str, int imtype)
802 {
803         ImBuf *ibuf=NULL;
804         int dx;
805         int retval;
806         
807         if(env->cube[1]==NULL) {
808                 BKE_report(op->reports, RPT_ERROR, "There is no generated environment map available to save");
809                 return OPERATOR_CANCELLED;
810         }
811         
812         dx= env->cube[1]->x;
813         
814         if (env->type == ENV_CUBE) {
815                 ibuf = IMB_allocImBuf(3*dx, 2*dx, 24, IB_rectfloat, 0);
816
817                 IMB_rectcpy(ibuf, env->cube[0], 0, 0, 0, 0, dx, dx);
818                 IMB_rectcpy(ibuf, env->cube[1], dx, 0, 0, 0, dx, dx);
819                 IMB_rectcpy(ibuf, env->cube[2], 2*dx, 0, 0, 0, dx, dx);
820                 IMB_rectcpy(ibuf, env->cube[3], 0, dx, 0, 0, dx, dx);
821                 IMB_rectcpy(ibuf, env->cube[4], dx, dx, 0, 0, dx, dx);
822                 IMB_rectcpy(ibuf, env->cube[5], 2*dx, dx, 0, 0, dx, dx);
823         }
824         else if (env->type == ENV_PLANE) {
825                 ibuf = IMB_allocImBuf(dx, dx, 24, IB_rectfloat, 0);
826                 IMB_rectcpy(ibuf, env->cube[1], 0, 0, 0, 0, dx, dx);            
827         }
828         
829         if (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
830                 ibuf->profile = IB_PROFILE_LINEAR_RGB;
831         
832         if (BKE_write_ibuf(scene, ibuf, str, imtype, scene->r.subimtype, scene->r.quality)) {
833                 retval = OPERATOR_FINISHED;
834         }
835         else {
836                 BKE_reportf(op->reports, RPT_ERROR, "Error saving environment map to %s.", str);
837                 retval = OPERATOR_CANCELLED;
838         }
839         
840         IMB_freeImBuf(ibuf);
841         ibuf = NULL;
842         
843         return retval;
844 }
845
846 static int envmap_save_exec(bContext *C, wmOperator *op)
847 {
848         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
849         Scene *scene = CTX_data_scene(C);
850         //int imtype = RNA_enum_get(op->ptr, "file_type");
851         int imtype = scene->r.imtype;
852         char path[FILE_MAX];
853         
854         RNA_string_get(op->ptr, "filepath", path);
855         
856         if(scene->r.scemode & R_EXTENSION)  {
857                 BKE_add_image_extension(path, imtype);
858         }
859         
860         WM_cursor_wait(1);
861         
862         save_envmap(op, scene, tex->env, path, imtype);
863         
864         WM_cursor_wait(0);
865         
866         WM_event_add_notifier(C, NC_TEXTURE, tex);
867         
868         return OPERATOR_FINISHED;
869 }
870
871 static int envmap_save_invoke(bContext *C, wmOperator *op, wmEvent *event)
872 {
873         //Scene *scene= CTX_data_scene(C);
874         
875         if(!RNA_property_is_set(op->ptr, "relative_path"))
876                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
877         
878         if(RNA_property_is_set(op->ptr, "filepath"))
879                 return envmap_save_exec(C, op);
880
881         //RNA_enum_set(op->ptr, "file_type", scene->r.imtype);
882         
883         RNA_string_set(op->ptr, "filepath", G.sce);
884         WM_event_add_fileselect(C, op);
885         
886         return OPERATOR_RUNNING_MODAL;
887 }
888
889 static int envmap_save_poll(bContext *C)
890 {
891         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
892
893         if (!tex) 
894                 return 0;
895         if (!tex->env || !tex->env->ok)
896                 return 0;
897         if (tex->env->cube[1]==NULL)
898                 return 0;
899         
900         return 1;
901 }
902
903 void TEXTURE_OT_envmap_save(wmOperatorType *ot)
904 {
905         /* identifiers */
906         ot->name= "Save Environment Map";
907         ot->idname= "TEXTURE_OT_envmap_save";
908         ot->description="Save the current generated Environment map to an image file";
909         
910         /* api callbacks */
911         ot->exec= envmap_save_exec;
912         ot->invoke= envmap_save_invoke;
913         ot->poll= envmap_save_poll;
914         
915         /* flags */
916         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
917         
918         /* properties */
919         //RNA_def_enum(ot->srna, "file_type", image_file_type_items, R_PNG, "File Type", "File type to save image as.");
920         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
921 }
922
923 static int envmap_clear_exec(bContext *C, wmOperator *op)
924 {
925         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
926         
927         BKE_free_envmapdata(tex->env);
928         
929         WM_event_add_notifier(C, NC_TEXTURE|NA_EDITED, tex);
930         
931         return OPERATOR_FINISHED;
932 }
933
934 static int envmap_clear_poll(bContext *C)
935 {
936         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
937         
938         if (!tex) 
939                 return 0;
940         if (!tex->env || !tex->env->ok)
941                 return 0;
942         if (tex->env->cube[1]==NULL)
943                 return 0;
944         
945         return 1;
946 }
947
948 void TEXTURE_OT_envmap_clear(wmOperatorType *ot)
949 {
950         /* identifiers */
951         ot->name= "Clear Environment Map";
952         ot->idname= "TEXTURE_OT_envmap_clear";
953         ot->description="Discard the environment map and free it from memory";
954         
955         /* api callbacks */
956         ot->exec= envmap_clear_exec;
957         ot->poll= envmap_clear_poll;
958         
959         /* flags */
960         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
961 }
962
963 static int envmap_clear_all_exec(bContext *C, wmOperator *op)
964 {
965         Main *bmain = CTX_data_main(C);
966         Tex *tex;
967         
968         for (tex=bmain->tex.first; tex; tex=tex->id.next)
969                 if (tex->env)
970                         BKE_free_envmapdata(tex->env);
971         
972         WM_event_add_notifier(C, NC_TEXTURE|NA_EDITED, tex);
973         
974         return OPERATOR_FINISHED;
975 }
976
977 void TEXTURE_OT_envmap_clear_all(wmOperatorType *ot)
978 {
979         /* identifiers */
980         ot->name= "Clear All Environment Maps";
981         ot->idname= "TEXTURE_OT_envmap_clear_all";
982         ot->description="Discard all environment maps in the .blend file and free them from memory";
983         
984         /* api callbacks */
985         ot->exec= envmap_clear_all_exec;
986         ot->poll= envmap_clear_poll;
987         
988         /* flags */
989         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
990 }
991
992 /********************** material operators *********************/
993
994 /* material copy/paste */
995 static int copy_material_exec(bContext *C, wmOperator *op)
996 {
997         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
998
999         if(ma==NULL)
1000                 return OPERATOR_CANCELLED;
1001
1002         copy_matcopybuf(ma);
1003
1004         WM_event_add_notifier(C, NC_MATERIAL, ma);
1005
1006         return OPERATOR_FINISHED;
1007 }
1008
1009 void MATERIAL_OT_copy(wmOperatorType *ot)
1010 {
1011         /* identifiers */
1012         ot->name= "Copy Material";
1013         ot->idname= "MATERIAL_OT_copy";
1014         ot->description="Copy the material settings and nodes";
1015
1016         /* api callbacks */
1017         ot->exec= copy_material_exec;
1018
1019         /* flags */
1020         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1021 }
1022
1023 static int paste_material_exec(bContext *C, wmOperator *op)
1024 {
1025         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1026
1027         if(ma==NULL)
1028                 return OPERATOR_CANCELLED;
1029
1030         paste_matcopybuf(ma);
1031
1032         WM_event_add_notifier(C, NC_MATERIAL|ND_SHADING_DRAW, ma);
1033
1034         return OPERATOR_FINISHED;
1035 }
1036
1037 void MATERIAL_OT_paste(wmOperatorType *ot)
1038 {
1039         /* identifiers */
1040         ot->name= "Paste Material";
1041         ot->idname= "MATERIAL_OT_paste";
1042         ot->description="Paste the material settings and nodes";
1043
1044         /* api callbacks */
1045         ot->exec= paste_material_exec;
1046
1047         /* flags */
1048         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1049 }
1050
1051
1052 static short mtexcopied=0; /* must be reset on file load */
1053 static MTex mtexcopybuf;
1054
1055 void ED_render_clear_mtex_copybuf(void)
1056 {       /* use for file reload */
1057         mtexcopied= 0;
1058 }
1059
1060 void copy_mtex_copybuf(ID *id)
1061 {
1062         MTex **mtex= NULL;
1063         
1064         switch(GS(id->name)) {
1065                 case ID_MA:
1066                         mtex= &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
1067                         break;
1068                 case ID_LA:
1069                         mtex= &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
1070                         // la->mtex[(int)la->texact] // TODO
1071                         break;
1072                 case ID_WO:
1073                         mtex= &(((World *)id)->mtex[(int)((World *)id)->texact]);
1074                         // mtex= wrld->mtex[(int)wrld->texact]; // TODO
1075                         break;
1076         }
1077         
1078         if(mtex && *mtex) {
1079                 memcpy(&mtexcopybuf, *mtex, sizeof(MTex));
1080                 mtexcopied= 1;
1081         }
1082         else {
1083                 mtexcopied= 0;
1084         }
1085 }
1086
1087 void paste_mtex_copybuf(ID *id)
1088 {
1089         MTex **mtex= NULL;
1090         
1091         if(mtexcopied == 0 || mtexcopybuf.tex==NULL)
1092                 return;
1093         
1094         switch(GS(id->name)) {
1095                 case ID_MA:
1096                         mtex= &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
1097                         break;
1098                 case ID_LA:
1099                         mtex= &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
1100                         // la->mtex[(int)la->texact] // TODO
1101                         break;
1102                 case ID_WO:
1103                         mtex= &(((World *)id)->mtex[(int)((World *)id)->texact]);
1104                         // mtex= wrld->mtex[(int)wrld->texact]; // TODO
1105                         break;
1106         }
1107         
1108         if(mtex) {
1109                 if(*mtex==NULL) {
1110                         *mtex= MEM_mallocN(sizeof(MTex), "mtex copy");
1111                 }
1112                 else if((*mtex)->tex) {
1113                         (*mtex)->tex->id.us--;
1114                 }
1115                 
1116                 memcpy(*mtex, &mtexcopybuf, sizeof(MTex));
1117                 
1118                 id_us_plus((ID *)mtexcopybuf.tex);
1119         }
1120 }
1121
1122
1123 static int copy_mtex_exec(bContext *C, wmOperator *op)
1124 {
1125         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1126
1127         if(id==NULL) {
1128                 /* copying empty slot */
1129                 ED_render_clear_mtex_copybuf();
1130                 return OPERATOR_CANCELLED;
1131         }
1132
1133         copy_mtex_copybuf(id);
1134
1135         WM_event_add_notifier(C, NC_TEXTURE, NULL);
1136
1137         return OPERATOR_FINISHED;
1138 }
1139
1140 static int copy_mtex_poll(bContext *C)
1141 {
1142         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1143         
1144         return (id != NULL);
1145 }
1146
1147 void TEXTURE_OT_slot_copy(wmOperatorType *ot)
1148 {
1149         /* identifiers */
1150         ot->name= "Copy Texture Slot Settings";
1151         ot->idname= "TEXTURE_OT_slot_copy";
1152         ot->description="Copy the material texture settings and nodes";
1153
1154         /* api callbacks */
1155         ot->exec= copy_mtex_exec;
1156         ot->poll= copy_mtex_poll;
1157         
1158         /* flags */
1159         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1160 }
1161
1162 static int paste_mtex_exec(bContext *C, wmOperator *op)
1163 {
1164         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1165
1166         if(id==NULL) {
1167                 Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1168                 Lamp *la= CTX_data_pointer_get_type(C, "lamp", &RNA_Lamp).data;
1169                 World *wo= CTX_data_pointer_get_type(C, "world", &RNA_World).data;
1170                 
1171                 if (ma)
1172                         id = &ma->id;
1173                 else if (la)
1174                         id = &la->id;
1175                 else if (wo)
1176                         id = &wo->id;
1177                 
1178                 if (id==NULL)
1179                         return OPERATOR_CANCELLED;
1180         }
1181
1182         paste_mtex_copybuf(id);
1183
1184         WM_event_add_notifier(C, NC_TEXTURE|ND_SHADING_DRAW, NULL);
1185
1186         return OPERATOR_FINISHED;
1187 }
1188
1189 void TEXTURE_OT_slot_paste(wmOperatorType *ot)
1190 {
1191         /* identifiers */
1192         ot->name= "Paste Texture Slot Settings";
1193         ot->idname= "TEXTURE_OT_slot_paste";
1194         ot->description="Copy the texture settings and nodes";
1195
1196         /* api callbacks */
1197         ot->exec= paste_mtex_exec;
1198
1199         /* flags */
1200         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1201 }