Fix for bug #8073: texture nodes connected to a viewer could crash.
[blender.git] / source / blender / nodes / intern / CMP_nodes / CMP_texture.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2006 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 #include "../CMP_util.h"
31
32 /* **************** TEXTURE ******************** */
33 static bNodeSocketType cmp_node_texture_in[]= {
34         {       SOCK_VECTOR, 1, "Offset",               0.0f, 0.0f, 0.0f, 0.0f, -2.0f, 2.0f},
35         {       SOCK_VECTOR, 1, "Scale",                1.0f, 1.0f, 1.0f, 1.0f, -10.0f, 10.0f},
36         {       -1, 0, ""       }
37 };
38 static bNodeSocketType cmp_node_texture_out[]= {
39         {       SOCK_VALUE, 0, "Value",         1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
40         {       SOCK_RGBA , 0, "Color",         1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f},
41         {       -1, 0, ""       }
42 };
43
44 /* called without rect allocated */
45 static void texture_procedural(CompBuf *cbuf, float *out, float xco, float yco)
46 {
47         bNode *node= cbuf->node;
48         TexResult texres= {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL};
49         float vec[3], *size, nor[3]={0.0f, 0.0f, 0.0f}, col[4];
50         int retval, type= cbuf->procedural_type;
51         
52         size= cbuf->procedural_size;
53         
54         vec[0]= size[0]*(xco + cbuf->procedural_offset[0]);
55         vec[1]= size[1]*(yco + cbuf->procedural_offset[1]);
56         vec[2]= size[2]*cbuf->procedural_offset[2];
57         
58         retval= multitex_ext((Tex *)node->id, vec, NULL, NULL, 0, &texres);
59         
60         if(type==CB_VAL) {
61                 if(texres.talpha)
62                         col[0]= texres.ta;
63                 else
64                         col[0]= texres.tin;
65         }
66         else if(type==CB_RGBA) {
67                 if(texres.talpha)
68                         col[3]= texres.ta;
69                 else
70                         col[3]= texres.tin;
71                 
72                 if((retval & TEX_RGB)) {
73                         col[0]= texres.tr;
74                         col[1]= texres.tg;
75                         col[2]= texres.tb;
76                 }
77                 else col[0]= col[1]= col[2]= col[3];
78         }
79         else { 
80                 VECCOPY(col, nor);
81         }
82
83         typecheck_compbuf_color(out, col, cbuf->type, cbuf->procedural_type);
84 }
85
86 /* texture node outputs get a small rect, to make sure all other nodes accept it */
87 /* only the pixel-processor nodes do something with it though */
88 static void node_composit_exec_texture(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
89 {
90         /* outputs: value, color, normal */
91         
92         if(node->id) {
93                 RenderData *rd= data;
94                 RenderResult *rr= RE_GetResult(RE_GetRender(G.scene->id.name)); /* G.scene is WEAK! */
95                 short sizex, sizey;
96                 
97                 /* first make the preview image */
98                 CompBuf *prevbuf= alloc_compbuf(140, 140, CB_RGBA, 1); /* alloc */
99                 
100                 if (rr) {
101                         sizex = rr->rectx;
102                         sizey = rr->recty;
103                 } else {
104                         sizex = rd->xsch;
105                         sizey = rd->ysch;
106                 }
107                 
108                 prevbuf->rect_procedural= texture_procedural;
109                 prevbuf->node= node;
110                 VECCOPY(prevbuf->procedural_offset, in[0]->vec);
111                 VECCOPY(prevbuf->procedural_size, in[1]->vec);
112                 prevbuf->procedural_type= CB_RGBA;
113                 composit1_pixel_processor(node, prevbuf, prevbuf, out[0]->vec, do_copy_rgba, CB_RGBA);
114                 generate_preview(node, prevbuf);
115                 free_compbuf(prevbuf);
116                 
117                 if(out[0]->hasoutput) {
118                         CompBuf *stackbuf= alloc_compbuf(sizex, sizey, CB_VAL, 1); /* alloc */
119                         
120                         stackbuf->rect_procedural= texture_procedural;
121                         stackbuf->node= node;
122                         VECCOPY(stackbuf->procedural_offset, in[0]->vec);
123                         VECCOPY(stackbuf->procedural_size, in[1]->vec);
124                         stackbuf->procedural_type= CB_VAL;
125                         
126                         out[0]->data= stackbuf; 
127                 }
128                 if(out[1]->hasoutput) {
129                         CompBuf *stackbuf= alloc_compbuf(sizex, sizey, CB_RGBA, 1); /* alloc */
130                         
131                         stackbuf->rect_procedural= texture_procedural;
132                         stackbuf->node= node;
133                         VECCOPY(stackbuf->procedural_offset, in[0]->vec);
134                         VECCOPY(stackbuf->procedural_size, in[1]->vec);
135                         stackbuf->procedural_type= CB_RGBA;
136                         
137                         out[1]->data= stackbuf;
138                 }
139         }
140 }
141
142 bNodeType cmp_node_texture= {
143         /* *next,*prev */       NULL, NULL,
144         /* type code   */       CMP_NODE_TEXTURE,
145         /* name        */       "Texture",
146         /* width+range */       120, 80, 240,
147         /* class+opts  */       NODE_CLASS_INPUT, NODE_OPTIONS|NODE_PREVIEW,
148         /* input sock  */       cmp_node_texture_in,
149         /* output sock */       cmp_node_texture_out,
150         /* storage     */       "",
151         /* execfunc    */       node_composit_exec_texture,
152         /* butfunc     */       NULL,
153         /* initfunc    */       NULL,
154         /* freestoragefunc    */        NULL,
155         /* copystoragefunc    */        NULL,
156         /* id          */       NULL
157         
158 };
159
160