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