private void CalculateGeometry() { double phiSeg = 2 * Math.PI / nSegments; // The center of the top and bottom Points.Add(new Point3D(0, 0, radius / 2)); Points.Add(new Point3D(0, 0, -radius / 2)); // Points along the edge for (double iphi = 0; iphi < 2 * Math.PI; iphi += phiSeg) { double x = radius * Math.Cos(iphi); double y = radius * Math.Sin(iphi); double z = height / 2; Points.Add(new Point3D(x, y, z)); // Top ring is all even numbers, starting at 2 to 2 * nSegments Points.Add(new Point3D(x, y, -z)); // Bottom ring is all odd numbers, starting at 3 to 2 * nSegments + 1 } const int topCenterIndex = 0; const int bottomCenterIndex = 1; const int firstTopRing = 2; int lastTopRing = 2 * nSegments; // Top for (int index = firstTopRing; index <= lastTopRing; index += 2) { int nextIndex = index == lastTopRing ? firstTopRing : index + 2; // Top circle TriangleIndices.Add(index); TriangleIndices.Add(nextIndex); TriangleIndices.Add(topCenterIndex); // Bottom circle - opposite orientation to face out TriangleIndices.Add(index + 1); TriangleIndices.Add(bottomCenterIndex); TriangleIndices.Add(nextIndex + 1); // Sides TriangleIndices.Add(index); TriangleIndices.Add(index + 1); TriangleIndices.Add(nextIndex); TriangleIndices.Add(index + 1); TriangleIndices.Add(nextIndex + 1); TriangleIndices.Add(nextIndex); } }
public void AddTriangleIndex(uint index) { if (_pointTally == 0) { _fanStartIndex = index; } if (_pointTally < 3) //first time { TriangleIndices.Add(Offset(index)); // _meshGeometry.Positions.Add(_points[(int)index]); } else { switch (_meshType) { case TriangleType.GL_Triangles: // 0x0004 TriangleIndices.Add(Offset(index)); break; case TriangleType.GL_Triangles_Strip: // 0x0005 if (_pointTally % 2 == 0) { TriangleIndices.Add(Offset(_previousToLastIndex)); TriangleIndices.Add(Offset(_lastIndex)); } else { TriangleIndices.Add(Offset(_lastIndex)); TriangleIndices.Add(Offset(_previousToLastIndex)); } TriangleIndices.Add(Offset(index)); break; case TriangleType.GL_Triangles_Fan: // 0x0006 TriangleIndices.Add(Offset(_fanStartIndex)); TriangleIndices.Add(Offset(_lastIndex)); TriangleIndices.Add(Offset(index)); break; default: break; } } _previousToLastIndex = _lastIndex; _lastIndex = index; _pointTally++; }
private void CalculateGeometry() { double thetaSeg = Math.PI / nSegments; double phiSeg = 2 * Math.PI / nSegments; Points.Add(new Point3D(0, 0, radius)); for (double itheta = thetaSeg; itheta < Math.PI; itheta += thetaSeg) { for (double iphi = 0; iphi < 2 * Math.PI; iphi += phiSeg) { double x = radius * Math.Cos(iphi) * Math.Sin(itheta); double y = radius * Math.Sin(iphi) * Math.Sin(itheta); double z = radius * Math.Cos(itheta); Points.Add(new Point3D(x, y, z)); } } Points.Add(new Point3D(0, 0, -radius)); // Top ring for (int index = 1; index <= nSegments; ++index) { TriangleIndices.Add(0); TriangleIndices.Add(index); TriangleIndices.Add(index == nSegments ? 1 : index + 1); } // Middle section int maxTheta = nSegments * (nSegments - 2); // Index of the last point of the second-to-last theta ring for (int thetaIndex = 1; thetaIndex <= maxTheta; thetaIndex += nSegments) { for (int phiIndex = 0; phiIndex < nSegments; ++phiIndex) { int thisPoint = thetaIndex + phiIndex; int nextPhi = phiIndex == nSegments - 1 ? thetaIndex : thisPoint + 1; int nextTheta = thisPoint + nSegments; int nextThetaPhi = nextPhi + nSegments; TriangleIndices.Add(thisPoint); TriangleIndices.Add(nextTheta); TriangleIndices.Add(nextThetaPhi); TriangleIndices.Add(thisPoint); TriangleIndices.Add(nextThetaPhi); TriangleIndices.Add(nextPhi); } } // Bottom ring int lastThetaRing = maxTheta + 1; int lastIndex = nSegments * (nSegments - 1) + 1; for (int index = lastThetaRing; index < lastIndex; ++index) { TriangleIndices.Add(lastIndex); TriangleIndices.Add(index == lastIndex - 1 ? lastThetaRing : index + 1); TriangleIndices.Add(index); } }