Blender V2.61 - r43446

node_texture_proc.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version. 
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2005 Blender Foundation.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): Robin Allen
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include "node_texture_util.h"
00034 #include "NOD_texture.h"
00035 
00036 #include "RE_shader_ext.h"
00037 
00038 /* 
00039     In this file: wrappers to use procedural textures as nodes
00040 */
00041 
00042 
00043 static bNodeSocketTemplate outputs_both[]= {
00044     { SOCK_RGBA, 0, "Color",  1.0f, 0.0f, 0.0f, 1.0f },
00045     { SOCK_VECTOR, 0, "Normal", 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, PROP_DIRECTION },
00046     { -1, 0, "" }
00047 };
00048 static bNodeSocketTemplate outputs_color_only[]= {
00049     { SOCK_RGBA, 0, "Color" },
00050     { -1, 0, "" }
00051 };
00052 
00053 /* Inputs common to all, #defined because nodes will need their own inputs too */
00054 #define I 2 /* count */
00055 #define COMMON_INPUTS \
00056     { SOCK_RGBA, 1, "Color 1", 0.0f, 0.0f, 0.0f, 1.0f }, \
00057     { SOCK_RGBA, 1, "Color 2", 1.0f, 1.0f, 1.0f, 1.0f }
00058 
00059 /* Calls multitex and copies the result to the outputs. Called by xxx_exec, which handles inputs. */
00060 static void do_proc(float *result, TexParams *p, float *col1, float *col2, char is_normal, Tex *tex, short thread)
00061 {
00062     TexResult texres;
00063     int textype;
00064     
00065     if(is_normal) {
00066         texres.nor = result;
00067     }
00068     else
00069         texres.nor = NULL;
00070     
00071     textype = multitex_nodes(tex, p->co, p->dxt, p->dyt, p->osatex,
00072         &texres, thread, 0, p->shi, p->mtex);
00073     
00074     if(is_normal)
00075         return;
00076     
00077     if(textype & TEX_RGB) {
00078         copy_v4_v4(result, &texres.tr);
00079     }
00080     else {
00081         copy_v4_v4(result, col1);
00082         ramp_blend(MA_RAMP_BLEND, result, texres.tin, col2);
00083     }
00084 }
00085 
00086 typedef void (*MapFn) (Tex *tex, bNodeStack **in, TexParams *p, short thread);
00087 
00088 static void texfn(
00089     float *result, 
00090     TexParams *p,
00091     bNode *node, 
00092     bNodeStack **in,
00093     char is_normal, 
00094     MapFn map_inputs,
00095     short thread)
00096 {
00097     Tex tex = *((Tex*)(node->storage));
00098     float col1[4], col2[4];
00099     tex_input_rgba(col1, in[0], p, thread);
00100     tex_input_rgba(col2, in[1], p, thread);
00101     
00102     map_inputs(&tex, in, p, thread);
00103     
00104     do_proc(result, p, col1, col2, is_normal, &tex, thread);
00105 }
00106 
00107 static int count_outputs(bNode *node)
00108 {
00109     bNodeSocket *sock;
00110     int num = 0;
00111     for(sock= node->outputs.first; sock; sock= sock->next) {
00112         num++;
00113     }
00114     return num;
00115 }
00116 
00117 /* Boilerplate generators */
00118 
00119 #define ProcNoInputs(name) \
00120         static void name##_map_inputs(Tex *UNUSED(tex), bNodeStack **UNUSED(in), TexParams *UNUSED(p), short UNUSED(thread)) \
00121         {}
00122 
00123 #define ProcDef(name) \
00124         static void name##_colorfn(float *result, TexParams *p, bNode *node, bNodeStack **in, short thread)  \
00125         {                                                                                                    \
00126                 texfn(result, p, node, in, 0, &name##_map_inputs, thread);                               \
00127         }                                                                                                    \
00128         static void name##_normalfn(float *result, TexParams *p, bNode *node, bNodeStack **in, short thread) \
00129         {                                                                                                    \
00130                 texfn(result, p, node, in, 1, &name##_map_inputs, thread);                               \
00131         }                                                                                                    \
00132         static void name##_exec(void *data, bNode *node, bNodeStack **in, bNodeStack **out)                  \
00133         {                                                                                                    \
00134                 int outs = count_outputs(node);                                                              \
00135                 if(outs >= 1) tex_output(node, in, out[0], &name##_colorfn, data);                                 \
00136                 if(outs >= 2) tex_output(node, in, out[1], &name##_normalfn, data);                                \
00137         }
00138 
00139 
00140 /* --- VORONOI -- */
00141 static bNodeSocketTemplate voronoi_inputs[]= {
00142     COMMON_INPUTS,
00143     { SOCK_FLOAT, 1, "W1", 1.0f, 0.0f, 0.0f, 0.0f,   -2.0f, 2.0f, PROP_NONE },
00144     { SOCK_FLOAT, 1, "W2", 0.0f, 0.0f, 0.0f, 0.0f,   -2.0f, 2.0f, PROP_NONE },
00145     { SOCK_FLOAT, 1, "W3", 0.0f, 0.0f, 0.0f, 0.0f,   -2.0f, 2.0f, PROP_NONE },
00146     { SOCK_FLOAT, 1, "W4", 0.0f, 0.0f, 0.0f, 0.0f,   -2.0f, 2.0f, PROP_NONE },
00147     
00148     { SOCK_FLOAT, 1, "iScale", 1.0f, 0.0f, 0.0f, 0.0f,    0.01f,  10.0f, PROP_UNSIGNED },
00149     { SOCK_FLOAT, 1, "Size",   0.25f, 0.0f, 0.0f, 0.0f,   0.0001f, 4.0f, PROP_UNSIGNED },
00150     
00151     { -1, 0, "" }
00152 };
00153 static void voronoi_map_inputs(Tex *tex, bNodeStack **in, TexParams *p, short thread)
00154 {
00155     tex->vn_w1 = tex_input_value(in[I+0], p, thread);
00156     tex->vn_w2 = tex_input_value(in[I+1], p, thread);
00157     tex->vn_w3 = tex_input_value(in[I+2], p, thread);
00158     tex->vn_w4 = tex_input_value(in[I+3], p, thread);
00159     
00160     tex->ns_outscale = tex_input_value(in[I+4], p, thread);
00161     tex->noisesize   = tex_input_value(in[I+5], p, thread);
00162 }
00163 ProcDef(voronoi)
00164 
00165 /* --- BLEND -- */
00166 static bNodeSocketTemplate blend_inputs[]= {
00167     COMMON_INPUTS,
00168     { -1, 0, "" }
00169 };
00170 ProcNoInputs(blend)
00171 ProcDef(blend)
00172 
00173 /* -- MAGIC -- */
00174 static bNodeSocketTemplate magic_inputs[]= {
00175     COMMON_INPUTS,
00176     { SOCK_FLOAT, 1, "Turbulence", 5.0f, 0.0f, 0.0f, 0.0f,   0.0f, 200.0f, PROP_UNSIGNED },
00177     { -1, 0, "" }
00178 };
00179 static void magic_map_inputs(Tex *tex, bNodeStack **in, TexParams *p, short thread)
00180 {
00181     tex->turbul = tex_input_value(in[I+0], p, thread);
00182 }
00183 ProcDef(magic)
00184 
00185 /* --- MARBLE --- */
00186 static bNodeSocketTemplate marble_inputs[]= {
00187     COMMON_INPUTS,
00188     { SOCK_FLOAT, 1, "Size",       0.25f, 0.0f, 0.0f, 0.0f,   0.0001f, 2.0f, PROP_UNSIGNED },
00189     { SOCK_FLOAT, 1, "Turbulence", 5.0f,  0.0f, 0.0f, 0.0f,   0.0f, 200.0f, PROP_UNSIGNED },
00190     { -1, 0, "" }
00191 };
00192 static void marble_map_inputs(Tex *tex, bNodeStack **in, TexParams *p, short thread)
00193 {
00194     tex->noisesize = tex_input_value(in[I+0], p, thread);
00195     tex->turbul    = tex_input_value(in[I+1], p, thread);
00196 }
00197 ProcDef(marble)
00198 
00199 /* --- CLOUDS --- */
00200 static bNodeSocketTemplate clouds_inputs[]= {
00201     COMMON_INPUTS,
00202     { SOCK_FLOAT, 1, "Size",       0.25f, 0.0f, 0.0f, 0.0f,   0.0001f, 2.0f, PROP_UNSIGNED },
00203     { -1, 0, "" }
00204 };
00205 static void clouds_map_inputs(Tex *tex, bNodeStack **in, TexParams *p, short thread)
00206 {
00207     tex->noisesize = tex_input_value(in[I+0], p, thread);
00208 }
00209 ProcDef(clouds)
00210 
00211 /* --- DISTORTED NOISE --- */
00212 static bNodeSocketTemplate distnoise_inputs[]= {
00213     COMMON_INPUTS,
00214     { SOCK_FLOAT, 1, "Size",       0.25f, 0.0f, 0.0f, 0.0f,   0.0001f,  2.0f, PROP_UNSIGNED },
00215     { SOCK_FLOAT, 1, "Distortion", 1.00f, 0.0f, 0.0f, 0.0f,   0.0000f, 10.0f, PROP_UNSIGNED },
00216     { -1, 0, "" }
00217 };
00218 static void distnoise_map_inputs(Tex *tex, bNodeStack **in, TexParams *p, short thread)
00219 {
00220     tex->noisesize   = tex_input_value(in[I+0], p, thread);
00221     tex->dist_amount = tex_input_value(in[I+1], p, thread);
00222 }
00223 ProcDef(distnoise)
00224 
00225 /* --- WOOD --- */
00226 static bNodeSocketTemplate wood_inputs[]= {
00227     COMMON_INPUTS,
00228     { SOCK_FLOAT, 1, "Size",       0.25f, 0.0f, 0.0f, 0.0f,   0.0001f, 2.0f, PROP_UNSIGNED },
00229     { SOCK_FLOAT, 1, "Turbulence", 5.0f,  0.0f, 0.0f, 0.0f,   0.0f, 200.0f, PROP_UNSIGNED },
00230     { -1, 0, "" }
00231 };
00232 static void wood_map_inputs(Tex *tex, bNodeStack **in, TexParams *p, short thread)
00233 {
00234     tex->noisesize = tex_input_value(in[I+0], p, thread);
00235     tex->turbul    = tex_input_value(in[I+1], p, thread);
00236 }
00237 ProcDef(wood)
00238 
00239 /* --- MUSGRAVE --- */
00240 static bNodeSocketTemplate musgrave_inputs[]= {
00241     COMMON_INPUTS,
00242     { SOCK_FLOAT, 1, "H",          1.0f, 0.0f, 0.0f, 0.0f,   0.0001f, 2.0f, PROP_UNSIGNED },
00243     { SOCK_FLOAT, 1, "Lacunarity", 2.0f, 0.0f, 0.0f, 0.0f,   0.0f,    6.0f, PROP_UNSIGNED },
00244     { SOCK_FLOAT, 1, "Octaves",    2.0f, 0.0f, 0.0f, 0.0f,   0.0f,    8.0f, PROP_UNSIGNED },
00245     
00246     { SOCK_FLOAT, 1, "iScale",     1.0f,  0.0f, 0.0f, 0.0f,  0.0f,   10.0f, PROP_UNSIGNED },
00247     { SOCK_FLOAT, 1, "Size",       0.25f, 0.0f, 0.0f, 0.0f,  0.0001f, 2.0f, PROP_UNSIGNED },
00248     { -1, 0, "" }
00249 };
00250 static void musgrave_map_inputs(Tex *tex, bNodeStack **in, TexParams *p, short thread)
00251 {
00252     tex->mg_H          = tex_input_value(in[I+0], p, thread);
00253     tex->mg_lacunarity = tex_input_value(in[I+1], p, thread);
00254     tex->mg_octaves    = tex_input_value(in[I+2], p, thread);
00255     tex->ns_outscale   = tex_input_value(in[I+3], p, thread);
00256     tex->noisesize     = tex_input_value(in[I+4], p, thread);
00257 }
00258 ProcDef(musgrave)
00259 
00260 /* --- NOISE --- */
00261 static bNodeSocketTemplate noise_inputs[]= {
00262     COMMON_INPUTS,
00263     { -1, 0, "" }
00264 };
00265 ProcNoInputs(noise)
00266 ProcDef(noise)
00267 
00268 /* --- STUCCI --- */
00269 static bNodeSocketTemplate stucci_inputs[]= {
00270     COMMON_INPUTS,
00271     { SOCK_FLOAT, 1, "Size",       0.25f, 0.0f, 0.0f, 0.0f,   0.0001f, 2.0f, PROP_UNSIGNED },
00272     { SOCK_FLOAT, 1, "Turbulence", 5.0f,  0.0f, 0.0f, 0.0f,   0.0f, 200.0f, PROP_UNSIGNED },
00273     { -1, 0, "" }
00274 };
00275 static void stucci_map_inputs(Tex *tex, bNodeStack **in, TexParams *p, short thread)
00276 {
00277     tex->noisesize = tex_input_value(in[I+0], p, thread);
00278     tex->turbul    = tex_input_value(in[I+1], p, thread);
00279 }
00280 ProcDef(stucci)
00281 
00282 /* --- */
00283 
00284 static void init(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
00285 {
00286     Tex *tex = MEM_callocN(sizeof(Tex), "Tex");
00287     node->storage= tex;
00288     
00289     default_tex(tex);
00290     tex->type = node->type - TEX_NODE_PROC;
00291     
00292     if(tex->type == TEX_WOOD)
00293         tex->stype = TEX_BANDNOISE;
00294     
00295 }
00296 
00297 /* Node type definitions */
00298 #define TexDef(TEXTYPE, outputs, name, Name) \
00299 void register_node_type_tex_proc_##name(bNodeTreeType *ttype) \
00300 { \
00301     static bNodeType ntype; \
00302     \
00303     node_type_base(ttype, &ntype, TEX_NODE_PROC+TEXTYPE, Name, NODE_CLASS_TEXTURE, NODE_PREVIEW|NODE_OPTIONS); \
00304     node_type_socket_templates(&ntype, name##_inputs, outputs); \
00305     node_type_size(&ntype, 140, 80, 140); \
00306     node_type_init(&ntype, init); \
00307     node_type_storage(&ntype, "Tex", node_free_standard_storage, node_copy_standard_storage); \
00308     node_type_exec(&ntype, name##_exec); \
00309     \
00310     nodeRegisterType(ttype, &ntype); \
00311 }
00312     
00313 #define C outputs_color_only
00314 #define CV outputs_both
00315     
00316 TexDef(TEX_VORONOI,   CV, voronoi,   "Voronoi"  )
00317 TexDef(TEX_BLEND,     C,  blend,     "Blend"    )
00318 TexDef(TEX_MAGIC,     C,  magic,     "Magic"    )
00319 TexDef(TEX_MARBLE,    CV, marble,    "Marble"   )
00320 TexDef(TEX_CLOUDS,    CV, clouds,    "Clouds"   )
00321 TexDef(TEX_WOOD,      CV, wood,      "Wood"     )
00322 TexDef(TEX_MUSGRAVE,  CV, musgrave,  "Musgrave" )
00323 TexDef(TEX_NOISE,     C,  noise,     "Noise"    )
00324 TexDef(TEX_STUCCI,    CV, stucci,    "Stucci"   )
00325 TexDef(TEX_DISTNOISE, CV, distnoise, "Distorted Noise" )