~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/blender/nodes/shader/nodes/node_shader_math.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 
35
35
 
36
36
/* **************** SCALAR MATH ******************** */ 
37
 
static bNodeSocketTemplate sh_node_math_in[]= { 
38
 
        { SOCK_FLOAT, 1, "Value", 0.5f, 0.5f, 0.5f, 1.0f, -10000.0f, 10000.0f, PROP_NONE}, 
39
 
        { SOCK_FLOAT, 1, "Value", 0.5f, 0.5f, 0.5f, 1.0f, -10000.0f, 10000.0f, PROP_NONE}, 
40
 
        { -1, 0, "" } 
 
37
static bNodeSocketTemplate sh_node_math_in[] = {
 
38
        { SOCK_FLOAT, 1, N_("Value"), 0.5f, 0.5f, 0.5f, 1.0f, -10000.0f, 10000.0f, PROP_NONE},
 
39
        { SOCK_FLOAT, 1, N_("Value"), 0.5f, 0.5f, 0.5f, 1.0f, -10000.0f, 10000.0f, PROP_NONE},
 
40
        { -1, 0, "" }
41
41
};
42
42
 
43
 
static bNodeSocketTemplate sh_node_math_out[]= { 
44
 
        { SOCK_FLOAT, 0, "Value"}, 
45
 
        { -1, 0, "" } 
 
43
static bNodeSocketTemplate sh_node_math_out[] = {
 
44
        { SOCK_FLOAT, 0, N_("Value")},
 
45
        { -1, 0, "" }
46
46
};
47
47
 
48
48
static void node_shader_exec_math(void *UNUSED(data), bNode *node, bNodeStack **in, 
49
49
bNodeStack **out) 
50
50
{
51
 
        switch(node->custom1) { 
 
51
        switch (node->custom1) {
52
52
        
53
53
        case 0: /* Add */
54
 
                out[0]->vec[0]= in[0]->vec[0] + in[1]->vec[0]; 
 
54
                out[0]->vec[0] = in[0]->vec[0] + in[1]->vec[0];
55
55
                break; 
56
56
        case 1: /* Subtract */
57
 
                out[0]->vec[0]= in[0]->vec[0] - in[1]->vec[0];
 
57
                out[0]->vec[0] = in[0]->vec[0] - in[1]->vec[0];
58
58
                break; 
59
59
        case 2: /* Multiply */
60
 
                out[0]->vec[0]= in[0]->vec[0] * in[1]->vec[0]; 
 
60
                out[0]->vec[0] = in[0]->vec[0] * in[1]->vec[0];
61
61
                break; 
62
62
        case 3: /* Divide */
63
63
                {
64
64
                        if (in[1]->vec[0]==0)   /* We don't want to divide by zero. */
65
 
                                out[0]->vec[0]= 0.0;
 
65
                                out[0]->vec[0] = 0.0;
66
66
                        else
67
 
                                out[0]->vec[0]= in[0]->vec[0] / in[1]->vec[0];
 
67
                                out[0]->vec[0] = in[0]->vec[0] / in[1]->vec[0];
68
68
                        }
69
69
                break;
70
70
        case 4: /* Sine */
71
71
                {
72
72
                        if (in[0]->hasinput || !in[1]->hasinput)  /* This one only takes one input, so we've got to choose. */
73
 
                                out[0]->vec[0]= sin(in[0]->vec[0]);
 
73
                                out[0]->vec[0] = sin(in[0]->vec[0]);
74
74
                        else
75
 
                                out[0]->vec[0]= sin(in[1]->vec[0]);
 
75
                                out[0]->vec[0] = sin(in[1]->vec[0]);
76
76
                }
77
77
                break;
78
78
        case 5: /* Cosine */
79
79
                {
80
 
                        if (in[0]->hasinput || !in[1]->hasinput)  /* This one only takes one input, so we've got to choose. */  
81
 
                                out[0]->vec[0]= cos(in[0]->vec[0]);
 
80
                        if (in[0]->hasinput || !in[1]->hasinput)  /* This one only takes one input, so we've got to choose. */
 
81
                                out[0]->vec[0] = cos(in[0]->vec[0]);
82
82
                        else
83
 
                                out[0]->vec[0]= cos(in[1]->vec[0]);
 
83
                                out[0]->vec[0] = cos(in[1]->vec[0]);
84
84
                }
85
85
                break;
86
86
        case 6: /* Tangent */
87
87
                {
88
 
                        if (in[0]->hasinput || !in[1]->hasinput)  /* This one only takes one input, so we've got to choose. */  
89
 
                                out[0]->vec[0]= tan(in[0]->vec[0]);
 
88
                        if (in[0]->hasinput || !in[1]->hasinput)  /* This one only takes one input, so we've got to choose. */
 
89
                                out[0]->vec[0] = tan(in[0]->vec[0]);
90
90
                        else
91
 
                                out[0]->vec[0]= tan(in[1]->vec[0]);
 
91
                                out[0]->vec[0] = tan(in[1]->vec[0]);
92
92
                }
93
93
                break;
94
94
        case 7: /* Arc-Sine */
96
96
                        if (in[0]->hasinput || !in[1]->hasinput) { /* This one only takes one input, so we've got to choose. */
97
97
                                /* Can't do the impossible... */
98
98
                                if ( in[0]->vec[0] <= 1 && in[0]->vec[0] >= -1 )
99
 
                                        out[0]->vec[0]= asin(in[0]->vec[0]);
 
99
                                        out[0]->vec[0] = asin(in[0]->vec[0]);
100
100
                                else
101
 
                                        out[0]->vec[0]= 0.0;
 
101
                                        out[0]->vec[0] = 0.0;
102
102
                        }
103
103
                        else {
104
104
                                /* Can't do the impossible... */
105
105
                                if ( in[1]->vec[0] <= 1 && in[1]->vec[0] >= -1 )
106
 
                                        out[0]->vec[0]= asin(in[1]->vec[0]);
 
106
                                        out[0]->vec[0] = asin(in[1]->vec[0]);
107
107
                                else
108
 
                                        out[0]->vec[0]= 0.0;
 
108
                                        out[0]->vec[0] = 0.0;
109
109
                        }
110
110
                }
111
111
                break;
114
114
                        if (in[0]->hasinput || !in[1]->hasinput) { /* This one only takes one input, so we've got to choose. */
115
115
                                /* Can't do the impossible... */
116
116
                                if ( in[0]->vec[0] <= 1 && in[0]->vec[0] >= -1 )
117
 
                                        out[0]->vec[0]= acos(in[0]->vec[0]);
 
117
                                        out[0]->vec[0] = acos(in[0]->vec[0]);
118
118
                                else
119
 
                                        out[0]->vec[0]= 0.0;
 
119
                                        out[0]->vec[0] = 0.0;
120
120
                        }
121
121
                        else {
122
122
                                /* Can't do the impossible... */
123
123
                                if ( in[1]->vec[0] <= 1 && in[1]->vec[0] >= -1 )
124
 
                                        out[0]->vec[0]= acos(in[1]->vec[0]);
 
124
                                        out[0]->vec[0] = acos(in[1]->vec[0]);
125
125
                                else
126
 
                                        out[0]->vec[0]= 0.0;
 
126
                                        out[0]->vec[0] = 0.0;
127
127
                        }
128
128
                }
129
129
                break;
130
130
        case 9: /* Arc-Tangent */
131
131
                {
132
132
                        if (in[0]->hasinput || !in[1]->hasinput) /* This one only takes one input, so we've got to choose. */
133
 
                                out[0]->vec[0]= atan(in[0]->vec[0]);
 
133
                                out[0]->vec[0] = atan(in[0]->vec[0]);
134
134
                        else
135
 
                                out[0]->vec[0]= atan(in[1]->vec[0]);
 
135
                                out[0]->vec[0] = atan(in[1]->vec[0]);
136
136
                }
137
137
                break;
138
138
        case 10: /* Power */
139
139
                {
140
 
                        /* Don't want any imaginary numbers... */
141
 
                        if ( in[0]->vec[0] >= 0 )
142
 
                                out[0]->vec[0]= pow(in[0]->vec[0], in[1]->vec[0]);
143
 
                        else
144
 
                                out[0]->vec[0]= 0.0;
 
140
                        /* Only raise negative numbers by full integers */
 
141
                        if ( in[0]->vec[0] >= 0 ) {
 
142
                                out[0]->vec[0] = pow(in[0]->vec[0], in[1]->vec[0]);
 
143
                        }
 
144
                        else {
 
145
                                float y_mod_1 = fabsf(fmodf(in[1]->vec[0], 1.0f));
 
146
                                
 
147
                                /* if input value is not nearly an integer, fall back to zero, nicer than straight rounding */
 
148
                                if (y_mod_1 > 0.999f || y_mod_1 < 0.001f) {
 
149
                                        out[0]->vec[0] = powf(in[0]->vec[0], floorf(in[1]->vec[0] + 0.5f));
 
150
                                }
 
151
                                else {
 
152
                                        out[0]->vec[0] = 0.0f;
 
153
                                }
 
154
                        }
 
155
 
145
156
                }
146
157
                break;
147
158
        case 11: /* Logarithm */
148
159
                {
149
160
                        /* Don't want any imaginary numbers... */
150
161
                        if ( in[0]->vec[0] > 0  && in[1]->vec[0] > 0 )
151
 
                                out[0]->vec[0]= log(in[0]->vec[0]) / log(in[1]->vec[0]);
 
162
                                out[0]->vec[0] = log(in[0]->vec[0]) / log(in[1]->vec[0]);
152
163
                        else
153
 
                                out[0]->vec[0]= 0.0;
 
164
                                out[0]->vec[0] = 0.0;
154
165
                }
155
166
                break;
156
167
        case 12: /* Minimum */
157
168
                {
158
169
                        if ( in[0]->vec[0] < in[1]->vec[0] )
159
 
                                out[0]->vec[0]= in[0]->vec[0];
 
170
                                out[0]->vec[0] = in[0]->vec[0];
160
171
                        else
161
 
                                out[0]->vec[0]= in[1]->vec[0];
 
172
                                out[0]->vec[0] = in[1]->vec[0];
162
173
                }
163
174
                break;
164
175
        case 13: /* Maximum */
165
176
                {
166
177
                        if ( in[0]->vec[0] > in[1]->vec[0] )
167
 
                                out[0]->vec[0]= in[0]->vec[0];
 
178
                                out[0]->vec[0] = in[0]->vec[0];
168
179
                        else
169
 
                                out[0]->vec[0]= in[1]->vec[0];
 
180
                                out[0]->vec[0] = in[1]->vec[0];
170
181
                }
171
182
                break;
172
183
        case 14: /* Round */
173
184
                {
174
185
                        if (in[0]->hasinput || !in[1]->hasinput) /* This one only takes one input, so we've got to choose. */
175
 
                                out[0]->vec[0]= (in[0]->vec[0]<0)?(int)(in[0]->vec[0] - 0.5f):(int)(in[0]->vec[0] + 0.5f);
 
186
                                out[0]->vec[0] = (in[0]->vec[0]<0)?(int)(in[0]->vec[0] - 0.5f):(int)(in[0]->vec[0] + 0.5f);
176
187
                        else
177
 
                                out[0]->vec[0]= (in[1]->vec[0]<0)?(int)(in[1]->vec[0] - 0.5f):(int)(in[1]->vec[0] + 0.5f);
 
188
                                out[0]->vec[0] = (in[1]->vec[0]<0)?(int)(in[1]->vec[0] - 0.5f):(int)(in[1]->vec[0] + 0.5f);
178
189
                }
179
190
                break;
180
191
        case 15: /* Less Than */
181
192
                {
182
193
                        if ( in[0]->vec[0] < in[1]->vec[0] )
183
 
                                out[0]->vec[0]= 1.0f;
 
194
                                out[0]->vec[0] = 1.0f;
184
195
                        else
185
 
                                out[0]->vec[0]= 0.0f;
 
196
                                out[0]->vec[0] = 0.0f;
186
197
                }
187
198
                break;
188
199
        case 16: /* Greater Than */
189
200
                {
190
201
                        if ( in[0]->vec[0] > in[1]->vec[0] )
191
 
                                out[0]->vec[0]= 1.0f;
 
202
                                out[0]->vec[0] = 1.0f;
192
203
                        else
193
 
                                out[0]->vec[0]= 0.0f;
 
204
                                out[0]->vec[0] = 0.0f;
194
205
                }
195
206
                break;
196
 
        } 
 
207
        }
197
208
}
198
209
 
