[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [16097] branches/soc-2008-jaguarandi/ source/blender: Added subsurface levels option to normal projection.

André Pinto andresusanopinto at gmail.com
Thu Aug 14 05:05:14 CEST 2008


Revision: 16097
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=16097
Author:   jaguarandi
Date:     2008-08-14 05:05:13 +0200 (Thu, 14 Aug 2008)

Log Message:
-----------
Added subsurface levels option to normal projection.
Now when doing normal projeciton is possible to ask it to project along the normals
that the vertex would have if it was subsurfaced... this gives "better" projections on
low polys.

Point of this commit is to add the feature request from Eclectiel
http://blenderartists.org/forum/showpost.php?p=1181531&postcount=9

workflow as Eclectiel wanted is now possible:

to archieve a nice low-res retopology that aproximates a high-res mesh when subsurfaced:
1 - make base low-poly retopo
2 - apply a shrinkwrap with projection along normal.. and with SS level = N (where N>0)
3 - add a Subsurface with level N
4 - run bretch's script (ss_fit)

Modified Paths:
--------------
    branches/soc-2008-jaguarandi/source/blender/blenkernel/intern/modifier.c
    branches/soc-2008-jaguarandi/source/blender/blenkernel/intern/shrinkwrap.c
    branches/soc-2008-jaguarandi/source/blender/makesdna/DNA_modifier_types.h
    branches/soc-2008-jaguarandi/source/blender/src/buttons_editing.c

Modified: branches/soc-2008-jaguarandi/source/blender/blenkernel/intern/modifier.c
===================================================================
--- branches/soc-2008-jaguarandi/source/blender/blenkernel/intern/modifier.c	2008-08-14 01:36:55 UTC (rev 16096)
+++ branches/soc-2008-jaguarandi/source/blender/blenkernel/intern/modifier.c	2008-08-14 03:05:13 UTC (rev 16097)
@@ -7316,7 +7316,7 @@
 	if(dataMask)
 	{
 		if(derivedData) dm = CDDM_copy(derivedData);
-		else if(ob->type==OB_MESH) dm = CDDM_from_editmesh(editData, ob);
+		else if(ob->type==OB_MESH) dm = CDDM_from_editmesh(editData, ob->data);
 		else return;
 
 		if(dataMask & CD_MVERT)

Modified: branches/soc-2008-jaguarandi/source/blender/blenkernel/intern/shrinkwrap.c
===================================================================
--- branches/soc-2008-jaguarandi/source/blender/blenkernel/intern/shrinkwrap.c	2008-08-14 01:36:55 UTC (rev 16096)
+++ branches/soc-2008-jaguarandi/source/blender/blenkernel/intern/shrinkwrap.c	2008-08-14 03:05:13 UTC (rev 16097)
@@ -32,10 +32,12 @@
 #include <memory.h>
 #include <stdio.h>
 #include <time.h>
+#include <assert.h>
 
 #include "DNA_object_types.h"
 #include "DNA_modifier_types.h"
 #include "DNA_meshdata_types.h"
+#include "DNA_mesh_types.h"
 
 #include "BKE_shrinkwrap.h"
 #include "BKE_DerivedMesh.h"
@@ -44,6 +46,7 @@
 #include "BKE_cdderivedmesh.h"
 #include "BKE_displist.h"
 #include "BKE_global.h"
+#include "BKE_subsurf.h"
 
 #include "BLI_arithb.h"
 #include "BLI_kdtree.h"
@@ -344,6 +347,7 @@
 	const char use_normal    = calc->smd->shrinkOpts;
 	float proj_axis[3] = {0.0f, 0.0f, 0.0f};
 	MVert *vert  = NULL; //Needed in case of vertex normal
+	DerivedMesh* ss_mesh = NULL;
 
 	//Vertex group data
 	const int vgroup		   = get_named_vertexgroup_num(calc->ob, calc->smd->vgroup_name);
@@ -359,11 +363,46 @@
 	BVHTreeFromMesh auxData= NULL_BVHTreeFromMesh;
 	SpaceTransform local2aux;
 
+do
+{
 
 	//Prepare data to retrieve the direction in which we should project each vertex
 	if(calc->smd->projAxis == MOD_SHRINKWRAP_PROJECT_OVER_NORMAL)
 	{
-		vert = calc->original ? calc->original->getVertDataArray(calc->original, CD_MVERT) : NULL;
+		//No Mvert information: jump to "free memory and return" part
+		if(calc->original == NULL) break;
+
+		if(calc->smd->subsurfLevels)
+		{
+			SubsurfModifierData smd;
+			memset(&smd, 0, sizeof(smd));
+			smd.subdivType = ME_CC_SUBSURF;			//catmull clark
+			smd.levels = calc->smd->subsurfLevels;	//levels
+
+			ss_mesh = subsurf_make_derived_from_derived(calc->original, &smd, FALSE, NULL, 0, 0);
+
+			if(ss_mesh)
+			{
+				vert = ss_mesh->getVertDataArray(ss_mesh, CD_MVERT);
+				if(vert)
+				{
+					//TRICKY: this code assumes subsurface will have the transformed original vertices
+					//in their original order at the end of the vert array.
+					vert = vert
+						 + ss_mesh->getNumVerts(ss_mesh)
+						 - calc->original->getNumVerts(calc->original);
+				}
+			}
+
+			//To make sure we are not letting any memory behind
+			assert(smd.emCache == NULL);
+			assert(smd.mCache == NULL);
+		}
+		else
+			vert = calc->original->getVertDataArray(calc->original, CD_MVERT);
+
+		//Not able to get vert information: jump to "free memory and return" part
+		if(vert == NULL) break;
 	}
 	else
 	{
@@ -373,23 +412,22 @@
 		if(calc->smd->projAxis & MOD_SHRINKWRAP_PROJECT_OVER_Z_AXIS) proj_axis[2] = 1.0f;
 
 		Normalize(proj_axis);
-	}
 
-	if(vert == NULL && (INPR(proj_axis, proj_axis) < FLT_EPSILON))
-	{
-		printf("Shrinkwrap can't project witouth normal information");
-		return;
+		//Invalid projection direction: jump to "free memory and return" part
+		if(INPR(proj_axis, proj_axis) < FLT_EPSILON) break; 
 	}
 
 	//If the user doesn't allows to project in any direction of projection axis... then theres nothing todo.
 	if((use_normal & (MOD_SHRINKWRAP_PROJECT_ALLOW_POS_DIR | MOD_SHRINKWRAP_PROJECT_ALLOW_NEG_DIR)) == 0)
-		return;
+		break; //jump to "free memory and return" part
 
 
 	//Build target tree
 	BENCH(bvhtree_from_mesh_faces(&treeData, calc->target, calc->keepDist, 4, 6));
-	if(treeData.tree == NULL) return OUT_OF_MEMORY();
+	if(treeData.tree == NULL)
+		break; //jump to "free memory and return" part
 
+
 	//Build auxiliar target
 	if(calc->smd->auxTarget)
 	{
@@ -414,8 +452,16 @@
 
 		if(weight == 0.0f) continue;
 
-		VECCOPY(tmp_co, co);
+		if(ss_mesh)
+		{
+			VECCOPY(tmp_co, vert[i].co);
+		}
+		else
+		{
+			VECCOPY(tmp_co, co);
+		}
 
+
 		if(vert)
 			NormalShortToFloat(tmp_no, vert[i].no);
 		else
@@ -456,6 +502,9 @@
 	}
 
 
+//Simple do{} while(0) structure to allow to easily jump to the "free memory and return" part
+} while(0);
+
 	//free data structures
 
 	free_bvhtree_from_mesh(&treeData);
@@ -463,6 +512,9 @@
 
 	if(aux_mesh)
 		aux_mesh->release(aux_mesh);
+
+	if(ss_mesh)
+		ss_mesh->release(ss_mesh);
 }
 
 /*

Modified: branches/soc-2008-jaguarandi/source/blender/makesdna/DNA_modifier_types.h
===================================================================
--- branches/soc-2008-jaguarandi/source/blender/makesdna/DNA_modifier_types.h	2008-08-14 01:36:55 UTC (rev 16096)
+++ branches/soc-2008-jaguarandi/source/blender/makesdna/DNA_modifier_types.h	2008-08-14 03:05:13 UTC (rev 16097)
@@ -502,8 +502,16 @@
 	short shrinkType;		/* shrink type projection */
 	short shrinkOpts;		/* shrink options */
 	char projAxis;			/* axis to project over */
-	char pad[7];
 
+	/*
+	 * if using projection over vertex normal this controls the
+	 * the level of subsurface that must be done before getting the
+	 * vertex coordinates and normal
+	 */
+	char subsurfLevels;
+
+	char pad[6];
+
 } ShrinkwrapModifierData;
 
 /* Shrinkwrap->shrinkType */

