aboutsummaryrefslogtreecommitdiff
path: root/vrayPlug/plugin/shaveVrayVoxelPrim.cpp
blob: 2e6cd2b32eab58b00229ad16a4f3df89fa4d7191 (plain) (blame)
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
// Shave and a Haircut
// (c) 2019 Epic Games
// US Patent 6720962

/**********************************************************************
 *<
	FILE:			shaveVrayVoxelPrim.cpp ( was shaveVrayVoxelPrim.cpp ) -- implementation file  

	DESCRIPTION:	Generic hair voxel primitive

	CREATED BY:		Vladimir Dubovoy <[email protected]>

	HISTORY:		created  02-09-2008 ( as part of 3ds Max + VRay hair shaders)
					merged   30-03-2010

 *>
 **********************************************************************/

#include "assert.h"
#include "shaveVrayVoxelPrim.h"
//#include "shaveVrayPrimsSharedFunctions.h"
#include "shaveVrayShadeable.h"
#include "shaveVrayShadeInstance.h"
#include "shaveVrayShadeData.h"
#include "shaveVrayInstance.h"


shaveVrayVoxelPrim::shaveVrayVoxelPrim(IHairVoxel* vox, 
									 VR::VRayCore *vray,
									 shaveVrayInstance *inst):
	shaveVrayVoxelPrimBase(vox,vray)
{
	_hinst()    = inst;
	_numknots() = eDefNumKnots;
	
	for (int i=0; i < eNumSides; i++) 
	{
		float angle=2.0f*VR::pi()*float(i)/float(eNumSides);
		_uc(i)=cosf(angle);
		_vc(i)=sinf(angle);
	}

	if(voxel())
	{
		const HairType& hair = voxel()->GetHair();
		int   numHairs = hair.GetNumStrands();
		if(numHairs == 0)
		{
			//assume all hairs have the same number of segments
			_numknots() = hair.face_end[0] - hair.face_start[0];
		}
	}
	
	shaveVrayShadeable* sh = new shaveVrayShadeable(this);
	if(vray)
	{
		sh->initBSDFpool(vray);
	}
	_shade() = sh;

	//_shinst() = new shaveVrayShadeInstance<moving>(this);
	_shinst() = (VR::VRayShadeInstance*) new shaveVrayShadeInstance(this);

//	_shdata() = new HairVrShadeData();

	//_shdata() = new shaveVrayShadeData(hinst());
	_shdata() = inst->GetShData();

//	_sfprops() = new VR::SurfaceProperties;

	_tipfade() = false;
}

shaveVrayVoxelPrim::~shaveVrayVoxelPrim()
{
	if(shade())
		delete shade();

	if(shinst())
		delete shinst();

	//if(shdata())
	//	delete shdata();

	//if(sfprops())
	//	delete sfprops();
}

bool shaveVrayVoxelPrim::IsColorConst(int strandidx) const
{
	const HairType& hair = voxel()->GetHair();
	return hair.IsColorConst(strandidx);
}

void shaveVrayVoxelPrim::GetRootColor(int strandidx,VR::Color& c) const
{
	const HairType& hair = voxel()->GetHair();
	return hair.GetRootColor(strandidx,c.r,c.g,c.b);
}

float shaveVrayVoxelPrim::GetOpacity(int strandidx) const
{
	const HairType& hair = voxel()->GetHair();
	return hair.GetStrandOpacity(strandidx);
}

float shaveVrayVoxelPrim::GetAmbDiff(int strandidx) const
{
	const HairType& hair = voxel()->GetHair();
	return hair.GetStrandAmbDiff(strandidx);
}

float shaveVrayVoxelPrim::GetGlossiness(int strandidx) const
{
	const HairType& hair = voxel()->GetHair();
	return hair.GetStrandGlossiness(strandidx);
}

float shaveVrayVoxelPrim::GetSpecLevel(int strandidx) const
{
	const HairType& hair = voxel()->GetHair();
	return hair.GetStrandSpecLevel(strandidx);
}

