[Bf-blender-cvs] [209c5cebb06] blender2.8: Freestyle: use depsgraph to get geometry, instead of Blender Internal.

Brecht Van Lommel noreply at git.blender.org
Mon Apr 9 11:46:31 CEST 2018


Commit: 209c5cebb06abc06f7afd6b70298cf14a7138b64
Author: Brecht Van Lommel
Date:   Wed Apr 4 15:51:03 2018 +0200
Branches: blender2.8
https://developer.blender.org/rB209c5cebb06abc06f7afd6b70298cf14a7138b64

Freestyle: use depsgraph to get geometry, instead of Blender Internal.

Some of the code is simpler because we use Blender's triangulation directly
instead of dealing with quads. Also some progress printing code was removed
because the depsgraph can not tell us the number of objects ahead of time.

Differential Revision: https://developer.blender.org/D3127

===================================================================

M	source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp
M	source/blender/freestyle/intern/blender_interface/BlenderFileLoader.h
M	source/blender/render/extern/include/RE_pipeline.h
M	source/blender/render/intern/source/convertblender.c
M	source/blender/render/intern/source/pipeline.c

===================================================================

diff --git a/source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp b/source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp
index a0da68849f7..b159bade13d 100644
--- a/source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp
+++ b/source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp
@@ -52,8 +52,6 @@ BlenderFileLoader::~BlenderFileLoader()
 
 NodeGroup *BlenderFileLoader::Load()
 {
-	ObjectInstanceRen *obi;
-
 	if (G.debug & G_DEBUG_FREESTYLE) {
 		cout << "\n===  Importing triangular meshes into Blender  ===" << endl;
 	}
@@ -81,6 +79,24 @@ NodeGroup *BlenderFileLoader::Load()
 		_z_offset = 0.f;
 	}
 
