2
* $Id: SHD_math.c,v 1.4 2007/04/04 13:58:12 jesterking Exp $
4
* ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
* The Original Code is Copyright (C) 2005 Blender Foundation.
21
* All rights reserved.
23
* The Original Code is: all of this file.
25
* Contributor(s): none yet.
27
* ***** END GPL LICENSE BLOCK *****
30
#include "../SHD_util.h"
34
/* **************** SCALAR MATH ******************** */
35
static bNodeSocketType sh_node_math_in[]= {
36
{ SOCK_VALUE, 1, "Value", 0.5f, 0.5f, 0.5f, 1.0f, -100.0f, 100.0f},
37
{ SOCK_VALUE, 1, "Value", 0.5f, 0.5f, 0.5f, 1.0f, -100.0f, 100.0f},
41
static bNodeSocketType sh_node_math_out[]= {
42
{ SOCK_VALUE, 0, "Value", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
46
static void node_shader_exec_math(void *data, bNode *node, bNodeStack **in,
49
switch(node->custom1){
52
out[0]->vec[0]= in[0]->vec[0] + in[1]->vec[0];
54
case 1: /* Subtract */
55
out[0]->vec[0]= in[0]->vec[0] - in[1]->vec[0];
57
case 2: /* Multiply */
58
out[0]->vec[0]= in[0]->vec[0] * in[1]->vec[0];
62
if(in[1]->vec[0]==0) /* We don't want to divide by zero. */
65
out[0]->vec[0]= in[0]->vec[0] / in[1]->vec[0];
70
if(in[0]->hasinput || !in[1]->hasinput) /* This one only takes one input, so we've got to choose. */
71
out[0]->vec[0]= sin(in[0]->vec[0]);
73
out[0]->vec[0]= sin(in[1]->vec[0]);
78
if(in[0]->hasinput || !in[1]->hasinput) /* This one only takes one input, so we've got to choose. */
79
out[0]->vec[0]= cos(in[0]->vec[0]);
81
out[0]->vec[0]= cos(in[1]->vec[0]);
86
if(in[0]->hasinput || !in[1]->hasinput) /* This one only takes one input, so we've got to choose. */
87
out[0]->vec[0]= tan(in[0]->vec[0]);
89
out[0]->vec[0]= tan(in[1]->vec[0]);
92
case 7: /* Arc-Sine */
94
if(in[0]->hasinput || !in[1]->hasinput) { /* This one only takes one input, so we've got to choose. */
95
/* Can't do the impossible... */
96
if( in[0]->vec[0] <= 1 && in[0]->vec[0] >= -1 )
97
out[0]->vec[0]= asin(in[0]->vec[0]);
102
/* Can't do the impossible... */
103
if( in[1]->vec[0] <= 1 && in[1]->vec[0] >= -1 )
104
out[0]->vec[0]= asin(in[1]->vec[0]);
110
case 8: /* Arc-Cosine */
112
if(in[0]->hasinput || !in[1]->hasinput) { /* This one only takes one input, so we've got to choose. */
113
/* Can't do the impossible... */
114
if( in[0]->vec[0] <= 1 && in[0]->vec[0] >= -1 )
115
out[0]->vec[0]= acos(in[0]->vec[0]);
120
/* Can't do the impossible... */
121
if( in[1]->vec[0] <= 1 && in[1]->vec[0] >= -1 )
122
out[0]->vec[0]= acos(in[1]->vec[0]);
128
case 9: /* Arc-Tangent */
130
if(in[0]->hasinput || !in[1]->hasinput) /* This one only takes one input, so we've got to choose. */
131
out[0]->vec[0]= atan(in[0]->vec[0]);
133
out[0]->vec[0]= atan(in[1]->vec[0]);
138
/* Don't want any imaginary numbers... */
139
if( in[0]->vec[0] >= 0 )
140
out[0]->vec[0]= pow(in[0]->vec[0], in[1]->vec[0]);
145
case 11: /* Logarithm */
147
/* Don't want any imaginary numbers... */
148
if( in[0]->vec[0] > 0 && in[1]->vec[0] > 0 )
149
out[0]->vec[0]= log(in[0]->vec[0]) / log(in[1]->vec[0]);
154
case 12: /* Minimum */
156
if( in[0]->vec[0] < in[1]->vec[0] )
157
out[0]->vec[0]= in[0]->vec[0];
159
out[0]->vec[0]= in[1]->vec[0];
162
case 13: /* Maximum */
164
if( in[0]->vec[0] > in[1]->vec[0] )
165
out[0]->vec[0]= in[0]->vec[0];
167
out[0]->vec[0]= in[1]->vec[0];
172
if(in[0]->hasinput || !in[1]->hasinput) /* This one only takes one input, so we've got to choose. */
173
out[0]->vec[0]= (int)(in[0]->vec[0] + 0.5f);
175
out[0]->vec[0]= (int)(in[1]->vec[0] + 0.5f);
181
bNodeType sh_node_math= {
182
/* *next,*prev */ NULL, NULL,
183
/* type code */ SH_NODE_MATH,
185
/* width+range */ 120, 110, 160,
186
/* class+opts */ NODE_CLASS_CONVERTOR, NODE_OPTIONS,
187
/* input sock */ sh_node_math_in,
188
/* output sock */ sh_node_math_out,
189
/* storage */ "node_math",
190
/* execfunc */ node_shader_exec_math,
193
/* freestoragefunc */ NULL,
194
/* copystoragefunc */ NULL,