Added new render pass: "Mist".
[blender-staging.git] / source / blender / nodes / intern / CMP_nodes / CMP_image.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  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2006 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include "../CMP_util.h"
31
32
33 /* **************** IMAGE (and RenderResult, multilayer image) ******************** */
34
35 static bNodeSocketType cmp_node_rlayers_out[]= {
36         {       SOCK_RGBA, 0, "Image",          0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
37         {       SOCK_VALUE, 0, "Alpha",         1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
38         {       SOCK_VALUE, 0, "Z",                     1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
39         {       SOCK_VECTOR, 0, "Normal",       0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
40         {       SOCK_VECTOR, 0, "UV",           1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
41         {       SOCK_VECTOR, 0, "Speed",        1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
42         {       SOCK_RGBA, 0, "Color",          0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
43         {       SOCK_RGBA, 0, "Diffuse",        0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
44         {       SOCK_RGBA, 0, "Specular",       0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
45         {       SOCK_RGBA, 0, "Shadow",         0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
46         {       SOCK_RGBA, 0, "AO",                     0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
47         {       SOCK_RGBA, 0, "Reflect",        0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
48         {       SOCK_RGBA, 0, "Refract",        0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
49         {       SOCK_RGBA, 0, "Radio",          0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
50         {       SOCK_VALUE, 0, "IndexOB",       0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
51         {       SOCK_VALUE, 0, "Mist",          0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
52         {       -1, 0, ""       }
53 };
54
55
56 /* note: this function is used for multilayer too, to ensure uniform 
57    handling with BKE_image_get_ibuf() */
58 static CompBuf *node_composit_get_image(RenderData *rd, Image *ima, ImageUser *iuser)
59 {
60         ImBuf *ibuf;
61         CompBuf *stackbuf;
62         int type;
63         
64         ibuf= BKE_image_get_ibuf(ima, iuser);
65         if(ibuf==NULL)
66                 return NULL;
67         
68         if(ibuf->rect_float==NULL)
69                 IMB_float_from_rect(ibuf);
70         
71         type= ibuf->channels;
72         
73         if(rd->scemode & R_COMP_CROP) {
74                 stackbuf= get_cropped_compbuf(&rd->disprect, ibuf->rect_float, ibuf->x, ibuf->y, type);
75         }
76         else {
77                 /* we put imbuf copy on stack, cbuf knows rect is from other ibuf when freed! */
78                 stackbuf= alloc_compbuf(ibuf->x, ibuf->y, type, 0);
79                 stackbuf->rect= ibuf->rect_float;
80         }
81         
82         return stackbuf;
83 };
84
85 static CompBuf *node_composit_get_zimage(bNode *node, RenderData *rd)
86 {
87         ImBuf *ibuf= BKE_image_get_ibuf((Image *)node->id, node->storage);
88         CompBuf *zbuf= NULL;
89         
90         if(ibuf && ibuf->zbuf_float) {
91                 if(rd->scemode & R_COMP_CROP) {
92                         zbuf= get_cropped_compbuf(&rd->disprect, ibuf->zbuf_float, ibuf->x, ibuf->y, CB_VAL);
93                 }
94                 else {
95                         zbuf= alloc_compbuf(ibuf->x, ibuf->y, CB_VAL, 0);
96                         zbuf->rect= ibuf->zbuf_float;
97                 }
98         }
99         return zbuf;
100 };
101
102 /* check if layer is available, returns pass buffer */
103 static CompBuf *compbuf_multilayer_get(RenderData *rd, RenderLayer *rl, Image *ima, ImageUser *iuser, int passtype)
104 {
105         RenderPass *rpass;
106         short index;
107         
108         for(index=0, rpass= rl->passes.first; rpass; rpass= rpass->next, index++)
109                 if(rpass->passtype==passtype)
110                         break;
111         
112         if(rpass) {
113                 CompBuf *cbuf;
114                 
115                 iuser->pass= index;
116                 BKE_image_multilayer_index(ima->rr, iuser);
117                 cbuf= node_composit_get_image(rd, ima, iuser);
118                 
119                 return cbuf;
120         }
121         return NULL;
122 };
123
124 void outputs_multilayer_get(RenderData *rd, RenderLayer *rl, bNodeStack **out, Image *ima, ImageUser *iuser)
125 {
126         if(out[RRES_OUT_Z]->hasoutput)
127                 out[RRES_OUT_Z]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_Z);
128         if(out[RRES_OUT_VEC]->hasoutput)
129                 out[RRES_OUT_VEC]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_VECTOR);
130         if(out[RRES_OUT_NORMAL]->hasoutput)
131                 out[RRES_OUT_NORMAL]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_NORMAL);
132         if(out[RRES_OUT_UV]->hasoutput)
133                 out[RRES_OUT_UV]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_UV);
134         
135         if(out[RRES_OUT_RGBA]->hasoutput)
136                 out[RRES_OUT_RGBA]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_RGBA);
137         if(out[RRES_OUT_DIFF]->hasoutput)
138                 out[RRES_OUT_DIFF]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_DIFFUSE);
139         if(out[RRES_OUT_SPEC]->hasoutput)
140                 out[RRES_OUT_SPEC]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_SPEC);
141         if(out[RRES_OUT_SHADOW]->hasoutput)
142                 out[RRES_OUT_SHADOW]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_SHADOW);
143         if(out[RRES_OUT_AO]->hasoutput)
144                 out[RRES_OUT_AO]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_AO);
145         if(out[RRES_OUT_REFLECT]->hasoutput)
146                 out[RRES_OUT_REFLECT]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_REFLECT);
147         if(out[RRES_OUT_REFRACT]->hasoutput)
148                 out[RRES_OUT_REFRACT]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_REFRACT);
149         if(out[RRES_OUT_RADIO]->hasoutput)
150                 out[RRES_OUT_RADIO]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_RADIO);
151         if(out[RRES_OUT_INDEXOB]->hasoutput)
152                 out[RRES_OUT_INDEXOB]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_INDEXOB);
153         if(out[RRES_OUT_MIST]->hasoutput)
154                 out[RRES_OUT_MIST]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_MIST);
155         
156 };
157
158
159 static void node_composit_exec_image(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
160 {
161         
162         /* image assigned to output */
163         /* stack order input sockets: col, alpha */
164         if(node->id) {
165                 RenderData *rd= data;
166                 Image *ima= (Image *)node->id;
167                 ImageUser *iuser= (ImageUser *)node->storage;
168                 CompBuf *stackbuf= NULL;
169                 
170                 /* first set the right frame number in iuser */
171                 BKE_image_user_calc_imanr(iuser, rd->cfra, 0);
172                 
173                 /* force a load, we assume iuser index will be set OK anyway */
174                 if(ima->type==IMA_TYPE_MULTILAYER)
175                         BKE_image_get_ibuf(ima, iuser);
176                 
177                 if(ima->type==IMA_TYPE_MULTILAYER && ima->rr) {
178                         RenderLayer *rl= BLI_findlink(&ima->rr->layers, iuser->layer);
179                         
180                         if(rl) {
181                                 out[0]->data= stackbuf= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_COMBINED);
182                                 
183                                 /* go over all layers */
184                                 outputs_multilayer_get(rd, rl, out, ima, iuser);
185                         }
186                 }
187                 else {
188                         stackbuf= node_composit_get_image(rd, ima, iuser);
189
190                         /* put image on stack */        
191                         out[0]->data= stackbuf;
192                         
193                         if(out[2]->hasoutput)
194                                 out[2]->data= node_composit_get_zimage(node, rd);
195                 }
196                 
197                 /* alpha and preview for both types */
198                 if(stackbuf) {
199                         if(out[1]->hasoutput)
200                                 out[1]->data= valbuf_from_rgbabuf(stackbuf, CHAN_A);
201
202                         generate_preview(node, stackbuf);
203                 }
204         }       
205 };
206
207 static void node_composit_init_image(bNode* node)
208 {
209    ImageUser *iuser= MEM_callocN(sizeof(ImageUser), "node image user");
210    node->storage= iuser;
211    iuser->sfra= 1;
212    iuser->fie_ima= 2;
213    iuser->ok= 1;
214 }
215
216 bNodeType cmp_node_image= {
217         /* *next,*prev */       NULL, NULL,
218         /* type code   */       CMP_NODE_IMAGE,
219         /* name        */       "Image",
220         /* width+range */       120, 80, 300,
221         /* class+opts  */       NODE_CLASS_INPUT, NODE_PREVIEW|NODE_OPTIONS,
222         /* input sock  */       NULL,
223         /* output sock */       cmp_node_rlayers_out,
224         /* storage     */       "ImageUser",
225         /* execfunc    */       node_composit_exec_image,
226         /* butfunc     */       NULL,
227         /* initfunc    */       node_composit_init_image,
228         /* freestoragefunc    */        node_free_standard_storage,
229         /* copystoragefunc    */        node_copy_standard_storage,
230         /* id          */       NULL
231 };
232
233 /* **************** RENDER RESULT ******************** */
234
235 static CompBuf *compbuf_from_pass(RenderData *rd, RenderLayer *rl, int rectx, int recty, int passcode)
236 {
237    float *fp= RE_RenderLayerGetPass(rl, passcode);
238    if(fp) {
239       CompBuf *buf;
240       int buftype= CB_VEC3;
241
242       if(ELEM3(passcode, SCE_PASS_Z, SCE_PASS_INDEXOB, SCE_PASS_MIST))
243          buftype= CB_VAL;
244       else if(passcode==SCE_PASS_VECTOR)
245          buftype= CB_VEC4;
246       else if(ELEM(passcode, SCE_PASS_COMBINED, SCE_PASS_RGBA))
247          buftype= CB_RGBA;
248
249       if(rd->scemode & R_COMP_CROP)
250          buf= get_cropped_compbuf(&rd->disprect, fp, rectx, recty, buftype);
251       else {
252          buf= alloc_compbuf(rectx, recty, buftype, 0);
253          buf->rect= fp;
254       }
255       return buf;
256    }
257    return NULL;
258 };
259
260 void node_composit_rlayers_out(RenderData *rd, RenderLayer *rl, bNodeStack **out, int rectx, int recty)
261 {
262    if(out[RRES_OUT_Z]->hasoutput)
263       out[RRES_OUT_Z]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_Z);
264    if(out[RRES_OUT_VEC]->hasoutput)
265       out[RRES_OUT_VEC]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_VECTOR);
266    if(out[RRES_OUT_NORMAL]->hasoutput)
267       out[RRES_OUT_NORMAL]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_NORMAL);
268    if(out[RRES_OUT_UV]->hasoutput)
269       out[RRES_OUT_UV]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_UV);
270
271    if(out[RRES_OUT_RGBA]->hasoutput)
272       out[RRES_OUT_RGBA]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_RGBA);
273    if(out[RRES_OUT_DIFF]->hasoutput)
274       out[RRES_OUT_DIFF]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_DIFFUSE);
275    if(out[RRES_OUT_SPEC]->hasoutput)
276       out[RRES_OUT_SPEC]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_SPEC);
277    if(out[RRES_OUT_SHADOW]->hasoutput)
278       out[RRES_OUT_SHADOW]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_SHADOW);
279    if(out[RRES_OUT_AO]->hasoutput)
280       out[RRES_OUT_AO]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_AO);
281    if(out[RRES_OUT_REFLECT]->hasoutput)
282       out[RRES_OUT_REFLECT]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_REFLECT);
283    if(out[RRES_OUT_REFRACT]->hasoutput)
284       out[RRES_OUT_REFRACT]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_REFRACT);
285    if(out[RRES_OUT_RADIO]->hasoutput)
286       out[RRES_OUT_RADIO]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_RADIO);
287    if(out[RRES_OUT_INDEXOB]->hasoutput)
288            out[RRES_OUT_INDEXOB]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_INDEXOB);
289    if(out[RRES_OUT_MIST]->hasoutput)
290            out[RRES_OUT_MIST]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_MIST);
291
292 };
293
294 static void node_composit_exec_rlayers(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
295 {
296    Scene *sce= node->id?(Scene *)node->id:G.scene; /* G.scene is WEAK! */
297    RenderData *rd= data;
298    RenderResult *rr;
299
300    rr= RE_GetResult(RE_GetRender(sce->id.name));
301
302    if(rr) {
303       SceneRenderLayer *srl= BLI_findlink(&sce->r.layers, node->custom1);
304       if(srl) {
305          RenderLayer *rl= RE_GetRenderLayer(rr, srl->name);
306          if(rl && rl->rectf) {
307             CompBuf *stackbuf;
308
309             /* we put render rect on stack, cbuf knows rect is from other ibuf when freed! */
310             if(rd->scemode & R_COMP_CROP)
311                stackbuf= get_cropped_compbuf(&rd->disprect, rl->rectf, rr->rectx, rr->recty, CB_RGBA);
312             else {
313                stackbuf= alloc_compbuf(rr->rectx, rr->recty, CB_RGBA, 0);
314                stackbuf->rect= rl->rectf;
315             }
316             if(stackbuf==NULL) {
317                printf("Error; Preview Panel in UV Window returns zero sized image\n");
318             }
319             else {
320                stackbuf->xof= rr->xof;
321                stackbuf->yof= rr->yof;
322
323                /* put on stack */       
324                out[RRES_OUT_IMAGE]->data= stackbuf;
325
326                if(out[RRES_OUT_ALPHA]->hasoutput)
327                   out[RRES_OUT_ALPHA]->data= valbuf_from_rgbabuf(stackbuf, CHAN_A);
328
329                node_composit_rlayers_out(rd, rl, out, rr->rectx, rr->recty);
330
331                generate_preview(node, stackbuf);
332             }
333          }
334       }
335    }    
336 };
337
338
339 bNodeType cmp_node_rlayers= {
340         /* *next,*prev */       NULL, NULL,
341         /* type code   */       CMP_NODE_R_LAYERS,
342         /* name        */       "Render Layers",
343         /* width+range */       150, 100, 300,
344         /* class+opts  */       NODE_CLASS_INPUT, NODE_PREVIEW|NODE_OPTIONS,
345         /* input sock  */       NULL,
346         /* output sock */       cmp_node_rlayers_out,
347         /* storage     */       "",
348         /* execfunc    */       node_composit_exec_rlayers,
349         /* butfunc     */       NULL,
350         /* initfunc    */       NULL,
351         /* freestoragefunc    */        NULL,
352         /* copystoragefunc    */        NULL,
353         /* id          */       NULL
354
355 };
356
357