ClangFormat: apply to source, most of intern
[blender.git] / intern / cycles / kernel / shaders / node_image_texture.osl
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "stdosl.h"
18 #include "node_color.h"
19
20 point texco_remap_square(point co)
21 {
22   return (co - point(0.5, 0.5, 0.5)) * 2.0;
23 }
24
25 point map_to_tube(vector dir)
26 {
27   float u, v;
28   v = (dir[2] + 1.0) * 0.5;
29   float len = sqrt(dir[0] * dir[0] + dir[1] * dir[1]);
30   if (len > 0.0) {
31     u = (1.0 - (atan2(dir[0] / len, dir[1] / len) / M_PI)) * 0.5;
32   }
33   else {
34     v = u = 0.0; /* To avoid un-initialized variables. */
35   }
36   return point(u, v, 0.0);
37 }
38
39 point map_to_sphere(vector dir)
40 {
41   float len = length(dir);
42   float v, u;
43   if (len > 0.0) {
44     if (dir[0] == 0.0 && dir[1] == 0.0) {
45       u = 0.0; /* Othwise domain error. */
46     }
47     else {
48       u = (1.0 - atan2(dir[0], dir[1]) / M_PI) / 2.0;
49     }
50     v = 1.0 - acos(dir[2] / len) / M_PI;
51   }
52   else {
53     v = u = 0.0; /* To avoid un-initialized variables. */
54   }
55   return point(u, v, 0.0);
56 }
57
58 color image_texture_lookup(string filename,
59                            string color_space,
60                            float u,
61                            float v,
62                            output float Alpha,
63                            int use_alpha,
64                            int is_float,
65                            string interpolation,
66                            string extension)
67 {
68   color rgb = (color)texture(
69       filename, u, 1.0 - v, "wrap", extension, "interp", interpolation, "alpha", Alpha);
70
71   if (use_alpha) {
72     rgb = color_unpremultiply(rgb, Alpha);
73
74     if (!is_float)
75       rgb = min(rgb, 1.0);
76   }
77
78   if (color_space == "sRGB") {
79     rgb = color_srgb_to_scene_linear(rgb);
80   }
81
82   return rgb;
83 }
84
85 shader node_image_texture(int use_mapping = 0,
86                           matrix mapping = matrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
87                           point Vector = P,
88                           string filename = "",
89                           string color_space = "sRGB",
90                           string projection = "flat",
91                           string interpolation = "smartcubic",
92                           string extension = "periodic",
93                           float projection_blend = 0.0,
94                           int is_float = 1,
95                           int use_alpha = 1,
96                           output color Color = 0.0,
97                           output float Alpha = 1.0)
98 {
99   point p = Vector;
100
101   if (use_mapping)
102     p = transform(mapping, p);
103
104   if (projection == "flat") {
105     Color = image_texture_lookup(
106         filename, color_space, p[0], p[1], Alpha, use_alpha, is_float, interpolation, extension);
107   }
108   else if (projection == "box") {
109     /* object space normal */
110     vector Nob = transform("world", "object", N);
111
112     /* project from direction vector to barycentric coordinates in triangles */
113     Nob = vector(fabs(Nob[0]), fabs(Nob[1]), fabs(Nob[2]));
114     Nob /= (Nob[0] + Nob[1] + Nob[2]);
115
116     /* basic idea is to think of this as a triangle, each corner representing
117      * one of the 3 faces of the cube. in the corners we have single textures,
118      * in between we blend between two textures, and in the middle we a blend
119      * between three textures.
120      *
121      * the Nxyz values are the barycentric coordinates in an equilateral
122      * triangle, which in case of blending, in the middle has a smaller
123      * equilateral triangle where 3 textures blend. this divides things into
124      * 7 zones, with an if () test for each zone */
125
126     vector weight = vector(0.0, 0.0, 0.0);
127     float blend = projection_blend;
128     float limit = 0.5 * (1.0 + blend);
129
130     /* first test for corners with single texture */
131     if (Nob[0] > limit * (Nob[0] + Nob[1]) && Nob[0] > limit * (Nob[0] + Nob[2])) {
132       weight[0] = 1.0;
133     }
134     else if (Nob[1] > limit * (Nob[0] + Nob[1]) && Nob[1] > limit * (Nob[1] + Nob[2])) {
135       weight[1] = 1.0;
136     }
137     else if (Nob[2] > limit * (Nob[0] + Nob[2]) && Nob[2] > limit * (Nob[1] + Nob[2])) {
138       weight[2] = 1.0;
139     }
140     else if (blend > 0.0) {
141       /* in case of blending, test for mixes between two textures */
142       if (Nob[2] < (1.0 - limit) * (Nob[1] + Nob[0])) {
143         weight[0] = Nob[0] / (Nob[0] + Nob[1]);
144         weight[0] = clamp((weight[0] - 0.5 * (1.0 - blend)) / blend, 0.0, 1.0);
145         weight[1] = 1.0 - weight[0];
146       }
147       else if (Nob[0] < (1.0 - limit) * (Nob[1] + Nob[2])) {
148         weight[1] = Nob[1] / (Nob[1] + Nob[2]);
149         weight[1] = clamp((weight[1] - 0.5 * (1.0 - blend)) / blend, 0.0, 1.0);
150         weight[2] = 1.0 - weight[1];
151       }
152       else if (Nob[1] < (1.0 - limit) * (Nob[0] + Nob[2])) {
153         weight[0] = Nob[0] / (Nob[0] + Nob[2]);
154         weight[0] = clamp((weight[0] - 0.5 * (1.0 - blend)) / blend, 0.0, 1.0);
155         weight[2] = 1.0 - weight[0];
156       }
157       else {
158         /* last case, we have a mix between three */
159         weight[0] = ((2.0 - limit) * Nob[0] + (limit - 1.0)) / (2.0 * limit - 1.0);
160         weight[1] = ((2.0 - limit) * Nob[1] + (limit - 1.0)) / (2.0 * limit - 1.0);
161         weight[2] = ((2.0 - limit) * Nob[2] + (limit - 1.0)) / (2.0 * limit - 1.0);
162       }
163     }
164     else {
165       /* Desperate mode, no valid choice anyway, fallback to one side.*/
166       weight[0] = 1.0;
167     }
168
169     Color = color(0.0, 0.0, 0.0);
170     Alpha = 0.0;
171
172     float tmp_alpha;
173
174     if (weight[0] > 0.0) {
175       Color += weight[0] * image_texture_lookup(filename,
176                                                 color_space,
177                                                 p[1],
178                                                 p[2],
179                                                 tmp_alpha,
180                                                 use_alpha,
181                                                 is_float,
182                                                 interpolation,
183                                                 extension);
184       Alpha += weight[0] * tmp_alpha;
185     }
186     if (weight[1] > 0.0) {
187       Color += weight[1] * image_texture_lookup(filename,
188                                                 color_space,
189                                                 p[0],
190                                                 p[2],
191                                                 tmp_alpha,
192                                                 use_alpha,
193                                                 is_float,
194                                                 interpolation,
195                                                 extension);
196       Alpha += weight[1] * tmp_alpha;
197     }
198     if (weight[2] > 0.0) {
199       Color += weight[2] * image_texture_lookup(filename,
200                                                 color_space,
201                                                 p[1],
202                                                 p[0],
203                                                 tmp_alpha,
204                                                 use_alpha,
205                                                 is_float,
206                                                 interpolation,
207                                                 extension);
208       Alpha += weight[2] * tmp_alpha;
209     }
210   }
211   else if (projection == "sphere") {
212     point projected = map_to_sphere(texco_remap_square(p));
213     Color = image_texture_lookup(filename,
214                                  color_space,
215                                  projected[0],
216                                  projected[1],
217                                  Alpha,
218                                  use_alpha,
219                                  is_float,
220                                  interpolation,
221                                  extension);
222   }
223   else if (projection == "tube") {
224     point projected = map_to_tube(texco_remap_square(p));
225     Color = image_texture_lookup(filename,
226                                  color_space,
227                                  projected[0],
228                                  projected[1],
229                                  Alpha,
230                                  use_alpha,
231                                  is_float,
232                                  interpolation,
233                                  extension);
234   }
235 }