[Bf-blender-cvs] [5d4c8c3] temp-cycles-microdisplacement: Split ngons into quads

Mai Lavelle noreply at git.blender.org
Fri Jun 24 19:26:39 CEST 2016


Commit: 5d4c8c35d670ddd5ca55035245790cdb5ff299f7
Author: Mai Lavelle
Date:   Mon Jun 20 05:49:51 2016 -0400
Branches: temp-cycles-microdisplacement
https://developer.blender.org/rB5d4c8c35d670ddd5ca55035245790cdb5ff299f7

Split ngons into quads

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

M	intern/cycles/render/mesh.cpp
M	intern/cycles/util/util_math.h

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

diff --git a/intern/cycles/render/mesh.cpp b/intern/cycles/render/mesh.cpp
index c961ecb..2968f7a 100644
--- a/intern/cycles/render/mesh.cpp
+++ b/intern/cycles/render/mesh.cpp
@@ -1642,53 +1642,27 @@ void Mesh::tessellate(DiagSplit *split)
 	float3* vN = attr_vN->data_float3();
 
 	for(int f = 0; f < num_faces; f++) {
-		if(!subd_faces[f].is_quad()) {
-			/* triangle */
-			LinearTrianglePatch patch;
-			SubdFace p = subd_faces[f];
-			float3 *hull = patch.hull;
-			float3 *normals = patch.normals;
-
-			patch.patch_index = f;
+		SubdFace& face = subd_faces[f];
 
-			for(int i = 0; i < 3; i++) {
-				hull[i] = verts[subd_face_corners[p.start_corner+i]];
-			}
-
-			if(p.smooth) {
-				for(int i = 0; i < 3; i++) {
-					normals[i] = vN[subd_face_corners[p.start_corner+i]];
-				}
-			}
-			else {
-				float3 N = p.normal(this);
-				for(int i = 0; i < 3; i++) {
-					normals[i] = N;
-				}
-			}
-
-			split->split_triangle(&patch);
-		}
-		else {
+		if(face.is_quad()) {
 			/* quad */
 			LinearQuadPatch patch;
-			SubdFace p = subd_faces[f];
 			float3 *hull = patch.hull;
 			float3 *normals = patch.normals;
 
 			patch.patch_index = f;
 
 			for(int i = 0; i < 4; i++) {
-				hull[i] = verts[subd_face_corners[p.start_corner+i]];
+				hull[i] = verts[subd_face_corners[face.start_corner+i]];
 			}
 
-			if(p.smooth) {
+			if(face.smooth) {
 				for(int i = 0; i < 4; i++) {
-					normals[i] = vN[subd_face_corners[p.start_corner+i]];
+					normals[i] = vN[subd_face_corners[face.start_corner+i]];
 				}
 			}
 			else {
-				float3 N = p.normal(this);
+				float3 N = face.normal(this);
 				for(int i = 0; i < 4; i++) {
 					normals[i] = N;
 				}
@@ -1699,6 +1673,51 @@ void Mesh::tessellate(DiagSplit *split)
 
 			split->split_quad(&patch);
 		}
+		else {
+			/* ngon */
+			float3 center_vert = make_float3(0.0f, 0.0f, 0.0f);
+			float3 center_normal = make_float3(0.0f, 0.0f, 0.0f);
+
+			float inv_num_corners = 1.0f/float(face.num_corners);
+			for(int corner = 0; corner < face.num_corners; corner++) {
+				center_vert += verts[subd_face_corners[face.start_corner + corner]] * inv_num_corners;
+				center_normal += vN[subd_face_corners[face.start_corner + corner]] * inv_num_corners;
+			}
+
+			for(int corner = 0; corner < face.num_corners; corner++) {
+				LinearQuadPatch patch;
+				float3 *hull = patch.hull;
+				float3 *normals = patch.normals;
+
+				patch.patch_index = f;
+
+				hull[0] = verts[subd_face_corners[face.start_corner + mod(corner + 0, face.num_corners)]];
+				hull[1] = verts[subd_face_corners[face.start_corner + mod(corner + 1, face.num_corners)]];
+				hull[2] = verts[subd_face_corners[face.start_corner + mod(corner - 1, face.num_corners)]];
+				hull[3] = center_vert;
+
+				hull[1] = (hull[1] + hull[0]) * 0.5;
+				hull[2] = (hull[2] + hull[0]) * 0.5;
+
+				if(face.smooth) {
+					normals[0] = vN[subd_face_corners[face.start_corner + mod(corner + 0, face.num_corners)]];
+					normals[1] = vN[subd_face_corners[face.start_corner + mod(corner + 1, face.num_corners)]];
+					normals[2] = vN[subd_face_corners[face.start_corner + mod(corner - 1, face.num_corners)]];
+					normals[3] = center_normal;
+
+					normals[1] = (normals[1] + normals[0]) * 0.5;
+					normals[2] = (normals[2] + normals[0]) * 0.5;
+				}
+				else {
+					float3 N = face.normal(this);
+					for(int i = 0; i < 4; i++) {
+						normals[i] = N;
+					}
+				}
+
+				split->split_quad(&patch);
+			}
+		}
 	}
 }
 
diff --git a/intern/cycles/util/util_math.h b/intern/cycles/util/util_math.h
index e1de8bb..ad39993 100644
--- a/intern/cycles/util/util_math.h
+++ b/intern/cycles/util/util_math.h
@@ -224,6 +224,10 @@ ccl_device_inline float smoothstepf(float f)
 	return (3.0f*ff - 2.0f*ff*f);
 }
 
+ccl_device_inline int mod(int x, int m) {
+	return (x % m + m) % m;
+}
+
 /* Float2 Vector */
 
 #ifndef __KERNEL_OPENCL__




More information about the Bf-blender-cvs mailing list