Cycles: svn merge -r41225:41232 ^/trunk/blender
[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         tex->color_space = SHD_COLORSPACE_SRGB;
48
49         node->storage = tex;
50 }
51
52 static void node_shader_exec_tex_image(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
53 {
54         Image *ima= (Image*)node->id;
55         ShaderCallData *scd= (ShaderCallData*)data;
56         NodeTexImage *tex= (NodeTexImage*)node->storage;
57         bNodeSocket *vecsock = node->inputs.first;
58         float vec[3];
59         
60         if(vecsock->link)
61                 nodestack_get_vec(vec, SOCK_VECTOR, in[0]);
62         else
63                 copy_v3_v3(vec, scd->co);
64
65         if(ima) {
66                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
67
68                 if(ibuf) {
69                         float rgb[4];
70
71                         ibuf_sample(ibuf, vec[0], vec[1], 0.0f, 0.0f, rgb);
72
73                         if(tex->color_space == SHD_COLORSPACE_SRGB)
74                                 srgb_to_linearrgb_v3_v3(out[0]->vec, rgb);
75                         else
76                                 copy_v3_v3(out[0]->vec, rgb);
77                 }
78         }
79 }
80
81 static int node_shader_gpu_tex_image(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *out)
82 {
83         Image *ima= (Image*)node->id;
84         ImageUser *iuser= NULL;
85
86         if(!ima) {
87                 float black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
88                 GPUNodeLink *vec = GPU_uniform(black);
89                 return GPU_stack_link(mat, "set_rgba", out, out, vec);
90         }
91         
92         if(!in[0].link)
93                 in[0].link = GPU_attribute(CD_MTFACE, "");
94
95         return GPU_stack_link(mat, "node_tex_image", in, out, GPU_image(ima, iuser));
96 }
97
98 /* node type definition */
99 void register_node_type_sh_tex_image(ListBase *lb)
100 {
101         static bNodeType ntype;
102
103         node_type_base(&ntype, SH_NODE_TEX_IMAGE, "Image Texture", NODE_CLASS_TEXTURE, 0);
104         node_type_compatibility(&ntype, NODE_NEW_SHADING);
105         node_type_socket_templates(&ntype, sh_node_tex_image_in, sh_node_tex_image_out);
106         node_type_size(&ntype, 150, 60, 200);
107         node_type_init(&ntype, node_shader_init_tex_image);
108         node_type_storage(&ntype, "NodeTexImage", node_free_standard_storage, node_copy_standard_storage);
109         node_type_exec(&ntype, node_shader_exec_tex_image);
110         node_type_gpu(&ntype, node_shader_gpu_tex_image);
111
112         nodeRegisterType(lb, &ntype);
113 };
114