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