1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2006 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup texnodes
22  */
23 
24 #include "NOD_texture.h"
25 #include "node_texture_util.h"
26 
27 /* **************** COMPOSITE ******************** */
28 static bNodeSocketTemplate inputs[] = {
29     {SOCK_RGBA, N_("Color"), 0.0f, 0.0f, 0.0f, 1.0f},
30     {SOCK_VECTOR, N_("Normal"), 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, PROP_DIRECTION},
31     {-1, ""},
32 };
33 
34 /* applies to render pipeline */
exec(void * data,int UNUSED (thread),bNode * node,bNodeExecData * execdata,bNodeStack ** in,bNodeStack ** UNUSED (out))35 static void exec(void *data,
36                  int UNUSED(thread),
37                  bNode *node,
38                  bNodeExecData *execdata,
39                  bNodeStack **in,
40                  bNodeStack **UNUSED(out))
41 {
42   TexCallData *cdata = (TexCallData *)data;
43   TexResult *target = cdata->target;
44 
45   if (cdata->do_preview) {
46     TexParams params;
47     params_from_cdata(&params, cdata);
48 
49     if (in[1] && in[1]->hasinput && !in[0]->hasinput) {
50       tex_input_rgba(&target->tr, in[1], &params, cdata->thread);
51     }
52     else {
53       tex_input_rgba(&target->tr, in[0], &params, cdata->thread);
54     }
55     tex_do_preview(execdata->preview, params.co, &target->tr, cdata->do_manage);
56   }
57   else {
58     /* 0 means don't care, so just use first */
59     if (cdata->which_output == node->custom1 || (cdata->which_output == 0 && node->custom1 == 1)) {
60       TexParams params;
61       params_from_cdata(&params, cdata);
62 
63       tex_input_rgba(&target->tr, in[0], &params, cdata->thread);
64 
65       target->tin = (target->tr + target->tg + target->tb) / 3.0f;
66       target->talpha = true;
67 
68       if (target->nor) {
69         if (in[1] && in[1]->hasinput) {
70           tex_input_vec(target->nor, in[1], &params, cdata->thread);
71         }
72         else {
73           target->nor = NULL;
74         }
75       }
76     }
77   }
78 }
79 
unique_name(bNode * node)80 static void unique_name(bNode *node)
81 {
82   TexNodeOutput *tno = (TexNodeOutput *)node->storage;
83   char new_name[sizeof(tno->name)];
84   int new_len = 0;
85   int suffix;
86   bNode *i;
87   const char *name = tno->name;
88 
89   new_name[0] = '\0';
90   i = node;
91   while (i->prev) {
92     i = i->prev;
93   }
94   for (; i; i = i->next) {
95     if (i == node || i->type != TEX_NODE_OUTPUT ||
96         !STREQ(name, ((TexNodeOutput *)(i->storage))->name)) {
97       continue;
98     }
99 
100     if (new_name[0] == '\0') {
101       int len = strlen(name);
102       if (len >= 4 && sscanf(name + len - 4, ".%03d", &suffix) == 1) {
103         new_len = len;
104       }
105       else {
106         suffix = 0;
107         new_len = len + 4;
108         if (new_len > (sizeof(tno->name) - 1)) {
109           new_len = (sizeof(tno->name) - 1);
110         }
111       }
112 
113       BLI_strncpy(new_name, name, sizeof(tno->name));
114       name = new_name;
115     }
116     sprintf(new_name + new_len - 4, ".%03d", ++suffix);
117   }
118 
119   if (new_name[0] != '\0') {
120     BLI_strncpy(tno->name, new_name, sizeof(tno->name));
121   }
122 }
123 
assign_index(struct bNode * node)124 static void assign_index(struct bNode *node)
125 {
126   bNode *tnode;
127   int index = 1;
128 
129   tnode = node;
130   while (tnode->prev) {
131     tnode = tnode->prev;
132   }
133 
134 check_index:
135   for (; tnode; tnode = tnode->next) {
136     if (tnode->type == TEX_NODE_OUTPUT && tnode != node) {
137       if (tnode->custom1 == index) {
138         index++;
139         goto check_index;
140       }
141     }
142   }
143 
144   node->custom1 = index;
145 }
146 
init(bNodeTree * UNUSED (ntree),bNode * node)147 static void init(bNodeTree *UNUSED(ntree), bNode *node)
148 {
149   TexNodeOutput *tno = MEM_callocN(sizeof(TexNodeOutput), "TEX_output");
150   node->storage = tno;
151 
152   strcpy(tno->name, "Default");
153   unique_name(node);
154   assign_index(node);
155 }
156 
copy(bNodeTree * dest_ntree,bNode * dest_node,const bNode * src_node)157 static void copy(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node)
158 {
159   node_copy_standard_storage(dest_ntree, dest_node, src_node);
160   unique_name(dest_node);
161   assign_index(dest_node);
162 }
163 
register_node_type_tex_output(void)164 void register_node_type_tex_output(void)
165 {
166   static bNodeType ntype;
167 
168   tex_node_type_base(&ntype, TEX_NODE_OUTPUT, "Output", NODE_CLASS_OUTPUT, NODE_PREVIEW);
169   node_type_socket_templates(&ntype, inputs, NULL);
170   node_type_size_preset(&ntype, NODE_SIZE_MIDDLE);
171   node_type_init(&ntype, init);
172   node_type_storage(&ntype, "TexNodeOutput", node_free_standard_storage, copy);
173   node_type_exec(&ntype, NULL, NULL, exec);
174 
175   /* Do not allow muting output. */
176   node_type_internal_links(&ntype, NULL);
177 
178   nodeRegisterType(&ntype);
179 }
180