converted more mixed tab/space indentations to tabs. only whitespace changes.
[blender.git] / source / blender / nodes / intern / SHD_nodes / SHD_material.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2005 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 /** \file blender/nodes/intern/SHD_nodes/SHD_material.c
31  *  \ingroup shdnodes
32  */
33
34
35 #include "../SHD_util.h"
36
37 /* **************** MATERIAL ******************** */
38
39 static bNodeSocketType sh_node_material_in[]= {
40         {       SOCK_RGBA, 1, "Color",          0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
41         {       SOCK_RGBA, 1, "Spec",           0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
42         {       SOCK_VALUE, 1, "Refl",          0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
43         {       SOCK_VECTOR, 1, "Normal",       0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},
44         {       -1, 0, ""       }
45 };
46
47 static bNodeSocketType sh_node_material_out[]= {
48         {       SOCK_RGBA, 0, "Color",          0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
49         {       SOCK_VALUE, 0, "Alpha",         1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
50         {       SOCK_VECTOR, 0, "Normal",       0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},
51         {       -1, 0, ""       }
52 };
53
54 /* **************** EXTENDED MATERIAL ******************** */
55
56 static bNodeSocketType sh_node_material_ext_in[]= {
57         {       SOCK_RGBA, 1, "Color",          0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
58         {       SOCK_RGBA, 1, "Spec",           0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
59         {       SOCK_VALUE, 1, "Refl",          0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
60         {       SOCK_VECTOR, 1, "Normal",       0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},
61         {       SOCK_RGBA, 1, "Mirror",         0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
62         {       SOCK_VALUE, 1, "Ambient",       0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
63         {       SOCK_VALUE, 1, "Emit",          0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
64         {       SOCK_VALUE, 1, "SpecTra",       0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
65         {       SOCK_VALUE, 1, "Ray Mirror",    0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
66         {       SOCK_VALUE, 1, "Alpha",         0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
67         {       SOCK_VALUE, 1, "Translucency",  0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
68         {       -1, 0, ""       }
69 };
70
71 static bNodeSocketType sh_node_material_ext_out[]= {
72         {       SOCK_RGBA, 0, "Color",          0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
73         {       SOCK_VALUE, 0, "Alpha",         1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
74         {       SOCK_VECTOR, 0, "Normal",       0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},
75         {       SOCK_RGBA, 0, "Diffuse",                0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
76         {       SOCK_RGBA, 0, "Spec",           0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
77         {       SOCK_RGBA, 0, "AO",             0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
78         {       -1, 0, ""       }
79 };
80
81 static void node_shader_exec_material(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
82 {
83         if(data && node->id) {
84                 ShadeResult shrnode;
85                 ShadeInput *shi;
86                 ShaderCallData *shcd= data;
87                 float col[4];
88                 
89                 shi= shcd->shi;
90                 shi->mat= (Material *)node->id;
91                 
92                 /* copy all relevant material vars, note, keep this synced with render_types.h */
93                 memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));
94                 shi->har= shi->mat->har;
95                 
96                 /* write values */
97                 if(in[MAT_IN_COLOR]->hasinput)
98                         nodestack_get_vec(&shi->r, SOCK_VECTOR, in[MAT_IN_COLOR]);
99                 
100                 if(in[MAT_IN_SPEC]->hasinput)
101                         nodestack_get_vec(&shi->specr, SOCK_VECTOR, in[MAT_IN_SPEC]);
102                 
103                 if(in[MAT_IN_REFL]->hasinput)
104                         nodestack_get_vec(&shi->refl, SOCK_VALUE, in[MAT_IN_REFL]);
105                 
106                 /* retrieve normal */
107                 if(in[MAT_IN_NORMAL]->hasinput) {
108                         nodestack_get_vec(shi->vn, SOCK_VECTOR, in[MAT_IN_NORMAL]);
109                         normalize_v3(shi->vn);
110                 }
111                 else
112                         VECCOPY(shi->vn, shi->vno);
113                 
114                 /* custom option to flip normal */
115                 if(node->custom1 & SH_NODE_MAT_NEG) {
116                         shi->vn[0]= -shi->vn[0];
117                         shi->vn[1]= -shi->vn[1];
118                         shi->vn[2]= -shi->vn[2];
119                 }
120                 
121                 if (node->type == SH_NODE_MATERIAL_EXT) {
122                         if(in[MAT_IN_MIR]->hasinput)
123                                 nodestack_get_vec(&shi->mirr, SOCK_VECTOR, in[MAT_IN_MIR]);
124                         if(in[MAT_IN_AMB]->hasinput)
125                                 nodestack_get_vec(&shi->amb, SOCK_VALUE, in[MAT_IN_AMB]);
126                         if(in[MAT_IN_EMIT]->hasinput)
127                                 nodestack_get_vec(&shi->emit, SOCK_VALUE, in[MAT_IN_EMIT]);
128                         if(in[MAT_IN_SPECTRA]->hasinput)
129                                 nodestack_get_vec(&shi->spectra, SOCK_VALUE, in[MAT_IN_SPECTRA]);
130                         if(in[MAT_IN_RAY_MIRROR]->hasinput)
131                                 nodestack_get_vec(&shi->ray_mirror, SOCK_VALUE, in[MAT_IN_RAY_MIRROR]);
132                         if(in[MAT_IN_ALPHA]->hasinput)
133                                 nodestack_get_vec(&shi->alpha, SOCK_VALUE, in[MAT_IN_ALPHA]);
134                         if(in[MAT_IN_TRANSLUCENCY]->hasinput)
135                                 nodestack_get_vec(&shi->translucency, SOCK_VALUE, in[MAT_IN_TRANSLUCENCY]);                     
136                 }
137                 
138                 shi->nodes= 1; /* temp hack to prevent trashadow recursion */
139                 node_shader_lamp_loop(shi, &shrnode);   /* clears shrnode */
140                 shi->nodes= 0;
141                 
142                 /* write to outputs */
143                 if(node->custom1 & SH_NODE_MAT_DIFF) {
144                         VECCOPY(col, shrnode.combined);
145                         if(!(node->custom1 & SH_NODE_MAT_SPEC)) {
146                                 sub_v3_v3(col, shrnode.spec);
147                         }
148                 }
149                 else if(node->custom1 & SH_NODE_MAT_SPEC) {
150                         VECCOPY(col, shrnode.spec);
151                 }
152                 else
153                         col[0]= col[1]= col[2]= 0.0f;
154                 
155                 col[3]= shrnode.alpha;
156                 
157                 if(shi->do_preview)
158                         nodeAddToPreview(node, col, shi->xs, shi->ys, shi->do_manage);
159                 
160                 VECCOPY(out[MAT_OUT_COLOR]->vec, col);
161                 out[MAT_OUT_ALPHA]->vec[0]= shrnode.alpha;
162                 
163                 if(node->custom1 & SH_NODE_MAT_NEG) {
164                         shi->vn[0]= -shi->vn[0];
165                         shi->vn[1]= -shi->vn[1];
166                         shi->vn[2]= -shi->vn[2];
167                 }
168                 
169                 VECCOPY(out[MAT_OUT_NORMAL]->vec, shi->vn);
170                 
171                 /* Extended material options */
172                 if (node->type == SH_NODE_MATERIAL_EXT) {
173                         /* Shadow, Reflect, Refract, Radiosity, Speed seem to cause problems inside
174                          * a node tree :( */
175                         VECCOPY(out[MAT_OUT_DIFFUSE]->vec, shrnode.diff);
176                         VECCOPY(out[MAT_OUT_SPEC]->vec, shrnode.spec);
177                         VECCOPY(out[MAT_OUT_AO]->vec, shrnode.ao);
178                 }
179                 
180                 /* copy passes, now just active node */
181                 if(node->flag & NODE_ACTIVE_ID) {
182                         float combined[4], alpha;
183
184                         copy_v4_v4(combined, shcd->shr->combined);
185                         alpha= shcd->shr->alpha;
186
187                         *(shcd->shr)= shrnode;
188
189                         copy_v4_v4(shcd->shr->combined, combined);
190                         shcd->shr->alpha= alpha;
191                 }
192         }
193 }
194
195
196 static void node_shader_init_material(bNode* node)
197 {
198         node->custom1= SH_NODE_MAT_DIFF|SH_NODE_MAT_SPEC;
199 }
200
201 static int gpu_shader_material(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *out)
202 {
203         if(node->id) {
204                 GPUShadeInput shi;
205                 GPUShadeResult shr;
206
207                 GPU_shadeinput_set(mat, (Material*)node->id, &shi);
208
209                 /* write values */
210                 if(in[MAT_IN_COLOR].hasinput)
211                         shi.rgb = in[MAT_IN_COLOR].link;
212                 
213                 if(in[MAT_IN_SPEC].hasinput)
214                         shi.specrgb = in[MAT_IN_SPEC].link;
215                 
216                 if(in[MAT_IN_REFL].hasinput)
217                         shi.refl = in[MAT_IN_REFL].link;
218                 
219                 /* retrieve normal */
220                 if(in[MAT_IN_NORMAL].hasinput) {
221                         GPUNodeLink *tmp;
222                         shi.vn = in[MAT_IN_NORMAL].link;
223                         GPU_link(mat, "vec_math_normalize", shi.vn, &shi.vn, &tmp);
224                 }
225                 
226                 /* custom option to flip normal */
227                 if(node->custom1 & SH_NODE_MAT_NEG)
228                         GPU_link(mat, "vec_math_negate", shi.vn, &shi.vn);
229
230                 if (node->type == SH_NODE_MATERIAL_EXT) {
231                         if(in[MAT_IN_AMB].hasinput)
232                                 shi.amb= in[MAT_IN_AMB].link;
233                         if(in[MAT_IN_EMIT].hasinput)
234                                 shi.emit= in[MAT_IN_EMIT].link;
235                         if(in[MAT_IN_ALPHA].hasinput)
236                                 shi.alpha= in[MAT_IN_ALPHA].link;
237                 }
238
239                 GPU_shaderesult_set(&shi, &shr); /* clears shr */
240                 
241                 /* write to outputs */
242                 if(node->custom1 & SH_NODE_MAT_DIFF) {
243                         out[MAT_OUT_COLOR].link= shr.combined;
244
245                         if(!(node->custom1 & SH_NODE_MAT_SPEC)) {
246                                 GPUNodeLink *link;
247                                 GPU_link(mat, "vec_math_sub", shr.combined, shr.spec, &out[MAT_OUT_COLOR].link, &link);
248                         }
249                 }
250                 else if(node->custom1 & SH_NODE_MAT_SPEC) {
251                         out[MAT_OUT_COLOR].link= shr.spec;
252                 }
253                 else
254                         GPU_link(mat, "set_rgb_zero", &out[MAT_OUT_COLOR].link);
255
256                 GPU_link(mat, "mtex_alpha_to_col", out[MAT_OUT_COLOR].link, shr.alpha, &out[MAT_OUT_COLOR].link);
257
258                 out[MAT_OUT_ALPHA].link = shr.alpha; //
259                 
260                 if(node->custom1 & SH_NODE_MAT_NEG)
261                         GPU_link(mat, "vec_math_negate", shi.vn, &shi.vn);
262                 out[MAT_OUT_NORMAL].link = shi.vn;
263
264                 if (node->type == SH_NODE_MATERIAL_EXT) {
265                         out[MAT_OUT_DIFFUSE].link = shr.diff;
266                         out[MAT_OUT_SPEC].link = shr.spec;
267                 }
268
269                 return 1;
270         }
271
272         return 0;
273 }
274
275 void register_node_type_sh_material(ListBase *lb)
276 {
277         static bNodeType ntype;
278
279         node_type_base(&ntype, SH_NODE_MATERIAL, "Material", NODE_CLASS_INPUT, NODE_OPTIONS|NODE_PREVIEW,
280                 sh_node_material_in, sh_node_material_out);
281         node_type_size(&ntype, 120, 80, 240);
282         node_type_init(&ntype, node_shader_init_material);
283         node_type_exec(&ntype, node_shader_exec_material);
284         node_type_gpu(&ntype, gpu_shader_material);
285
286         nodeRegisterType(lb, &ntype);
287 }
288
289
290 void register_node_type_sh_material_ext(ListBase *lb)
291 {
292         static bNodeType ntype;
293
294         node_type_base(&ntype, SH_NODE_MATERIAL_EXT, "Extended Material", NODE_CLASS_INPUT, NODE_OPTIONS|NODE_PREVIEW,
295                 sh_node_material_ext_in, sh_node_material_ext_out);
296         node_type_size(&ntype, 120, 80, 240);
297         node_type_init(&ntype, node_shader_init_material);
298         node_type_exec(&ntype, node_shader_exec_material);
299         node_type_gpu(&ntype, gpu_shader_material);
300
301         nodeRegisterType(lb, &ntype);
302 }
303
304