[Bf-blender-cvs] [4f24cac] cycles-ptex-08: WIP Subsurf ptex coords

Nicholas Bishop noreply at git.blender.org
Tue Jan 20 13:57:18 CET 2015


Commit: 4f24cac3e7bd53640fad135cb6d43b9d1de1697c
Author: Nicholas Bishop
Date:   Tue Jan 20 13:55:01 2015 +0100
Branches: cycles-ptex-08
https://developer.blender.org/rB4f24cac3e7bd53640fad135cb6d43b9d1de1697c

WIP Subsurf ptex coords

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

M	source/blender/blenkernel/intern/subsurf_ccg.c

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

diff --git a/source/blender/blenkernel/intern/subsurf_ccg.c b/source/blender/blenkernel/intern/subsurf_ccg.c
index 45ec337..6379405 100644
--- a/source/blender/blenkernel/intern/subsurf_ccg.c
+++ b/source/blender/blenkernel/intern/subsurf_ccg.c
@@ -2869,6 +2869,28 @@ static void ccg_loops_to_corners(CustomData *fdata, CustomData *ldata,
 			copy_v2_v2(of->uv[j], lof->uv);
 		}
 	}
+
+#if 0
+	// TODO(bishop): why this special (incomplete?) tesselation
+	{
+		int *dst = CustomData_get(fdata, findex, CD_PTEX_TESSFACE);
+		const int *src = CustomData_get(pdata, polyindex,
+		                                CD_PTEX_QUAD_FACE_ID);
+		*dst = *src;
+	}
+#endif
+#if 1
+	{
+		MPtexTessFace *dst = CustomData_get(fdata, findex, CD_PTEX_TESSFACE);
+		
+		//ME_MTEXFACE_CPY(texface, texpoly);
+
+		MPtexLoop *mloopuv = CustomData_get(ldata, loopstart, CD_PTEX_LOOP);
+		for (j = 0; j < 4; j++, mloopuv++) {
+			dst->corners[j] = *mloopuv;
+		}
+	}
+#endif
 }
 
 static void *ccgDM_get_vert_data_layer(DerivedMesh *dm, int type)
@@ -3009,15 +3031,146 @@ static void *ccgDM_get_tessface_data_layer(DerivedMesh *dm, int type)
 	return DM_get_tessface_data_layer(dm, type);
 }
 
