[Bf-blender-cvs] [8464641e775] geometry-nodes-mesh-primitives: Sphere edges working

Hans Goudey noreply at git.blender.org
Thu Mar 11 23:15:09 CET 2021


Commit: 8464641e775bce7f9f4afa457f4801808a51dc94
Author: Hans Goudey
Date:   Thu Feb 25 13:37:37 2021 -0600
Branches: geometry-nodes-mesh-primitives
https://developer.blender.org/rB8464641e775bce7f9f4afa457f4801808a51dc94

Sphere edges working

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

M	source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc

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

diff --git a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc
index 55e81b09cf8..2cdcffd499a 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc
@@ -50,13 +50,11 @@ static int vert_total(const int segments, const int rings)
 
 static int edge_total(const int segments, const int rings)
 {
-  return 0;
-  return segments * (rings + 2);
+  return segments * (rings * 2 - 1);
 }
 
 static int corner_total(const int segments, const int rings)
 {
-  return 0;
   const int quad_corners = 4 * segments * (rings - 2);
   const int tri_corners = 3 * segments * 2;
   return quad_corners + tri_corners;
@@ -64,12 +62,127 @@ static int corner_total(const int segments, const int rings)
 
 static int face_total(const int segments, const int rings)
 {
-  return 0;
   const int quads = segments * (rings - 2);
   const int triangles = segments * 2;
   return quads + triangles;
 }
 
+static void calculate_vertex_coords_and_normals(MutableSpan<MVert> verts,
+                                                const float radius,
+                                                const int segments,
+                                                const int rings)
+{
+  const float delta_theta = M_PI / rings;
+  const float delta_phi = (2 * M_PI) / segments;
+
+  copy_v3_v3(verts[0].co, float3(0.0f, 0.0f, radius));
+  normal_float_to_short_v3(verts[0].no, float3(0.0f, 0.0f, 1.0f));
+
+  int vert_index = 1;
+  float theta = delta_theta;
+  for (const int UNUSED(ring) : IndexRange(rings - 1)) {
+    float phi = 0.0f;
+    const float z = cosf(theta);
+    for (const int UNUSED(segment) : IndexRange(segments)) {
+      const float x = sinf(theta) * cosf(phi);
+      const float y = sinf(theta) * sinf(phi);
+      copy_v3_v3(verts[vert_index].co, float3(x, y, z) * radius);
+      normal_float_to_short_v3(verts[vert_index].no, float3(x, y, z));
+      vert_index++;
+      phi += delta_phi;
+    }
+    theta += delta_theta;
+  }
+
+  copy_v3_v3(verts.last().co, float3(0.0f, 0.0f, -radius));
+  normal_float_to_short_v3(verts.last().no, float3(0.0f, 0.0f, -1.0f));
+}
+
+static void calculate_edge_indices(MutableSpan<MEdge> edges, const int segments, const int rings)
+{
+  int edge_index = 0;
+
+  /* Add the edges connecting the top vertex to the first ring. */
+  int vert_index = 1;
+  for (const int UNUSED(segment) : IndexRange(segments)) {
+    MEdge &edge = edges[edge_index];
+    edge.flag |= ME_LOOSEEDGE;
+    edge.v1 = 0;
+    edge.v2 = vert_index;
+    vert_index++;
+    edge_index++;
+  }
+
+  int ring_vert_index_start = 1;
+  for (const int ring : IndexRange(rings - 1)) {
+    int next_ring_vert_index_start = ring_vert_index_start + segments;
+
+    /* Add the edges running along each ring. */
+    for (const int segment : IndexRange(segments)) {
+      MEdge &edge_in_ring = edges[edge_index];
+      edge_in_ring.flag |= ME_LOOSEEDGE;
+      edge_in_ring.v1 = ring_vert_index_start + segment;
+      edge_in_ring.v2 = ring_vert_index_start + ((segment + 1) % segments);
+      edge_index++;
+    }
+
+    /* Add the edges connecting to the next ring. */
+    if (ring < rings - 2) {
+      for (const int segment : IndexRange(segments)) {
+        MEdge &edge_to_next_ring = edges[edge_index];
+        edge_to_next_ring.flag |= ME_LOOSEEDGE;
+        edge_to_next_ring.v1 = ring_vert_index_start + segment;
+        edge_to_next_ring.v2 = next_ring_vert_index_start + segment;
+        edge_index++;
+      }
+    }
+    ring_vert_index_start += segments;
+  }
+
+  /* Add the edges connecting the last ring to the bottom vertex. */
+  vert_index = segments * (rings - 2) + 1;
+  const int last_vert_index = vert_total(segments, rings) - 1;
+  for (const int UNUSED(segment) : IndexRange(segments)) {
+    MEdge &edge = edges[edge_index];
+    edge.flag |= ME_LOOSEEDGE;
+    edge.v1 = last_vert_index;
+    edge.v2 = vert_index;
+    vert_index++;
+    edge_index++;
+  }
+}
+
+static void calculate_faces(MutableSpan<MLoop> loops,
+                            MutableSpan<MPoly> polys,
+                            const int segments,
+                            const int rings)
+{
+  int loop_index = 0;
+  int poly_index = 0;
+
+  /* Add the triangles conntected to the top vertex. */
+  for (const int segment : IndexRange(segments)) {
+    MPoly &poly = polys[poly_index];
+    poly.loopstart = loop_index;
+    poly.totloop = 3;
+
+    MLoop &loop_a = loops[loop_index];
+    loop_a.e = segment;
+    loop_a.v = 0;
+    loop_index++;
+
+    MLoop &loop_b = loops[loop_index];
+    loop_b.e = segments + segment;
+    loop_b.v = 1 + segment;
+    loop_index++;
+
+    MLoop &loop_c = loops[loop_index];
+    loop_c.e = (segments + 1) % segments;
+    loop_c.v = 1 + (segment + 1) % segments;
+    loop_index++;
+  }
+}
+
 static Mesh *create_uv_sphere_mesh(const float3 location,
                                    const float3 rotation,
                                    const float radius,
@@ -89,34 +202,13 @@ static Mesh *create_uv_sphere_mesh(const float3 location,
   MutableSpan<MLoop> loops = MutableSpan<MLoop>(mesh->mloop, mesh->totloop);
   MutableSpan<MPoly> polys = MutableSpan<MPoly>(mesh->mpoly, mesh->totpoly);
 
-  const float delta_theta = (2 * M_PI) / segments;
-  const float delta_phi = (2 * M_PI) / rings;
-  const float phi_start = -M_PI + delta_theta;
+  calculate_vertex_coords_and_normals(verts, radius, segments, rings);
 
-  copy_v3_v3(verts[0].co, float3(0.0f, 0.0f, radius));
-  normal_float_to_short_v3(verts[0].no, float3(0.0f, 0.0f, 1.0f));
-  copy_v3_v3(verts.last().co, float3(0.0f, 0.0f, radius));
-  normal_float_to_short_v3(verts.last().no, float3(0.0f, 0.0f, -1.0f));
+  // calculate_edge_indices(edges, segments, rings);
 
-  float theta = 0.0f;
-  int vert_index = 1;
-  for (const int segment : IndexRange(segments)) {
-    float phi = phi_start;
-    for (const int ring : IndexRange(rings - 1)) {
-      const float x = sinf(phi) * cosf(theta);
-      const float y = sinf(phi) * sinf(theta);
-      const float z = sinf(phi);
-      // const float z = cosf(phi);
-      const float3 unit_sphere_point = float3(x, y, z);
-      copy_v3_v3(verts[vert_index].co, float3(x, y, z) * radius);
-      normal_float_to_short_v3(verts[vert_index].no, unit_sphere_point);
-      ++vert_index;
-      phi += delta_phi;
-    }
-    theta += delta_theta;
-  }
+  // calculate_faces(loops, polys, segments, rings);
 
-  BLI_assert(BKE_mesh_is_valid(mesh));
+  // BLI_assert(BKE_mesh_is_valid(mesh));
 
   return mesh;
 }



More information about the Bf-blender-cvs mailing list