Support multiple tangents for BI render & viewport
[blender.git] / source / blender / nodes / shader / nodes / node_shader_normal_map.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_normal_map_in[] = {
33         {   SOCK_FLOAT, 1, N_("Strength"),      1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 10.0f},
34         {       SOCK_RGBA, 1, N_("Color"), 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f},
35         {       -1, 0, ""       }
36 };
37
38 static bNodeSocketTemplate sh_node_normal_map_out[] = {
39         {       SOCK_VECTOR, 0, N_("Normal"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
40         {       -1, 0, ""       }
41 };
42
43 static void node_shader_init_normal_map(bNodeTree *UNUSED(ntree), bNode *node)
44 {
45         NodeShaderNormalMap *attr = MEM_callocN(sizeof(NodeShaderNormalMap), "NodeShaderNormalMap");
46         node->storage = attr;
47 }
48
49 static void node_shader_exec_normal_map(void *data, int UNUSED(thread), bNode *node, bNodeExecData *UNUSED(execdata), bNodeStack **in, bNodeStack **out)
50  {
51     float vecIn[3];
52     float strength;
53     float B[4];
54     float *T;
55     float *N;
56     int j;
57
58         if (data) {
59                 ShadeInput *shi = ((ShaderCallData *)data)->shi;
60
61                 NodeShaderNormalMap *nm = node->storage;
62
63                 nodestack_get_vec(&strength, SOCK_FLOAT, in[0]);
64                 nodestack_get_vec(vecIn, SOCK_VECTOR, in[1]);
65
66                 vecIn[0] = -2 * (vecIn[0] - 0.5f);
67                 vecIn[1] =  2 * (vecIn[1] - 0.5f);
68                 vecIn[2] =  2 * (vecIn[2] - 0.5f);
69
70                 CLAMP_MIN(strength, 0.0f);
71
72                 N = shi->vno;
73                 int uv_index = 0;
74                 switch (nm->space) {
75                         case SHD_NORMAL_MAP_TANGENT:
76                                 if (nm->uv_map[0]) {
77                                         /* find uv map by name */
78                                         for (int i = 0; i < shi->totuv; i++) {
79                                                 if (STREQ(shi->uv[i].name, nm->uv_map)) {
80                                                         uv_index = i;
81                                                         break;
82                                                 }
83                                         }
84                                 }
85                                 else {
86                                         uv_index = shi->actuv;
87                                 }
88
89                                 T = shi->tangents[uv_index];
90
91                                 cross_v3_v3v3(B, N, T);
92                                 mul_v3_fl(B, T[3]);
93
94                                 for (j = 0; j < 3; j++)
95                                         out[0]->vec[j] = vecIn[0] * T[j] + vecIn[1] * B[j] + vecIn[2] * N[j];
96                                 interp_v3_v3v3(out[0]->vec, N, out[0]->vec, strength);
97                                 break;
98
99                         case SHD_NORMAL_MAP_OBJECT:
100                         case SHD_NORMAL_MAP_BLENDER_OBJECT:
101                                 mul_mat3_m4_v3((float (*)[4])RE_object_instance_get_matrix(shi->obi, RE_OBJECT_INSTANCE_MATRIX_LOCALTOVIEW), vecIn);
102                                 interp_v3_v3v3(out[0]->vec, N, vecIn, strength);
103                                 break;
104
105                         case SHD_NORMAL_MAP_WORLD:
106                         case SHD_NORMAL_MAP_BLENDER_WORLD:
107                                 mul_mat3_m4_v3((float (*)[4])RE_render_current_get_matrix(RE_VIEW_MATRIX), vecIn);
108                                 interp_v3_v3v3(out[0]->vec, N, vecIn, strength);
109                                 break;
110                 }
111                 normalize_v3(out[0]->vec);
112         }
113 }
114
115 static int gpu_shader_normal_map(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
116 {
117         int r;
118         NodeShaderNormalMap *nm = node->storage;
119         GPUNodeLink *negnorm;
120         GPUNodeLink *realnorm;
121         GPUNodeLink *strength;
122
123         float d[4] = {0, 0, 0, 0};
124
125         if (in[0].link)
126                 strength = in[0].link;
127         else
128                 strength = GPU_uniform(in[0].vec);
129
130         if (in[1].link) {
131                 r = GPU_link(mat, "color_to_normal", in[1].link, &realnorm);
132                 if (!r) return r;
133                 r = GPU_link(mat, "mtex_negate_texnormal", realnorm,  &realnorm);
134         }
135         else
136                 r = 1;
137
138         GPU_link(mat, "math_max", strength, GPU_uniform(d), &strength);
139         GPU_link(mat, "vec_math_negate", GPU_builtin(GPU_VIEW_NORMAL), &negnorm);
140         switch (nm->space) {
141                 case SHD_NORMAL_MAP_TANGENT:
142                         if (in[1].link) {
143                                 r = GPU_link(mat, "node_normal_map", GPU_attribute(CD_TANGENT, nm->uv_map), negnorm, realnorm, &out[0].link);
144                                 if (!r) return r;
145                         }
146                         break;
147                 case SHD_NORMAL_MAP_OBJECT:
148                 case SHD_NORMAL_MAP_BLENDER_OBJECT:
149                         if (in[1].link) {
150                                 r = GPU_link(mat, "direction_transform_m4v3", realnorm, GPU_builtin(GPU_LOC_TO_VIEW_MATRIX),  &out[0].link);
151                                 if (!r) return r;
152                         }
153                         break;
154                 case SHD_NORMAL_MAP_WORLD:
155                 case SHD_NORMAL_MAP_BLENDER_WORLD:
156                         if (in[1].link) {
157                                 r = GPU_link(mat, "direction_transform_m4v3", realnorm, GPU_builtin(GPU_VIEW_MATRIX),  &out[0].link);
158                                 if (!r) return r;
159                         }
160                         break;
161         }
162         if (out[0].link) {
163                 r = GPU_link(mat, "vec_math_mix", strength, out[0].link, negnorm,  &out[0].link);
164                 if (!r) return r;
165
166                 r = GPU_link(mat, "vect_normalize", out[0].link, &out[0].link);
167                 if (!r) return r;
168         }
169         return r;
170 }
171
172 /* node type definition */
173 void register_node_type_sh_normal_map(void)
174 {
175         static bNodeType ntype;
176
177         sh_node_type_base(&ntype, SH_NODE_NORMAL_MAP, "Normal Map", NODE_CLASS_OP_VECTOR, 0);
178         node_type_compatibility(&ntype, NODE_NEW_SHADING | NODE_OLD_SHADING);
179         node_type_socket_templates(&ntype, sh_node_normal_map_in, sh_node_normal_map_out);
180         node_type_size_preset(&ntype, NODE_SIZE_MIDDLE);
181         node_type_init(&ntype, node_shader_init_normal_map);
182         node_type_storage(&ntype, "NodeShaderNormalMap", node_free_standard_storage, node_copy_standard_storage);
183         node_type_gpu(&ntype, gpu_shader_normal_map);
184         node_type_exec(&ntype, NULL, NULL, node_shader_exec_normal_map);
185
186         nodeRegisterType(&ntype);
187 }
188