cf7c64c9d5e3910910e6a1bdb0004162223912f7
[blender.git] / source / blender / nodes / intern / SHD_util.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_util.c
31  *  \ingroup nodes
32  */
33
34
35 #include "SHD_util.h"
36
37
38
39
40
41 /* ****** */
42
43 void nodestack_get_vec(float *in, short type_in, bNodeStack *ns)
44 {
45         float *from= ns->vec;
46                 
47         if(type_in==SOCK_VALUE) {
48                 if(ns->sockettype==SOCK_VALUE)
49                         *in= *from;
50                 else 
51                         *in= 0.333333f*(from[0]+from[1]+from[2]);
52         }
53         else if(type_in==SOCK_VECTOR) {
54                 if(ns->sockettype==SOCK_VALUE) {
55                         in[0]= from[0];
56                         in[1]= from[0];
57                         in[2]= from[0];
58                 }
59                 else {
60                         VECCOPY(in, from);
61                 }
62         }
63         else { /* type_in==SOCK_RGBA */
64                 if(ns->sockettype==SOCK_RGBA) {
65                         QUATCOPY(in, from);
66                 }
67                 else if(ns->sockettype==SOCK_VALUE) {
68                         in[0]= from[0];
69                         in[1]= from[0];
70                         in[2]= from[0];
71                         in[3]= 1.0f;
72                 }
73                 else {
74                         VECCOPY(in, from);
75                         in[3]= 1.0f;
76                 }
77         }
78 }
79
80
81 /* ******************* execute and parse ************ */
82
83 void ntreeShaderExecTree(bNodeTree *ntree, ShadeInput *shi, ShadeResult *shr)
84 {
85         ShaderCallData scd;
86         
87         /* convert caller data to struct */
88         scd.shi= shi;
89         scd.shr= shr;
90         
91         /* each material node has own local shaderesult, with optional copying */
92         memset(shr, 0, sizeof(ShadeResult));
93                    
94         ntreeExecTree(ntree, &scd, shi->thread);        /* threads */
95         
96         /* better not allow negative for now */
97         if(shr->combined[0]<0.0f) shr->combined[0]= 0.0f;
98         if(shr->combined[1]<0.0f) shr->combined[1]= 0.0f;
99         if(shr->combined[2]<0.0f) shr->combined[2]= 0.0f;
100         
101 }
102
103 /* go over all used Geometry and Texture nodes, and return a texco flag */
104 /* no group inside needed, this function is called for groups too */
105 void ntreeShaderGetTexcoMode(bNodeTree *ntree, int r_mode, short *texco, int *mode)
106 {
107         bNode *node;
108         bNodeSocket *sock;
109         int a;
110         
111         ntreeSocketUseFlags(ntree);
112
113         for(node= ntree->nodes.first; node; node= node->next) {
114                 if(node->type==SH_NODE_TEXTURE) {
115                         if((r_mode & R_OSA) && node->id) {
116                                 Tex *tex= (Tex *)node->id;
117                                 if ELEM3(tex->type, TEX_IMAGE, TEX_PLUGIN, TEX_ENVMAP) 
118                                         *texco |= TEXCO_OSA|NEED_UV;
119                         }
120                         /* usability exception... without input we still give the node orcos */
121                         sock= node->inputs.first;
122                         if(sock==NULL || sock->link==NULL)
123                                 *texco |= TEXCO_ORCO|NEED_UV;
124                 }
125                 else if(node->type==SH_NODE_GEOMETRY) {
126                         /* note; sockets always exist for the given type! */
127                         for(a=0, sock= node->outputs.first; sock; sock= sock->next, a++) {
128                                 if(sock->flag & SOCK_IN_USE) {
129                                         switch(a) {
130                                                 case GEOM_OUT_GLOB: 
131                                                         *texco |= TEXCO_GLOB|NEED_UV; break;
132                                                 case GEOM_OUT_VIEW: 
133                                                         *texco |= TEXCO_VIEW|NEED_UV; break;
134                                                 case GEOM_OUT_ORCO: 
135                                                         *texco |= TEXCO_ORCO|NEED_UV; break;
136                                                 case GEOM_OUT_UV: 
137                                                         *texco |= TEXCO_UV|NEED_UV; break;
138                                                 case GEOM_OUT_NORMAL: 
139                                                         *texco |= TEXCO_NORM|NEED_UV; break;
140                                                 case GEOM_OUT_VCOL:
141                                                         *texco |= NEED_UV; *mode |= MA_VERTEXCOL; break;
142                                         }
143                                 }
144                         }
145                 }
146         }
147 }
148
149 /* nodes that use ID data get synced with local data */
150 void nodeShaderSynchronizeID(bNode *node, int copyto)
151 {
152         if(node->id==NULL) return;
153         
154         if(ELEM(node->type, SH_NODE_MATERIAL, SH_NODE_MATERIAL_EXT)) {
155                 bNodeSocket *sock;
156                 Material *ma= (Material *)node->id;
157                 int a;
158                 
159                 /* hrmf, case in loop isnt super fast, but we dont edit 100s of material at same time either! */
160                 for(a=0, sock= node->inputs.first; sock; sock= sock->next, a++) {
161                         if(!(sock->flag & SOCK_HIDDEN)) {
162                                 if(copyto) {
163                                         switch(a) {
164                                                 case MAT_IN_COLOR:
165                                                         VECCOPY(&ma->r, sock->ns.vec); break;
166                                                 case MAT_IN_SPEC:
167                                                         VECCOPY(&ma->specr, sock->ns.vec); break;
168                                                 case MAT_IN_REFL:
169                                                         ma->ref= sock->ns.vec[0]; break;
170                                                 case MAT_IN_MIR:
171                                                         VECCOPY(&ma->mirr, sock->ns.vec); break;
172                                                 case MAT_IN_AMB:
173                                                         ma->amb= sock->ns.vec[0]; break;
174                                                 case MAT_IN_EMIT:
175                                                         ma->emit= sock->ns.vec[0]; break;
176                                                 case MAT_IN_SPECTRA:
177                                                         ma->spectra= sock->ns.vec[0]; break;
178                                                 case MAT_IN_RAY_MIRROR:
179                                                         ma->ray_mirror= sock->ns.vec[0]; break;
180                                                 case MAT_IN_ALPHA:
181                                                         ma->alpha= sock->ns.vec[0]; break;
182                                                 case MAT_IN_TRANSLUCENCY:
183                                                         ma->translucency= sock->ns.vec[0]; break;
184                                         }
185                                 }
186                                 else {
187                                         switch(a) {
188                                                 case MAT_IN_COLOR:
189                                                         VECCOPY(sock->ns.vec, &ma->r); break;
190                                                 case MAT_IN_SPEC:
191                                                         VECCOPY(sock->ns.vec, &ma->specr); break;
192                                                 case MAT_IN_REFL:
193                                                         sock->ns.vec[0]= ma->ref; break;
194                                                 case MAT_IN_MIR:
195                                                         VECCOPY(sock->ns.vec, &ma->mirr); break;
196                                                 case MAT_IN_AMB:
197                                                         sock->ns.vec[0]= ma->amb; break;
198                                                 case MAT_IN_EMIT:
199                                                         sock->ns.vec[0]= ma->emit; break;
200                                                 case MAT_IN_SPECTRA:
201                                                         sock->ns.vec[0]= ma->spectra; break;
202                                                 case MAT_IN_RAY_MIRROR:
203                                                         sock->ns.vec[0]= ma->ray_mirror; break;
204                                                 case MAT_IN_ALPHA:
205                                                         sock->ns.vec[0]= ma->alpha; break;
206                                                 case MAT_IN_TRANSLUCENCY:
207                                                         sock->ns.vec[0]= ma->translucency; break;
208                                         }
209                                 }
210                         }
211                 }
212         }
213         
214 }