Blender V2.61 - r43446

node_shader_geom.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 "node_shader_util.h"
00034 
00035 #include "DNA_customdata_types.h"
00036 
00037 /* **************** GEOMETRY  ******************** */
00038 
00039 /* output socket type definition */
00040 static bNodeSocketTemplate sh_node_geom_out[]= {
00041     {   SOCK_VECTOR, 0, "Global"},
00042     {   SOCK_VECTOR, 0, "Local"},
00043     {   SOCK_VECTOR, 0, "View"},
00044     {   SOCK_VECTOR, 0, "Orco"},
00045     {   SOCK_VECTOR, 0, "UV"},
00046     {   SOCK_VECTOR, 0, "Normal"},
00047     {   SOCK_RGBA,   0, "Vertex Color"},
00048     {   SOCK_FLOAT,   0, "Vertex Alpha"},
00049     {   SOCK_FLOAT,   0, "Front/Back"},
00050     {   -1, 0, ""   }
00051 };
00052 
00053 /* node execute callback */
00054 static void node_shader_exec_geom(void *data, bNode *node, bNodeStack **UNUSED(in), bNodeStack **out)
00055 {
00056     if(data) {
00057         ShadeInput *shi= ((ShaderCallData *)data)->shi;
00058         NodeGeometry *ngeo= (NodeGeometry*)node->storage;
00059         ShadeInputUV *suv= &shi->uv[shi->actuv];
00060         static float defaultvcol[4] = {1.0f, 1.0f, 1.0f, 1.0f};
00061         int i;
00062 
00063         if(ngeo->uvname[0]) {
00064             /* find uv map by name */
00065             for(i = 0; i < shi->totuv; i++) {
00066                 if(strcmp(shi->uv[i].name, ngeo->uvname)==0) {
00067                     suv= &shi->uv[i];
00068                     break;
00069                 }
00070             }
00071         }
00072 
00073         /* out: global, local, view, orco, uv, normal, vertex color */
00074         copy_v3_v3(out[GEOM_OUT_GLOB]->vec, shi->gl);
00075         copy_v3_v3(out[GEOM_OUT_LOCAL]->vec, shi->co);
00076         copy_v3_v3(out[GEOM_OUT_VIEW]->vec, shi->view);
00077         copy_v3_v3(out[GEOM_OUT_ORCO]->vec, shi->lo);
00078         copy_v3_v3(out[GEOM_OUT_UV]->vec, suv->uv);
00079         copy_v3_v3(out[GEOM_OUT_NORMAL]->vec, shi->vno);
00080 
00081         if (shi->totcol) {
00082             /* find vertex color layer by name */
00083             ShadeInputCol *scol= &shi->col[0];
00084 
00085             if(ngeo->colname[0]) {
00086                 for(i = 0; i < shi->totcol; i++) {
00087                     if(strcmp(shi->col[i].name, ngeo->colname)==0) {
00088                         scol= &shi->col[i];
00089                         break;
00090                     }
00091                 }
00092             }
00093 
00094             copy_v3_v3(out[GEOM_OUT_VCOL]->vec, scol->col);
00095             out[GEOM_OUT_VCOL]->vec[3]= scol->col[3];
00096             out[GEOM_OUT_VCOL_ALPHA]->vec[0]= scol->col[3];
00097         }
00098         else  {
00099             memcpy(out[GEOM_OUT_VCOL]->vec, defaultvcol, sizeof(defaultvcol));
00100             out[GEOM_OUT_VCOL_ALPHA]->vec[0]= 1.0f;
00101         }
00102         
00103         if(shi->osatex) {
00104             out[GEOM_OUT_GLOB]->data= shi->dxgl;
00105             out[GEOM_OUT_GLOB]->datatype= NS_OSA_VECTORS;
00106             out[GEOM_OUT_LOCAL]->data= shi->dxco;
00107             out[GEOM_OUT_LOCAL]->datatype= NS_OSA_VECTORS;
00108             out[GEOM_OUT_VIEW]->data= &shi->dxview;
00109             out[GEOM_OUT_VIEW]->datatype= NS_OSA_VALUES;
00110             out[GEOM_OUT_ORCO]->data= shi->dxlo;
00111             out[GEOM_OUT_ORCO]->datatype= NS_OSA_VECTORS;
00112             out[GEOM_OUT_UV]->data= suv->dxuv;
00113             out[GEOM_OUT_UV]->datatype= NS_OSA_VECTORS;
00114             out[GEOM_OUT_NORMAL]->data= shi->dxno;
00115             out[GEOM_OUT_NORMAL]->datatype= NS_OSA_VECTORS;
00116         }
00117         
00118         /* front/back, normal flipping was stored */
00119         out[GEOM_OUT_FRONTBACK]->vec[0]= (shi->flippednor)? 0.0f: 1.0f;
00120     }
00121 }
00122 
00123 static void node_shader_init_geometry(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
00124 {
00125     node->storage= MEM_callocN(sizeof(NodeGeometry), "NodeGeometry");
00126 }
00127 
00128 static int gpu_shader_geom(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *out)
00129 {
00130     NodeGeometry *ngeo= (NodeGeometry*)node->storage;
00131     GPUNodeLink *orco = GPU_attribute(CD_ORCO, "");
00132     GPUNodeLink *mtface = GPU_attribute(CD_MTFACE, ngeo->uvname);
00133     GPUNodeLink *mcol = GPU_attribute(CD_MCOL, ngeo->colname);
00134 
00135     return GPU_stack_link(mat, "geom", in, out,
00136         GPU_builtin(GPU_VIEW_POSITION), GPU_builtin(GPU_VIEW_NORMAL),
00137         GPU_builtin(GPU_INVERSE_VIEW_MATRIX), orco, mtface, mcol);
00138 }
00139 
00140 /* node type definition */
00141 void register_node_type_sh_geom(bNodeTreeType *ttype)
00142 {
00143     static bNodeType ntype;
00144 
00145     node_type_base(ttype, &ntype, SH_NODE_GEOMETRY, "Geometry", NODE_CLASS_INPUT, NODE_OPTIONS);
00146     node_type_compatibility(&ntype, NODE_OLD_SHADING);
00147     node_type_socket_templates(&ntype, NULL, sh_node_geom_out);
00148     node_type_size(&ntype, 120, 80, 160);
00149     node_type_init(&ntype, node_shader_init_geometry);
00150     node_type_storage(&ntype, "NodeGeometry", node_free_standard_storage, node_copy_standard_storage);
00151     node_type_exec(&ntype, node_shader_exec_geom);
00152     node_type_gpu(&ntype, gpu_shader_geom);
00153 
00154     nodeRegisterType(ttype, &ntype);
00155 }