style cleanup: follow style guide for formatting of if/for/while loops, and else...
[blender.git] / source / blender / nodes / shader / nodes / node_shader_material.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/nodes/shader/nodes/node_shader_material.c
29  *  \ingroup shdnodes
30  */
31
32
33 #include "node_shader_util.h"
34
35 /* **************** MATERIAL ******************** */
36
37 static bNodeSocketTemplate sh_node_material_in[]= {
38         {       SOCK_RGBA, 1, "Color",          0.0f, 0.0f, 0.0f, 1.0f},
39         {       SOCK_RGBA, 1, "Spec",           0.0f, 0.0f, 0.0f, 1.0f},
40         {       SOCK_FLOAT, 1, "Refl",          0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, PROP_FACTOR},
41         {       SOCK_VECTOR, 1, "Normal",       0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, PROP_DIRECTION},
42         {       -1, 0, ""       }
43 };
44
45 static bNodeSocketTemplate sh_node_material_out[]= {
46         {       SOCK_RGBA, 0, "Color"},
47         {       SOCK_FLOAT, 0, "Alpha"},
48         {       SOCK_VECTOR, 0, "Normal"},
49         {       -1, 0, ""       }
50 };
51
52 /* **************** EXTENDED MATERIAL ******************** */
53
54 static bNodeSocketTemplate sh_node_material_ext_in[]= {
55         {       SOCK_RGBA, 1, "Color",          0.0f, 0.0f, 0.0f, 1.0f},
56         {       SOCK_RGBA, 1, "Spec",           0.0f, 0.0f, 0.0f, 1.0f},
57         {       SOCK_FLOAT, 1, "Refl",          0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, PROP_FACTOR},
58         {       SOCK_VECTOR, 1, "Normal",       0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, PROP_DIRECTION},
59         {       SOCK_RGBA, 1, "Mirror",         0.0f, 0.0f, 0.0f, 1.0f},
60         {       SOCK_FLOAT, 1, "Ambient",       0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, PROP_FACTOR},
61         {       SOCK_FLOAT, 1, "Emit",          0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, PROP_UNSIGNED},
62         {       SOCK_FLOAT, 1, "SpecTra",       0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, PROP_FACTOR},
63         {       SOCK_FLOAT, 1, "Ray Mirror",    0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_FACTOR},
64         {       SOCK_FLOAT, 1, "Alpha",         0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, PROP_UNSIGNED},
65         {       SOCK_FLOAT, 1, "Translucency",  0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, PROP_FACTOR},
66         {       -1, 0, ""       }
67 };
68
69 static bNodeSocketTemplate sh_node_material_ext_out[]= {
70         {       SOCK_RGBA, 0, "Color"},
71         {       SOCK_FLOAT, 0, "Alpha"},
72         {       SOCK_VECTOR, 0, "Normal"},
73         {       SOCK_RGBA, 0, "Diffuse"},
74         {       SOCK_RGBA, 0, "Spec"},
75         {       SOCK_RGBA, 0, "AO"},
76         {       -1, 0, ""       }
77 };
78
79 static void node_shader_exec_material(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
80 {
81         if (data && node->id) {
82                 ShadeResult shrnode;
83                 ShadeInput *shi;
84                 ShaderCallData *shcd= data;
85                 float col[4];
86                 bNodeSocket *sock;
87                 char hasinput[NUM_MAT_IN]= {'\0'};
88                 int i;
89                 
90                 /* note: cannot use the in[]->hasinput flags directly, as these are not necessarily
91                  * the constant input stack values (e.g. in case material node is inside a group).
92                  * we just want to know if a node input uses external data or the material setting.
93                  * this is an ugly hack, but so is this node as a whole.
94                  */
95                 for (sock=node->inputs.first, i=0; sock; sock=sock->next, ++i)
96                         hasinput[i] = (sock->link != NULL);
97                 
98                 shi= shcd->shi;
99                 shi->mat= (Material *)node->id;
100                 
101                 /* copy all relevant material vars, note, keep this synced with render_types.h */
102                 memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));
103                 shi->har= shi->mat->har;
104                 
105                 /* write values */
106                 if (hasinput[MAT_IN_COLOR])
107                         nodestack_get_vec(&shi->r, SOCK_VECTOR, in[MAT_IN_COLOR]);
108                 
109                 if (hasinput[MAT_IN_SPEC])
110                         nodestack_get_vec(&shi->specr, SOCK_VECTOR, in[MAT_IN_SPEC]);
111                 
112                 if (hasinput[MAT_IN_REFL])
113                         nodestack_get_vec(&shi->refl, SOCK_FLOAT, in[MAT_IN_REFL]);
114                 
115                 /* retrieve normal */
116                 if (hasinput[MAT_IN_NORMAL]) {
117                         nodestack_get_vec(shi->vn, SOCK_VECTOR, in[MAT_IN_NORMAL]);
118                         normalize_v3(shi->vn);
119                 }
120                 else
121                         copy_v3_v3(shi->vn, shi->vno);
122                 
123                 /* custom option to flip normal */
124                 if (node->custom1 & SH_NODE_MAT_NEG) {
125                         negate_v3(shi->vn);
126                 }
127                 
128                 if (node->type == SH_NODE_MATERIAL_EXT) {
129                         if (hasinput[MAT_IN_MIR])
130                                 nodestack_get_vec(&shi->mirr, SOCK_VECTOR, in[MAT_IN_MIR]);
131                         if (hasinput[MAT_IN_AMB])
132                                 nodestack_get_vec(&shi->amb, SOCK_FLOAT, in[MAT_IN_AMB]);
133                         if (hasinput[MAT_IN_EMIT])
134                                 nodestack_get_vec(&shi->emit, SOCK_FLOAT, in[MAT_IN_EMIT]);
135                         if (hasinput[MAT_IN_SPECTRA])
136                                 nodestack_get_vec(&shi->spectra, SOCK_FLOAT, in[MAT_IN_SPECTRA]);
137                         if (hasinput[MAT_IN_RAY_MIRROR])
138                                 nodestack_get_vec(&shi->ray_mirror, SOCK_FLOAT, in[MAT_IN_RAY_MIRROR]);
139                         if (hasinput[MAT_IN_ALPHA])
140                                 nodestack_get_vec(&shi->alpha, SOCK_FLOAT, in[MAT_IN_ALPHA]);
141                         if (hasinput[MAT_IN_TRANSLUCENCY])
142                                 nodestack_get_vec(&shi->translucency, SOCK_FLOAT, in[MAT_IN_TRANSLUCENCY]);                     
143                 }
144                 
145                 shi->nodes= 1; /* temp hack to prevent trashadow recursion */
146                 node_shader_lamp_loop(shi, &shrnode);   /* clears shrnode */
147                 shi->nodes= 0;
148                 
149                 /* write to outputs */
150                 if (node->custom1 & SH_NODE_MAT_DIFF) {
151                         copy_v3_v3(col, shrnode.combined);
152                         if (!(node->custom1 & SH_NODE_MAT_SPEC)) {
153                                 sub_v3_v3(col, shrnode.spec);
154                         }
155                 }
156                 else if (node->custom1 & SH_NODE_MAT_SPEC) {
157                         copy_v3_v3(col, shrnode.spec);
158                 }
159                 else
160                         col[0]= col[1]= col[2]= 0.0f;
161                 
162                 col[3]= shrnode.alpha;
163                 
164                 if (shi->do_preview)
165                         nodeAddToPreview(node, col, shi->xs, shi->ys, shi->do_manage);
166                 
167                 copy_v3_v3(out[MAT_OUT_COLOR]->vec, col);
168                 out[MAT_OUT_ALPHA]->vec[0]= shrnode.alpha;
169                 
170                 if (node->custom1 & SH_NODE_MAT_NEG) {
171                         shi->vn[0]= -shi->vn[0];
172                         shi->vn[1]= -shi->vn[1];
173                         shi->vn[2]= -shi->vn[2];
174                 }
175                 
176                 copy_v3_v3(out[MAT_OUT_NORMAL]->vec, shi->vn);
177                 
178                 /* Extended material options */
179                 if (node->type == SH_NODE_MATERIAL_EXT) {
180                         /* Shadow, Reflect, Refract, Radiosity, Speed seem to cause problems inside
181                          * a node tree :( */
182                         copy_v3_v3(out[MAT_OUT_DIFFUSE]->vec, shrnode.diff);
183                         copy_v3_v3(out[MAT_OUT_SPEC]->vec, shrnode.spec);
184                         copy_v3_v3(out[MAT_OUT_AO]->vec, shrnode.ao);
185                 }
186                 
187                 /* copy passes, now just active node */
188                 if (node->flag & NODE_ACTIVE_ID) {
189                         float combined[4], alpha;
190
191                         copy_v4_v4(combined, shcd->shr->combined);
192                         alpha= shcd->shr->alpha;
193
194                         *(shcd->shr)= shrnode;
195
196                         copy_v4_v4(shcd->shr->combined, combined);
197                         shcd->shr->alpha= alpha;
198                 }
199         }
200 }
201
202
203 static void node_shader_init_material(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
204 {
205         node->custom1= SH_NODE_MAT_DIFF|SH_NODE_MAT_SPEC;
206 }
207
208 /* XXX this is also done as a local static function in gpu_codegen.c,
209  * but we need this to hack around the crappy material node.
210  */
211 static GPUNodeLink *gpu_get_input_link(GPUNodeStack *in)
212 {
213         if (in->link)
214                 return in->link;
215         else
216                 return GPU_uniform(in->vec);
217 }
218
219 static int gpu_shader_material(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *out)
220 {
221         if (node->id) {
222                 GPUShadeInput shi;
223                 GPUShadeResult shr;
224                 bNodeSocket *sock;
225                 char hasinput[NUM_MAT_IN]= {'\0'};
226                 int i;
227                 
228                 /* note: cannot use the in[]->hasinput flags directly, as these are not necessarily
229                  * the constant input stack values (e.g. in case material node is inside a group).
230                  * we just want to know if a node input uses external data or the material setting.
231                  */
232                 for (sock=node->inputs.first, i=0; sock; sock=sock->next, ++i)
233                         hasinput[i] = (sock->link != NULL);
234
235                 GPU_shadeinput_set(mat, (Material*)node->id, &shi);
236
237                 /* write values */
238                 if (hasinput[MAT_IN_COLOR])
239                         shi.rgb = gpu_get_input_link(&in[MAT_IN_COLOR]);
240                 
241                 if (hasinput[MAT_IN_SPEC])
242                         shi.specrgb = gpu_get_input_link(&in[MAT_IN_SPEC]);
243                 
244                 if (hasinput[MAT_IN_REFL])
245                         shi.refl = gpu_get_input_link(&in[MAT_IN_REFL]);
246                 
247                 /* retrieve normal */
248                 if (hasinput[MAT_IN_NORMAL]) {
249                         GPUNodeLink *tmp;
250                         shi.vn = gpu_get_input_link(&in[MAT_IN_NORMAL]);
251                         GPU_link(mat, "vec_math_normalize", shi.vn, &shi.vn, &tmp);
252                 }
253                 
254                 /* custom option to flip normal */
255                 if (node->custom1 & SH_NODE_MAT_NEG)
256                         GPU_link(mat, "vec_math_negate", shi.vn, &shi.vn);
257
258                 if (node->type == SH_NODE_MATERIAL_EXT) {
259                         if (hasinput[MAT_IN_AMB])
260                                 shi.amb= gpu_get_input_link(&in[MAT_IN_AMB]);
261                         if (hasinput[MAT_IN_EMIT])
262                                 shi.emit= gpu_get_input_link(&in[MAT_IN_EMIT]);
263                         if (hasinput[MAT_IN_ALPHA])
264                                 shi.alpha= gpu_get_input_link(&in[MAT_IN_ALPHA]);
265                 }
266
267                 GPU_shaderesult_set(&shi, &shr); /* clears shr */
268                 
269                 /* write to outputs */
270                 if (node->custom1 & SH_NODE_MAT_DIFF) {
271                         out[MAT_OUT_COLOR].link= shr.combined;
272
273                         if (!(node->custom1 & SH_NODE_MAT_SPEC)) {
274                                 GPUNodeLink *link;
275                                 GPU_link(mat, "vec_math_sub", shr.combined, shr.spec, &out[MAT_OUT_COLOR].link, &link);
276                         }
277                 }
278                 else if (node->custom1 & SH_NODE_MAT_SPEC) {
279                         out[MAT_OUT_COLOR].link= shr.spec;
280                 }
281                 else
282                         GPU_link(mat, "set_rgb_zero", &out[MAT_OUT_COLOR].link);
283
284                 GPU_link(mat, "mtex_alpha_to_col", out[MAT_OUT_COLOR].link, shr.alpha, &out[MAT_OUT_COLOR].link);
285
286                 out[MAT_OUT_ALPHA].link = shr.alpha; //
287                 
288                 if (node->custom1 & SH_NODE_MAT_NEG)
289                         GPU_link(mat, "vec_math_negate", shi.vn, &shi.vn);
290                 out[MAT_OUT_NORMAL].link = shi.vn;
291
292                 if (node->type == SH_NODE_MATERIAL_EXT) {
293                         out[MAT_OUT_DIFFUSE].link = shr.diff;
294                         out[MAT_OUT_SPEC].link = shr.spec;
295                 }
296
297                 return 1;
298         }
299
300         return 0;
301 }
302
303 void register_node_type_sh_material(bNodeTreeType *ttype)
304 {
305         static bNodeType ntype;
306
307         node_type_base(ttype, &ntype, SH_NODE_MATERIAL, "Material", NODE_CLASS_INPUT, NODE_OPTIONS|NODE_PREVIEW);
308         node_type_compatibility(&ntype, NODE_OLD_SHADING);
309         node_type_socket_templates(&ntype, sh_node_material_in, sh_node_material_out);
310         node_type_size(&ntype, 120, 80, 240);
311         node_type_init(&ntype, node_shader_init_material);
312         node_type_exec(&ntype, node_shader_exec_material);
313         node_type_gpu(&ntype, gpu_shader_material);
314
315         nodeRegisterType(ttype, &ntype);
316 }
317
318
319 void register_node_type_sh_material_ext(bNodeTreeType *ttype)
320 {
321         static bNodeType ntype;
322
323         node_type_base(ttype, &ntype, SH_NODE_MATERIAL_EXT, "Extended Material", NODE_CLASS_INPUT, NODE_OPTIONS|NODE_PREVIEW);
324         node_type_compatibility(&ntype, NODE_OLD_SHADING);
325         node_type_socket_templates(&ntype, sh_node_material_ext_in, sh_node_material_ext_out);
326         node_type_size(&ntype, 120, 80, 240);
327         node_type_init(&ntype, node_shader_init_material);
328         node_type_exec(&ntype, node_shader_exec_material);
329         node_type_gpu(&ntype, gpu_shader_material);
330
331         nodeRegisterType(ttype, &ntype);
332 }