Blender V2.61 - r43446

node_shader_texture.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): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include "DNA_texture_types.h"
00034 
00035 #include "node_shader_util.h"
00036 
00037 /* **************** TEXTURE ******************** */
00038 static bNodeSocketTemplate sh_node_texture_in[]= {
00039     {   SOCK_VECTOR, 1, "Vector",   0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, PROP_NONE, SOCK_HIDE_VALUE},   /* no limit */
00040     {   -1, 0, ""   }
00041 };
00042 static bNodeSocketTemplate sh_node_texture_out[]= {
00043     {   SOCK_FLOAT, 0, "Value"},
00044     {   SOCK_RGBA , 0, "Color"},
00045     {   SOCK_VECTOR, 0, "Normal"},
00046     {   -1, 0, ""   }
00047 };
00048 
00049 static void node_shader_exec_texture(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
00050 {
00051     if(data && node->id) {
00052         ShadeInput *shi= ((ShaderCallData *)data)->shi;
00053         TexResult texres;
00054         bNodeSocket *sock_vector= node->inputs.first;
00055         float vec[3], nor[3]={0.0f, 0.0f, 0.0f};
00056         int retval;
00057         short which_output = node->custom1;
00058         
00059         short thread = shi->thread;
00060         
00061         /* out: value, color, normal */
00062         
00063         /* we should find out if a normal as output is needed, for now we do all */
00064         texres.nor= nor;
00065         texres.tr= texres.tg= texres.tb= 0.0f;
00066         
00067         /* don't use in[0]->hasinput, see material node for explanation */
00068         if(sock_vector->link) {
00069             nodestack_get_vec(vec, SOCK_VECTOR, in[0]);
00070             
00071             if(in[0]->datatype==NS_OSA_VECTORS) {
00072                 float *fp= in[0]->data;
00073                 retval= multitex_nodes((Tex *)node->id, vec, fp, fp+3, shi->osatex, &texres, thread, which_output, NULL, NULL);
00074             }
00075             else if(in[0]->datatype==NS_OSA_VALUES) {
00076                 float *fp= in[0]->data;
00077                 float dxt[3], dyt[3];
00078                 
00079                 dxt[0]= fp[0]; dxt[1]= dxt[2]= 0.0f;
00080                 dyt[0]= fp[1]; dyt[1]= dyt[2]= 0.0f;
00081                 retval= multitex_nodes((Tex *)node->id, vec, dxt, dyt, shi->osatex, &texres, thread, which_output, NULL, NULL);
00082             }
00083             else
00084                 retval= multitex_nodes((Tex *)node->id, vec, NULL, NULL, 0, &texres, thread, which_output, NULL, NULL);
00085         }
00086         else {
00087             copy_v3_v3(vec, shi->lo);
00088             retval= multitex_nodes((Tex *)node->id, vec, NULL, NULL, 0, &texres, thread, which_output, NULL, NULL);
00089         }
00090         
00091         /* stupid exception */
00092         if( ((Tex *)node->id)->type==TEX_STUCCI) {
00093             texres.tin= 0.5f + 0.7f*texres.nor[0];
00094             CLAMP(texres.tin, 0.0f, 1.0f);
00095         }
00096         
00097         /* intensity and color need some handling */
00098         if(texres.talpha)
00099             out[0]->vec[0]= texres.ta;
00100         else
00101             out[0]->vec[0]= texres.tin;
00102         
00103         if((retval & TEX_RGB)==0) {
00104             out[1]->vec[0]= out[0]->vec[0];
00105             out[1]->vec[1]= out[0]->vec[0];
00106             out[1]->vec[2]= out[0]->vec[0];
00107             out[1]->vec[3]= 1.0f;
00108         }
00109         else {
00110             out[1]->vec[0]= texres.tr;
00111             out[1]->vec[1]= texres.tg;
00112             out[1]->vec[2]= texres.tb;
00113             out[1]->vec[3]= 1.0f;
00114         }
00115         
00116         copy_v3_v3(out[2]->vec, nor);
00117         
00118         if(shi->do_preview)
00119             nodeAddToPreview(node, out[1]->vec, shi->xs, shi->ys, shi->do_manage);
00120         
00121     }
00122 }
00123 
00124 static int gpu_shader_texture(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *out)
00125 {
00126     Tex *tex = (Tex*)node->id;
00127 
00128     if(tex && tex->type == TEX_IMAGE && tex->ima) {
00129         GPUNodeLink *texlink = GPU_image(tex->ima, NULL);
00130         return GPU_stack_link(mat, "texture_image", in, out, texlink);
00131     }
00132     else
00133         return 0;
00134 }
00135 
00136 void register_node_type_sh_texture(bNodeTreeType *ttype)
00137 {
00138     static bNodeType ntype;
00139 
00140     node_type_base(ttype, &ntype, SH_NODE_TEXTURE, "Texture", NODE_CLASS_INPUT, NODE_OPTIONS|NODE_PREVIEW);
00141     node_type_compatibility(&ntype, NODE_OLD_SHADING);
00142     node_type_socket_templates(&ntype, sh_node_texture_in, sh_node_texture_out);
00143     node_type_size(&ntype, 120, 80, 240);
00144     node_type_exec(&ntype, node_shader_exec_texture);
00145     node_type_gpu(&ntype, gpu_shader_texture);
00146 
00147     nodeRegisterType(ttype, &ntype);
00148 }