Blender  V2.59
TEX_bricks.c
Go to the documentation of this file.
00001 /*
00002  *
00003  * ***** BEGIN GPL LICENSE BLOCK *****
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version. 
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  * The Original Code is Copyright (C) 2005 Blender Foundation.
00020  * All rights reserved.
00021  *
00022  * The Original Code is: all of this file.
00023  *
00024  * Contributor(s): Robin Allen
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 #include "../TEX_util.h"
00035 #include "TEX_node.h"
00036 
00037 #include <math.h>
00038 
00039 static bNodeSocketType inputs[]= {
00040         { SOCK_RGBA,  1, "Bricks 1",    0.596f, 0.282f, 0.0f,  1.0f,  0.0f,    1.0f },
00041         { SOCK_RGBA,  1, "Bricks 2",    0.632f, 0.504f, 0.05f, 1.0f,  0.0f,    1.0f },
00042         { SOCK_RGBA,  1, "Mortar",      0.0f,   0.0f,   0.0f,  1.0f,  0.0f,    1.0f },
00043         { SOCK_VALUE, 1, "Thickness",   0.02f,  0.0f,   0.0f,  0.0f,  0.0f,    1.0f },
00044         { SOCK_VALUE, 1, "Bias",        0.0f,   0.0f,   0.0f,  0.0f, -1.0f,    1.0f },
00045         { SOCK_VALUE, 1, "Brick Width", 0.5f,   0.0f,   0.0f,  0.0f,  0.001f, 99.0f },
00046         { SOCK_VALUE, 1, "Row Height",  0.25f,  0.0f,   0.0f,  0.0f,  0.001f, 99.0f },
00047         { -1, 0, "" }
00048 };
00049 static bNodeSocketType outputs[]= {
00050         { SOCK_RGBA, 0, "Color", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
00051         { -1, 0, ""     }
00052 };
00053 
00054 static void init(bNode *node) {
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                 QUATCOPY( out, mortar );
00111         } else {
00112                 QUATCOPY( out, bricks1 );
00113                 ramp_blend( MA_RAMP_BLEND, out, out+1, out+2, 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(ListBase *lb)
00123 {
00124         static bNodeType ntype;
00125         
00126         node_type_base(&ntype, TEX_NODE_BRICKS, "Bricks", NODE_CLASS_PATTERN, NODE_PREVIEW|NODE_OPTIONS,
00127                                    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(lb, &ntype);
00133 }