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

subd_mesh.cpp « subd « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 17a730e5efe30ec76b9ee9a477ffa73c56f84f4b (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * Original code in the public domain -- castanyo@yahoo.es
 * 
 * Modifications copyright (c) 2011, Blender Foundation.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>

#include "subd_mesh.h"
#include "subd_patch.h"
#include "subd_split.h"

#include "util_debug.h"
#include "util_foreach.h"

#ifdef WITH_OPENSUBDIV

#include <osd/vertex.h>
#include <osd/mesh.h>
#include <osd/cpuComputeController.h>
#include <osd/cpuVertexBuffer.h>
#include <osd/cpuEvalLimitController.h>
#include <osd/evalLimitContext.h>

CCL_NAMESPACE_BEGIN

/* typedefs */
typedef OpenSubdiv::OsdVertex OsdVertex;
typedef OpenSubdiv::FarMesh<OsdVertex> OsdFarMesh;
typedef OpenSubdiv::FarMeshFactory<OsdVertex> OsdFarMeshFactory;
typedef OpenSubdiv::HbrCatmarkSubdivision<OsdVertex> OsdHbrCatmarkSubdivision;
typedef OpenSubdiv::HbrFace<OsdVertex> OsdHbrFace;
typedef OpenSubdiv::HbrHalfedge<OsdVertex> OsdHbrHalfEdge;
typedef OpenSubdiv::HbrMesh<OsdVertex> OsdHbrMesh;
typedef OpenSubdiv::HbrVertex<OsdVertex> OsdHbrVertex;
typedef OpenSubdiv::OsdCpuComputeContext OsdCpuComputeContext;
typedef OpenSubdiv::OsdCpuComputeController OsdCpuComputeController;
typedef OpenSubdiv::OsdCpuEvalLimitContext OsdCpuEvalLimitContext;
typedef OpenSubdiv::OsdCpuEvalLimitController OsdCpuEvalLimitController;
typedef OpenSubdiv::OsdCpuVertexBuffer OsdCpuVertexBuffer;
typedef OpenSubdiv::OsdEvalCoords OsdEvalCoords;
typedef OpenSubdiv::OsdVertexBufferDescriptor OsdVertexBufferDescriptor;

/* OpenSubdiv Patch */

class OpenSubdPatch : public Patch {
public:
	int face_id;

	OpenSubdPatch(OsdFarMesh *farmesh, OsdCpuVertexBuffer *vbuf_base)
	{
		face_id = 0;

		/* create buffers for evaluation */
		vbuf_P = OsdCpuVertexBuffer::Create(3, 1);
		vbuf_dPdu = OsdCpuVertexBuffer::Create(3, 1);
		vbuf_dPdv = OsdCpuVertexBuffer::Create(3, 1);

		P = vbuf_P->BindCpuBuffer();
		dPdu = vbuf_dPdu->BindCpuBuffer();
		dPdv = vbuf_dPdv->BindCpuBuffer();

		/* setup evaluation context */
		OsdVertexBufferDescriptor in_desc(0, 3, 3), out_desc(0, 3, 3); /* offset, length, stride */

		evalctx = OsdCpuEvalLimitContext::Create(farmesh, false);
		evalctx->GetVertexData().Bind(in_desc, vbuf_base, out_desc, vbuf_P, vbuf_dPdu, vbuf_dPdv);
	}

	~OpenSubdPatch()
	{
		evalctx->GetVertexData().Unbind();

		delete evalctx;
		delete vbuf_P;
		delete vbuf_dPdu;
		delete vbuf_dPdv;
	}

	void eval(float3 *P_, float3 *dPdu_, float3 *dPdv_, float u, float v)
	{
		OsdEvalCoords coords;
		coords.u = u;
		coords.v = v;
		coords.face = face_id;

		evalctrl.EvalLimitSample<OsdCpuVertexBuffer,OsdCpuVertexBuffer>(coords, evalctx, 0);

		*P_ = make_float3(P[0], P[1], P[2]);
		if(dPdu_) *dPdu_ = make_float3(dPdv[0], dPdv[1], dPdv[2]);
		if(dPdv_) *dPdv_ = make_float3(dPdu[0], dPdu[1], dPdu[2]);

		/* optimize: skip evaluating derivatives when not needed */
		/* todo: swapped derivatives, different winding convention? */
	}

	BoundBox bound()
	{
		/* not implemented */
		BoundBox bbox = BoundBox::empty;
		return bbox;
	}

	int ptex_face_id()
	{
		return face_id;
	}

protected:
	OsdCpuEvalLimitController evalctrl;
	OsdCpuEvalLimitContext *evalctx;
	OsdCpuVertexBuffer *vbuf_P;
	OsdCpuVertexBuffer *vbuf_dPdu;
	OsdCpuVertexBuffer *vbuf_dPdv;
	float *P;
	float *dPdu;
	float *dPdv;
};

/* OpenSubdiv Mesh */

OpenSubdMesh::OpenSubdMesh()
{
	/* create osd mesh */
	static OsdHbrCatmarkSubdivision	catmark;
	OsdHbrMesh *hbrmesh = new OsdHbrMesh(&catmark);

	/* initialize class */
	num_verts = 0;
	num_ptex_faces = 0;
	_hbrmesh = (void*)hbrmesh;
}

OpenSubdMesh::~OpenSubdMesh()
{
	OsdHbrMesh *hbrmesh = (OsdHbrMesh*)_hbrmesh;

	if(hbrmesh)
		delete hbrmesh;
}

void OpenSubdMesh::add_vert(const float3& co)
{
	OsdHbrMesh *hbrmesh = (OsdHbrMesh*)_hbrmesh;

	OsdVertex v;
	positions.push_back(co.x);
	positions.push_back(co.y);
	positions.push_back(co.z);
	hbrmesh->NewVertex(num_verts++, v);
}

void OpenSubdMesh::add_face(int v0, int v1, int v2)
{
	int index[3] = {v0, v1, v2};
	return add_face(index, 3);
}

void OpenSubdMesh::add_face(int v0, int v1, int v2, int v3)
{
	int index[4] = {v0, v1, v2, v3};
	add_face(index, 4);
}

void OpenSubdMesh::add_face(int *index, int num)
{
	OsdHbrMesh *hbrmesh = (OsdHbrMesh*)_hbrmesh;

#ifndef NDEBUG
	/* sanity checks */
	for(int j = 0; j < num; j++) {
		OsdHbrVertex *origin = hbrmesh->GetVertex(index[j]);
		OsdHbrVertex *destination = hbrmesh->GetVertex(index[(j+1)%num]);
		OsdHbrHalfEdge *opposite = destination->GetEdge(origin);

		if(origin==NULL || destination==NULL)
			assert("An edge was specified that connected a nonexistent vertex\n");

		if(origin == destination)
			assert("An edge was specified that connected a vertex to itself\n");

		if(opposite && opposite->GetOpposite())
			assert("A non-manifold edge incident to more than 2 faces was found\n");

		if(origin->GetEdge(destination))
			assert("An edge connecting two vertices was specified more than once."
				 "It's likely that an incident face was flipped\n");
	}
#endif

	OsdHbrFace *face = hbrmesh->NewFace(num, index, 0);

	/* this is required for limit eval patch table? */
	face->SetPtexIndex(num_ptex_faces);

	if(num == 4)
		num_ptex_faces++;
	else
		num_ptex_faces += num;
}

bool OpenSubdMesh::finish()
{
	OsdHbrMesh *hbrmesh = (OsdHbrMesh*)_hbrmesh;

	/* finish hbr mesh construction */
	hbrmesh->SetInterpolateBoundaryMethod(OsdHbrMesh::k_InterpolateBoundaryEdgeOnly);
	hbrmesh->Finish();

	return true;
}

void OpenSubdMesh::tessellate(DiagSplit *split)
{
	if(num_ptex_faces == 0)
		return;

	const int level = 3;
	const bool requirefvar = false;

	/* convert HRB to FAR mesh */
	OsdHbrMesh *hbrmesh = (OsdHbrMesh*)_hbrmesh;

	OsdFarMeshFactory meshFactory(hbrmesh, level, true);
	OsdFarMesh *farmesh = meshFactory.Create(requirefvar);
	int num_hbr_verts = hbrmesh->GetNumVertices();

	delete hbrmesh;
	hbrmesh = NULL;
	_hbrmesh = NULL;

	/* refine HBR mesh with vertex coordinates */
	OsdCpuComputeController *compute_controller = new OsdCpuComputeController();
	OsdCpuComputeContext *compute_context = OsdCpuComputeContext::Create(farmesh);

	OsdCpuVertexBuffer *vbuf_base = OsdCpuVertexBuffer::Create(3, num_hbr_verts);
	vbuf_base->UpdateData(&positions[0], 0, num_verts);

	compute_controller->Refine(compute_context, farmesh->GetKernelBatches(), vbuf_base);
	compute_controller->Synchronize();

	/* split & dice patches */
	OpenSubdPatch patch(farmesh, vbuf_base);

	for(int f = 0; f < num_ptex_faces; f++) {
		patch.face_id = f;
		split->split_quad(&patch);
	}

	/* clean up */
	delete farmesh;
	delete compute_controller;
	delete compute_context;
	delete vbuf_base;
}

CCL_NAMESPACE_END

#else /* WITH_OPENSUBDIV */

CCL_NAMESPACE_BEGIN

/* Subd Vertex */

class SubdVert
{
public:
	int id;
	float3 co;
	
	SubdVert(int id_)
	{
		id = id_;
		co = make_float3(0.0f, 0.0f, 0.0f);
	}
};

/* Subd Face */

class SubdFace
{
public:
	int id;
	int numverts;
	int verts[4];

	SubdFace(int id_)
	{
		id = id_;
		numverts = 0;
	}
};

/* Subd Mesh */

SubdMesh::SubdMesh()
{
}

SubdMesh::~SubdMesh()
{
	foreach(SubdVert *vertex, verts)
		delete vertex;
	foreach(SubdFace *face, faces)
		delete face;

	verts.clear();
	faces.clear();
}

SubdVert *SubdMesh::add_vert(const float3& co)
{
	SubdVert *v = new SubdVert(verts.size());
	v->co = co;
	verts.push_back(v);

	return v;
}

SubdFace *SubdMesh::add_face(int v0, int v1, int v2)
{
	int index[3] = {v0, v1, v2};
	return add_face(index, 3);
}

SubdFace *SubdMesh::add_face(int v0, int v1, int v2, int v3)
{
	int index[4] = {v0, v1, v2, v3};
	return add_face(index, 4);
}

SubdFace *SubdMesh::add_face(int *index, int num)
{
	/* skip ngons */
	if(num < 3 || num > 4)
		return NULL;

	SubdFace *f = new SubdFace(faces.size());

	for(int i = 0; i < num; i++)
		f->verts[i] = index[i];

	f->numverts = num;
	faces.push_back(f);

	return f;
}

bool SubdMesh::finish()
{
	return true;
}

void SubdMesh::tessellate(DiagSplit *split)
{
	int num_faces = faces.size();
		        
	for(int f = 0; f < num_faces; f++) {
		SubdFace *face = faces[f];
		Patch *patch;
		float3 *hull;

		if(face->numverts == 3) {
			LinearTrianglePatch *lpatch = new LinearTrianglePatch();
			hull = lpatch->hull;
			patch = lpatch;
		}
		else if(face->numverts == 4) {
			LinearQuadPatch *lpatch = new LinearQuadPatch();
			hull = lpatch->hull;
			patch = lpatch;
		}
		else {
			assert(0); /* n-gons should have been split already */
			continue;
		}

		for(int i = 0; i < face->numverts; i++)
			hull[i] = verts[face->verts[i]]->co;

		if(face->numverts == 4)
			swap(hull[2], hull[3]);

		if(patch->is_triangle())
			split->split_triangle(patch);
		else
			split->split_quad(patch);

		delete patch;
	}
}

CCL_NAMESPACE_END

#endif /* WITH_OPENSUBDIV */