Blender V2.61 - r43446

node_texture_math.c

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