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