Cycles: working towards texture workflow design
[blender.git] / source / blender / nodes / intern / SHD_nodes / SHD_tex_environment.c
1 /**
2  * $Id: SHD_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 "../SHD_util.h"
31
32 /* **************** OUTPUT ******************** */
33
34 static bNodeSocketType sh_node_tex_environment_in[]= {
35         {       SOCK_VECTOR, 1, "Vector",               0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, SOCK_NO_VALUE},
36         {       -1, 0, ""       }
37 };
38
39 static bNodeSocketType sh_node_tex_environment_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_environment(bNode *node)
45 {
46         NodeTexEnvironment *tex = MEM_callocN(sizeof(NodeTexEnvironment), "NodeTexEnvironment");
47         tex->color_space = SHD_COLORSPACE_SRGB;
48
49         node->storage = tex;
50 }
51
52 static void node_shader_exec_tex_environment(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
53 {
54         Image *ima= (Image*)node->id;
55         ShaderCallData *scd= (ShaderCallData*)data;
56         NodeTexEnvironment *tex= (NodeTexEnvironment*)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 u= (atan2f(vec[1], vec[0]) + (float)M_PI)/(2*M_PI);
70                         float v= atan2f(vec[2], hypotf(vec[0], vec[1]))/M_PI + 0.5f;
71                         float rgb[3];
72
73                         ibuf_sample(ibuf, u, v, 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_environment(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_builtin(GPU_VIEW_POSITION);
96
97         return GPU_stack_link(mat, "node_tex_environment", in, out, GPU_image(ima, iuser));
98 }
99
100 /* node type definition */
101 void register_node_type_sh_tex_environment(ListBase *lb)
102 {
103         static bNodeType ntype;
104
105         node_type_base(&ntype, SH_NODE_TEX_ENVIRONMENT, "Environment Texture", NODE_CLASS_TEXTURE, 0,
106                 sh_node_tex_environment_in, sh_node_tex_environment_out);
107         node_type_size(&ntype, 150, 60, 200);
108         node_type_init(&ntype, node_shader_init_tex_environment);
109         node_type_storage(&ntype, "NodeTexEnvironment", node_free_standard_storage, node_copy_standard_storage);
110         node_type_exec(&ntype, node_shader_exec_tex_environment);
111         node_type_gpu(&ntype, node_shader_gpu_tex_environment);
112
113         nodeRegisterType(lb, &ntype);
114 };
115