03349a5783258c863bcea00af2b22bde4006b2c9
[blender.git] / source / blender / nodes / texture / nodes / node_texture_math.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): Robin Allen
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/nodes/texture/nodes/node_texture_math.c
29  *  \ingroup texnodes
30  */
31
32
33 #include "node_texture_util.h"
34 #include "NOD_texture.h"
35
36
37 /* **************** SCALAR MATH ******************** */ 
38 static bNodeSocketTemplate inputs[] = {
39         { SOCK_FLOAT, 1, N_("Value"), 0.5f, 0.5f, 0.5f, 1.0f, -100.0f, 100.0f, PROP_NONE},
40         { SOCK_FLOAT, 1, N_("Value"), 0.5f, 0.5f, 0.5f, 1.0f, -100.0f, 100.0f, PROP_NONE},
41         { -1, 0, "" }
42 };
43
44 static bNodeSocketTemplate outputs[] = {
45         { SOCK_FLOAT, 0, N_("Value")},
46         { -1, 0, "" }
47 };
48
49 static void valuefn(float *out, TexParams *p, bNode *node, bNodeStack **in, short thread)
50 {
51         float in0 = tex_input_value(in[0], p, thread);
52         float in1 = tex_input_value(in[1], p, thread);
53         
54         switch (node->custom1) {
55         
56         case 0: /* Add */
57                 *out= in0 + in1; 
58                 break; 
59         case 1: /* Subtract */
60                 *out= in0 - in1;
61                 break; 
62         case 2: /* Multiply */
63                 *out= in0 * in1; 
64                 break; 
65         case 3: /* Divide */
66                 {
67                         if (in1==0)     /* We don't want to divide by zero. */
68                                 *out= 0.0;
69                         else
70                                 *out= in0 / in1;
71                         }
72                 break;
73         case 4: /* Sine */
74                 {
75                         *out= sin(in0);
76                 }
77                 break;
78         case 5: /* Cosine */
79                 {
80                         *out= cos(in0);
81                 }
82                 break;
83         case 6: /* Tangent */
84                 {
85                         *out= tan(in0);
86                 }
87                 break;
88         case 7: /* Arc-Sine */
89                 {
90                         /* Can't do the impossible... */
91                         if (in0 <= 1 && in0 >= -1)
92                                 *out= asin(in0);
93                         else
94                                 *out= 0.0;
95                 }
96                 break;
97         case 8: /* Arc-Cosine */
98                 {
99                         /* Can't do the impossible... */
100                         if (in0 <= 1 && in0 >= -1)
101                                 *out= acos(in0);
102                         else
103                                 *out= 0.0;
104                 }
105                 break;
106         case 9: /* Arc-Tangent */
107                 {
108                         *out= atan(in0);
109                 }
110                 break;
111         case 10: /* Power */
112                 {
113                         /* Only raise negative numbers by full integers */
114                         if (in0 >= 0) {
115                                 out[0] = pow(in0, in1);
116                         }
117                         else {
118                                 float y_mod_1 = fmod(in1, 1);
119                                 if (y_mod_1 > 0.999f || y_mod_1 < 0.001f) {
120                                         *out = pow(in0, floor(in1 + 0.5f));
121                                 }
122                                 else {
123                                         *out = 0.0;
124                                 }
125                         }
126                 }
127                 break;
128         case 11: /* Logarithm */
129                 {
130                         /* Don't want any imaginary numbers... */
131                         if (in0 > 0  && in1 > 0)
132                                 *out= log(in0) / log(in1);
133                         else
134                                 *out= 0.0;
135                 }
136                 break;
137         case 12: /* Minimum */
138                 {
139                         if (in0 < in1)
140                                 *out= in0;
141                         else
142                                 *out= in1;
143                 }
144                 break;
145         case 13: /* Maximum */
146                 {
147                         if (in0 > in1)
148                                 *out= in0;
149                         else
150                                 *out= in1;
151                 }
152                 break;
153         case 14: /* Round */
154                 {
155                         *out= (in0 < 0) ? (int)(in0 - 0.5f) : (int)(in0 + 0.5f);
156                 }
157                 break; 
158                 
159         case 15: /* Less Than */
160                 {
161                         if (in0 < in1)
162                                 *out= 1.0f;
163                         else
164                                 *out= 0.0f;
165                 }
166                 break;
167                 
168         case 16: /* Greater Than */
169                 {
170                         if (in0 > in1)
171                                 *out= 1.0f;
172                         else
173                                 *out= 0.0f;
174                 }
175                 break;
176                 
177         default:
178                 fprintf(stderr,
179                         "%s:%d: unhandeld value in switch statement: %d\n",
180                         __FILE__, __LINE__, node->custom1);
181         }
182 }
183
184 static void exec(void *data, int UNUSED(thread), bNode *node, bNodeExecData *execdata, bNodeStack **in, bNodeStack **out)
185 {
186         tex_output(node, execdata, in, out[0], &valuefn, data);
187 }
188
189 void register_node_type_tex_math(void)
190 {
191         static bNodeType ntype;
192         
193         tex_node_type_base(&ntype, TEX_NODE_MATH, "Math", NODE_CLASS_CONVERTOR, NODE_OPTIONS);
194         node_type_socket_templates(&ntype, inputs, outputs);
195         node_type_size(&ntype, 120, 110, 160);
196         node_type_label(&ntype, node_math_label);
197         node_type_storage(&ntype, "node_math", NULL, NULL);
198         node_type_exec(&ntype, NULL, NULL, exec);
199         
200         nodeRegisterType(&ntype);
201 }