Cycles: add location/rotate/scale and XYZ mapping options for all texture nodes,
[blender.git] / source / blender / nodes / shader / nodes / node_shader_tex_image.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 /* **************** OUTPUT ******************** */
33
34 static bNodeSocketTemplate sh_node_tex_image_in[]= {
35         {       SOCK_VECTOR, 1, "Vector",               0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_NONE, SOCK_HIDE_VALUE},
36         {       -1, 0, ""       }
37 };
38
39 static bNodeSocketTemplate sh_node_tex_image_out[]= {
40         {       SOCK_RGBA, 0, "Color",          0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
41         {       -1, 0, ""       }
42 };
43
44 static void node_shader_init_tex_image(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
45 {
46         NodeTexImage *tex = MEM_callocN(sizeof(NodeTexImage), "NodeTexImage");
47         default_tex_mapping(&tex->base.tex_mapping);
48         default_color_mapping(&tex->base.color_mapping);
49         tex->color_space = SHD_COLORSPACE_SRGB;
50
51         node->storage = tex;
52 }
53
54 static void node_shader_exec_tex_image(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
55 {
56         Image *ima= (Image*)node->id;
57         ShaderCallData *scd= (ShaderCallData*)data;
58         NodeTexImage *tex= (NodeTexImage*)node->storage;
59         bNodeSocket *vecsock = node->inputs.first;
60         float vec[3];
61         
62         if(vecsock->link)
63                 nodestack_get_vec(vec, SOCK_VECTOR, in[0]);
64         else
65                 copy_v3_v3(vec, scd->co);
66
67         if(ima) {
68                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
69
70                 if(ibuf) {
71                         float rgb[4];
72
73                         ibuf_sample(ibuf, vec[0], vec[1], 0.0f, 0.0f, rgb);
74
75                         if(tex->color_space == SHD_COLORSPACE_SRGB)
76                                 srgb_to_linearrgb_v3_v3(out[0]->vec, rgb);
77                         else
78                                 copy_v3_v3(out[0]->vec, rgb);
79                 }
80         }
81 }
82
83 static int node_shader_gpu_tex_image(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *out)
84 {
85         Image *ima= (Image*)node->id;
86         ImageUser *iuser= NULL;
87
88         if(!ima) {
89                 float black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
90                 GPUNodeLink *vec = GPU_uniform(black);
91                 return GPU_stack_link(mat, "set_rgba", out, out, vec);
92         }
93         
94         if(!in[0].link)
95                 in[0].link = GPU_attribute(CD_MTFACE, "");
96
97         node_shader_gpu_tex_mapping(mat, node, in, out);
98
99         return GPU_stack_link(mat, "node_tex_image", in, out, GPU_image(ima, iuser));
100 }
101
102 /* node type definition */
103 void register_node_type_sh_tex_image(ListBase *lb)
104 {
105         static bNodeType ntype;
106
107         node_type_base(&ntype, SH_NODE_TEX_IMAGE, "Image Texture", NODE_CLASS_TEXTURE, 0);
108         node_type_compatibility(&ntype, NODE_NEW_SHADING);
109         node_type_socket_templates(&ntype, sh_node_tex_image_in, sh_node_tex_image_out);
110         node_type_size(&ntype, 150, 60, 200);
111         node_type_init(&ntype, node_shader_init_tex_image);
112         node_type_storage(&ntype, "NodeTexImage", node_free_standard_storage, node_copy_standard_storage);
113         node_type_exec(&ntype, node_shader_exec_tex_image);
114         node_type_gpu(&ntype, node_shader_gpu_tex_image);
115
116         nodeRegisterType(lb, &ntype);
117 };
118