Blender  V2.93
node_texture_output.c
Go to the documentation of this file.
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 
24 #include "NOD_texture.h"
25 #include "node_texture_util.h"
26 
27 /* **************** COMPOSITE ******************** */
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 */
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) {
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)) {
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 
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 
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 
148 {
149  TexNodeOutput *tno = MEM_callocN(sizeof(TexNodeOutput), "TEX_output");
150  node->storage = tno;
151 
152  strcpy(tno->name, "Default");
153  unique_name(node);
155 }
156 
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 
165 {
166  static bNodeType ntype;
167 
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. */
177 
178  nodeRegisterType(&ntype);
179 }
#define NODE_CLASS_OUTPUT
Definition: BKE_node.h:335
void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
Definition: node.cc:4527
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4559
void node_type_size_preset(struct bNodeType *ntype, eNodeSizePreset size)
Definition: node.cc:4577
void node_type_internal_links(struct bNodeType *ntype, void(*update_internal_links)(struct bNodeTree *, struct bNode *))
void node_type_storage(struct bNodeType *ntype, const char *storagename, void(*freefunc)(struct bNode *node), void(*copyfunc)(struct bNodeTree *dest_ntree, struct bNode *dest_node, const struct bNode *src_node))
Definition: node.cc:4599
#define TEX_NODE_OUTPUT
Definition: BKE_node.h:1325
void node_type_exec(struct bNodeType *ntype, NodeInitExecFunction init_exec_fn, NodeFreeExecFunction free_exec_fn, NodeExecFunction exec_fn)
Definition: node.cc:4635
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1298
@ NODE_SIZE_MIDDLE
Definition: BKE_node.h:372
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
#define UNUSED(x)
#define STREQ(a, b)
#define N_(msgid)
#define NODE_PREVIEW
@ SOCK_VECTOR
@ SOCK_RGBA
@ PROP_DIRECTION
Definition: RNA_types.h:141
OperationNode * node
bNodeTree * ntree
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
static void assign_index(struct bNode *node)
static void copy(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node)
void register_node_type_tex_output(void)
static void unique_name(bNode *node)
static bNodeSocketTemplate inputs[]
static void init(bNodeTree *UNUSED(ntree), bNode *node)
static void exec(void *data, int UNUSED(thread), bNode *node, bNodeExecData *execdata, bNodeStack **in, bNodeStack **UNUSED(out))
void tex_do_preview(bNodePreview *preview, const float coord[2], const float col[4], bool do_manage)
void tex_input_vec(float *out, bNodeStack *in, TexParams *params, short thread)
void tex_node_type_base(struct bNodeType *ntype, int type, const char *name, short nclass, short flag)
void tex_input_rgba(float *out, bNodeStack *in, TexParams *params, short thread)
void params_from_cdata(TexParams *out, TexCallData *in)
void node_copy_standard_storage(bNodeTree *UNUSED(dest_ntree), bNode *dest_node, const bNode *src_node)
Definition: node_util.c:67
void node_free_standard_storage(bNode *node)
Definition: node_util.c:55
TexResult * target
int talpha
Definition: RE_texture.h:85
float tb
Definition: RE_texture.h:84
float * nor
Definition: RE_texture.h:86
float tin
Definition: RE_texture.h:84
float tr
Definition: RE_texture.h:84
float tg
Definition: RE_texture.h:84
struct bNodePreview * preview
Definition: node_util.h:54
Compact definition of a node socket.
Definition: BKE_node.h:95
Defines a node type.
Definition: BKE_node.h:221
short custom1
struct bNode * prev
short type
struct bNode * next
void * storage
uint len