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