Dynamic Paint:
[blender.git] / source / blender / nodes / intern / SHD_nodes / SHD_geom.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2005 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/nodes/intern/SHD_nodes/SHD_geom.c
31  *  \ingroup shdnodes
32  */
33
34
35 #include "../SHD_util.h"
36
37 #include "DNA_customdata_types.h"
38
39 /* **************** GEOMETRY  ******************** */
40
41 /* output socket type definition */
42 static bNodeSocketType sh_node_geom_out[]= {
43         {       SOCK_VECTOR, 0, "Global",       0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},   /* btw; uses no limit */
44         {       SOCK_VECTOR, 0, "Local",        0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},
45         {       SOCK_VECTOR, 0, "View", 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},
46         {       SOCK_VECTOR, 0, "Orco", 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},
47         {       SOCK_VECTOR, 0, "UV",   0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},
48         {       SOCK_VECTOR, 0, "Normal",       0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},
49         {       SOCK_RGBA,   0, "Vertex Color", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
50         {       SOCK_VALUE,   0, "Vertex Alpha", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
51         {       SOCK_VALUE,   0, "Front/Back", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
52         {       -1, 0, ""       }
53 };
54
55 /* node execute callback */
56 static void node_shader_exec_geom(void *data, bNode *node, bNodeStack **UNUSED(in), bNodeStack **out)
57 {
58         if(data) {
59                 ShadeInput *shi= ((ShaderCallData *)data)->shi;
60                 NodeGeometry *ngeo= (NodeGeometry*)node->storage;
61                 ShadeInputUV *suv= &shi->uv[shi->actuv];
62                 static float defaultvcol[4] = {1.0f, 1.0f, 1.0f, 1.0f};
63                 int i;
64
65                 if(ngeo->uvname[0]) {
66                         /* find uv layer by name */
67                         for(i = 0; i < shi->totuv; i++) {
68                                 if(strcmp(shi->uv[i].name, ngeo->uvname)==0) {
69                                         suv= &shi->uv[i];
70                                         break;
71                                 }
72                         }
73                 }
74
75                 /* out: global, local, view, orco, uv, normal, vertex color */
76                 VECCOPY(out[GEOM_OUT_GLOB]->vec, shi->gl);
77                 VECCOPY(out[GEOM_OUT_LOCAL]->vec, shi->co);
78                 VECCOPY(out[GEOM_OUT_VIEW]->vec, shi->view);
79                 VECCOPY(out[GEOM_OUT_ORCO]->vec, shi->lo);
80                 VECCOPY(out[GEOM_OUT_UV]->vec, suv->uv);
81                 VECCOPY(out[GEOM_OUT_NORMAL]->vec, shi->vno);
82
83                 if (shi->totcol) {
84                         /* find vertex color layer by name */
85                         ShadeInputCol *scol= &shi->col[0];
86
87                         if(ngeo->colname[0]) {
88                                 for(i = 0; i < shi->totcol; i++) {
89                                         if(strcmp(shi->col[i].name, ngeo->colname)==0) {
90                                                 scol= &shi->col[i];
91                                                 break;
92                                         }
93                                 }
94                         }
95
96                         VECCOPY(out[GEOM_OUT_VCOL]->vec, scol->col);
97                         out[GEOM_OUT_VCOL]->vec[3]= scol->col[3];
98
99                         out[GEOM_OUT_VCOL_ALPHA]->vec[0]= scol->col[3];
100                 }
101                 else {
102                         memcpy(out[GEOM_OUT_VCOL]->vec, defaultvcol, sizeof(defaultvcol));
103                         out[GEOM_OUT_VCOL_ALPHA]->vec[0]= 1.0f;
104                 }
105                 
106                 if(shi->osatex) {
107                         out[GEOM_OUT_GLOB]->data= shi->dxgl;
108                         out[GEOM_OUT_GLOB]->datatype= NS_OSA_VECTORS;
109                         out[GEOM_OUT_LOCAL]->data= shi->dxco;
110                         out[GEOM_OUT_LOCAL]->datatype= NS_OSA_VECTORS;
111                         out[GEOM_OUT_VIEW]->data= &shi->dxview;
112                         out[GEOM_OUT_VIEW]->datatype= NS_OSA_VALUES;
113                         out[GEOM_OUT_ORCO]->data= shi->dxlo;
114                         out[GEOM_OUT_ORCO]->datatype= NS_OSA_VECTORS;
115                         out[GEOM_OUT_UV]->data= suv->dxuv;
116                         out[GEOM_OUT_UV]->datatype= NS_OSA_VECTORS;
117                         out[GEOM_OUT_NORMAL]->data= shi->dxno;
118                         out[GEOM_OUT_NORMAL]->datatype= NS_OSA_VECTORS;
119                 }
120                 
121                 /* front/back, normal flipping was stored */
122                 out[GEOM_OUT_FRONTBACK]->vec[0]= (shi->flippednor)? 0.0f: 1.0f;
123         }
124 }
125
126 static void node_shader_init_geometry(bNode *node)
127 {
128         node->storage= MEM_callocN(sizeof(NodeGeometry), "NodeGeometry");
129 }
130
131 static int gpu_shader_geom(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *out)
132 {
133         NodeGeometry *ngeo= (NodeGeometry*)node->storage;
134         GPUNodeLink *orco = GPU_attribute(CD_ORCO, "");
135         GPUNodeLink *mtface = GPU_attribute(CD_MTFACE, ngeo->uvname);
136         GPUNodeLink *mcol = GPU_attribute(CD_MCOL, ngeo->colname);
137
138         return GPU_stack_link(mat, "geom", in, out,
139                 GPU_builtin(GPU_VIEW_POSITION), GPU_builtin(GPU_VIEW_NORMAL),
140                 GPU_builtin(GPU_INVERSE_VIEW_MATRIX), orco, mtface, mcol);
141 }
142
143 /* node type definition */
144 void register_node_type_sh_geom(ListBase *lb)
145 {
146         static bNodeType ntype;
147
148         node_type_base(&ntype, SH_NODE_GEOMETRY, "Geometry", NODE_CLASS_INPUT, NODE_OPTIONS,
149                 NULL, sh_node_geom_out);
150         node_type_size(&ntype, 120, 80, 160);
151         node_type_init(&ntype, node_shader_init_geometry);
152         node_type_storage(&ntype, "NodeGeometry", node_free_standard_storage, node_copy_standard_storage);
153         node_type_exec(&ntype, node_shader_exec_geom);
154         node_type_gpu(&ntype, gpu_shader_geom);
155
156         nodeRegisterType(lb, &ntype);
157 }
158