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

BlenderStrokeRenderer.cpp « blender_interface « intern « freestyle « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4f17846678836a0e8f553687f23ed02978ec1da0 (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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/*
 * ***** 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp
 *  \ingroup freestyle
 */

#include "BlenderStrokeRenderer.h"

#include "../application/AppConfig.h"
#include "../stroke/Canvas.h"

extern "C" {
#include "MEM_guardedalloc.h"

#include "RNA_access.h"

#include "DNA_camera_types.h"
#include "DNA_listBase.h"
#include "DNA_material_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_mesh_types.h"
#include "DNA_object_types.h"
#include "DNA_screen_types.h"
#include "DNA_scene_types.h"

#include "BKE_customdata.h"
#include "BKE_depsgraph.h"
#include "BKE_global.h"
#include "BKE_library.h" /* free_libblock */
#include "BKE_material.h"
#include "BKE_mesh.h"
#include "BKE_node.h"
#include "BKE_object.h"
#include "BKE_scene.h"

#include "BLI_utildefines.h"

#include "RE_pipeline.h"

#include "render_types.h"
}

#include <limits.h>

#define USE_CYCLES_FOR_STROKE_RENDERING 1

namespace Freestyle {

BlenderStrokeRenderer::BlenderStrokeRenderer(bContext *C, Render *re, int render_count) : StrokeRenderer()
{
	freestyle_bmain = re->freestyle_bmain;

	// for stroke mesh generation
	_context = C;
	_width = re->winx;
	_height = re->winy;

	old_scene = re->scene;

	char name[MAX_ID_NAME - 2];
	BLI_snprintf(name, sizeof(name), "FRS%d_%s", render_count, re->scene->id.name + 2);
	freestyle_scene = BKE_scene_add(freestyle_bmain, name);
	freestyle_scene->r.cfra = old_scene->r.cfra;
	freestyle_scene->r.mode = old_scene->r.mode &
	                          ~(R_EDGE_FRS | R_SHADOW | R_SSS | R_PANORAMA | R_ENVMAP | R_MBLUR | R_BORDER);
	freestyle_scene->r.xsch = re->rectx; // old_scene->r.xsch
	freestyle_scene->r.ysch = re->recty; // old_scene->r.ysch
	freestyle_scene->r.xasp = 1.0f; // old_scene->r.xasp;
	freestyle_scene->r.yasp = 1.0f; // old_scene->r.yasp;
	freestyle_scene->r.tilex = old_scene->r.tilex;
	freestyle_scene->r.tiley = old_scene->r.tiley;
	freestyle_scene->r.size = 100; // old_scene->r.size
	//freestyle_scene->r.maximsize = old_scene->r.maximsize; /* DEPRECATED */
	freestyle_scene->r.ocres = old_scene->r.ocres;
	freestyle_scene->r.color_mgt_flag = 0; // old_scene->r.color_mgt_flag;
	freestyle_scene->r.scemode = old_scene->r.scemode & ~(R_SINGLE_LAYER | R_NO_FRAME_UPDATE);
	freestyle_scene->r.flag = old_scene->r.flag;
	freestyle_scene->r.threads = old_scene->r.threads;
	freestyle_scene->r.border.xmin = old_scene->r.border.xmin;
	freestyle_scene->r.border.ymin = old_scene->r.border.ymin;
	freestyle_scene->r.border.xmax = old_scene->r.border.xmax;
	freestyle_scene->r.border.ymax = old_scene->r.border.ymax;
	strcpy(freestyle_scene->r.pic, old_scene->r.pic);
	freestyle_scene->r.safety.xmin = old_scene->r.safety.xmin;
	freestyle_scene->r.safety.ymin = old_scene->r.safety.ymin;
	freestyle_scene->r.safety.xmax = old_scene->r.safety.xmax;
	freestyle_scene->r.safety.ymax = old_scene->r.safety.ymax;
	freestyle_scene->r.osa = old_scene->r.osa;
	freestyle_scene->r.filtertype = old_scene->r.filtertype;
	freestyle_scene->r.gauss = old_scene->r.gauss;
	freestyle_scene->r.dither_intensity = old_scene->r.dither_intensity;
#ifdef USE_CYCLES_FOR_STROKE_RENDERING
	BLI_strncpy(freestyle_scene->r.engine, old_scene->r.engine, sizeof(freestyle_scene->r.engine));
#else
	BLI_strncpy(freestyle_scene->r.engine, "BLENDER_RENDER", sizeof(freestyle_scene->r.engine));
#endif
	freestyle_scene->r.im_format.planes = R_IMF_PLANES_RGBA; 
	freestyle_scene->r.im_format.imtype = R_IMF_IMTYPE_PNG;
	BKE_scene_disable_color_management(freestyle_scene);

	if (G.debug & G_DEBUG_FREESTYLE) {
		printf("%s: %d thread(s)\n", __func__, BKE_render_num_threads(&freestyle_scene->r));
	}

	// Render layer
	SceneRenderLayer *srl = (SceneRenderLayer *)freestyle_scene->r.layers.first;
	srl->layflag = SCE_LAY_SOLID | SCE_LAY_ZTRA;

	BKE_scene_set_background(freestyle_bmain, freestyle_scene);

	// Camera
	Object *object_camera = BKE_object_add(freestyle_bmain, freestyle_scene, OB_CAMERA);

	Camera *camera = (Camera *)object_camera->data;
	camera->type = CAM_ORTHO;
	camera->ortho_scale = max(re->rectx, re->recty);
	camera->clipsta = 0.1f;
	camera->clipend = 100.0f;

	_z_delta = 0.00001f;
	_z = camera->clipsta + _z_delta;

	object_camera->loc[0] = re->disprect.xmin + 0.5f * re->rectx;
	object_camera->loc[1] = re->disprect.ymin + 0.5f * re->recty;
	object_camera->loc[2] = 1.0f;

	freestyle_scene->camera = object_camera;

	// Reset serial mesh ID (used for BlenderStrokeRenderer::NewMesh())
	_mesh_id = 0xffffffff;
}

BlenderStrokeRenderer::~BlenderStrokeRenderer()
{
	// The freestyle_scene object is not released here.  Instead,
	// the scene is released in free_all_freestyle_renders() in
	// source/blender/render/intern/source/pipeline.c, after the
	// compositor has finished.

	// release objects and data blocks
	for (Base *b = (Base *)freestyle_scene->base.first; b; b = b->next) {
		Object *ob = b->object;
		void *data = ob->data;
		char *name = ob->id.name;
#if 0
		if (G.debug & G_DEBUG_FREESTYLE) {
			cout << "removing " << name[0] << name[1] << ":" << (name+2) << endl;
		}
#endif
		switch (ob->type) {
		case OB_MESH:
			BKE_libblock_free(freestyle_bmain, ob);
			BKE_libblock_free(freestyle_bmain, data);
			break;
		case OB_CAMERA:
			BKE_libblock_free(freestyle_bmain, ob);
			BKE_libblock_free(freestyle_bmain, data);
			freestyle_scene->camera = NULL;
			break;
		default:
			cerr << "Warning: unexpected object in the scene: " << name[0] << name[1] << ":" << (name + 2) << endl;
		}
	}
	BLI_freelistN(&freestyle_scene->base);

	// release materials
	Link *lnk = (Link *)freestyle_bmain->mat.first;

	while (lnk)
	{
		Material *ma = (Material*)lnk;
		// We want to retain the linestyle mtexs, so let's detach them first
		for (int a = 0; a < MAX_MTEX; a++) {
			if (ma->mtex[a]) {
				ma->mtex[a] = NULL;
			}
			else {
				break; // Textures are ordered, no empty slots between two textures
			}
		}
		lnk = lnk->next;
		BKE_libblock_free(freestyle_bmain, ma);
	}
}

float BlenderStrokeRenderer::get_stroke_vertex_z(void) const
{
	float z = _z;
	BlenderStrokeRenderer *self = const_cast<BlenderStrokeRenderer *>(this);
	if (!(_z < _z_delta * 100000.0f))
		self->_z_delta *= 10.0f;
	self->_z += _z_delta;
	return -z;
}

unsigned int BlenderStrokeRenderer::get_stroke_mesh_id(void) const
{
	unsigned mesh_id = _mesh_id;
	BlenderStrokeRenderer *self = const_cast<BlenderStrokeRenderer *>(this);
	self->_mesh_id--;
	return mesh_id;
}

Material* BlenderStrokeRenderer::GetStrokeMaterial(bContext *C, Main *bmain, Scene *scene)
{
	Material *ma = BKE_material_add(bmain, "stroke_material");

	ma->mode |= MA_VERTEXCOLP;
	ma->mode |= MA_TRANSP;
	ma->mode |= MA_SHLESS;
	ma->vcol_alpha = 1;

	if (BKE_scene_use_new_shading_nodes(scene)) {
		bNodeTree *ntree;
		bNodeSocket *fromsock, *tosock;

		ntree = ntreeAddTree(NULL, "stroke_material", "ShaderNodeTree");
		ma->nodetree = ntree;
		ma->use_nodes = 1;

		bNode *input_attribute = nodeAddStaticNode(C, ntree, SH_NODE_ATTRIBUTE);
		input_attribute->locx = 0.0f;
		input_attribute->locy = 0.0f;
		NodeShaderAttribute *storage = (NodeShaderAttribute *)input_attribute->storage;
		BLI_strncpy(storage->name, "Col", sizeof(storage->name));

		bNode *shader_emission = nodeAddStaticNode(C, ntree, SH_NODE_EMISSION);
		shader_emission->locx = 200.0f;
		shader_emission->locy = 0.0f;

		bNode *input_light_path = nodeAddStaticNode(C, ntree, SH_NODE_LIGHT_PATH);
		input_light_path->locx = 200.0f;
		input_light_path->locy = 300.0f;

		bNode *shader_mix = nodeAddStaticNode(C, ntree, SH_NODE_MIX_SHADER);
		shader_mix->locx = 400.0f;
		shader_mix->locy = 100.0f;

		bNode *output_material = nodeAddStaticNode(C, ntree, SH_NODE_OUTPUT_MATERIAL);
		output_material->locx = 600.0f;
		output_material->locy = 100.0f;

		fromsock = (bNodeSocket *)BLI_findlink(&input_attribute->outputs, 0);
		tosock = (bNodeSocket *)BLI_findlink(&shader_emission->inputs, 0);
		nodeAddLink(ntree, input_attribute, fromsock, shader_emission, tosock);

		fromsock = (bNodeSocket *)BLI_findlink(&shader_emission->outputs, 0);
		tosock = (bNodeSocket *)BLI_findlink(&shader_mix->inputs, 2);
		nodeAddLink(ntree, shader_emission, fromsock, shader_mix, tosock);

		fromsock = (bNodeSocket *)BLI_findlink(&input_light_path->outputs, 0);
		tosock = (bNodeSocket *)BLI_findlink(&shader_mix->inputs, 0);
		nodeAddLink(ntree, input_light_path, fromsock, shader_mix, tosock);

		fromsock = (bNodeSocket *)BLI_findlink(&shader_mix->outputs, 0);
		tosock = (bNodeSocket *)BLI_findlink(&output_material->inputs, 0);
		nodeAddLink(ntree, shader_mix, fromsock, output_material, tosock);

		nodeSetActive(ntree, shader_mix);
		ntreeUpdateTree(bmain, ntree);
	}

	return ma;
}

void BlenderStrokeRenderer::RenderStrokeRep(StrokeRep *iStrokeRep) const
{
	bool has_mat = false;
	int a = 0;

	// Look for a good existing material
	for (Link *lnk = (Link *)freestyle_bmain->mat.first; lnk; lnk = lnk->next) {
		Material *ma = (Material*) lnk;
		bool texs_are_good = true;
		// as soon as textures differ it's not the right one
		for (int a = 0; a < MAX_MTEX; a++) {
			if (ma->mtex[a] != iStrokeRep->getMTex(a)) {
				texs_are_good = false;
				break;
			}
		}

		if (texs_are_good) {
			iStrokeRep->setMaterial(ma);
			has_mat = true;
			break; // if textures are good, no need to search anymore
		}
	}

	// If still no material, create one
	if (!has_mat) {
		Material *ma = BlenderStrokeRenderer::GetStrokeMaterial(_context, freestyle_bmain, freestyle_scene);

		// Textures
		//for (int a = 0; a < MAX_MTEX; a++) {
		while (iStrokeRep->getMTex(a)) {
			ma->mtex[a] = (MTex *)iStrokeRep->getMTex(a);

			// We'll generate both with tips and without tips
			// coordinates, on two different UV layers.
			if (ma->mtex[a]->texflag & MTEX_TIPS)  {
				BLI_strncpy(ma->mtex[a]->uvname, "along_stroke_tips", sizeof(ma->mtex[a]->uvname));
			}
			else {
				BLI_strncpy(ma->mtex[a]->uvname, "along_stroke", sizeof(ma->mtex[a]->uvname));
			}
			a++;
		}

		if (strcmp(freestyle_scene->r.engine, "CYCLES") == 0) {
			PointerRNA scene_ptr;
			RNA_pointer_create(NULL, &RNA_Scene, freestyle_scene, &scene_ptr);
			PointerRNA cycles_ptr = RNA_pointer_get(&scene_ptr, "cycles");
			RNA_boolean_set(&cycles_ptr, "film_transparent", 1);
		}

		iStrokeRep->setMaterial(ma);
	}

	RenderStrokeRepBasic(iStrokeRep);
}

// Check if the triangle is visible (i.e., within the render image boundary)
bool BlenderStrokeRenderer::test_triangle_visibility(StrokeVertexRep *svRep[3]) const
{
	int xl, xu, yl, yu;
	Vec2r p;

	xl = xu = yl = yu = 0;
	for (int i = 0; i < 3; i++) {
		p = svRep[i]->point2d();
		if (p[0] < 0.0)
			xl++;
		else if (p[0] > _width)
			xu++;
		if (p[1] < 0.0)
			yl++;
		else if (p[1] > _height)
			yu++;
	}
	return !(xl == 3 || xu == 3 || yl == 3 || yu == 3);
}

// Check the visibility of faces and strip segments.
void BlenderStrokeRenderer::test_strip_visibility(Strip::vertex_container& strip_vertices,
	int *visible_faces, int *visible_segments) const
{
	const int strip_vertex_count = strip_vertices.size();
	Strip::vertex_container::iterator v[3];
	StrokeVertexRep *svRep[3];
	bool visible;

	// iterate over all vertices and count visible faces and strip segments
	// (note: a strip segment is a series of visible faces, while two strip
	// segments are separated by one or more invisible faces)
	v[0] = strip_vertices.begin();
	v[1] = v[0] + 1;
	v[2] = v[0] + 2;
	*visible_faces = *visible_segments = 0;
	visible = false;
	for (int n = 2; n < strip_vertex_count; n++, v[0]++, v[1]++, v[2]++) {
		svRep[0] = *(v[0]);
		svRep[1] = *(v[1]);
		svRep[2] = *(v[2]);
		if (test_triangle_visibility(svRep)) {
			(*visible_faces)++;
			if (!visible)
				(*visible_segments)++;
			visible = true;
		}
		else {
			visible = false;
		}
	}
}

// Build a mesh object representing a stroke
void BlenderStrokeRenderer::RenderStrokeRepBasic(StrokeRep *iStrokeRep) const
{
	vector<Strip*>& strips = iStrokeRep->getStrips();
	const bool hasTex = iStrokeRep->getMTex(0) != NULL;
	Strip::vertex_container::iterator v[3];
	StrokeVertexRep *svRep[3];
	unsigned int vertex_index, edge_index, loop_index;
	Vec2r p;

	int totvert = 0, totedge = 0, totpoly = 0, totloop = 0;
	int visible_faces, visible_segments;

	bool visible;
	for (vector<Strip*>::iterator s = strips.begin(), send = strips.end(); s != send; ++s) {
		Strip::vertex_container& strip_vertices = (*s)->vertices();

		// count visible faces and strip segments
		test_strip_visibility(strip_vertices, &visible_faces, &visible_segments);
		if (visible_faces == 0)
			continue;

		totvert += visible_faces + visible_segments * 2;
		totedge += visible_faces * 2 + visible_segments;
		totpoly += visible_faces;
		totloop += visible_faces * 3;
	}

#if 0
	Object *object_mesh = BKE_object_add(freestyle_bmain, freestyle_scene, OB_MESH);
#else
	Object *object_mesh = NewMesh();
#endif
	Mesh *mesh = (Mesh *)object_mesh->data;
	mesh->mat = (Material **)MEM_mallocN(1 * sizeof(Material *), "MaterialList");
	mesh->mat[0] = iStrokeRep->getMaterial();
	mesh->totcol = 1;
	test_object_materials(freestyle_bmain, (ID *)mesh);

	// vertices allocation
	mesh->totvert = totvert; // visible_faces + visible_segments * 2;
	mesh->mvert = (MVert *)CustomData_add_layer(&mesh->vdata, CD_MVERT, CD_CALLOC, NULL, mesh->totvert);

	// edges allocation
	mesh->totedge = totedge; // visible_faces * 2 + visible_segments;
	mesh->medge = (MEdge *)CustomData_add_layer(&mesh->edata, CD_MEDGE, CD_CALLOC, NULL, mesh->totedge);

	// faces allocation
	mesh->totpoly = totpoly; // visible_faces;
	mesh->mpoly = (MPoly *)CustomData_add_layer(&mesh->pdata, CD_MPOLY, CD_CALLOC, NULL, mesh->totpoly);

	// loops allocation
	mesh->totloop = totloop; // visible_faces * 3;
	mesh->mloop = (MLoop *)CustomData_add_layer(&mesh->ldata, CD_MLOOP, CD_CALLOC, NULL, mesh->totloop);

	// colors allocation
	mesh->mloopcol = (MLoopCol *)CustomData_add_layer(&mesh->ldata, CD_MLOOPCOL, CD_CALLOC, NULL, mesh->totloop);

	////////////////////
	//  Data copy
	////////////////////

	MVert *vertices = mesh->mvert;
	MEdge *edges = mesh->medge;
	MPoly *polys = mesh->mpoly;
	MLoop *loops = mesh->mloop;
	MLoopCol *colors = mesh->mloopcol;
	MLoopUV *loopsuv[2] = {NULL};

	if (hasTex) {
		// First UV layer
		CustomData_add_layer_named(&mesh->pdata, CD_MTEXPOLY, CD_CALLOC, NULL, mesh->totpoly, "along_stroke");
		CustomData_add_layer_named(&mesh->ldata, CD_MLOOPUV, CD_CALLOC, NULL, mesh->totloop, "along_stroke");
		CustomData_set_layer_active(&mesh->pdata, CD_MTEXPOLY, 0);
		CustomData_set_layer_active(&mesh->ldata, CD_MLOOPUV, 0);
		BKE_mesh_update_customdata_pointers(mesh, true);

		loopsuv[0] = mesh->mloopuv;

		// Second UV layer
		CustomData_add_layer_named(&mesh->pdata, CD_MTEXPOLY, CD_CALLOC, NULL, mesh->totpoly, "along_stroke_tips");
		CustomData_add_layer_named(&mesh->ldata, CD_MLOOPUV, CD_CALLOC, NULL, mesh->totloop, "along_stroke_tips");
		CustomData_set_layer_active(&mesh->pdata, CD_MTEXPOLY, 1);
		CustomData_set_layer_active(&mesh->ldata, CD_MLOOPUV, 1);
		BKE_mesh_update_customdata_pointers(mesh, true);

		loopsuv[1] = mesh->mloopuv;
	}

	vertex_index = edge_index = loop_index = 0;

	for (vector<Strip*>::iterator s = strips.begin(), send = strips.end(); s != send; ++s) {
		Strip::vertex_container& strip_vertices = (*s)->vertices();
		int strip_vertex_count = strip_vertices.size();

		// count visible faces and strip segments
		test_strip_visibility(strip_vertices, &visible_faces, &visible_segments);
		if (visible_faces == 0)
			continue;

		v[0] = strip_vertices.begin();
		v[1] = v[0] + 1;
		v[2] = v[0] + 2;

		visible = false;

		// Note: Mesh generation in the following loop assumes stroke strips
		// to be triangle strips.
		for (int n = 2; n < strip_vertex_count; n++, v[0]++, v[1]++, v[2]++) {
			svRep[0] = *(v[0]);
			svRep[1] = *(v[1]);
			svRep[2] = *(v[2]);
			if (!test_triangle_visibility(svRep)) {
				visible = false;
			}
			else {
				if (!visible) {
					// first vertex
					vertices->co[0] = svRep[0]->point2d()[0];
					vertices->co[1] = svRep[0]->point2d()[1];
					vertices->co[2] = get_stroke_vertex_z();
					vertices->no[0] = 0;
					vertices->no[1] = 0;
					vertices->no[2] = SHRT_MAX;
					++vertices;
					++vertex_index;

					// second vertex
					vertices->co[0] = svRep[1]->point2d()[0];
					vertices->co[1] = svRep[1]->point2d()[1];
					vertices->co[2] = get_stroke_vertex_z();
					vertices->no[0] = 0;
					vertices->no[1] = 0;
					vertices->no[2] = SHRT_MAX;
					++vertices;
					++vertex_index;

					// first edge
					edges->v1 = vertex_index - 2;
					edges->v2 = vertex_index - 1;
					++edges;
					++edge_index;
				}
				visible = true;

				// vertex
				vertices->co[0] = svRep[2]->point2d()[0];
				vertices->co[1] = svRep[2]->point2d()[1];
				vertices->co[2] = get_stroke_vertex_z();
				vertices->no[0] = 0;
				vertices->no[1] = 0;
				vertices->no[2] = SHRT_MAX;
				++vertices;
				++vertex_index;

				// edges
				edges->v1 = vertex_index - 1;
				edges->v2 = vertex_index - 3;
				++edges;
				++edge_index;

				edges->v1 = vertex_index - 1;
				edges->v2 = vertex_index - 2;
				++edges;
				++edge_index;

				// poly
				polys->loopstart = loop_index;
				polys->totloop = 3;
				++polys;

				// Even and odd loops connect triangles vertices differently
				bool is_odd = n % 2;
				// loops
				if (is_odd) {
					loops[0].v = vertex_index - 1;
					loops[0].e = edge_index - 2;

					loops[1].v = vertex_index - 3;
					loops[1].e = edge_index - 3;

					loops[2].v = vertex_index - 2;
					loops[2].e = edge_index - 1;
				}
				else {
					loops[0].v = vertex_index - 1;
					loops[0].e = edge_index - 1;

					loops[1].v = vertex_index - 2;
					loops[1].e = edge_index - 3;

					loops[2].v = vertex_index - 3;
					loops[2].e = edge_index - 2;
				}
				loops += 3;
				loop_index += 3;

				// UV
				if (hasTex) {
					// First UV layer (loopsuv[0]) has no tips (texCoord(0)).
					// Second UV layer (loopsuv[1]) has tips:  (texCoord(1)).
					for (int L = 0; L < 2; L++) {
						if (is_odd) {
							loopsuv[L][0].uv[0] = svRep[2]->texCoord(L).x();
							loopsuv[L][0].uv[1] = svRep[2]->texCoord(L).y();

							loopsuv[L][1].uv[0] = svRep[0]->texCoord(L).x();
							loopsuv[L][1].uv[1] = svRep[0]->texCoord(L).y();

							loopsuv[L][2].uv[0] = svRep[1]->texCoord(L).x();
							loopsuv[L][2].uv[1] = svRep[1]->texCoord(L).y();
						}
						else {
							loopsuv[L][0].uv[0] = svRep[2]->texCoord(L).x();
							loopsuv[L][0].uv[1] = svRep[2]->texCoord(L).y();

							loopsuv[L][1].uv[0] = svRep[1]->texCoord(L).x();
							loopsuv[L][1].uv[1] = svRep[1]->texCoord(L).y();

							loopsuv[L][2].uv[0] = svRep[0]->texCoord(L).x();
							loopsuv[L][2].uv[1] = svRep[0]->texCoord(L).y();
						}
						loopsuv[L] += 3;
					}
				}

				// colors
				if (is_odd) {
					colors[0].r = (short)(255.0f * svRep[2]->color()[0]);
					colors[0].g = (short)(255.0f * svRep[2]->color()[1]);
					colors[0].b = (short)(255.0f * svRep[2]->color()[2]);
					colors[0].a = (short)(255.0f * svRep[2]->alpha());

					colors[1].r = (short)(255.0f * svRep[0]->color()[0]);
					colors[1].g = (short)(255.0f * svRep[0]->color()[1]);
					colors[1].b = (short)(255.0f * svRep[0]->color()[2]);
					colors[1].a = (short)(255.0f * svRep[0]->alpha());

					colors[2].r = (short)(255.0f * svRep[1]->color()[0]);
					colors[2].g = (short)(255.0f * svRep[1]->color()[1]);
					colors[2].b = (short)(255.0f * svRep[1]->color()[2]);
					colors[2].a = (short)(255.0f * svRep[1]->alpha());
					}
				else {
					colors[0].r = (short)(255.0f * svRep[2]->color()[0]);
					colors[0].g = (short)(255.0f * svRep[2]->color()[1]);
					colors[0].b = (short)(255.0f * svRep[2]->color()[2]);
					colors[0].a = (short)(255.0f * svRep[2]->alpha());

					colors[1].r = (short)(255.0f * svRep[1]->color()[0]);
					colors[1].g = (short)(255.0f * svRep[1]->color()[1]);
					colors[1].b = (short)(255.0f * svRep[1]->color()[2]);
					colors[1].a = (short)(255.0f * svRep[1]->alpha());

					colors[2].r = (short)(255.0f * svRep[0]->color()[0]);
					colors[2].g = (short)(255.0f * svRep[0]->color()[1]);
					colors[2].b = (short)(255.0f * svRep[0]->color()[2]);
					colors[2].a = (short)(255.0f * svRep[0]->alpha());
				}
				colors += 3;
			}
		} // loop over strip vertices
	} // loop over strips
#if 0
	BLI_assert(totvert == vertex_index);
	BLI_assert(totedge == edge_index);
	BLI_assert(totloop == loop_index);
	BKE_mesh_validate(mesh, true);
#endif
}

// A replacement of BKE_object_add() for better performance.
Object *BlenderStrokeRenderer::NewMesh() const
{
	Object *ob;
	Base *base;
	char name[MAX_ID_NAME];
	unsigned int mesh_id = get_stroke_mesh_id();

	BLI_snprintf(name, MAX_ID_NAME, "0%08xOB", mesh_id);
	ob = BKE_object_add_only_object(freestyle_bmain, OB_MESH, name);
	BLI_snprintf(name, MAX_ID_NAME, "0%08xME", mesh_id);
	ob->data = BKE_mesh_add(freestyle_bmain, name);
	ob->lay = 1;

	base = BKE_scene_base_add(freestyle_scene, ob);
#if 0
	BKE_scene_base_deselect_all(scene);
	BKE_scene_base_select(scene, base);
#else
	(void)base;
#endif

	DAG_id_tag_update_ex(freestyle_bmain, &ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);

	return ob;
}

Render *BlenderStrokeRenderer::RenderScene(Render *re, bool render)
{
	Camera *camera = (Camera *)freestyle_scene->camera->data;
	if (camera->clipend < _z)
		camera->clipend = _z + _z_delta * 100.0f;
#if 0
	if (G.debug & G_DEBUG_FREESTYLE) {
		cout << "clipsta " << camera->clipsta << ", clipend " << camera->clipend << endl;
	}
#endif

	Render *freestyle_render = RE_NewRender(freestyle_scene->id.name);

	RE_RenderFreestyleStrokes(freestyle_render, freestyle_bmain, freestyle_scene, render);

	return freestyle_render;
}

} /* namespace Freestyle */