Blender  V2.59
TEX_math.c
Go to the documentation of this file.
00001 /*
00002  *
00003  * ***** BEGIN GPL LICENSE BLOCK *****
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version. 
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  * The Original Code is Copyright (C) 2005 Blender Foundation.
00020  * All rights reserved.
00021  *
00022  * The Original Code is: all of this file.
00023  *
00024  * Contributor(s): Robin Allen
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 #include "../TEX_util.h"
00035 #include "TEX_node.h"
00036 
00037 
00038 /* **************** SCALAR MATH ******************** */ 
00039 static bNodeSocketType inputs[]= { 
00040         { SOCK_VALUE, 1, "Value", 0.5f, 0.5f, 0.5f, 1.0f, -100.0f, 100.0f}, 
00041         { SOCK_VALUE, 1, "Value", 0.5f, 0.5f, 0.5f, 1.0f, -100.0f, 100.0f}, 
00042         { -1, 0, "" } 
00043 };
00044 
00045 static bNodeSocketType outputs[]= { 
00046         { SOCK_VALUE, 0, "Value", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f}, 
00047         { -1, 0, "" } 
00048 };
00049 
00050 static void valuefn(float *out, TexParams *p, bNode *node, bNodeStack **in, short thread)
00051 {
00052         float in0 = tex_input_value(in[0], p, thread);
00053         float in1 = tex_input_value(in[1], p, thread);
00054         
00055         switch(node->custom1){ 
00056         
00057         case 0: /* Add */
00058                 *out= in0 + in1; 
00059                 break; 
00060         case 1: /* Subtract */
00061                 *out= in0 - in1;
00062                 break; 
00063         case 2: /* Multiply */
00064                 *out= in0 * in1; 
00065                 break; 
00066         case 3: /* Divide */
00067                 {
00068                         if(in1==0)      /* We don't want to divide by zero. */
00069                                 *out= 0.0;
00070                         else
00071                                 *out= in0 / in1;
00072                         }
00073                 break;
00074         case 4: /* Sine */
00075                 {
00076                         *out= sin(in0);
00077                 }
00078                 break;
00079         case 5: /* Cosine */
00080                 {
00081                         *out= cos(in0);
00082                 }
00083                 break;
00084         case 6: /* Tangent */
00085                 {
00086                         *out= tan(in0);
00087                 }
00088                 break;
00089         case 7: /* Arc-Sine */
00090                 {
00091                         /* Can't do the impossible... */
00092                         if( in0 <= 1 && in0 >= -1 )
00093                                 *out= asin(in0);
00094                         else
00095                                 *out= 0.0;
00096                 }
00097                 break;
00098         case 8: /* Arc-Cosine */
00099                 {
00100                         /* Can't do the impossible... */
00101                         if( in0 <= 1 && in0 >= -1 )
00102                                 *out= acos(in0);
00103                         else
00104                                 *out= 0.0;
00105                 }
00106                 break;
00107         case 9: /* Arc-Tangent */
00108                 {
00109                         *out= atan(in0);
00110                 }
00111                 break;
00112         case 10: /* Power */
00113                 {
00114                         /* Only raise negative numbers by full integers */
00115                         if( in0 >= 0 ) {
00116                                 out[0]= pow(in0, in1);
00117                         } else {
00118                                 float y_mod_1 = fmod(in1, 1);
00119                                 if (y_mod_1 > 0.999f || y_mod_1 < 0.001f) {
00120                                         *out = pow(in0, floor(in1 + 0.5f));
00121                                 } else {
00122                                         *out = 0.0;
00123                                 }
00124                         }
00125                 }
00126                 break;
00127         case 11: /* Logarithm */
00128                 {
00129                         /* Don't want any imaginary numbers... */
00130                         if( in0 > 0  && in1 > 0 )
00131                                 *out= log(in0) / log(in1);
00132                         else
00133                                 *out= 0.0;
00134                 }
00135                 break;
00136         case 12: /* Minimum */
00137                 {
00138                         if( in0 < in1 )
00139                                 *out= in0;
00140                         else
00141                                 *out= in1;
00142                 }
00143                 break;
00144         case 13: /* Maximum */
00145                 {
00146                         if( in0 > in1 )
00147                                 *out= in0;
00148                         else
00149                                 *out= in1;
00150                 }
00151                 break;
00152         case 14: /* Round */
00153                 {
00154                         *out= (in0<0)?(int)(in0 - 0.5f):(int)(in0 + 0.5f);
00155                 }
00156                 break; 
00157                 
00158         case 15: /* Less Than */
00159                 {
00160                         if( in0 < in1 )
00161                                 *out= 1.0f;
00162                         else
00163                                 *out= 0.0f;
00164                 }
00165                 break;
00166                 
00167         case 16: /* Greater Than */
00168                 {
00169                         if( in0 > in1 )
00170                                 *out= 1.0f;
00171                         else
00172                                 *out= 0.0f;
00173                 }
00174                 break;
00175                 
00176         default:
00177                 fprintf(stderr,
00178                         "%s:%d: unhandeld value in switch statement: %d\n",
00179                         __FILE__, __LINE__, node->custom1);
00180         } 
00181 }
00182 
00183 static void exec(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
00184 {
00185         tex_output(node, in, out[0], &valuefn, data);
00186 }
00187 
00188 void register_node_type_tex_math(ListBase *lb)
00189 {
00190         static bNodeType ntype;
00191         
00192         node_type_base(&ntype, TEX_NODE_MATH, "Math", NODE_CLASS_CONVERTOR, NODE_OPTIONS,
00193                                    inputs, outputs);
00194         node_type_size(&ntype, 120, 110, 160);
00195         node_type_label(&ntype, node_math_label);
00196         node_type_storage(&ntype, "node_math", NULL, NULL);
00197         node_type_exec(&ntype, exec);
00198         
00199         nodeRegisterType(lb, &ntype);
00200 }
00201