Blender  V2.59
SHD_texture.c
Go to the documentation of this file.
00001 /*
00002  * $Id: SHD_texture.c 35385 2011-03-07 11:51:09Z ton $
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) 2005 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 "DNA_texture_types.h"
00036 
00037 #include "../SHD_util.h"
00038 
00039 /* **************** TEXTURE ******************** */
00040 static bNodeSocketType sh_node_texture_in[]= {
00041         {       SOCK_VECTOR, 1, "Vector",       0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},   /* no limit */
00042         {       -1, 0, ""       }
00043 };
00044 static bNodeSocketType sh_node_texture_out[]= {
00045         {       SOCK_VALUE, 0, "Value",         1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
00046         {       SOCK_RGBA , 0, "Color",         1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f},
00047         {       SOCK_VECTOR, 0, "Normal",       0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},
00048         {       -1, 0, ""       }
00049 };
00050 
00051 static void node_shader_exec_texture(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
00052 {
00053         if(data && node->id) {
00054                 ShadeInput *shi= ((ShaderCallData *)data)->shi;
00055                 TexResult texres;
00056                 float vec[3], nor[3]={0.0f, 0.0f, 0.0f};
00057                 int retval;
00058                 short which_output = node->custom1;
00059                 
00060                 short thread = shi->thread;
00061                 
00062                 /* out: value, color, normal */
00063                 
00064                 /* we should find out if a normal as output is needed, for now we do all */
00065                 texres.nor= nor;
00066                 texres.tr= texres.tg= texres.tb= 0.0f;
00067                 
00068                 if(in[0]->hasinput) {
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                         VECCOPY(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                 VECCOPY(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(ListBase *lb)
00137 {
00138         static bNodeType ntype;
00139 
00140         node_type_base(&ntype, SH_NODE_TEXTURE, "Texture", NODE_CLASS_INPUT, NODE_OPTIONS|NODE_PREVIEW,
00141                 sh_node_texture_in, sh_node_texture_out);
00142         node_type_size(&ntype, 120, 80, 240);
00143         node_type_exec(&ntype, node_shader_exec_texture);
00144         node_type_gpu(&ntype, gpu_shader_texture);
00145 
00146         nodeRegisterType(lb, &ntype);
00147 }
00148 
00149