Blender  V2.59
SHD_vectMath.c
Go to the documentation of this file.
00001 /*
00002  * $Id: SHD_vectMath.c 35237 2011-02-27 20:13:22Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version. 
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2005 Blender Foundation.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): none yet.
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  */
00029 
00036 #include "../SHD_util.h"
00037 
00038 
00039 /* **************** VECTOR MATH ******************** */ 
00040 static bNodeSocketType sh_node_vect_math_in[]= { 
00041         { SOCK_VECTOR, 1, "Vector", 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f}, 
00042         { SOCK_VECTOR, 1, "Vector", 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f}, 
00043         { -1, 0, "" } 
00044 };
00045 
00046 static bNodeSocketType sh_node_vect_math_out[]= {
00047         { SOCK_VECTOR, 0, "Vector", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f}, 
00048         { SOCK_VALUE, 0, "Value", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
00049         { -1, 0, "" } 
00050 };
00051 
00052 static void node_shader_exec_vect_math(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out) 
00053 { 
00054         float vec1[3], vec2[3];
00055         
00056         nodestack_get_vec(vec1, SOCK_VECTOR, in[0]);
00057         nodestack_get_vec(vec2, SOCK_VECTOR, in[1]);
00058         
00059         if(node->custom1 == 0) {        /* Add */
00060                 out[0]->vec[0]= vec1[0] + vec2[0];
00061                 out[0]->vec[1]= vec1[1] + vec2[1];
00062                 out[0]->vec[2]= vec1[2] + vec2[2];
00063                 
00064                 out[1]->vec[0]= (fabs(out[0]->vec[0]) + fabs(out[0]->vec[0]) + fabs(out[0]->vec[0])) / 3;
00065         }
00066         else if(node->custom1 == 1) {   /* Subtract */
00067                 out[0]->vec[0]= vec1[0] - vec2[0];
00068                 out[0]->vec[1]= vec1[1] - vec2[1];
00069                 out[0]->vec[2]= vec1[2] - vec2[2];
00070                 
00071                 out[1]->vec[0]= (fabs(out[0]->vec[0]) + fabs(out[0]->vec[0]) + fabs(out[0]->vec[0])) / 3;
00072         }
00073         else if(node->custom1 == 2) {   /* Average */
00074                 out[0]->vec[0]= vec1[0] + vec2[0];
00075                 out[0]->vec[1]= vec1[1] + vec2[1];
00076                 out[0]->vec[2]= vec1[2] + vec2[2];
00077                 
00078                 out[1]->vec[0] = normalize_v3( out[0]->vec );
00079         }
00080         else if(node->custom1 == 3) {   /* Dot product */
00081                 out[1]->vec[0]= (vec1[0] * vec2[0]) + (vec1[1] * vec2[1]) + (vec1[2] * vec2[2]);
00082         }
00083         else if(node->custom1 == 4) {   /* Cross product */
00084                 out[0]->vec[0]= (vec1[1] * vec2[2]) - (vec1[2] * vec2[1]);
00085                 out[0]->vec[1]= (vec1[2] * vec2[0]) - (vec1[0] * vec2[2]);
00086                 out[0]->vec[2]= (vec1[0] * vec2[1]) - (vec1[1] * vec2[0]);
00087                 
00088                 out[1]->vec[0] = normalize_v3( out[0]->vec );
00089         }
00090         else if(node->custom1 == 5) {   /* Normalize */
00091                 if(in[0]->hasinput || !in[1]->hasinput) {       /* This one only takes one input, so we've got to choose. */
00092                         out[0]->vec[0]= vec1[0];
00093                         out[0]->vec[1]= vec1[1];
00094                         out[0]->vec[2]= vec1[2];
00095                 }
00096                 else {
00097                         out[0]->vec[0]= vec2[0];
00098                         out[0]->vec[1]= vec2[1];
00099                         out[0]->vec[2]= vec2[2];
00100                 }
00101                 
00102                 out[1]->vec[0] = normalize_v3( out[0]->vec );
00103         }
00104         
00105 }
00106 
00107 static int gpu_shader_vect_math(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *out)
00108 {
00109         static const char *names[] = {"vec_math_add", "vec_math_sub",
00110                 "vec_math_average", "vec_math_dot", "vec_math_cross",
00111                 "vec_math_normalize"};
00112 
00113         switch (node->custom1) {
00114                 case 0:
00115                 case 1:
00116                 case 2:
00117                 case 3:
00118                 case 4:
00119                         GPU_stack_link(mat, names[node->custom1], NULL, out,
00120                                 GPU_socket(&in[0]), GPU_socket(&in[1]));
00121                         break;
00122                 case 5:
00123                         if(in[0].hasinput || !in[1].hasinput)
00124                                 GPU_stack_link(mat, names[node->custom1], NULL, out, GPU_socket(&in[0]));
00125                         else
00126                                 GPU_stack_link(mat, names[node->custom1], NULL, out, GPU_socket(&in[1]));
00127                         break;
00128                 default:
00129                         return 0;
00130         }
00131         
00132         return 1;
00133 }
00134 
00135 void register_node_type_sh_vect_math(ListBase *lb)
00136 {
00137         static bNodeType ntype;
00138 
00139         node_type_base(&ntype, SH_NODE_VECT_MATH, "Vector Math", NODE_CLASS_CONVERTOR, NODE_OPTIONS,
00140                 sh_node_vect_math_in, sh_node_vect_math_out);
00141         node_type_size(&ntype, 80, 75, 140);
00142         node_type_label(&ntype, node_vect_math_label);
00143         node_type_storage(&ntype, "node_vect_math", NULL, NULL);
00144         node_type_exec(&ntype, node_shader_exec_vect_math);
00145         node_type_gpu(&ntype, gpu_shader_vect_math);
00146 
00147         nodeRegisterType(lb, &ntype);
00148 }
00149 
00150