Code cleanup, use bools here. Forgot to change that.
[blender.git] / intern / cycles / kernel / svm / svm_vector_transform.h
1 /*
2  * Copyright 2013, Blender Foundation.
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
19 CCL_NAMESPACE_BEGIN
20
21 /* Vector Transform */
22
23 __device void svm_node_vector_transform(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
24 {
25         uint itype, ifrom, ito;
26         uint vector_in, vector_out;
27         
28         decode_node_uchar4(node.y, &itype, &ifrom, &ito, NULL);
29         decode_node_uchar4(node.z, &vector_in, &vector_out, NULL, NULL);
30         
31         float3 in = stack_load_float3(stack, vector_in);
32         
33         NodeVectorTransformType type = (NodeVectorTransformType)itype;
34         NodeVectorTransformConvertSpace from = (NodeVectorTransformConvertSpace)ifrom;
35         NodeVectorTransformConvertSpace to = (NodeVectorTransformConvertSpace)ito;
36         
37         Transform tfm;
38         bool is_object = (sd->object != ~0);
39         bool is_direction = (type == NODE_VECTOR_TRANSFORM_TYPE_VECTOR || type == NODE_VECTOR_TRANSFORM_TYPE_NORMAL);
40         
41         /* From world */
42         if(from == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_WORLD) {
43                 if(to == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_CAMERA) {
44                         tfm = kernel_data.cam.worldtocamera;
45                         if(is_direction)
46                                 in = transform_direction(&tfm, in);
47                         else
48                                 in = transform_point(&tfm, in);
49                 }
50                 else if (to == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT && is_object) {
51                         if(is_direction)
52                                 object_inverse_dir_transform(kg, sd, &in);
53                         else
54                                 object_inverse_position_transform(kg, sd, &in);
55                 }
56         }
57         
58         /* From camera */
59         else if (from == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_CAMERA) {
60                 if(to == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_WORLD || to == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT) {
61                         tfm = kernel_data.cam.cameratoworld;
62                         if(is_direction)
63                                 in = transform_direction(&tfm, in);
64                         else
65                                 in = transform_point(&tfm, in);
66                 }
67                 if(to == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT && is_object) {
68                         if(is_direction)
69                                 object_inverse_dir_transform(kg, sd, &in);
70                         else
71                                 object_inverse_position_transform(kg, sd, &in);
72                 }
73         }
74         
75         /* From object */
76         else if(from == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT) {
77                 if((to == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_WORLD || to == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_CAMERA) && is_object) {
78                         if(is_direction)
79                                 object_dir_transform(kg, sd, &in);
80                         else
81                                 object_position_transform(kg, sd, &in);
82                 }
83                 if(to == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_CAMERA) {
84                         tfm = kernel_data.cam.worldtocamera;
85                         if(is_direction)
86                                 in = transform_direction(&tfm, in);
87                         else
88                                 in = transform_point(&tfm, in);
89                 }
90         }
91         
92         /* Normalize Normal */
93         if(type == NODE_VECTOR_TRANSFORM_TYPE_NORMAL)
94                 in = normalize(in);
95         
96         /* Output */    
97         if(stack_valid(vector_out)) {
98                         stack_store_float3(stack, vector_out, in);
99         }
100 }
101
102 CCL_NAMESPACE_END
103