MCPcopy
hub / github.com/g3n/engine / NewSphereSector

Function NewSphereSector

geometry/sphere.go:21–88  ·  view source on GitHub ↗

NewSphereSector creates a sphere sector geometry with the specified radius, number of radial segments in each dimension, elevation start angle in radians, elevation size angle in radians, sector start angle in radians, and sector size angle in radians.

(radius float64, widthSegments, heightSegments int, phiStart, phiLength, thetaStart, thetaLength float64)

Source from the content-addressed store, hash-verified

19// NewSphereSector creates a sphere sector geometry with the specified radius, number of radial segments in each dimension, elevation
20// start angle in radians, elevation size angle in radians, sector start angle in radians, and sector size angle in radians.
21func NewSphereSector(radius float64, widthSegments, heightSegments int, phiStart, phiLength, thetaStart, thetaLength float64) *Geometry {
22
23 s := NewGeometry()
24
25 thetaEnd := thetaStart + thetaLength
26 vertexCount := (widthSegments + 1) * (heightSegments + 1)
27
28 // Create buffers
29 positions := math32.NewArrayF32(vertexCount*3, vertexCount*3)
30 normals := math32.NewArrayF32(vertexCount*3, vertexCount*3)
31 uvs := math32.NewArrayF32(vertexCount*2, vertexCount*2)
32 indices := math32.NewArrayU32(0, vertexCount)
33
34 index := 0
35 vertices := make([][]uint32, 0)
36 var normal math32.Vector3
37
38 for y := 0; y <= heightSegments; y++ {
39 verticesRow := make([]uint32, 0)
40 v := float64(y) / float64(heightSegments)
41 for x := 0; x <= widthSegments; x++ {
42 u := float64(x) / float64(widthSegments)
43 px := -radius * math.Cos(phiStart+u*phiLength) * math.Sin(thetaStart+v*thetaLength)
44 py := radius * math.Cos(thetaStart+v*thetaLength)
45 pz := radius * math.Sin(phiStart+u*phiLength) * math.Sin(thetaStart+v*thetaLength)
46 normal.Set(float32(px), float32(py), float32(pz)).Normalize()
47
48 positions.Set(index*3, float32(px), float32(py), float32(pz))
49 normals.SetVector3(index*3, &normal)
50 uvs.Set(index*2, float32(u), float32(v))
51 verticesRow = append(verticesRow, uint32(index))
52 index++
53 }
54 vertices = append(vertices, verticesRow)
55 }
56
57 for y := 0; y < heightSegments; y++ {
58 for x := 0; x < widthSegments; x++ {
59 v1 := vertices[y][x+1]
60 v2 := vertices[y][x]
61 v3 := vertices[y+1][x]
62 v4 := vertices[y+1][x+1]
63 if y != 0 || thetaStart > 0 {
64 indices.Append(v1, v2, v4)
65 }
66 if y != heightSegments-1 || thetaEnd < math.Pi {
67 indices.Append(v2, v3, v4)
68 }
69 }
70 }
71
72 s.SetIndices(indices)
73 s.AddVBO(gls.NewVBO(positions).AddAttrib(gls.VertexPosition))
74 s.AddVBO(gls.NewVBO(normals).AddAttrib(gls.VertexNormal))
75 s.AddVBO(gls.NewVBO(uvs).AddAttrib(gls.VertexTexcoord))
76
77 r := float32(radius)
78

Callers 1

NewSphereFunction · 0.85

Calls 12

SetMethod · 0.95
SetMethod · 0.95
SetVector3Method · 0.95
AppendMethod · 0.95
SetIndicesMethod · 0.95
AddVBOMethod · 0.95
NewArrayF32Function · 0.92
NewArrayU32Function · 0.92
NewVBOFunction · 0.92
NewGeometryFunction · 0.85
NormalizeMethod · 0.45
AddAttribMethod · 0.45

Tested by

no test coverage detected