~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

Viewing changes to source/blender/nodes/intern/SHD_nodes/SHD_math.c

  • Committer: Bazaar Package Importer
  • Author(s): Florian Ernst
  • Date: 2007-05-17 11:47:59 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20070517114759-yp4ybrnhp2u7pk66
Tags: 2.44-1
* New upstream release.
* Drop debian/patches/01_64bits_stupidity, not needed anymore: as of this
  version blender is 64 bits safe again. Adjust README.Debian accordingly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * $Id: SHD_math.c,v 1.4 2007/04/04 13:58:12 jesterking Exp $
 
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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
/* **************** 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}, 
 
38
        { -1, 0, "" } 
 
39
};
 
40
 
 
41
static bNodeSocketType sh_node_math_out[]= { 
 
42
        { SOCK_VALUE, 0, "Value", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f}, 
 
43
        { -1, 0, "" } 
 
44
};
 
45
 
 
46
static void node_shader_exec_math(void *data, bNode *node, bNodeStack **in, 
 
47
bNodeStack **out) 
 
48
{
 
49
        switch(node->custom1){ 
 
50
        
 
51
        case 0: /* Add */
 
52
                out[0]->vec[0]= in[0]->vec[0] + in[1]->vec[0]; 
 
53
                break; 
 
54
        case 1: /* Subtract */
 
55
                out[0]->vec[0]= in[0]->vec[0] - in[1]->vec[0];
 
56
                break; 
 
57
        case 2: /* Multiply */
 
58
                out[0]->vec[0]= in[0]->vec[0] * in[1]->vec[0]; 
 
59
                break; 
 
60
        case 3: /* Divide */
 
61
                {
 
62
                        if(in[1]->vec[0]==0)    /* We don't want to divide by zero. */
 
63
                                out[0]->vec[0]= 0.0;
 
64
                        else
 
65
                                out[0]->vec[0]= in[0]->vec[0] / in[1]->vec[0];
 
66
                        }
 
67
                break;
 
68
        case 4: /* Sine */
 
69
                {
 
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]);
 
72
                        else
 
73
                                out[0]->vec[0]= sin(in[1]->vec[0]);
 
74
                }
 
75
                break;
 
76
        case 5: /* Cosine */
 
77
                {
 
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]);
 
80
                        else
 
81
                                out[0]->vec[0]= cos(in[1]->vec[0]);
 
82
                }
 
83
                break;
 
84
        case 6: /* Tangent */
 
85
                {
 
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]);
 
88
                        else
 
89
                                out[0]->vec[0]= tan(in[1]->vec[0]);
 
90
                }
 
91
                break;
 
92
        case 7: /* Arc-Sine */
 
93
                {
 
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]);
 
98
                                else
 
99
                                        out[0]->vec[0]= 0.0;
 
100
                        }
 
101
                        else {
 
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]);
 
105
                                else
 
106
                                        out[0]->vec[0]= 0.0;
 
107
                        }
 
108
                }
 
109
                break;
 
110
        case 8: /* Arc-Cosine */
 
111
                {
 
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]);
 
116
                                else
 
117
                                        out[0]->vec[0]= 0.0;
 
118
                        }
 
119
                        else {
 
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]);
 
123
                                else
 
124
                                        out[0]->vec[0]= 0.0;
 
125
                        }
 
126
                }
 
127
                break;
 
128
        case 9: /* Arc-Tangent */
 
129
                {
 
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]);
 
132
                        else
 
133
                                out[0]->vec[0]= atan(in[1]->vec[0]);
 
134
                }
 
135
                break;
 
136
        case 10: /* Power */
 
137
                {
 
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]);
 
141
                        else
 
142
                                out[0]->vec[0]= 0.0;
 
143
                }
 
144
                break;
 
145
        case 11: /* Logarithm */
 
146
                {
 
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]);
 
150
                        else
 
151
                                out[0]->vec[0]= 0.0;
 
152
                }
 
153
                break;
 
154
        case 12: /* Minimum */
 
155
                {
 
156
                        if( in[0]->vec[0] < in[1]->vec[0] )
 
157
                                out[0]->vec[0]= in[0]->vec[0];
 
158
                        else
 
159
                                out[0]->vec[0]= in[1]->vec[0];
 
160
                }
 
161
                break;
 
162
        case 13: /* Maximum */
 
163
                {
 
164
                        if( in[0]->vec[0] > in[1]->vec[0] )
 
165
                                out[0]->vec[0]= in[0]->vec[0];
 
166
                        else
 
167
                                out[0]->vec[0]= in[1]->vec[0];
 
168
                }
 
169
                break;
 
170
        case 14: /* Round */
 
171
                {
 
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);
 
174
                        else
 
175
                                out[0]->vec[0]= (int)(in[1]->vec[0] + 0.5f);
 
176
                }
 
177
                break; 
 
178
        } 
 
179
}
 
180
 
 
181
bNodeType sh_node_math= {
 
182
        /* *next,*prev */       NULL, NULL,
 
183
        /* type code   */       SH_NODE_MATH, 
 
184
        /* name        */       "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,
 
191
        /* butfunc     */       NULL,
 
192
        /* initfunc    */       NULL,
 
193
        /* freestoragefunc    */        NULL,
 
194
        /* copystoragefunc    */        NULL,
 
195
        /* id          */       NULL
 
196
};
 
197