Modified: branches/soc-2008-jaguarandi/source/blender/src/buttons_editing.c
===================================================================
--- branches/soc-2008-jaguarandi/source/blender/src/buttons_editing.c	2008-08-14 01:36:55 UTC (rev 16096)
+++ branches/soc-2008-jaguarandi/source/blender/src/buttons_editing.c	2008-08-14 03:05:13 UTC (rev 16097)
@@ -1903,7 +1903,10 @@
 			ShrinkwrapModifierData *smd = (ShrinkwrapModifierData*) md;
 			height = 86 + 3;
 			if (smd->shrinkType == MOD_SHRINKWRAP_PROJECT)
+			{
 				height += 19*5;
+				if(smd->projAxis == 0) height += 19;
+			}
 			else if (smd->shrinkType == MOD_SHRINKWRAP_NEAREST_SURFACE)
 				height += 19;
 
@@ -2556,6 +2559,11 @@
 				/* UI for projection axis */
 				uiBlockBeginAlign(block);
 				uiDefButC(block, ROW, B_MODIFIER_RECALC, "Normal"    , lx,(cy-=19),buttonWidth,19, &smd->projAxis, 18.0, MOD_SHRINKWRAP_PROJECT_OVER_NORMAL, 0, 0, "Projection over X axis");