+static void ccgDM_ptex_update_grid(MPtexLoop (*output)[4],
+                                   const int gridFaces,
+                                   const float origin[2],
+                                   const float step[2],
+                                   const bool swp) {
+	int output_index = 0;
+	int y;
+	float v = origin[1];
+	for (y = 0; y < gridFaces; y++) {
+		int x;
+		float u = origin[0];
+		for (x = 0; x < gridFaces; x++) {
+			float uv[4][2] = {
+				{u,           v          },
+				{u,           v + step[1]},
+				{u + step[0], v + step[1]},
+				{u + step[0], v          },
+			};
+			int s;
+			for (s = 0; s < 4; s++) {
+				copy_v2_v2(output[output_index][s].uv, uv[s]);
+				if (swp) {
+					SWAP(float,
+					     output[output_index][s].uv[0],
+					     output[output_index][s].uv[1]);
+				}
+				// TODO
+				output[output_index][s].uv[1] = 1 - output[output_index][s].uv[1];
+			}
+
+			output_index++;
+			u += step[0];
+		}
+		v += step[1];
+	}
+}
+
+static void ccgDM_ptex_update_quad_loops(MPtexLoop (*output)[4],
+                                         const int gridFaces) {
+	const float f = 0.5f / ((float)(gridFaces));
+	const float origin[2] = {0.5, 0.5};
+	const float step[4][2] = {
+		{f, -f},
+		{f, f},
+		{-f, f},
+		{-f, -f},
+	};
+	const bool swp[4] = {true, false, true, false};
+	int output_index = 0;
+	int s;
+	for (s = 0; s < 4; s++) {
+		ccgDM_ptex_update_grid(output + output_index, gridFaces, origin,
+		                       step[s], swp[s]);
+		output_index += gridFaces * gridFaces;
+	}
+}
+
+static void ccgDM_ptex_update_subface_loops(MPtexLoop (*output)[4],
+                                            const int gridFaces,
+                                            const int numVerts) {
+	const float f = 1.0f / ((float)(gridFaces));
+	int output_index = 0;
+	int s;
+	for (s = 0; s < numVerts; s++) {
+		float origin[2] = {0, 1};
+		float step[2] = {f, -f};
+		bool swp = true;
+		ccgDM_ptex_update_grid(output + output_index, gridFaces, origin,
+		                       step, swp);
+		output_index += gridFaces * gridFaces;
+	}
+}
+
+static void *ccgDM_get_loop_data_layer(DerivedMesh *dm, int type)
+{
+	CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
+	CCGSubSurf *ss = ccgdm->ss;
+	const int gridFaces = ccgSubSurf_getGridSize(ss) - 1;
+
+	if (type == CD_PTEX_LOOP) {
+		MPtexLoop (*output)[4] = DM_get_loop_data_layer(dm, CD_PTEX_LOOP);
+
+		if (!output) {
+			const int totface = ccgSubSurf_getNumFaces(ss);
+			int ptex_id = 0;
+			int i, output_index = 0;
+
+			DM_add_loop_layer(dm, CD_PTEX_LOOP, CD_CALLOC, NULL);
+			output = DM_get_loop_data_layer(dm, CD_PTEX_LOOP);
+
+			for (i = 0; i < totface; i++) {
+				CCGFace *f = ccgdm->faceMap[i].face;
+				const int numVerts = ccgSubSurf_getFaceNumVerts(f);
+				int j;
+
+				if (numVerts == 4) {
+					ccgDM_ptex_update_quad_loops(output + output_index,
+					                             gridFaces);
+				} else {
+					ccgDM_ptex_update_subface_loops(output + output_index,
+					                                gridFaces,
+					                                numVerts);
+				}
+				
+				for (j = 0; j < numVerts; j++) {
+					int k;
+					for (k = 0; k < gridFaces * gridFaces; k++) {
+						int l;
+						for (l = 0; l < 4; l++) {
+							output[output_index][l].id = ptex_id;
+						}
+						output_index++;
+					}
+					if (numVerts != 4) {
+						ptex_id++;
+					}
+				}
+				if (numVerts == 4) {
+					ptex_id++;
+				}
+
+				//output_index += numVerts * gridFaces * gridFaces;
+			}
+		}
+		return output;
+	}
+
+	return DM_get_loop_data_layer(dm, type);
+}
+
 static void *ccgDM_get_poly_data_layer(DerivedMesh *dm, int type)
 {
+	CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
+	CCGSubSurf *ss = ccgdm->ss;
+	const int gridFaces = ccgSubSurf_getGridSize(ss) - 1;
+
 	if (type == CD_ORIGINDEX) {
 		/* create origindex on demand to save memory */
-		CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
-		CCGSubSurf *ss = ccgdm->ss;
 		int *origindex;
 		int a, i, index, totface;
-		int gridFaces = ccgSubSurf_getGridSize(ss) - 1;
 
 		/* Avoid re-creation if the layer exists already */
 		origindex = DM_get_poly_data_layer(dm, CD_ORIGINDEX);
@@ -3067,7 +3220,7 @@ static void *ccgDM_get_edge_data(DerivedMesh *dm, int index, int type)
 
 static void *ccgDM_get_tessface_data(DerivedMesh *dm, int index, int type)
 {
-	if (ELEM(type, CD_ORIGINDEX, CD_TESSLOOPNORMAL)) {
+	if (ELEM(type, CD_ORIGINDEX, CD_TESSLOOPNORMAL, CD_PTEX_TESSFACE)) {
 		/* ensure creation of CD_ORIGINDEX/CD_TESSLOOPNORMAL layers */
 		ccgDM_get_tessface_data_layer(dm, type);
 	}
@@ -3077,10 +3230,12 @@ static void *ccgDM_get_tessface_data(DerivedMesh *dm, int index, int type)
 
 static void *ccgDM_get_poly_data(DerivedMesh *dm, int index, int type)
 {
-	if (type == CD_ORIGINDEX) {
+#if 1
+	if (type == CD_ORIGINDEX || type == CD_PTEX_TESSFACE) {
 		/* ensure creation of CD_ORIGINDEX layer */
 		ccgDM_get_tessface_data_layer(dm, type);
 	}
+#endif
 
 	return DM_get_poly_data(dm, index, type);
 }
@@ -3451,6 +3606,7 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
 	ccgdm->dm.getVertDataArray = ccgDM_get_vert_data_layer;
 	ccgdm->dm.getEdgeDataArray = ccgDM_get_edge_data_layer;
 	ccgdm->dm.getTessFaceDataArray = ccgDM_get_tessface_data_layer;
+	ccgdm->dm.getLoopDataArray = ccgDM_get_loop_data_layer;
 	ccgdm->dm.getPolyDataArray = ccgDM_get_poly_data_layer;
 	ccgdm->dm.getNumGrids = ccgDM_getNumGrids;
 	ccgdm->dm.getGridSize = ccgDM_getGridSize;
@@ -3559,6 +3715,15 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
 	mcol = DM_get_tessface_data_layer(&ccgdm->dm, CD_MCOL);
 #endif
 
+	
+	// TODO, quick hacks
+#if 1
+	ccgDM_get_loop_data_layer(&ccgdm->dm, CD_PTEX_LOOP);
+	ccgDM_get_poly_data_layer(&ccgdm->dm, CD_PTEX_TESSFACE);
+	CustomData_add_layer(&ccgdm->dm.faceData, CD_PTEX_TESSFACE, 
+	                     CD_CALLOC, NULL, ccgdm->dm.numTessFaceData); 
+#endif
+
 	loopindex = loopindex2 = 0; /* current loop index */
 	for (index = 0; index < totface; index++) {
 		CCGFace *f = ccgdm->faceMap[index].face;
@@ -3572,6 +3737,11 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
 		int loopidx[numVerts], vertidx[numVerts];
 #endif
 
+		// TODO: shouldn't do this lookup for every face
+#if 0
+		MLoopUV *ptex_uvs = DM_get_loop_data_layer(&ccgdm->dm, CD_PTEX_UV);
+#endif
+
 		w = get_ss_weights(&wtable, gridCuts, numVerts);
 
 		ccgdm->faceMap[index].startVert = vertNum;
@@ -3658,25 +3828,26 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
 			/*interpolate per-face data*/
 			for (y = 0; y < gridFaces; y++) {
 				for (x = 0; x < gridFaces; x++) {
-					w2 = w + s * numVerts * g2_wid * g2_wid + (y * g2_wid + x) * numVerts;
-					CustomData_interp(&dm->loopData, &ccgdm->dm.loopData,
-					                  loopidx, w2, NULL, numVerts, loopindex2);
-					loopindex2++;
-
-					w2 = w + s * numVerts * g2_wid * g2_wid + ((y + 1) * g2_wid + (x)) * numVerts;
-					CustomData_interp(&dm->loopData, &ccgdm->dm.loopData,
-					                  loopidx, w2, NULL, numVerts, loopindex2);
-					loopindex2++;
-
-					w2 = w + s * numVerts * g2_wid * g2_wid + ((y + 1) * g2_wid + (x + 1)) * numVerts;
-					CustomData_interp(&dm->loopData, &ccgdm->dm.loopData,
-					                  loopidx, w2, NULL, numVerts, loopindex2);
-					loopindex2++;
-					
-					w2 = w + s * numVerts * g2_wid * g2_wid + ((y) * g2_wid + (x + 1)) * numVerts;
-					CustomData_interp(&dm->loopData, &ccgdm->dm.loopData,
-					                  loopidx, w2, NULL, numVerts, loopindex2);
-					loopindex2++;
+					int x2, y2;
+					for (x2 = 0; x2 <= 1; x2++) {
+						for (y2 = 0; y2 <= 1; y2++) {
+							/* This gives the (x,y) pattern:
+							 * (0,0) (0,1) (1,1) (1,0) */
+							int y3 = (x2 == 0) ? y2 : 1 - y2;
+
+							/* Pointer arithmetic */
+							w2 = w + (s * numVerts * g2_wid * g2_wid +
+							          ((y + y3) * g2_wid +
+							           (x + x2)) * numVerts);
+
+							CustomData_interp(&dm->loopData, &ccgdm->dm.loopData,
+							                  loopidx, w2, NULL, numVerts, loopindex2);
+							/* ptex_uvs[loopindex2].uv[0] = w2[1]; */
+							/* ptex_uvs[loopindex2].uv[1] = w2[3]; */
+
+							loopindex2++;
+						}
+					}
 
 					/*copy over poly data, e.g. mtexpoly*/
 					CustomData_copy_data(&dm->polyData, &ccgdm->dm.polyData, origIndex, faceNum, 1);
@@ -3799,7 +3970,7 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
 	ccgdm->dm.numPolyData = faceNum;
 
 	/* All tessellated CD layers were updated! */
-	ccgdm->dm.dirty &= ~DM_DIRTY_TESS_CDLAYERS;
+	//ccgdm->dm.dirty &= ~DM_DIRTY_TESS_CDLAYERS;
 
 #ifndef USE_DYNSIZE
 	BLI_array_free(vertidx);




More information about the Bf-blender-cvs mailing list