31afe74bd11403696fa764afa6de599df0cc5e74
[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 *col, float xco, float yco)
46 {
47         bNode *node= cbuf->node;
48         bNodeSocket *sock= node->inputs.first;
49         TexResult texres= {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL};
50         float vec[3], *size, nor[3]={0.0f, 0.0f, 0.0f};
51         int retval, type= cbuf->type;
52         
53         size= sock->next->ns.vec;
54         
55         vec[0]= size[0]*(xco + sock->ns.vec[0]);
56         vec[1]= size[1]*(yco + sock->ns.vec[1]);
57         vec[2]= size[2]*sock->ns.vec[2];
58         
59         retval= multitex_ext((Tex *)node->id, vec, NULL, NULL, 0, &texres);
60         
61         if(type==CB_VAL) {
62                 if(texres.talpha)
63                         col[0]= texres.ta;
64                 else
65                         col[0]= texres.tin;
66         }
67         else if(type==CB_RGBA) {
68                 if(texres.talpha)
69                         col[3]= texres.ta;
70                 else
71                         col[3]= texres.tin;
72                 
73                 if((retval & TEX_RGB)) {
74                         col[0]= texres.tr;
75                         col[1]= texres.tg;
76                         col[2]= texres.tb;
77                 }
78                 else col[0]= col[1]= col[2]= col[3];
79         }
80         else { 
81                 VECCOPY(col, nor);
82         }
83 }
84
85 /* texture node outputs get a small rect, to make sure all other nodes accept it */
86 /* only the pixel-processor nodes do something with it though */
87 static void node_composit_exec_texture(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
88 {
89         /* outputs: value, color, normal */
90         
91         if(node->id) {
92                 RenderData *rd= data;
93                 RenderResult *rr= RE_GetResult(RE_GetRender(G.scene->id.name)); /* G.scene is WEAK! */
94                 short sizex, sizey;
95                 
96                 /* first make the preview image */
97                 CompBuf *prevbuf= alloc_compbuf(140, 140, CB_RGBA, 1); /* alloc */
98                 
99                 if (rr) {
100                         sizex = rr->rectx;
101                         sizey = rr->recty;
102                 } else {
103                         sizex = rd->xsch;
104                         sizey = rd->ysch;
105                 }
106                 
107                 prevbuf->rect_procedural= texture_procedural;
108                 prevbuf->node= node;
109                 composit1_pixel_processor(node, prevbuf, prevbuf, out[0]->vec, do_copy_rgba, CB_RGBA);
110                 generate_preview(node, prevbuf);
111                 free_compbuf(prevbuf);
112                 
113                 if(out[0]->hasoutput) {
114                         CompBuf *stackbuf= alloc_compbuf(sizex, sizey, CB_VAL, 1); /* alloc */
115                         
116                         stackbuf->rect_procedural= texture_procedural;
117                         stackbuf->node= node;
118                         
119                         out[0]->data= stackbuf; 
120                 }
121                 if(out[1]->hasoutput) {
122                         CompBuf *stackbuf= alloc_compbuf(sizex, sizey, CB_RGBA, 1); /* alloc */
123                         
124                         stackbuf->rect_procedural= texture_procedural;
125                         stackbuf->node= node;
126                         
127                         out[1]->data= stackbuf;
128                 }
129         }
130 }
131
132 bNodeType cmp_node_texture= {
133         /* *next,*prev */       NULL, NULL,
134         /* type code   */       CMP_NODE_TEXTURE,
135         /* name        */       "Texture",
136         /* width+range */       120, 80, 240,
137         /* class+opts  */       NODE_CLASS_INPUT, NODE_OPTIONS|NODE_PREVIEW,
138         /* input sock  */       cmp_node_texture_in,
139         /* output sock */       cmp_node_texture_out,
140         /* storage     */       "",
141         /* execfunc    */       node_composit_exec_texture,
142         /* butfunc     */       NULL,
143         /* initfunc    */       NULL,
144         /* freestoragefunc    */        NULL,
145         /* copystoragefunc    */        NULL,
146         /* id          */       NULL
147         
148 };
149
150