f0a8cda045e89f6e995f4c3e2851ee5bfff0bbc4
[blender.git] / source / blender / nodes / shader / nodes / node_shader_tex_image.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 #include "../node_shader_util.h"
29
30 /* **************** OUTPUT ******************** */
31
32 static bNodeSocketTemplate sh_node_tex_image_in[] = {
33         {       SOCK_VECTOR, 1, N_("Vector"),           0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_NONE, SOCK_HIDE_VALUE},
34         {       -1, 0, ""       }
35 };
36
37 static bNodeSocketTemplate sh_node_tex_image_out[] = {
38         {       SOCK_RGBA, 0, N_("Color"),              0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_NONE, SOCK_NO_INTERNAL_LINK},
39         {       SOCK_FLOAT, 0, N_("Alpha"),             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_NONE, SOCK_NO_INTERNAL_LINK},
40         {       -1, 0, ""       }
41 };
42
43 static void node_shader_init_tex_image(bNodeTree *UNUSED(ntree), bNode *node)
44 {
45         NodeTexImage *tex = MEM_callocN(sizeof(NodeTexImage), "NodeTexImage");
46         BKE_texture_mapping_default(&tex->base.tex_mapping, TEXMAP_TYPE_POINT);
47         BKE_texture_colormapping_default(&tex->base.color_mapping);
48         tex->color_space = SHD_COLORSPACE_COLOR;
49         tex->iuser.frames = 1;
50         tex->iuser.sfra = 1;
51         tex->iuser.fie_ima = 2;
52         tex->iuser.ok = 1;
53
54         node->storage = tex;
55 }
56
57 static int node_shader_gpu_tex_image(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
58 {
59         Image *ima = (Image *)node->id;
60         ImageUser *iuser = NULL;
61         NodeTexImage *tex = node->storage;
62         int isdata = tex->color_space == SHD_COLORSPACE_NONE;
63
64         if (!ima)
65                 return GPU_stack_link(mat, "node_tex_image_empty", in, out);
66         
67         if (!in[0].link)
68                 in[0].link = GPU_attribute(CD_MTFACE, "");
69
70         node_shader_gpu_tex_mapping(mat, node, in, out);
71
72         GPU_stack_link(mat, "node_tex_image", in, out, GPU_image(ima, iuser, isdata));
73
74         ImBuf *ibuf = BKE_image_acquire_ibuf(ima, iuser, NULL);
75         if ((tex->color_space == SHD_COLORSPACE_COLOR) &&
76             ibuf && (ibuf->colormanage_flag & IMB_COLORMANAGE_IS_DATA) == 0 &&
77             GPU_material_do_color_management(mat))
78         {
79                 GPU_link(mat, "srgb_to_linearrgb", out[0].link, &out[0].link);
80         }
81         BKE_image_release_ibuf(ima, ibuf, NULL);
82
83         return true;
84 }
85
86 /* node type definition */
87 void register_node_type_sh_tex_image(void)
88 {
89         static bNodeType ntype;
90
91         sh_node_type_base(&ntype, SH_NODE_TEX_IMAGE, "Image Texture", NODE_CLASS_TEXTURE, 0);
92         node_type_compatibility(&ntype, NODE_NEW_SHADING);
93         node_type_socket_templates(&ntype, sh_node_tex_image_in, sh_node_tex_image_out);
94         node_type_init(&ntype, node_shader_init_tex_image);
95         node_type_storage(&ntype, "NodeTexImage", node_free_standard_storage, node_copy_standard_storage);
96         node_type_gpu(&ntype, node_shader_gpu_tex_image);
97
98         nodeRegisterType(&ntype);
99 }