svn merge -r39834:40222 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / nodes / shader / nodes / node_shader_texture.c
1 /*
2  * $Id$
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 /** \file blender/nodes/shader/nodes/node_shader_texture.c
31  *  \ingroup shdnodes
32  */
33
34
35 #include "DNA_texture_types.h"
36
37 #include "node_shader_util.h"
38
39 /* **************** TEXTURE ******************** */
40 static bNodeSocketTemplate sh_node_texture_in[]= {
41         {       SOCK_VECTOR, 1, "Vector",       0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, PROP_NONE},        /* no limit */
42         {       -1, 0, ""       }
43 };
44 static bNodeSocketTemplate sh_node_texture_out[]= {
45         {       SOCK_FLOAT, 0, "Value"},
46         {       SOCK_RGBA , 0, "Color"},
47         {       SOCK_VECTOR, 0, "Normal"},
48         {       -1, 0, ""       }
49 };
50
51 static void node_shader_exec_texture(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
52 {
53         if(data && node->id) {
54                 ShadeInput *shi= ((ShaderCallData *)data)->shi;
55                 TexResult texres;
56                 float vec[3], nor[3]={0.0f, 0.0f, 0.0f};
57                 int retval;
58                 short which_output = node->custom1;
59                 
60                 short thread = shi->thread;
61                 
62                 /* out: value, color, normal */
63                 
64                 /* we should find out if a normal as output is needed, for now we do all */
65                 texres.nor= nor;
66                 texres.tr= texres.tg= texres.tb= 0.0f;
67                 
68                 if(in[0]->hasinput) {
69                         nodestack_get_vec(vec, SOCK_VECTOR, in[0]);
70                         
71                         if(in[0]->datatype==NS_OSA_VECTORS) {
72                                 float *fp= in[0]->data;
73                                 retval= multitex_nodes((Tex *)node->id, vec, fp, fp+3, shi->osatex, &texres, thread, which_output, NULL, NULL);
74                         }
75                         else if(in[0]->datatype==NS_OSA_VALUES) {
76                                 float *fp= in[0]->data;
77                                 float dxt[3], dyt[3];
78                                 
79                                 dxt[0]= fp[0]; dxt[1]= dxt[2]= 0.0f;
80                                 dyt[0]= fp[1]; dyt[1]= dyt[2]= 0.0f;
81                                 retval= multitex_nodes((Tex *)node->id, vec, dxt, dyt, shi->osatex, &texres, thread, which_output, NULL, NULL);
82                         }
83                         else
84                                 retval= multitex_nodes((Tex *)node->id, vec, NULL, NULL, 0, &texres, thread, which_output, NULL, NULL);
85                 }
86                 else {
87                         VECCOPY(vec, shi->lo);
88                         retval= multitex_nodes((Tex *)node->id, vec, NULL, NULL, 0, &texres, thread, which_output, NULL, NULL);
89                 }
90                 
91                 /* stupid exception */
92                 if( ((Tex *)node->id)->type==TEX_STUCCI) {
93                         texres.tin= 0.5f + 0.7f*texres.nor[0];
94                         CLAMP(texres.tin, 0.0f, 1.0f);
95                 }
96                 
97                 /* intensity and color need some handling */
98                 if(texres.talpha)
99                         out[0]->vec[0]= texres.ta;
100                 else
101                         out[0]->vec[0]= texres.tin;
102                 
103                 if((retval & TEX_RGB)==0) {
104                         out[1]->vec[0]= out[0]->vec[0];
105                         out[1]->vec[1]= out[0]->vec[0];
106                         out[1]->vec[2]= out[0]->vec[0];
107                         out[1]->vec[3]= 1.0f;
108                 }
109                 else {
110                         out[1]->vec[0]= texres.tr;
111                         out[1]->vec[1]= texres.tg;
112                         out[1]->vec[2]= texres.tb;
113                         out[1]->vec[3]= 1.0f;
114                 }
115                 
116                 VECCOPY(out[2]->vec, nor);
117                 
118                 if(shi->do_preview)
119                         nodeAddToPreview(node, out[1]->vec, shi->xs, shi->ys, shi->do_manage);
120                 
121         }
122 }
123
124 static int gpu_shader_texture(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *out)
125 {
126         Tex *tex = (Tex*)node->id;
127
128         if(tex && tex->type == TEX_IMAGE && tex->ima) {
129                 GPUNodeLink *texlink = GPU_image(tex->ima, NULL);
130                 return GPU_stack_link(mat, "texture_image", in, out, texlink);
131         }
132         else
133                 return 0;
134 }
135
136 void register_node_type_sh_texture(ListBase *lb)
137 {
138         static bNodeType ntype;
139
140         node_type_base(&ntype, SH_NODE_TEXTURE, "Texture", NODE_CLASS_INPUT, NODE_OPTIONS|NODE_PREVIEW);
141         node_type_socket_templates(&ntype, sh_node_texture_in, sh_node_texture_out);
142         node_type_size(&ntype, 120, 80, 240);
143         node_type_exec(&ntype, node_shader_exec_texture);
144         node_type_gpu(&ntype, gpu_shader_texture);
145
146         nodeRegisterType(lb, &ntype);
147 }
148
149