Cleanup: indentation, long lines
[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(
50         void *data, int UNUSED(thread), bNode *node, bNodeExecData *UNUSED(execdata),
51         bNodeStack **in, bNodeStack **out)
52 {
53         if (data) {
54                 ShadeInput *shi = ((ShaderCallData *)data)->shi;
55
56                 NodeShaderNormalMap *nm = node->storage;
57
58                 float strength, vecIn[3];
59                 nodestack_get_vec(&strength, SOCK_FLOAT, in[0]);
60                 nodestack_get_vec(vecIn, SOCK_VECTOR, in[1]);
61
62                 vecIn[0] = -2 * (vecIn[0] - 0.5f);
63                 vecIn[1] =  2 * (vecIn[1] - 0.5f);
64                 vecIn[2] =  2 * (vecIn[2] - 0.5f);
65
66                 CLAMP_MIN(strength, 0.0f);
67
68                 float *N = shi->nmapnorm;
69                 int uv_index = 0;
70                 switch (nm->space) {
71                         case SHD_NORMAL_MAP_TANGENT:
72                                 if (nm->uv_map[0]) {
73                                         /* find uv map by name */
74                                         for (int i = 0; i < shi->totuv; i++) {
75                                                 if (STREQ(shi->uv[i].name, nm->uv_map)) {
76                                                         uv_index = i;
77                                                         break;
78                                                 }
79                                         }
80                                 }
81                                 else {
82                                         uv_index = shi->actuv;
83                                 }
84
85                                 float *T = shi->tangents[uv_index];
86
87                                 float B[3];
88                                 cross_v3_v3v3(B, N, T);
89                                 mul_v3_fl(B, T[3]);
90
91                                 for (int j = 0; j < 3; j++)
92                                         out[0]->vec[j] = vecIn[0] * T[j] + vecIn[1] * B[j] + vecIn[2] * N[j];
93                                 interp_v3_v3v3(out[0]->vec, N, out[0]->vec, strength);
94                                 if (shi->use_world_space_shading) {
95                                         mul_mat3_m4_v3((float (*)[4])RE_render_current_get_matrix(RE_VIEWINV_MATRIX), out[0]->vec);
96                                 }
97                                 break;
98
99                         case SHD_NORMAL_MAP_OBJECT:
100                         case SHD_NORMAL_MAP_BLENDER_OBJECT:
101                                 if (shi->use_world_space_shading) {
102                                         mul_mat3_m4_v3((float (*)[4])RE_object_instance_get_matrix(shi->obi, RE_OBJECT_INSTANCE_MATRIX_OB), vecIn);
103                                         mul_mat3_m4_v3((float (*)[4])RE_render_current_get_matrix(RE_VIEWINV_MATRIX), N);
104                                 }
105                                 else
106                                         mul_mat3_m4_v3((float (*)[4])RE_object_instance_get_matrix(shi->obi, RE_OBJECT_INSTANCE_MATRIX_LOCALTOVIEW), vecIn);
107                                 interp_v3_v3v3(out[0]->vec, N, vecIn, strength);
108                                 break;
109
110                         case SHD_NORMAL_MAP_WORLD:
111                         case SHD_NORMAL_MAP_BLENDER_WORLD:
112                                 if (shi->use_world_space_shading)
113                                         mul_mat3_m4_v3((float (*)[4])RE_render_current_get_matrix(RE_VIEWINV_MATRIX), N);
114                                 else
115                                         mul_mat3_m4_v3((float (*)[4])RE_render_current_get_matrix(RE_VIEW_MATRIX), vecIn);
116                                 interp_v3_v3v3(out[0]->vec, N, vecIn, strength);
117                                 break;
118                 }
119                 if (shi->use_world_space_shading) {
120                         negate_v3(out[0]->vec);
121                 }
122                 normalize_v3(out[0]->vec);
123         }
124 }
125
126 static int gpu_shader_normal_map(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
127 {
128         NodeShaderNormalMap *nm = node->storage;
129         GPUNodeLink *negnorm;
130         GPUNodeLink *realnorm;
131         GPUNodeLink *strength;
132
133         float d[4] = {0, 0, 0, 0};
134
135         if (in[0].link)
136                 strength = in[0].link;
137         else
138                 strength = GPU_uniform(in[0].vec);
139
140         if (in[1].link)
141                 realnorm = in[1].link;
142         else
143                 realnorm = GPU_uniform(in[1].vec);
144
145         negnorm = GPU_builtin(GPU_VIEW_NORMAL);
146         GPU_link(mat, "math_max", strength, GPU_uniform(d), &strength);
147
148         if (GPU_material_use_world_space_shading(mat)) {
149
150                 /* ******* CYCLES or BLENDER INTERNAL with world space shading flag ******* */
151
152                 const char *color_to_normal_fnc_name = "color_to_normal_new_shading";
153                 if (nm->space == SHD_NORMAL_MAP_BLENDER_OBJECT || nm->space == SHD_NORMAL_MAP_BLENDER_WORLD || !GPU_material_use_new_shading_nodes(mat))
154                         color_to_normal_fnc_name = "color_to_blender_normal_new_shading";
155                 switch (nm->space) {
156                         case SHD_NORMAL_MAP_TANGENT:
157                                 GPU_link(mat, "color_to_normal_new_shading", realnorm, &realnorm);
158                                 GPU_link(mat, "node_normal_map", GPU_attribute(CD_TANGENT, nm->uv_map), negnorm, realnorm, &realnorm);
159                                 GPU_link(mat, "vec_math_mix", strength, realnorm, GPU_builtin(GPU_VIEW_NORMAL), &out[0].link);
160                                 /* for uniform scale this is sufficient to match Cycles */
161                                 GPU_link(mat, "direction_transform_m4v3", out[0].link, GPU_builtin(GPU_INVERSE_VIEW_MATRIX), &out[0].link);
162                                 GPU_link(mat, "vect_normalize", out[0].link, &out[0].link);
163                                 return true;
164                         case SHD_NORMAL_MAP_OBJECT:
165                         case SHD_NORMAL_MAP_BLENDER_OBJECT:
166                                 GPU_link(mat, "direction_transform_m4v3", negnorm, GPU_builtin(GPU_INVERSE_VIEW_MATRIX), &negnorm);
167                                 GPU_link(mat, color_to_normal_fnc_name, realnorm, &realnorm);
168                                 GPU_link(mat, "direction_transform_m4v3", realnorm, GPU_builtin(GPU_OBJECT_MATRIX), &realnorm);
169                                 break;
170                         case SHD_NORMAL_MAP_WORLD:
171                         case SHD_NORMAL_MAP_BLENDER_WORLD:
172                                 GPU_link(mat, "direction_transform_m4v3", negnorm, GPU_builtin(GPU_INVERSE_VIEW_MATRIX), &negnorm);
173                                 GPU_link(mat, color_to_normal_fnc_name, realnorm, &realnorm);
174                                 break;
175                 }
176
177         }
178         else {
179
180                 /* ************** BLENDER INTERNAL without world space shading flag ******* */
181
182                 GPU_link(mat, "color_to_normal", realnorm, &realnorm);
183                 GPU_link(mat, "mtex_negate_texnormal", realnorm,  &realnorm);
184                 GPU_link(mat, "vec_math_negate", negnorm, &negnorm);
185
186                 switch (nm->space) {
187                         case SHD_NORMAL_MAP_TANGENT:
188                                 GPU_link(mat, "node_normal_map", GPU_attribute(CD_TANGENT, nm->uv_map), negnorm, realnorm, &realnorm);
189                                 break;
190                         case SHD_NORMAL_MAP_OBJECT:
191                         case SHD_NORMAL_MAP_BLENDER_OBJECT:
192                                 GPU_link(mat, "direction_transform_m4v3", realnorm, GPU_builtin(GPU_LOC_TO_VIEW_MATRIX),  &realnorm);
193                                 break;
194                         case SHD_NORMAL_MAP_WORLD:
195                         case SHD_NORMAL_MAP_BLENDER_WORLD:
196                                 GPU_link(mat, "direction_transform_m4v3", realnorm, GPU_builtin(GPU_VIEW_MATRIX),  &realnorm);
197                                 break;
198                 }
199         }
200
201         GPU_link(mat, "vec_math_mix", strength, realnorm, negnorm,  &out[0].link);
202         GPU_link(mat, "vect_normalize", out[0].link, &out[0].link);
203
204         return true;
205 }
206
207 /* node type definition */
208 void register_node_type_sh_normal_map(void)
209 {
210         static bNodeType ntype;
211
212         sh_node_type_base(&ntype, SH_NODE_NORMAL_MAP, "Normal Map", NODE_CLASS_OP_VECTOR, 0);
213         node_type_compatibility(&ntype, NODE_NEW_SHADING | NODE_OLD_SHADING);
214         node_type_socket_templates(&ntype, sh_node_normal_map_in, sh_node_normal_map_out);
215         node_type_size_preset(&ntype, NODE_SIZE_MIDDLE);
216         node_type_init(&ntype, node_shader_init_normal_map);
217         node_type_storage(&ntype, "NodeShaderNormalMap", node_free_standard_storage, node_copy_standard_storage);
218         node_type_gpu(&ntype, gpu_shader_normal_map);
219         node_type_exec(&ntype, NULL, NULL, node_shader_exec_normal_map);
220
221         nodeRegisterType(&ntype);
222 }