VR::ShadeVec shaveVrayVoxelPrim::GetUVW(int segmentIdx, int strandidx, float segmentOffset, int channel) const {
#if 0 //vlad|22jan2013 - we still do not have valid data in UVSETS
	const HairUVs& hairUVs=voxel()->GetUVs();

	if (channel<0 || channel>=hairUVs.totalUVSets)
		return VR::Vector(0.0f, 0.0f, 0.0f);

	if (strandidx<0 || strandidx>=hairUVs.totalRoots)
		return VR::Vector(0.0f, 0.0f, 0.0f);

	const VERT &uv=hairUVs.uvRoot[strandidx*hairUVs.totalUVSets+channel];
	return VR::Vector(uv.x, uv.y, uv.z);

#else //grabbing it form defaut set of HAIRTYPE
	
	const HairType& hair = voxel()->GetHair();
	int   numHairs = hair.GetNumStrands();

	if (strandidx >= numHairs || strandidx < 0)
		return VR::ShadeVec(0.0f, 0.0f, 0.0f);

	int start;
	int end;
	hair.GetStrand(strandidx, start, end);

	int numKnots=end-start;
	if (segmentIdx<0 || segmentIdx>=numKnots-1)
		return VR::ShadeVec(0.0f, 0.0f, 0.0f);

	VR::ShadeVec uvw0;
	VR::ShadeVec uvw1;

	int min_i=segmentIdx+start;
	GetUV(hair, min_i+0, uvw0);
	GetUV(hair, min_i+1, uvw1);

	return uvw0+(uvw1-uvw0)*segmentOffset;
	
#endif
}

//not optimized routine
void shaveVrayVoxelPrim::GetInterpColor(int segmentIdx, int strandidx, float segmentOffset, VR::Color& res) const
{
	///////////// test ///////////
	//res = VR::Color(1.0f, 0.0f, 0.0f);
	//return;
	//////////////////////////////

	const HairType& hair = voxel()->GetHair();
	int   numHairs = hair.GetNumStrands();

	if(strandidx >= numHairs || strandidx < 0)
	{
		//red hair will be a signal that something goes wrong
		res = VR::Color(1.0f, 0.0f, 0.0f);
		return;
	}
	int start;
	int end;
	hair.GetStrand(strandidx,start,end);
	int nknots = end - start;

	float t=segmentOffset;

	float T=(float) (segmentIdx);
	float d=(T+t)/(float) nknots;

	hair.GetColor(strandidx,d,res.r,res.g,res.b);
}

//not optimized routine
float shaveVrayVoxelPrim::GetInterpOpacity(int segmentIdx, int strandidx, float segmentOffset) const
{
	///////////// test /////////
	//return 1.0f;
	///////////////////////////

	const HairType& hair = voxel()->GetHair();
	int   numHairs = hair.GetNumStrands();

	if(strandidx >= numHairs || strandidx < 0)
	{
		return 1.0f;
	}
	int start;
	int end;
	hair.GetStrand(strandidx,start,end);
	int nknots = end - start;

	float t=segmentOffset;

	float T=(float) (segmentIdx);
	float d=(T+t)/(float) nknots;

	if(d > 1.0f)
		d = 1.0f;

	if(d < 0.0f)
		d = 0.0f;

	float op = hair.GetStrandOpacity(strandidx);
	return op*(1.0f-d);
}

VR::ShadeVec shaveVrayVoxelPrim::GetHairDir(const VR::ShadeVec &hit, int segmentIdx, int strandidx, float segmentOffset) const
{
	//////////// test ////////////
	//return VR::Vector(0.0f, 0.0f, 1.0f);
	////////////////////////////

	const HairType& hair = voxel()->GetHair();
	int   numHairs = hair.GetNumStrands();

	if(strandidx >= numHairs || strandidx < 0)
	{
		return VR::ShadeVec(0.0f, 0.0f, 1.0f);
	}
	int start;
	int end;
	hair.GetStrand(strandidx,start,end);
	int nknots = end - start;

	VR::ShadeVec knot0;
	VR::ShadeVec knot1;
	int min_i=segmentIdx+start;

	GetVert(hair, min_i, knot0);
	GetVert(hair, min_i+1, knot1);
	VR::ShadeVec sg = knot1 - knot0;

	float t=segmentOffset;
	float T=(float) (segmentIdx);
	float d=(T+t)/(float) nknots;

	float sgLen=sg.length();
	VR::ShadeVec dir1=(sgLen<1e-12f)? VR::ShadeVec(0.0f, 0.0f, 0.0f) : (sg/sg.length()); // The direction of the current segment

	VR::ShadeVec dir0; // The direction of the previous segment
	if (min_i-1-start<0) {
		GetSurfNormal(hair, strandidx, dir0);
		dir0.makeNormalized0();
	} else {
		VR::ShadeVec knotb;
		GetVert(hair, min_i-1, knotb);
		dir0=knot0-knotb;
		dir0.makeNormalized0();
	}

	dir0+=(dir1-dir0)*t;
	dir0.makeNormalized0();
	return dir0;
	//return VR::normalize0(dir0+(dir1-dir0)*t);
}