2
* $Id: SHD_vectMath.c,v 1.6 2007/04/04 13:58:12 jesterking Exp $
4
* ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20
* The Original Code is Copyright (C) 2005 Blender Foundation.
21
* All rights reserved.
23
* The Original Code is: all of this file.
25
* Contributor(s): none yet.
27
* ***** END GPL LICENSE BLOCK *****
31
#include "../SHD_util.h"
34
/* **************** VECTOR MATH ******************** */
35
static bNodeSocketType sh_node_vect_math_in[]= {
36
{ SOCK_VECTOR, 1, "Vector", 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f},
37
{ SOCK_VECTOR, 1, "Vector", 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f},
41
static bNodeSocketType sh_node_vect_math_out[]= {
42
{ SOCK_VECTOR, 0, "Vector", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
43
{ SOCK_VALUE, 0, "Value", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
47
static void node_shader_exec_vect_math(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
49
float vec1[3], vec2[3];
51
nodestack_get_vec(vec1, SOCK_VECTOR, in[0]);
52
nodestack_get_vec(vec2, SOCK_VECTOR, in[1]);
54
if(node->custom1 == 0) { /* Add */
55
out[0]->vec[0]= vec1[0] + vec2[0];
56
out[0]->vec[1]= vec1[1] + vec2[1];
57
out[0]->vec[2]= vec1[2] + vec2[2];
59
out[1]->vec[0]= (fabs(out[0]->vec[0]) + fabs(out[0]->vec[0]) + fabs(out[0]->vec[0])) / 3;
61
else if(node->custom1 == 1) { /* Subtract */
62
out[0]->vec[0]= vec1[0] - vec2[0];
63
out[0]->vec[1]= vec1[1] - vec2[1];
64
out[0]->vec[2]= vec1[2] - vec2[2];
66
out[1]->vec[0]= (fabs(out[0]->vec[0]) + fabs(out[0]->vec[0]) + fabs(out[0]->vec[0])) / 3;
68
else if(node->custom1 == 2) { /* Average */
69
out[0]->vec[0]= vec1[0] + vec2[0];
70
out[0]->vec[1]= vec1[1] + vec2[1];
71
out[0]->vec[2]= vec1[2] + vec2[2];
73
out[1]->vec[0] = Normalize( out[0]->vec );
75
else if(node->custom1 == 3) { /* Dot product */
76
out[1]->vec[0]= (vec1[0] * vec2[0]) + (vec1[1] * vec2[1]) + (vec1[2] * vec2[2]);
78
else if(node->custom1 == 4) { /* Cross product */
79
out[0]->vec[0]= (vec1[1] * vec2[2]) - (vec1[2] * vec2[1]);
80
out[0]->vec[1]= (vec1[2] * vec2[0]) - (vec1[0] * vec2[2]);
81
out[0]->vec[2]= (vec1[0] * vec2[1]) - (vec1[1] * vec2[0]);
83
out[1]->vec[0] = Normalize( out[0]->vec );
85
else if(node->custom1 == 5) { /* Normalize */
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]= vec1[0];
88
out[0]->vec[1]= vec1[1];
89
out[0]->vec[2]= vec1[2];
92
out[0]->vec[0]= vec2[0];
93
out[0]->vec[1]= vec2[1];
94
out[0]->vec[2]= vec2[2];
97
out[1]->vec[0] = Normalize( out[0]->vec );
102
bNodeType sh_node_vect_math= {
103
/* *next,*prev */ NULL, NULL,
104
/* type code */ SH_NODE_VECT_MATH,
105
/* name */ "Vector Math",
106
/* width+range */ 80, 75, 140,
107
/* class+opts */ NODE_CLASS_CONVERTOR, NODE_OPTIONS,
108
/* input sock */ sh_node_vect_math_in,
109
/* output sock */ sh_node_vect_math_out,
110
/* storage */ "node_vect_math",
111
/* execfunc */ node_shader_exec_vect_math,
114
/* freestoragefunc */ NULL,
115
/* copystoragefunc */ NULL,