GHOST: Fix uninitialized var
[blender.git] / source / blender / nodes / shader / nodes / node_shader_geom.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 /** \file blender/nodes/shader/nodes/node_shader_geom.c
29  *  \ingroup shdnodes
30  */
31
32
33 #include "node_shader_util.h"
34
35 #include "DNA_customdata_types.h"
36
37 /* **************** GEOMETRY  ******************** */
38
39 /* output socket type definition */
40 static bNodeSocketTemplate sh_node_geom_out[] = {
41         {       SOCK_VECTOR, 0, N_("Global")},
42         {       SOCK_VECTOR, 0, N_("Local")},
43         {       SOCK_VECTOR, 0, N_("View")},
44         {       SOCK_VECTOR, 0, N_("Orco")},
45         {       SOCK_VECTOR, 0, N_("UV")},
46         {       SOCK_VECTOR, 0, N_("Normal")},
47         {       SOCK_RGBA,   0, N_("Vertex Color")},
48         {       SOCK_FLOAT,   0, N_("Vertex Alpha")},
49         {       SOCK_FLOAT,   0, N_("Front/Back")},
50         {       -1, 0, ""       }
51 };
52
53 /* node execute callback */
54 static void node_shader_exec_geom(void *data, int UNUSED(thread), bNode *node, bNodeExecData *UNUSED(execdata), bNodeStack **UNUSED(in), bNodeStack **out)
55 {
56         if (data) {
57                 ShadeInput *shi = ((ShaderCallData *)data)->shi;
58                 NodeGeometry *ngeo = (NodeGeometry *)node->storage;
59                 ShadeInputUV *suv = &shi->uv[shi->actuv];
60                 static float defaultvcol[4] = {1.0f, 1.0f, 1.0f, 1.0f};
61                 int i;
62
63                 if (ngeo->uvname[0]) {
64                         /* find uv map by name */
65                         for (i = 0; i < shi->totuv; i++) {
66                                 if (STREQ(shi->uv[i].name, ngeo->uvname)) {
67                                         suv = &shi->uv[i];
68                                         break;
69                                 }
70                         }
71                 }
72
73                 /* out: global, local, view, orco, uv, normal, vertex color */
74                 copy_v3_v3(out[GEOM_OUT_GLOB]->vec, shi->gl);
75                 copy_v3_v3(out[GEOM_OUT_LOCAL]->vec, shi->co);
76                 copy_v3_v3(out[GEOM_OUT_VIEW]->vec, shi->view);
77                 copy_v3_v3(out[GEOM_OUT_ORCO]->vec, shi->lo);
78                 copy_v3_v3(out[GEOM_OUT_UV]->vec, suv->uv);
79                 copy_v3_v3(out[GEOM_OUT_NORMAL]->vec, shi->vno);
80
81                 if (shi->use_world_space_shading) {
82                         negate_v3(out[GEOM_OUT_NORMAL]->vec);
83                         mul_mat3_m4_v3((float (*)[4])RE_render_current_get_matrix(RE_VIEWINV_MATRIX), out[GEOM_OUT_NORMAL]->vec);
84                 }
85                 if (shi->totcol) {
86                         /* find vertex color layer by name */
87                         ShadeInputCol *scol = &shi->col[0];
88
89                         if (ngeo->colname[0]) {
90                                 for (i = 0; i < shi->totcol; i++) {
91                                         if (STREQ(shi->col[i].name, ngeo->colname)) {
92                                                 scol = &shi->col[i];
93                                                 break;
94                                         }
95                                 }
96                         }
97
98                         srgb_to_linearrgb_v3_v3(out[GEOM_OUT_VCOL]->vec, scol->col);
99                         out[GEOM_OUT_VCOL]->vec[3] = scol->col[3];
100                         out[GEOM_OUT_VCOL_ALPHA]->vec[0] = scol->col[3];
101                 }
102                 else {
103                         memcpy(out[GEOM_OUT_VCOL]->vec, defaultvcol, sizeof(defaultvcol));
104                         out[GEOM_OUT_VCOL_ALPHA]->vec[0] = 1.0f;
105                 }
106
107                 if (shi->osatex) {
108                         out[GEOM_OUT_GLOB]->data = shi->dxgl;
109                         out[GEOM_OUT_GLOB]->datatype = NS_OSA_VECTORS;
110                         out[GEOM_OUT_LOCAL]->data = shi->dxco;
111                         out[GEOM_OUT_LOCAL]->datatype = NS_OSA_VECTORS;
112                         out[GEOM_OUT_VIEW]->data = &shi->dxview;
113                         out[GEOM_OUT_VIEW]->datatype = NS_OSA_VALUES;
114                         out[GEOM_OUT_ORCO]->data = shi->dxlo;
115                         out[GEOM_OUT_ORCO]->datatype = NS_OSA_VECTORS;
116                         out[GEOM_OUT_UV]->data = suv->dxuv;
117                         out[GEOM_OUT_UV]->datatype = NS_OSA_VECTORS;
118                         out[GEOM_OUT_NORMAL]->data = shi->dxno;
119                         out[GEOM_OUT_NORMAL]->datatype = NS_OSA_VECTORS;
120                 }
121
122                 /* front/back, normal flipping was stored */
123                 out[GEOM_OUT_FRONTBACK]->vec[0] = (shi->flippednor) ? 0.0f : 1.0f;
124         }
125 }
126
127 static void node_shader_init_geometry(bNodeTree *UNUSED(ntree), bNode *node)
128 {
129         node->storage = MEM_callocN(sizeof(NodeGeometry), "NodeGeometry");
130 }
131
132 static int gpu_shader_geom(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
133 {
134         NodeGeometry *ngeo = (NodeGeometry *)node->storage;
135         GPUNodeLink *orco = GPU_attribute(CD_ORCO, "");
136         GPUNodeLink *mtface = GPU_attribute(CD_MTFACE, ngeo->uvname);
137         GPUNodeLink *mcol = GPU_attribute(CD_MCOL, ngeo->colname);
138
139         bool ret = GPU_stack_link(mat, "geom", in, out,
140                               GPU_builtin(GPU_VIEW_POSITION), GPU_builtin(GPU_VIEW_NORMAL),
141                               GPU_builtin(GPU_INVERSE_VIEW_MATRIX), orco, mtface, mcol);
142         if (GPU_material_use_world_space_shading(mat)) {
143                 GPU_link(mat, "vec_math_negate", out[5].link, &out[5].link);
144                 ret &= GPU_link(mat, "direction_transform_m4v3", out[5].link, GPU_builtin(GPU_INVERSE_VIEW_MATRIX), &out[5].link);
145         }
146         return ret;
147 }
148
149 /* node type definition */
150 void register_node_type_sh_geom(void)
151 {
152         static bNodeType ntype;
153
154         sh_node_type_base(&ntype, SH_NODE_GEOMETRY, "Geometry", NODE_CLASS_INPUT, 0);
155         node_type_compatibility(&ntype, NODE_OLD_SHADING);
156         node_type_socket_templates(&ntype, NULL, sh_node_geom_out);
157         node_type_init(&ntype, node_shader_init_geometry);
158         node_type_storage(&ntype, "NodeGeometry", node_free_standard_storage, node_copy_standard_storage);
159         node_type_exec(&ntype, NULL, NULL, node_shader_exec_geom);
160         node_type_gpu(&ntype, gpu_shader_geom);
161
162         nodeRegisterType(&ntype);
163 }