-
Notifications
You must be signed in to change notification settings - Fork 1
/
torus.go
77 lines (63 loc) · 2.33 KB
/
torus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package geometry
import (
"github.com/lquesada/cavernal/lib/g3n/engine/gls"
"github.com/lquesada/cavernal/lib/g3n/engine/math32"
"math"
)
// Torus represents a torus geometry
type Torus struct {
Geometry // embedded geometry
Radius float64 // Torus radius
Tube float64 // Diameter of the torus tube
RadialSegments int // Number of radial segments
TubularSegments int // Number of tubular segments
Arc float64 // Central angle
}
// NewTorus returns a pointer to a new torus geometry
func NewTorus(radius, tube float64, radialSegments, tubularSegments int, arc float64) *Torus {
t := new(Torus)
t.Geometry.Init()
t.Radius = radius
t.Tube = tube
t.RadialSegments = radialSegments
t.TubularSegments = tubularSegments
t.Arc = arc
// Create buffers
positions := math32.NewArrayF32(0, 0)
normals := math32.NewArrayF32(0, 0)
uvs := math32.NewArrayF32(0, 0)
indices := math32.NewArrayU32(0, 0)
var center math32.Vector3
for j := 0; j <= radialSegments; j++ {
for i := 0; i <= tubularSegments; i++ {
u := float64(i) / float64(tubularSegments) * arc
v := float64(j) / float64(radialSegments) * math.Pi * 2
center.X = float32(radius * math.Cos(u))
center.Y = float32(radius * math.Sin(u))
var vertex math32.Vector3
vertex.X = float32((radius + tube*math.Cos(v)) * math.Cos(u))
vertex.Y = float32((radius + tube*math.Cos(v)) * math.Sin(u))
vertex.Z = float32(tube * math.Sin(v))
positions.AppendVector3(&vertex)
uvs.Append(float32(float64(i)/float64(tubularSegments)), float32(float64(j)/float64(radialSegments)))
normals.AppendVector3(vertex.Sub(¢er).Normalize())
}
}
for j := 1; j <= radialSegments; j++ {
for i := 1; i <= tubularSegments; i++ {
a := (tubularSegments+1)*j + i - 1
b := (tubularSegments+1)*(j-1) + i - 1
c := (tubularSegments+1)*(j-1) + i
d := (tubularSegments+1)*j + i
indices.Append(uint32(a), uint32(b), uint32(d), uint32(b), uint32(c), uint32(d))
}
}
t.SetIndices(indices)
t.AddVBO(gls.NewVBO(positions).AddAttrib(gls.VertexPosition))
t.AddVBO(gls.NewVBO(normals).AddAttrib(gls.VertexNormal))
t.AddVBO(gls.NewVBO(uvs).AddAttrib(gls.VertexTexcoord))
return t
}