+				if(smd->projAxis == 0)
+				{
+					uiDefButC(block, NUM, B_MODIFIER_RECALC, "SS Levels:",		lx, (cy-=19), buttonWidth,19, &smd->subsurfLevels, 0, 6, 0, 0, "This indicates the number of CCSubdivisions that must be performed before extracting vertexs positions and normals");
+				}
+
 				uiDefButBitC(block, TOG, MOD_SHRINKWRAP_PROJECT_OVER_X_AXIS, B_MODIFIER_RECALC, "X",	lx+buttonWidth/3*0,(cy-=19),buttonWidth/3,19, &smd->projAxis, 0, 0, 0, 0, "Projection over X axis");
 				uiDefButBitC(block, TOG, MOD_SHRINKWRAP_PROJECT_OVER_Y_AXIS, B_MODIFIER_RECALC, "Y",	lx+buttonWidth/3*1,cy,buttonWidth/3,19, &smd->projAxis, 0, 0, 0, 0, "Projection over Y axis");
 				uiDefButBitC(block, TOG, MOD_SHRINKWRAP_PROJECT_OVER_Z_AXIS, B_MODIFIER_RECALC, "Z",	lx+buttonWidth/3*2,cy,buttonWidth/3,19, &smd->projAxis, 0, 0, 0, 0, "Projection over Z axis");





More information about the Bf-blender-cvs mailing list