Merge with trunk r41342
[blender.git] / source / blender / nodes / shader / nodes / node_shader_texture.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/nodes/shader/nodes/node_shader_texture.c
29  *  \ingroup shdnodes
30  */
31
32
33 #include "DNA_texture_types.h"
34
35 #include "node_shader_util.h"
36
37 /* **************** TEXTURE ******************** */
38 static bNodeSocketTemplate sh_node_texture_in[]= {
39         {       SOCK_VECTOR, 1, "Vector",       0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, PROP_NONE},        /* no limit */
40         {       -1, 0, ""       }
41 };
42 static bNodeSocketTemplate sh_node_texture_out[]= {
43         {       SOCK_FLOAT, 0, "Value"},
44         {       SOCK_RGBA , 0, "Color"},
45         {       SOCK_VECTOR, 0, "Normal"},
46         {       -1, 0, ""       }
47 };
48
49 static void node_shader_exec_texture(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
50 {
51         if(data && node->id) {
52                 ShadeInput *shi= ((ShaderCallData *)data)->shi;
53                 TexResult texres;
54                 float vec[3], nor[3]={0.0f, 0.0f, 0.0f};
55                 int retval;
56                 short which_output = node->custom1;
57                 
58                 short thread = shi->thread;
59                 
60                 /* out: value, color, normal */
61                 
62                 /* we should find out if a normal as output is needed, for now we do all */
63                 texres.nor= nor;
64                 texres.tr= texres.tg= texres.tb= 0.0f;
65                 
66                 if(in[0]->hasinput) {
67                         nodestack_get_vec(vec, SOCK_VECTOR, in[0]);
68                         
69                         if(in[0]->datatype==NS_OSA_VECTORS) {
70                                 float *fp= in[0]->data;
71                                 retval= multitex_nodes((Tex *)node->id, vec, fp, fp+3, shi->osatex, &texres, thread, which_output, NULL, NULL);
72                         }
73                         else if(in[0]->datatype==NS_OSA_VALUES) {
74                                 float *fp= in[0]->data;
75                                 float dxt[3], dyt[3];
76                                 
77                                 dxt[0]= fp[0]; dxt[1]= dxt[2]= 0.0f;
78                                 dyt[0]= fp[1]; dyt[1]= dyt[2]= 0.0f;
79                                 retval= multitex_nodes((Tex *)node->id, vec, dxt, dyt, shi->osatex, &texres, thread, which_output, NULL, NULL);
80                         }
81                         else
82                                 retval= multitex_nodes((Tex *)node->id, vec, NULL, NULL, 0, &texres, thread, which_output, NULL, NULL);
83                 }
84                 else {
85                         VECCOPY(vec, shi->lo);
86                         retval= multitex_nodes((Tex *)node->id, vec, NULL, NULL, 0, &texres, thread, which_output, NULL, NULL);
87                 }
88                 
89                 /* stupid exception */
90                 if( ((Tex *)node->id)->type==TEX_STUCCI) {
91                         texres.tin= 0.5f + 0.7f*texres.nor[0];
92                         CLAMP(texres.tin, 0.0f, 1.0f);
93                 }
94                 
95                 /* intensity and color need some handling */
96                 if(texres.talpha)
97                         out[0]->vec[0]= texres.ta;
98                 else
99                         out[0]->vec[0]= texres.tin;
100                 
101                 if((retval & TEX_RGB)==0) {
102                         out[1]->vec[0]= out[0]->vec[0];
103                         out[1]->vec[1]= out[0]->vec[0];
104                         out[1]->vec[2]= out[0]->vec[0];
105                         out[1]->vec[3]= 1.0f;
106                 }
107                 else {
108                         out[1]->vec[0]= texres.tr;
109                         out[1]->vec[1]= texres.tg;
110                         out[1]->vec[2]= texres.tb;
111                         out[1]->vec[3]= 1.0f;
112                 }
113                 
114                 VECCOPY(out[2]->vec, nor);
115                 
116                 if(shi->do_preview)
117                         nodeAddToPreview(node, out[1]->vec, shi->xs, shi->ys, shi->do_manage);
118                 
119         }
120 }
121
122 static int gpu_shader_texture(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *out)
123 {
124         Tex *tex = (Tex*)node->id;
125
126         if(tex && tex->type == TEX_IMAGE && tex->ima) {
127                 GPUNodeLink *texlink = GPU_image(tex->ima, NULL);
128                 return GPU_stack_link(mat, "texture_image", in, out, texlink);
129         }
130         else
131                 return 0;
132 }
133
134 void register_node_type_sh_texture(ListBase *lb)
135 {
136         static bNodeType ntype;
137
138         node_type_base(&ntype, SH_NODE_TEXTURE, "Texture", NODE_CLASS_INPUT, NODE_OPTIONS|NODE_PREVIEW);
139         node_type_socket_templates(&ntype, sh_node_texture_in, sh_node_texture_out);
140         node_type_size(&ntype, 120, 80, 240);
141         node_type_exec(&ntype, node_shader_exec_texture);
142         node_type_gpu(&ntype, gpu_shader_texture);
143
144         nodeRegisterType(lb, &ntype);
145 }
146
147