Blender  V2.59
CMP_texture.c
Go to the documentation of this file.
00001 /*
00002  * $Id: CMP_texture.c 35237 2011-02-27 20:13:22Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version. 
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2006 Blender Foundation.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): none yet.
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  */
00029 
00035 #include "../CMP_util.h"
00036 
00037 /* **************** TEXTURE ******************** */
00038 static bNodeSocketType cmp_node_texture_in[]= {
00039         {       SOCK_VECTOR, 1, "Offset",               0.0f, 0.0f, 0.0f, 0.0f, -2.0f, 2.0f},
00040         {       SOCK_VECTOR, 1, "Scale",                1.0f, 1.0f, 1.0f, 1.0f, -10.0f, 10.0f},
00041         {       -1, 0, ""       }
00042 };
00043 static bNodeSocketType cmp_node_texture_out[]= {
00044         {       SOCK_VALUE, 0, "Value",         1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
00045         {       SOCK_RGBA , 0, "Color",         1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f},
00046         {       -1, 0, ""       }
00047 };
00048 
00049 /* called without rect allocated */
00050 static void texture_procedural(CompBuf *cbuf, float *out, float xco, float yco)
00051 {
00052         bNode *node= cbuf->node;
00053         TexResult texres= {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL};
00054         float vec[3], *size, nor[3]={0.0f, 0.0f, 0.0f}, col[4];
00055         int retval, type= cbuf->procedural_type;
00056         
00057         size= cbuf->procedural_size;
00058         
00059         vec[0]= size[0]*(xco + cbuf->procedural_offset[0]);
00060         vec[1]= size[1]*(yco + cbuf->procedural_offset[1]);
00061         vec[2]= size[2]*cbuf->procedural_offset[2];
00062         
00063         retval= multitex_ext((Tex *)node->id, vec, NULL, NULL, 0, &texres);
00064         
00065         if(type==CB_VAL) {
00066                 if(texres.talpha)
00067                         col[0]= texres.ta;
00068                 else
00069                         col[0]= texres.tin;
00070         }
00071         else if(type==CB_RGBA) {
00072                 if(texres.talpha)
00073                         col[3]= texres.ta;
00074                 else
00075                         col[3]= texres.tin;
00076                 
00077                 if((retval & TEX_RGB)) {
00078                         col[0]= texres.tr;
00079                         col[1]= texres.tg;
00080                         col[2]= texres.tb;
00081                 }
00082                 else col[0]= col[1]= col[2]= col[3];
00083         }
00084         else { 
00085                 VECCOPY(col, nor);
00086         }
00087         
00088         typecheck_compbuf_color(out, col, cbuf->type, cbuf->procedural_type);
00089 }
00090 
00091 /* texture node outputs get a small rect, to make sure all other nodes accept it */
00092 /* only the pixel-processor nodes do something with it though */
00093 static void node_composit_exec_texture(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
00094 {
00095         /* outputs: value, color, normal */
00096         
00097         if(node->id) {
00098                 RenderData *rd= data;
00099                 short sizex, sizey;
00100                 
00101                 /* first make the preview image */
00102                 CompBuf *prevbuf= alloc_compbuf(140, 140, CB_RGBA, 1); /* alloc */
00103 
00104                 prevbuf->rect_procedural= texture_procedural;
00105                 prevbuf->node= node;
00106                 VECCOPY(prevbuf->procedural_offset, in[0]->vec);
00107                 VECCOPY(prevbuf->procedural_size, in[1]->vec);
00108                 prevbuf->procedural_type= CB_RGBA;
00109                 composit1_pixel_processor(node, prevbuf, prevbuf, out[0]->vec, do_copy_rgba, CB_RGBA);
00110                 
00111                 generate_preview(data, node, prevbuf);
00112                 free_compbuf(prevbuf);
00113                 
00114                 /* texture procedural buffer type doesnt work well, we now render a buffer in scene size */
00115                 sizex = (rd->size*rd->xsch)/100;
00116                 sizey = (rd->size*rd->ysch)/100;
00117                 
00118                 if(out[0]->hasoutput) {
00119                         CompBuf *stackbuf= alloc_compbuf(sizex, sizey, CB_VAL, 1); /* alloc */
00120                         
00121                         stackbuf->rect_procedural= texture_procedural;
00122                         stackbuf->node= node;
00123                         VECCOPY(stackbuf->procedural_offset, in[0]->vec);
00124                         VECCOPY(stackbuf->procedural_size, in[1]->vec);
00125                         stackbuf->procedural_type= CB_VAL;
00126                         composit1_pixel_processor(node, stackbuf, stackbuf, out[0]->vec, do_copy_value, CB_VAL);
00127                         stackbuf->rect_procedural= NULL;
00128                         
00129                         out[0]->data= stackbuf; 
00130                 }
00131                 if(out[1]->hasoutput) {
00132                         CompBuf *stackbuf= alloc_compbuf(sizex, sizey, CB_RGBA, 1); /* alloc */
00133                         
00134                         stackbuf->rect_procedural= texture_procedural;
00135                         stackbuf->node= node;
00136                         VECCOPY(stackbuf->procedural_offset, in[0]->vec);
00137                         VECCOPY(stackbuf->procedural_size, in[1]->vec);
00138                         stackbuf->procedural_type= CB_RGBA;
00139                         composit1_pixel_processor(node, stackbuf, stackbuf, out[0]->vec, do_copy_rgba, CB_RGBA);
00140                         stackbuf->rect_procedural= NULL;
00141                         
00142                         out[1]->data= stackbuf;
00143                 }
00144         }
00145 }
00146 
00147 void register_node_type_cmp_texture(ListBase *lb)
00148 {
00149         static bNodeType ntype;
00150 
00151         node_type_base(&ntype, CMP_NODE_TEXTURE, "Texture", NODE_CLASS_INPUT, NODE_OPTIONS|NODE_PREVIEW,
00152                 cmp_node_texture_in, cmp_node_texture_out);
00153         node_type_size(&ntype, 120, 80, 240);
00154         node_type_exec(&ntype, node_composit_exec_texture);
00155 
00156         nodeRegisterType(lb, &ntype);
00157 }
00158 
00159 
00160