Blender V2.61 - r43446

node_texture_bricks.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 <math.h>
00037 
00038 static bNodeSocketTemplate inputs[]= {
00039     { SOCK_RGBA,  1, "Bricks 1",    0.596f, 0.282f, 0.0f,  1.0f },
00040     { SOCK_RGBA,  1, "Bricks 2",    0.632f, 0.504f, 0.05f, 1.0f },
00041     { SOCK_RGBA,  1, "Mortar",      0.0f,   0.0f,   0.0f,  1.0f },
00042     { SOCK_FLOAT, 1, "Thickness",   0.02f,  0.0f,   0.0f,  0.0f,  0.0f,    1.0f, PROP_UNSIGNED },
00043     { SOCK_FLOAT, 1, "Bias",        0.0f,   0.0f,   0.0f,  0.0f, -1.0f,    1.0f, PROP_NONE },
00044     { SOCK_FLOAT, 1, "Brick Width", 0.5f,   0.0f,   0.0f,  0.0f,  0.001f, 99.0f, PROP_UNSIGNED },
00045     { SOCK_FLOAT, 1, "Row Height",  0.25f,  0.0f,   0.0f,  0.0f,  0.001f, 99.0f, PROP_UNSIGNED },
00046     { -1, 0, "" }
00047 };
00048 static bNodeSocketTemplate outputs[]= {
00049     { SOCK_RGBA, 0, "Color"},
00050     { -1, 0, "" }
00051 };
00052 
00053 static void init(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
00054 {
00055     node->custom3 = 0.5; /* offset */
00056     node->custom4 = 1.0; /* squash */
00057 }
00058 
00059 static float noise(int n) /* fast integer noise */
00060 {
00061     int nn;
00062     n = (n >> 13) ^ n;
00063     nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
00064     return 0.5f * ((float)nn / 1073741824.0f);
00065 }
00066 
00067 static void colorfn(float *out, TexParams *p, bNode *node, bNodeStack **in, short thread)
00068 {
00069     float *co = p->co;
00070     
00071     float x = co[0];
00072     float y = co[1];
00073     
00074     int bricknum, rownum;
00075     float offset = 0;
00076     float ins_x, ins_y;
00077     float tint;
00078     
00079     float bricks1[4];
00080     float bricks2[4];
00081     float mortar[4];
00082     
00083     float mortar_thickness = tex_input_value(in[3], p, thread);
00084     float bias             = tex_input_value(in[4], p, thread);
00085     float brick_width      = tex_input_value(in[5], p, thread);
00086     float row_height       = tex_input_value(in[6], p, thread);
00087     
00088     tex_input_rgba(bricks1, in[0], p, thread);
00089     tex_input_rgba(bricks2, in[1], p, thread);
00090     tex_input_rgba(mortar,  in[2], p, thread);
00091     
00092     rownum = (int)floor(y / row_height);
00093     
00094     if( node->custom1 && node->custom2 ) {
00095         brick_width *= ((int)(rownum) % node->custom2 ) ? 1.0f : node->custom4;      /* squash */
00096         offset = ((int)(rownum) % node->custom1 ) ? 0 : (brick_width*node->custom3); /* offset */
00097     }
00098     
00099     bricknum = (int)floor((x+offset) / brick_width);
00100     
00101     ins_x = (x+offset) - brick_width*bricknum;
00102     ins_y = y - row_height*rownum;
00103     
00104     tint = noise((rownum << 16) + (bricknum & 0xFFFF)) + bias;
00105     CLAMP(tint,0.0f,1.0f);
00106     
00107     if( ins_x < mortar_thickness || ins_y < mortar_thickness ||
00108         ins_x > (brick_width - mortar_thickness) ||
00109         ins_y > (row_height - mortar_thickness) ) {
00110         copy_v4_v4( out, mortar );
00111     } else {
00112         copy_v4_v4( out, bricks1 );
00113         ramp_blend( MA_RAMP_BLEND, out, tint, bricks2 );
00114     }
00115 }
00116 
00117 static void exec(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
00118 {
00119     tex_output(node, in, out[0], &colorfn, data);
00120 }
00121 
00122 void register_node_type_tex_bricks(bNodeTreeType *ttype)
00123 {
00124     static bNodeType ntype;
00125     
00126     node_type_base(ttype, &ntype, TEX_NODE_BRICKS, "Bricks", NODE_CLASS_PATTERN, NODE_PREVIEW|NODE_OPTIONS);
00127     node_type_socket_templates(&ntype, inputs, outputs);
00128     node_type_size(&ntype, 150, 60, 150);
00129     node_type_init(&ntype, init);
00130     node_type_exec(&ntype, exec);
00131     
00132     nodeRegisterType(ttype, &ntype);
00133 }