199
210
static int gpu_shader_math(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *out)
214
225
                case 13:
215
226
                case 15:
216
227
                case 16:
217
 
                        GPU_stack_link(mat, names[node->custom1], NULL, out,
218
 
                                GPU_socket(&in[0]), GPU_socket(&in[1]));
 
228
                        GPU_stack_link(mat, names[node->custom1], in, out);
219
229
                        break;
220
230
                case 4:
221
231
                case 5:
224
234
                case 8:
225
235
                case 9:
226
236
                case 14:
227
 
                        if (in[0].hasinput || !in[1].hasinput)
228
 
                                GPU_stack_link(mat, names[node->custom1], NULL, out, GPU_socket(&in[0]));
229
 
                        else
230
 
                                GPU_stack_link(mat, names[node->custom1], NULL, out, GPU_socket(&in[1]));
 
237
                        if (in[0].hasinput || !in[1].hasinput) {
 
238
                                /* use only first item and terminator */
 
239
                                GPUNodeStack tmp_in[2];
 
240
                                memcpy(&tmp_in[0], &in[0], sizeof(GPUNodeStack));
 
241
                                memcpy(&tmp_in[1], &in[2], sizeof(GPUNodeStack));
 
242
                                GPU_stack_link(mat, names[node->custom1], tmp_in, out);
 
243
                        }
 
244
                        else {
 
245
                                /* use only second item and terminator */
 
246
                                GPUNodeStack tmp_in[2];
 
247
                                memcpy(&tmp_in[0], &in[1], sizeof(GPUNodeStack));
 
248
                                memcpy(&tmp_in[1], &in[2], sizeof(GPUNodeStack));
 
249
                                GPU_stack_link(mat, names[node->custom1], tmp_in, out);
 
250
                        }
231
251
                        break;
232
252
                default:
233
253
                        return 0;