NewDiskSector creates a disk (filled circle) or disk sector geometry with the specified radius, number of radial segments/triangles (minimum 3), sector start angle in radians, and sector size angle in radians. The center of the disk is at the origin, and theta runs counter-clockwise on the XY plane,
(radius float64, segments int, thetaStart, thetaLength float64)
| 21 | // number of radial segments/triangles (minimum 3), sector start angle in radians, and sector size angle in radians. |
| 22 | // The center of the disk is at the origin, and theta runs counter-clockwise on the XY plane, starting at (x,y,z)=(1,0,0). |
| 23 | func NewDiskSector(radius float64, segments int, thetaStart, thetaLength float64) *Geometry { |
| 24 | |
| 25 | d := NewGeometry() |
| 26 | |
| 27 | // Validate arguments |
| 28 | if segments < 3 { |
| 29 | panic("Invalid argument: segments. The number of segments needs to be greater or equal to 3.") |
| 30 | } |
| 31 | |
| 32 | // Create buffers |
| 33 | positions := math32.NewArrayF32(0, 16) |
| 34 | normals := math32.NewArrayF32(0, 16) |
| 35 | uvs := math32.NewArrayF32(0, 16) |
| 36 | indices := math32.NewArrayU32(0, 16) |
| 37 | |
| 38 | // Append circle center position |
| 39 | center := math32.NewVector3(0, 0, 0) |
| 40 | positions.AppendVector3(center) |
| 41 | |
| 42 | // Append circle center normal |
| 43 | var normal math32.Vector3 |
| 44 | normal.Z = 1 |
| 45 | normals.AppendVector3(&normal) |
| 46 | |
| 47 | // Append circle center uv coordinate |
| 48 | centerUV := math32.NewVector2(0.5, 0.5) |
| 49 | uvs.AppendVector2(centerUV) |
| 50 | |
| 51 | // Generate the segments |
| 52 | for i := 0; i <= segments; i++ { |
| 53 | segment := thetaStart + float64(i)/float64(segments)*thetaLength |
| 54 | |
| 55 | vx := float32(radius * math.Cos(segment)) |
| 56 | vy := float32(radius * math.Sin(segment)) |
| 57 | |
| 58 | // Appends vertex position, normal and uv coordinates |
| 59 | positions.Append(vx, vy, 0) |
| 60 | normals.AppendVector3(&normal) |
| 61 | uvs.Append((vx/float32(radius)+1)/2, (vy/float32(radius)+1)/2) |
| 62 | } |
| 63 | |
| 64 | for i := 1; i <= segments; i++ { |
| 65 | indices.Append(uint32(i), uint32(i)+1, 0) |
| 66 | } |
| 67 | |
| 68 | d.SetIndices(indices) |
| 69 | d.AddVBO(gls.NewVBO(positions).AddAttrib(gls.VertexPosition)) |
| 70 | d.AddVBO(gls.NewVBO(normals).AddAttrib(gls.VertexNormal)) |
| 71 | d.AddVBO(gls.NewVBO(uvs).AddAttrib(gls.VertexTexcoord)) |
| 72 | |
| 73 | // Update volume |
| 74 | d.volume = 0 |
| 75 | d.volumeValid = true |
| 76 | |
| 77 | return d |
| 78 | } |
no test coverage detected