+	EvaluationContext *eval_ctx = DEG_evaluation_context_new(DAG_EVAL_RENDER);
+	Depsgraph *depsgraph = DEG_graph_new();
+
+	ViewLayer *view_layer = (ViewLayer*)BLI_findstring(&_re->scene->view_layers, _view_layer->name, offsetof(ViewLayer, name));
+
+	DEG_evaluation_context_init_from_view_layer_for_render(
+		eval_ctx,
+		depsgraph,
+		_re->scene,
+		view_layer);
+
+	BKE_scene_graph_update_tagged(
+		eval_ctx,
+		depsgraph,
+		_re->main,
+		_re->scene,
+		view_layer);
+
 #if 0
 	if (G.debug & G_DEBUG_FREESTYLE) {
 		cout << "Frustum: l " << _viewplane_left << " r " << _viewplane_right
@@ -90,32 +106,39 @@ NodeGroup *BlenderFileLoader::Load()
 #endif
 
 	int id = 0;
-	unsigned cnt = 1;
-	unsigned cntStep = (unsigned)ceil(0.01f * _re->totinstance);
-	for (obi = (ObjectInstanceRen *)_re->instancetable.first; obi; obi = obi->next) {
-		if (_pRenderMonitor) {
-			if (_pRenderMonitor->testBreak())
-				break;
-			if (cnt % cntStep == 0) {
-				stringstream ss;
-				ss << "Freestyle: Mesh loading " << (100 * cnt / _re->totinstance) << "%";
-				_pRenderMonitor->setInfo(ss.str());
-				_pRenderMonitor->progress((float)cnt / _re->totinstance);
-			}
-			cnt++;
-		}
-
-		char *name = obi->ob->id.name;
-		//printf("%c%c:%s\n", name[0], name[1], name+2);
-		//print_m4("obi->mat", obi->mat);
 
-		if (obi->obr->totvlak > 0) {
-			insertShapeNode(obi, ++id);
-		}
-		else if (G.debug & G_DEBUG_FREESTYLE) {
-			cout << "Warning: " << (name + 2) << " is not a vlak-based object (ignored)" << endl;
+	DEG_OBJECT_ITER_BEGIN(
+	        depsgraph, ob, DEG_ITER_OBJECT_MODE_RENDER,
+	        DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY |
+	        DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET |
+	        DEG_ITER_OBJECT_FLAG_VISIBLE |
+	        DEG_ITER_OBJECT_FLAG_DUPLI)
+	{
+		if (_pRenderMonitor && _pRenderMonitor->testBreak()) {
+			break;
+		}
+
+		bool apply_modifiers = true;
+		bool calc_undeformed = false;
+		bool calc_tessface = false;
+		Mesh *mesh = BKE_mesh_new_from_object(eval_ctx,
+		                                      _re->main,
+		                                      _re->scene,
+		                                      ob,
+		                                      apply_modifiers,
+		                                      eModifierMode_Render,
+		                                      calc_tessface,
+		                                      calc_undeformed);
+
+		if (mesh) {
+			insertShapeNode(ob, mesh, ++id);
+			BKE_libblock_free_ex(_re->main, &mesh->id, true, false);
 		}
 	}
+	DEG_OBJECT_ITER_END;
+
+	DEG_graph_free(depsgraph);
+	DEG_evaluation_context_free(eval_ctx);
 
 	// Return the built scene.
 	return _Scene;
@@ -363,82 +386,87 @@ int BlenderFileLoader::testDegenerateTriangle(float v1[3], float v2[3], float v3
 	return 0;
 }
 
-// Checks if edge rotation (if necessary) can prevent the given quad from
-// being decomposed into a degenerate triangle
-bool BlenderFileLoader::testEdgeRotation(float v1[3], float v2[3], float v3[3], float v4[3])
+static bool testEdgeMark(Mesh *me, FreestyleEdge *fed, const MLoopTri *lt, int i)
 {
-	if (testDegenerateTriangle(v1, v2, v3) == 2 || testDegenerateTriangle(v1, v3, v4) == 2) {
-		if (testDegenerateTriangle(v1, v2, v4) == 2 || testDegenerateTriangle(v2, v3, v4) == 2) {
-#if 0
-			if (G.debug & G_DEBUG_FREESTYLE) {
-				printf("BlenderFileLoader::testEdgeRotation: edge rotation is unsuccessful.\n");
-			}
-#endif
-			return false;
-		}
-		return true;
+	MLoop *mloop = &me->mloop[lt->tri[i]];
+	MLoop *mloop_next = &me->mloop[lt->tri[(i+1)%3]];
+	MEdge *medge = &me->medge[mloop->e];
+
+	if (!ELEM(mloop_next->v, medge->v1, medge->v2)) {
+		/* Not an edge in the original mesh before triangulation. */
+		return false;
 	}
-	return false;
+
+	return (fed[mloop->e].flag & FREESTYLE_EDGE_MARK) != 0;
 }
 
-void BlenderFileLoader::insertShapeNode(ObjectInstanceRen *obi, int id)
+void BlenderFileLoader::insertShapeNode(Object *ob, Mesh *me, int id)
 {
-	ObjectRen *obr = obi->obr;
-	char *name = obi->ob->id.name + 2;
+	char *name = ob->id.name + 2;
+
+	// Compute loop triangles
+	int tottri = poly_to_tri_count(me->totpoly, me->totloop);
+	MLoopTri *mlooptri = (MLoopTri*)MEM_malloc_arrayN(tottri, sizeof(*mlooptri), __func__);
+	BKE_mesh_recalc_looptri(
+	        me->mloop, me->mpoly,
+	        me->mvert,
+	        me->totloop, me->totpoly,
+	        mlooptri);
+
+	// Compute loop normals
+	BKE_mesh_calc_normals_split(me);
+	float (*lnors)[3] = NULL;
+
+	if (CustomData_has_layer(&me->ldata, CD_NORMAL)) {
+		lnors = (float(*)[3])CustomData_get_layer(&me->ldata, CD_NORMAL);
+	}
 
-	// We parse vlak nodes and count the number of faces after the clipping by
-	// the near and far view planes is applied (Note: mesh vertices are in the
-	// camera coordinate system).
-	VlakRen *vlr = NULL;
+	// Get other mesh data
+	MVert *mvert = me->mvert;
+	MLoop *mloop = me->mloop;
+	MPoly *mpoly = me->mpoly;
+	FreestyleEdge *fed = (FreestyleEdge*)CustomData_get_layer(&me->edata, CD_FREESTYLE_EDGE);
+	FreestyleFace *ffa = (FreestyleFace*)CustomData_get_layer(&me->pdata, CD_FREESTYLE_FACE);
+
+	// Compute matrix including camera transform
+	float obmat[4][4], nmat[4][4];
+	mul_m4_m4m4(obmat, _re->viewmat, ob->obmat);
+	invert_m4_m4(nmat, obmat);
+	transpose_m4(nmat);
+
+	// We count the number of triangles after the clipping by the near and far view
+	// planes is applied (Note: mesh vertices are in the camera coordinate system).
 	unsigned numFaces = 0;
-	float v1[3], v2[3], v3[3], v4[3];
-	float n1[3], n2[3], n3[3], n4[3], facenormal[3];
-	int clip_1[3], clip_2[3];
+	float v1[3], v2[3], v3[3];
+	float n1[3], n2[3], n3[3], facenormal[3];
+	int clip[3];
 	int wire_material = 0;
-	for (int a = 0; a < obr->totvlak; a++) {
-		if ((a & 255) == 0)
-			vlr = obr->vlaknodes[a>>8].vlak;
-		else
-			vlr++;
-		if (vlr->mat->mode & MA_ONLYCAST)
+	for (int a = 0; a < tottri; a++) {
+		const MLoopTri *lt = &mlooptri[a];
+		const MPoly *mp = &mpoly[lt->poly];
+		Material *mat = give_current_material(ob, mp->mat_nr + 1);
+
+		if (mat && mat->mode & MA_ONLYCAST) {
 			continue;
-		if (vlr->mat->material_type == MA_TYPE_WIRE) {
+		}
+		if (mat && mat->material_type == MA_TYPE_WIRE) {
 			wire_material = 1;
 			continue;
 		}
-		copy_v3_v3(v1, vlr->v1->co);
-		copy_v3_v3(v2, vlr->v2->co);
-		copy_v3_v3(v3, vlr->v3->co);
-		if (vlr->v4)
-			copy_v3_v3(v4, vlr->v4->co);
-		if (obi->flag & R_TRANSFORMED) {
-			mul_m4_v3(obi->mat, v1);
-			mul_m4_v3(obi->mat, v2);
-			mul_m4_v3(obi->mat, v3);
-			if (vlr->v4)
-				mul_m4_v3(obi->mat, v4);
-		}
+
+		copy_v3_v3(v1, mvert[mloop[lt->tri[0]].v].co);
+		copy_v3_v3(v2, mvert[mloop[lt->tri[1]].v].co);
+		copy_v3_v3(v3, mvert[mloop[lt->tri[2]].v].co);
+
+		mul_m4_v3(obmat, v1);
+		mul_m4_v3(obmat, v2);
+		mul_m4_v3(obmat, v3);
+
 		v1[2] += _z_offset;
 		v2[2] += _z_offset;
 		v3[2] += _z_offset;
-		if (vlr->v4)
-			v4[2] += _z_offset;
-#if 0
-		print_v3("v1", v1);
-		print_v3("v2", v2);
-		print_v3("v3", v3);
-		if (vlr->v4)
-			print_v3("v4", v4);
-#endif
-		if (!vlr->v4 || !testEdgeRotation(v1, v2, v3, v4)) {
-			numFaces += countClippedFaces(v1, v2, v3, clip_1);
-			if (vlr->v4)
-				numFaces += countClippedFaces(v1, v3, v4, clip_2);
-		}
-		else {
-			numFaces += countClippedFaces(v1, v2, v4, clip_1);
-			numFaces += countClippedFaces(v2, v3, v4, clip_2);
-		}
+
+		numFaces += countClippedFaces(v1, v2, v3, clip);
 	}
 	if (wire_material) {
 		if (G.debug & G_DEBUG_FREESTYLE) {
@@ -450,8 +478,10 @@ void BlenderFileLoader::insertShapeNode(ObjectInstanceRen *obi, int id)
 		cout << "numFaces " << numFaces << endl;
 	}
 #endif
-	if (numFaces == 0)
+	if (numFaces == 0) {
+		MEM_freeN(mlooptri);
 		return;
+	}
 
 	// We allocate memory for the meshes to be imported
 	NodeGroup *currentMesh = new NodeGroup;
@@ -494,99 +524,60 @@ void BlenderFileLoader::insertShapeNode(ObjectInstanceRen *obi, int id)
 
 	// We parse the vlak nodes again and import meshes while applying the clipping
 	// by the near and far view planes.
-	int p;
-	for (p = 0; p < obr->totvlak; ++p) { // we parse the faces of the mesh
-		if ((p & 255) == 0)
-			vlr = obr->vlaknodes[p>>8].vlak;
-		else
-			vlr++;
-		if ((vlr->mat->mode & MA_ONLYCAST) || vlr->mat->material_type == MA_TYPE_WIRE)
+	for (int a = 0; a < tottri; a++) {
+		const MLoopTri *lt = &mlooptri[a];
+		const MPoly *mp = &mpoly[lt->poly];
+		Material *mat = give_current_material(ob, mp->mat_nr + 1);
+
+		if (mat && ((mat->mode & MA_ONLYCAST) || mat->material_type == MA_TYPE_WIRE))
 			continue;
-		copy_v3_v3(v1, vlr->v1->co);
-		copy_v3_v3(v2, vlr->v2->co);
-		copy_v3_v3(v3, vlr->v3->co);
-		if (vlr->v4)
-			copy_v3_v3(v4, vlr->v4->co);
-		if (obi->flag & R_TRANSFORMED) {
-			mul_m4_v3(obi->mat, v1);
-			mul_m4_v3(obi->mat, v2);
-			mul_m4_v3(obi->mat, v3);
-			if (vlr->v4)
-				mul_m4_v3(obi->mat, v4);
-		}
+
+		copy_v3_v3(v1, mvert[mloop[lt->tri[0]].v].co);
+		copy_v3_v3(v2, mvert[mloop[lt->tri[1]].v].co);
+		copy_v3_v3(v3, mvert[mloop[lt->tri[2]].v].co);
+
+		mul_m4_v3(obmat, v1);
+		mul_m4_v3(obmat, v2);
+		mul_m4_v3(obmat, v3);
+
 		v1[2] += _z_offset;
 		v2[2] += _z_offset;
 		v3[2] += _z_offset;
-		if (vlr->v4)
-			v4[2] += _z_offset;
-		if (_smooth && (vlr->flag & R_SMOOTH)) {
-			copy_v3_v3(n1, vlr->v1->n);
-			copy_v3_v3(n2, vlr->v2->n);
-			copy_v3_v3(n3, vlr->v3->n);
-			if (vlr->v4)
-				copy_v3_v3(n4, vlr->v4->n);
-			if (obi->flag & R_TRANSFORMED) {
-				mul_m3_v3(obi->nmat, n1);
-				mul_m3_v3(obi->nmat, n2);
-				mul_m3_v3(obi->nmat, n3);
-				normalize_v3(n1);
-				normalize_v3(n2);
-				normalize_v3(n3);
-				if (vlr->v4) {
-				

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list