Made texture nodes accessible in the interface.
[blender.git] / source / blender / nodes / intern / TEX_nodes / TEX_output.c
1 /**
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. 
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2006 Blender Foundation.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include "../TEX_util.h"
30
31 /* **************** COMPOSITE ******************** */
32 static bNodeSocketType inputs[]= {
33         { SOCK_RGBA,   1, "Color",  0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
34         { SOCK_VECTOR, 1, "Normal", 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f},
35         { -1, 0, ""     }
36 };
37
38 /* applies to render pipeline */
39 static void exec(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
40 {
41         TexCallData *cdata = (TexCallData *)data;
42         TexResult *target = cdata->target;
43         
44         if(in[1]->hasinput && !in[0]->hasinput)
45                 tex_do_preview(node, in[1], data);
46         else
47                 tex_do_preview(node, in[0], data);
48         
49         if(!cdata->do_preview) {
50                 if(cdata->which_output == node->custom1)
51                 {
52                         tex_input_rgba(&target->tr, in[0], cdata->coord, cdata->thread);
53                 
54                         target->tin = (target->tr + target->tg + target->tb) / 3.0f;
55                         target->talpha = 1.0f;
56                 
57                         if(target->nor) {
58                                 if(in[1]->hasinput)
59                                         tex_input_vec(target->nor, in[1], cdata->coord, cdata->thread);
60                                 else
61                                         target->nor = 0;
62                         }
63                 }
64         }
65 }
66
67 static void unique_name(bNode *node)
68 {
69         TexNodeOutput *tno = (TexNodeOutput *)node->storage;
70         char *new_name = 0;
71         int new_len;
72         int suffix;
73         bNode *i;
74         char *name = tno->name;
75         
76         i = node;
77         while(i->prev) i = i->prev;
78         for(; i; i=i->next) {
79                 if(
80                         i == node ||
81                         i->type != TEX_NODE_OUTPUT ||
82                         strcmp(name, ((TexNodeOutput*)(i->storage))->name)
83                 )
84                         continue;
85                 
86                 if(!new_name) {
87                         int len = strlen(name);
88                         if(len >= 4 && sscanf(name + len - 4, ".%03d", &suffix) == 1) {
89                                 new_len = len;
90                         } else {
91                                 suffix = 0;
92                                 new_len = len + 4;
93                                 if(new_len > 31)
94                                         new_len = 31;
95                         }
96                         
97                         new_name = malloc(new_len + 1);
98                         strcpy(new_name, name);
99                         name = new_name;
100                 }
101                 sprintf(new_name + new_len - 4, ".%03d", ++suffix);
102         }
103         
104         if(new_name) {
105                 strcpy(tno->name, new_name);
106                 free(new_name);
107         }
108 }
109
110 static void init(bNode *node)
111 {
112         TexNodeOutput *tno = MEM_callocN(sizeof(TexNodeOutput), "TEX_output");
113         node->storage= tno;
114         
115         strcpy(tno->name, "Default");
116         unique_name(node);
117         ntreeTexAssignIndex(0, node);
118 }
119
120 static void copy(bNode *orig, bNode *new)
121 {
122         node_copy_standard_storage(orig, new);
123         unique_name(new);
124         ntreeTexAssignIndex(0, new);
125 }
126
127
128 bNodeType tex_node_output= {
129         /* *next,*prev     */  NULL, NULL,
130         /* type code       */  TEX_NODE_OUTPUT,
131         /* name            */  "Output",
132         /* width+range     */  150, 60, 200,
133         /* class+opts      */  NODE_CLASS_OUTPUT, NODE_PREVIEW | NODE_OPTIONS, 
134         /* input sock      */  inputs,
135         /* output sock     */  NULL,
136         /* storage         */  "TexNodeOutput",
137         /* execfunc        */  exec,
138         /* butfunc         */  NULL,
139         /* initfunc        */  init,
140         /* freestoragefunc */  node_free_standard_storage,
141         /* copystoragefunc */  copy,  
142         /* id              */  NULL
143 };