Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SHD_material.c « SHD_nodes « intern « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2640f2979fd683e220c68f7ac491e4f549230b76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2005 Blender Foundation.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include "../SHD_util.h"

/* **************** MATERIAL ******************** */

static bNodeSocketType sh_node_material_in[]= {
	{	SOCK_RGBA, 1, "Color",		0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
	{	SOCK_RGBA, 1, "Spec",		0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
	{	SOCK_VALUE, 1, "Refl",		0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
	{	SOCK_VECTOR, 1, "Normal",	0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},
	{	-1, 0, ""	}
};

/* output socket defines */
#define MAT_OUT_COLOR	0
#define MAT_OUT_ALPHA	1
#define MAT_OUT_NORMAL	2

static bNodeSocketType sh_node_material_out[]= {
	{	SOCK_RGBA, 0, "Color",		0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
	{	SOCK_VALUE, 0, "Alpha",		1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
	{	SOCK_VECTOR, 0, "Normal",	0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f},
	{	-1, 0, ""	}
};

static void node_shader_exec_material(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
{
	if(data && node->id) {
		ShadeResult shrnode;
		ShadeInput *shi;
		ShaderCallData *shcd= data;
		float col[4];
		
		shi= shcd->shi;
		shi->mat= (Material *)node->id;
		
		/* copy all relevant material vars, note, keep this synced with render_types.h */
		memcpy(&shi->r, &shi->mat->r, 23*sizeof(float));
		shi->har= shi->mat->har;
		
		/* write values */
		if(in[MAT_IN_COLOR]->hasinput)
			nodestack_get_vec(&shi->r, SOCK_VECTOR, in[MAT_IN_COLOR]);
		
		if(in[MAT_IN_SPEC]->hasinput)
			nodestack_get_vec(&shi->specr, SOCK_VECTOR, in[MAT_IN_SPEC]);
		
		if(in[MAT_IN_REFL]->hasinput)
			nodestack_get_vec(&shi->refl, SOCK_VALUE, in[MAT_IN_REFL]);
		
		/* retrieve normal */
		if(in[MAT_IN_NORMAL]->hasinput) {
			nodestack_get_vec(shi->vn, SOCK_VECTOR, in[MAT_IN_NORMAL]);
			Normalise(shi->vn);
		}
		else
			VECCOPY(shi->vn, shi->vno);
		
		/* custom option to flip normal */
		if(node->custom1 & SH_NODE_MAT_NEG) {
			shi->vn[0]= -shi->vn[0];
			shi->vn[1]= -shi->vn[1];
			shi->vn[2]= -shi->vn[2];
		}
		
		node_shader_lamp_loop(shi, &shrnode);	/* clears shrnode */
		
		/* write to outputs */
		if(node->custom1 & SH_NODE_MAT_DIFF) {
			VECCOPY(col, shrnode.combined);
			if(!(node->custom1 & SH_NODE_MAT_SPEC)) {
				VecSubf(col, col, shrnode.spec);
			}
		}
		else if(node->custom1 & SH_NODE_MAT_SPEC) {
			VECCOPY(col, shrnode.spec);
		}
		else
			col[0]= col[1]= col[2]= 0.0f;
		
		col[3]= shrnode.alpha;
		
		if(shi->do_preview)
			nodeAddToPreview(node, col, shi->xs, shi->ys);
		
		VECCOPY(out[MAT_OUT_COLOR]->vec, col);
		out[MAT_OUT_ALPHA]->vec[0]= shrnode.alpha;
		
		if(node->custom1 & SH_NODE_MAT_NEG) {
			shi->vn[0]= -shi->vn[0];
			shi->vn[1]= -shi->vn[1];
			shi->vn[2]= -shi->vn[2];
		}
		
		VECCOPY(out[MAT_OUT_NORMAL]->vec, shi->vn);
		
		/* copy passes, now just active node */
		if(node->flag & NODE_ACTIVE_ID)
			*(shcd->shr)= shrnode;
	}
}

static void node_mat_alone_cb(void *node_v, void *unused)
{
   bNode *node= node_v;

   node->id= (ID *)copy_material((Material *)node->id);

   BIF_undo_push("Single user material");
   allqueue(REDRAWBUTSSHADING, 0);
   allqueue(REDRAWNODE, 0);
   allqueue(REDRAWOOPS, 0);
}

static void node_browse_mat_cb(void *ntree_v, void *node_v)
{
   bNodeTree *ntree= ntree_v;
   bNode *node= node_v;

   if(node->menunr<1) return;

   if(node->menunr==32767) {	/* code for Add New */
      if(node->id) {
         /* make copy, but make sure it doesnt have the node tag nor nodes */
         Material *ma= (Material *)node->id;
         ma->id.us--;
         ma= copy_material(ma);
         ma->use_nodes= 0;
         if(ma->nodetree) {
            ntreeFreeTree(ma->nodetree);
            MEM_freeN(ma->nodetree);
         }
         ma->nodetree= NULL;
         node->id= (ID *)ma;
      }
      else node->id= (ID *)add_material("MatNode");
   }
   else {
      if(node->id) node->id->us--;
      node->id= BLI_findlink(&G.main->mat, node->menunr-1);
      id_us_plus(node->id);
   }
   BLI_strncpy(node->name, node->id->name+2, 21);

   nodeSetActive(ntree, node);

   allqueue(REDRAWBUTSSHADING, 0);
   allqueue(REDRAWNODE, 0);
   BIF_preview_changed(ID_MA);

   node->menunr= 0;
}

static void node_new_mat_cb(void *ntree_v, void *node_v)
{
   bNodeTree *ntree= ntree_v;
   bNode *node= node_v;

   node->id= (ID *)add_material("MatNode");
   BLI_strncpy(node->name, node->id->name+2, 21);

   nodeSetActive(ntree, node);

   allqueue(REDRAWBUTSSHADING, 0);
   allqueue(REDRAWNODE, 0);
   BIF_preview_changed(ID_MA);

}
static int node_shader_buts_material(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
{
   if(block) {
      uiBut *bt;
      short dx= (short)((butr->xmax-butr->xmin)/3.0f), has_us= (node->id && node->id->us>1);
      short dy= (short)butr->ymin;
      char *strp;

      /* WATCH IT: we use this callback in material buttons, but then only want first row */
      if(butr->ymax-butr->ymin > 21.0f) dy+= 19;

      uiBlockBeginAlign(block);
      if(node->id==NULL) uiBlockSetCol(block, TH_REDALERT);
      else if(has_us) uiBlockSetCol(block, TH_BUT_SETTING1);
      else uiBlockSetCol(block, TH_BUT_SETTING2);

      /* browse button */
      IDnames_to_pupstring(&strp, NULL, "ADD NEW %x32767", &(G.main->mat), NULL, NULL);
      node->menunr= 0;
      bt= uiDefButS(block, MENU, B_NOP, strp, 
         butr->xmin, dy, 19, 19, 
         &node->menunr, 0, 0, 0, 0, "Browses existing choices or adds NEW");
      uiButSetFunc(bt, node_browse_mat_cb, ntree, node);
      if(strp) MEM_freeN(strp);

      /* Add New button */
      if(node->id==NULL) {
         bt= uiDefBut(block, BUT, B_NOP, "Add New",
            butr->xmin+19, dy, (short)(butr->xmax-butr->xmin-19.0f), 19, 
            NULL, 0.0, 0.0, 0, 0, "Add new Material");
         uiButSetFunc(bt, node_new_mat_cb, ntree, node);
         uiBlockSetCol(block, TH_AUTO);
      }
      else {
         /* name button */
         short width= (short)(butr->xmax-butr->xmin-19.0f - (has_us?19.0f:0.0f));
         bt= uiDefBut(block, TEX, B_NOP, "MA:",
            butr->xmin+19, dy, width, 19, 
            node->id->name+2, 0.0, 19.0, 0, 0, "Material name");
         uiButSetFunc(bt, node_ID_title_cb, node, NULL);

         /* user amount */
         if(has_us) {
            char str1[32];
            sprintf(str1, "%d", node->id->us);
            bt= uiDefBut(block, BUT, B_NOP, str1, 
               butr->xmax-19, dy, 19, 19, 
               NULL, 0, 0, 0, 0, "Displays number of users. Click to make a single-user copy.");
            uiButSetFunc(bt, node_mat_alone_cb, node, NULL);
         }

         /* WATCH IT: we use this callback in material buttons, but then only want first row */
         if(butr->ymax-butr->ymin > 21.0f) {
            /* node options */
            uiBlockSetCol(block, TH_AUTO);
            uiDefButBitS(block, TOG, SH_NODE_MAT_DIFF, B_NODE_EXEC+node->nr, "Diff",
               butr->xmin, butr->ymin, dx, 19, 
               &node->custom1, 0, 0, 0, 0, "Material Node outputs Diffuse");
            uiDefButBitS(block, TOG, SH_NODE_MAT_SPEC, B_NODE_EXEC+node->nr, "Spec",
               butr->xmin+dx, butr->ymin, dx, 19, 
               &node->custom1, 0, 0, 0, 0, "Material Node outputs Specular");
            uiDefButBitS(block, TOG, SH_NODE_MAT_NEG, B_NODE_EXEC+node->nr, "Neg Normal",
               butr->xmax-dx, butr->ymin, dx, 19,
               &node->custom1, 0, 0, 0, 0, "Material Node uses inverted Normal");
         }
      }
      uiBlockEndAlign(block);
   }	
   return 38;
}

static void node_shader_init_material(bNode* node)
{
   node->custom1= SH_NODE_MAT_DIFF|SH_NODE_MAT_SPEC;
}


bNodeType sh_node_material= {
	/* type code   */	SH_NODE_MATERIAL,
	/* name        */	"Material",
	/* width+range */	120, 80, 240,
	/* class+opts  */	NODE_CLASS_INPUT, NODE_OPTIONS|NODE_PREVIEW,
	/* input sock  */	sh_node_material_in,
	/* output sock */	sh_node_material_out,
	/* storage     */	"",
	/* execfunc    */	node_shader_exec_material,
   /* butfunc     */ node_shader_buts_material,
   /* initfunc    */ node_shader_init_material
	
};