5ccb42d99c4a2b64601d3bc8800a0ca46fe630fe
[blender.git] / source / blender / nodes / shader / nodes / node_shader_tex_magic.c
1 /**
2  * $Id: node_shader_output.c 32517 2010-10-16 14:32:17Z campbellbarton $
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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. 
10  *
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.
15  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2005 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include "../node_shader_util.h"
31
32 static void magic(float rgb[3], float p[3], int n, float turbulence)
33 {
34         float turb = turbulence/5.0f;
35
36         float x = sinf((p[0] + p[1] + p[2])*5.0f);
37         float y = cosf((-p[0] + p[1] - p[2])*5.0f);
38         float z = -cosf((-p[0] - p[1] + p[2])*5.0f);
39
40         if(n > 0) {
41                 x *= turb;
42                 y *= turb;
43                 z *= turb;
44                 y = -cosf(x-y+z);
45                 y *= turb;
46
47                 if(n > 1) {
48                         x= cosf(x-y-z);
49                         x *= turb;
50
51                         if(n > 2) {
52                                 z= sinf(-x-y-z);
53                                 z *= turb;
54
55                                 if(n > 3) {
56                                         x= -cosf(-x+y-z);
57                                         x *= turb;
58
59                                         if(n > 4) {
60                                                 y= -sinf(-x+y+z);
61                                                 y *= turb;
62
63                                                 if(n > 5) {
64                                                         y= -cosf(-x+y+z);
65                                                         y *= turb;
66
67                                                         if(n > 6) {
68                                                                 x= cosf(x+y+z);
69                                                                 x *= turb;
70
71                                                                 if(n > 7) {
72                                                                         z= sinf(x+y-z);
73                                                                         z *= turb;
74
75                                                                         if(n > 8) {
76                                                                                 x= -cosf(-x-y+z);
77                                                                                 x *= turb;
78
79                                                                                 if(n > 9) {
80                                                                                         y= -sinf(x-y+z);
81                                                                                         y *= turb;
82                                                                                 }
83                                                                         }
84                                                                 }
85                                                         }
86                                                 }
87                                         }
88                                 }
89                         }
90                 }
91         }
92
93         if(turb != 0.0f) {
94                 turb *= 2.0f;
95                 x /= turb;
96                 y /= turb;
97                 z /= turb;
98         }
99
100         rgb[0]= 0.5f - x;
101         rgb[1]= 0.5f - y;
102         rgb[2]= 0.5f - z;
103 }
104
105 /* **************** OUTPUT ******************** */
106
107 static bNodeSocketTemplate sh_node_tex_magic_in[]= {
108         {       SOCK_VECTOR, 1, "Vector",               0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_NONE, SOCK_HIDE_VALUE},
109         {       SOCK_FLOAT, 1, "Turbulence",    5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1000.0f},
110         {       -1, 0, ""       }
111 };
112
113 static bNodeSocketTemplate sh_node_tex_magic_out[]= {
114         {       SOCK_RGBA, 0, "Color",          0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
115         {       -1, 0, ""       }
116 };
117
118 static void node_shader_init_tex_magic(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
119 {
120         NodeTexMagic *tex = MEM_callocN(sizeof(NodeTexMagic), "NodeTexMagic");
121         tex->depth = 2;
122
123         node->storage = tex;
124 }
125
126 static void node_shader_exec_tex_magic(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
127 {
128         ShaderCallData *scd= (ShaderCallData*)data;
129         NodeTexMagic *tex= (NodeTexMagic*)node->storage;
130         bNodeSocket *vecsock = node->inputs.first;
131         float vec[3], turbulence;
132         
133         if(vecsock->link)
134                 nodestack_get_vec(vec, SOCK_VECTOR, in[0]);
135         else
136                 copy_v3_v3(vec, scd->co);
137
138         nodestack_get_vec(&turbulence, SOCK_FLOAT, in[1]);
139
140         magic(out[0]->vec, vec, tex->depth, turbulence);
141 }
142
143 static int node_shader_gpu_tex_magic(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *out)
144 {
145         NodeTexMagic *tex = (NodeTexMagic*)node->storage;
146         float depth = tex->depth;
147
148         if(!in[0].link)
149                 in[0].link = GPU_attribute(CD_ORCO, "");
150
151         return GPU_stack_link(mat, "node_tex_magic", in, out, GPU_uniform(&depth));
152 }
153
154 /* node type definition */
155 void register_node_type_sh_tex_magic(ListBase *lb)
156 {
157         static bNodeType ntype;
158
159         node_type_base(&ntype, SH_NODE_TEX_MAGIC, "Magic Texture", NODE_CLASS_TEXTURE, 0);
160         node_type_compatibility(&ntype, NODE_NEW_SHADING);
161         node_type_socket_templates(&ntype, sh_node_tex_magic_in, sh_node_tex_magic_out);
162         node_type_size(&ntype, 150, 60, 200);
163         node_type_init(&ntype, node_shader_init_tex_magic);
164         node_type_storage(&ntype, "NodeTexMagic", node_free_standard_storage, node_copy_standard_storage);
165         node_type_exec(&ntype, node_shader_exec_tex_magic);
166         node_type_gpu(&ntype, node_shader_gpu_tex_magic);
167
168         nodeRegisterType(lb, &ntype);
169 };
170