|
@@ -44,10 +44,10 @@ func NewCylinder(radiusTop, radiusBottom, height float64,
|
|
|
|
|
|
|
|
heightHalf := height / 2
|
|
heightHalf := height / 2
|
|
|
vertices := [][]int{}
|
|
vertices := [][]int{}
|
|
|
- uvs := [][]math32.Vector2{}
|
|
|
|
|
|
|
+ uvsOrig := [][]math32.Vector2{}
|
|
|
|
|
|
|
|
// Create buffer for vertex positions
|
|
// Create buffer for vertex positions
|
|
|
- Positions := math32.NewArrayF32(0, 0)
|
|
|
|
|
|
|
+ positions := math32.NewArrayF32(0, 0)
|
|
|
|
|
|
|
|
for y := 0; y <= heightSegments; y++ {
|
|
for y := 0; y <= heightSegments; y++ {
|
|
|
var verticesRow = []int{}
|
|
var verticesRow = []int{}
|
|
@@ -60,30 +60,30 @@ func NewCylinder(radiusTop, radiusBottom, height float64,
|
|
|
vertex.X = float32(radius * math.Sin(u*thetaLength+thetaStart))
|
|
vertex.X = float32(radius * math.Sin(u*thetaLength+thetaStart))
|
|
|
vertex.Y = float32(-v*height + heightHalf)
|
|
vertex.Y = float32(-v*height + heightHalf)
|
|
|
vertex.Z = float32(radius * math.Cos(u*thetaLength+thetaStart))
|
|
vertex.Z = float32(radius * math.Cos(u*thetaLength+thetaStart))
|
|
|
- Positions.AppendVector3(&vertex)
|
|
|
|
|
- verticesRow = append(verticesRow, Positions.Size()/3-1)
|
|
|
|
|
|
|
+ positions.AppendVector3(&vertex)
|
|
|
|
|
+ verticesRow = append(verticesRow, positions.Size()/3-1)
|
|
|
uvsRow = append(uvsRow, math32.Vector2{float32(u), 1.0 - float32(v)})
|
|
uvsRow = append(uvsRow, math32.Vector2{float32(u), 1.0 - float32(v)})
|
|
|
}
|
|
}
|
|
|
vertices = append(vertices, verticesRow)
|
|
vertices = append(vertices, verticesRow)
|
|
|
- uvs = append(uvs, uvsRow)
|
|
|
|
|
|
|
+ uvsOrig = append(uvsOrig, uvsRow)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
tanTheta := (radiusBottom - radiusTop) / height
|
|
tanTheta := (radiusBottom - radiusTop) / height
|
|
|
var na, nb math32.Vector3
|
|
var na, nb math32.Vector3
|
|
|
|
|
|
|
|
// Create preallocated buffers for normals and uvs and buffer for indices
|
|
// Create preallocated buffers for normals and uvs and buffer for indices
|
|
|
- npos := Positions.Size()
|
|
|
|
|
- Normals := math32.NewArrayF32(npos, npos)
|
|
|
|
|
- Uvs := math32.NewArrayF32(2*npos/3, 2*npos/3)
|
|
|
|
|
- Indices := math32.NewArrayU32(0, 0)
|
|
|
|
|
|
|
+ npos := positions.Size()
|
|
|
|
|
+ normals := math32.NewArrayF32(npos, npos)
|
|
|
|
|
+ uvs := math32.NewArrayF32(2*npos/3, 2*npos/3)
|
|
|
|
|
+ indices := math32.NewArrayU32(0, 0)
|
|
|
|
|
|
|
|
for x := 0; x < radialSegments; x++ {
|
|
for x := 0; x < radialSegments; x++ {
|
|
|
if radiusTop != 0 {
|
|
if radiusTop != 0 {
|
|
|
- Positions.GetVector3(3*vertices[0][x], &na)
|
|
|
|
|
- Positions.GetVector3(3*vertices[0][x+1], &nb)
|
|
|
|
|
|
|
+ positions.GetVector3(3*vertices[0][x], &na)
|
|
|
|
|
+ positions.GetVector3(3*vertices[0][x+1], &nb)
|
|
|
} else {
|
|
} else {
|
|
|
- Positions.GetVector3(3*vertices[1][x], &na)
|
|
|
|
|
- Positions.GetVector3(3*vertices[1][x+1], &nb)
|
|
|
|
|
|
|
+ positions.GetVector3(3*vertices[1][x], &na)
|
|
|
|
|
+ positions.GetVector3(3*vertices[1][x+1], &nb)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
na.SetY(float32(math.Sqrt(float64(na.X*na.X+na.Z*na.Z)) * tanTheta)).Normalize()
|
|
na.SetY(float32(math.Sqrt(float64(na.X*na.X+na.Z*na.Z)) * tanTheta)).Normalize()
|
|
@@ -100,162 +100,162 @@ func NewCylinder(radiusTop, radiusBottom, height float64,
|
|
|
n3 := nb
|
|
n3 := nb
|
|
|
n4 := nb
|
|
n4 := nb
|
|
|
|
|
|
|
|
- uv1 := uvs[y][x]
|
|
|
|
|
- uv2 := uvs[y+1][x]
|
|
|
|
|
- uv3 := uvs[y+1][x+1]
|
|
|
|
|
- uv4 := uvs[y][x+1]
|
|
|
|
|
-
|
|
|
|
|
- Indices.Append(uint32(v1), uint32(v2), uint32(v4))
|
|
|
|
|
- Normals.SetVector3(3*v1, &n1)
|
|
|
|
|
- Normals.SetVector3(3*v2, &n2)
|
|
|
|
|
- Normals.SetVector3(3*v4, &n4)
|
|
|
|
|
-
|
|
|
|
|
- Indices.Append(uint32(v2), uint32(v3), uint32(v4))
|
|
|
|
|
- Normals.SetVector3(3*v2, &n2)
|
|
|
|
|
- Normals.SetVector3(3*v3, &n3)
|
|
|
|
|
- Normals.SetVector3(3*v4, &n4)
|
|
|
|
|
-
|
|
|
|
|
- Uvs.SetVector2(2*v1, &uv1)
|
|
|
|
|
- Uvs.SetVector2(2*v2, &uv2)
|
|
|
|
|
- Uvs.SetVector2(2*v3, &uv3)
|
|
|
|
|
- Uvs.SetVector2(2*v4, &uv4)
|
|
|
|
|
|
|
+ uv1 := uvsOrig[y][x]
|
|
|
|
|
+ uv2 := uvsOrig[y+1][x]
|
|
|
|
|
+ uv3 := uvsOrig[y+1][x+1]
|
|
|
|
|
+ uv4 := uvsOrig[y][x+1]
|
|
|
|
|
+
|
|
|
|
|
+ indices.Append(uint32(v1), uint32(v2), uint32(v4))
|
|
|
|
|
+ normals.SetVector3(3*v1, &n1)
|
|
|
|
|
+ normals.SetVector3(3*v2, &n2)
|
|
|
|
|
+ normals.SetVector3(3*v4, &n4)
|
|
|
|
|
+
|
|
|
|
|
+ indices.Append(uint32(v2), uint32(v3), uint32(v4))
|
|
|
|
|
+ normals.SetVector3(3*v2, &n2)
|
|
|
|
|
+ normals.SetVector3(3*v3, &n3)
|
|
|
|
|
+ normals.SetVector3(3*v4, &n4)
|
|
|
|
|
+
|
|
|
|
|
+ uvs.SetVector2(2*v1, &uv1)
|
|
|
|
|
+ uvs.SetVector2(2*v2, &uv2)
|
|
|
|
|
+ uvs.SetVector2(2*v3, &uv3)
|
|
|
|
|
+ uvs.SetVector2(2*v4, &uv4)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
// First group is the body of the cylinder
|
|
// First group is the body of the cylinder
|
|
|
// without the caps
|
|
// without the caps
|
|
|
- c.AddGroup(0, Indices.Size(), 0)
|
|
|
|
|
- nextGroup := Indices.Size()
|
|
|
|
|
|
|
+ c.AddGroup(0, indices.Size(), 0)
|
|
|
|
|
+ nextGroup := indices.Size()
|
|
|
|
|
|
|
|
// Top cap
|
|
// Top cap
|
|
|
if top && radiusTop > 0 {
|
|
if top && radiusTop > 0 {
|
|
|
|
|
|
|
|
- // Array of vertex indices to build used to build the faces.
|
|
|
|
|
- indices := []uint32{}
|
|
|
|
|
- nextidx := Positions.Size() / 3
|
|
|
|
|
|
|
+ // Array of vertex indicesOrig to build used to build the faces.
|
|
|
|
|
+ indicesOrig := []uint32{}
|
|
|
|
|
+ nextidx := positions.Size() / 3
|
|
|
|
|
|
|
|
- // Appends top segments vertices and builds array of its indices
|
|
|
|
|
|
|
+ // Appends top segments vertices and builds array of its indicesOrig
|
|
|
var uv1, uv2, uv3 math32.Vector2
|
|
var uv1, uv2, uv3 math32.Vector2
|
|
|
for x := 0; x < radialSegments; x++ {
|
|
for x := 0; x < radialSegments; x++ {
|
|
|
- uv1 = uvs[0][x]
|
|
|
|
|
- uv2 = uvs[0][x+1]
|
|
|
|
|
|
|
+ uv1 = uvsOrig[0][x]
|
|
|
|
|
+ uv2 = uvsOrig[0][x+1]
|
|
|
uv3 = math32.Vector2{uv2.X, 0}
|
|
uv3 = math32.Vector2{uv2.X, 0}
|
|
|
// Appends CENTER with its own UV.
|
|
// Appends CENTER with its own UV.
|
|
|
- Positions.Append(0, float32(heightHalf), 0)
|
|
|
|
|
- Normals.Append(0, 1, 0)
|
|
|
|
|
- Uvs.AppendVector2(&uv3)
|
|
|
|
|
- indices = append(indices, uint32(nextidx))
|
|
|
|
|
|
|
+ positions.Append(0, float32(heightHalf), 0)
|
|
|
|
|
+ normals.Append(0, 1, 0)
|
|
|
|
|
+ uvs.AppendVector2(&uv3)
|
|
|
|
|
+ indicesOrig = append(indicesOrig, uint32(nextidx))
|
|
|
nextidx++
|
|
nextidx++
|
|
|
// Appends vertex
|
|
// Appends vertex
|
|
|
v := math32.Vector3{}
|
|
v := math32.Vector3{}
|
|
|
vi := vertices[0][x]
|
|
vi := vertices[0][x]
|
|
|
- Positions.GetVector3(3*vi, &v)
|
|
|
|
|
- Positions.AppendVector3(&v)
|
|
|
|
|
- Normals.Append(0, 1, 0)
|
|
|
|
|
- Uvs.AppendVector2(&uv1)
|
|
|
|
|
- indices = append(indices, uint32(nextidx))
|
|
|
|
|
|
|
+ positions.GetVector3(3*vi, &v)
|
|
|
|
|
+ positions.AppendVector3(&v)
|
|
|
|
|
+ normals.Append(0, 1, 0)
|
|
|
|
|
+ uvs.AppendVector2(&uv1)
|
|
|
|
|
+ indicesOrig = append(indicesOrig, uint32(nextidx))
|
|
|
nextidx++
|
|
nextidx++
|
|
|
}
|
|
}
|
|
|
// Appends copy of first vertex (center)
|
|
// Appends copy of first vertex (center)
|
|
|
var vertex, normal math32.Vector3
|
|
var vertex, normal math32.Vector3
|
|
|
var uv math32.Vector2
|
|
var uv math32.Vector2
|
|
|
- Positions.GetVector3(3*int(indices[0]), &vertex)
|
|
|
|
|
- Normals.GetVector3(3*int(indices[0]), &normal)
|
|
|
|
|
- Uvs.GetVector2(2*int(indices[0]), &uv)
|
|
|
|
|
- Positions.AppendVector3(&vertex)
|
|
|
|
|
- Normals.AppendVector3(&normal)
|
|
|
|
|
- Uvs.AppendVector2(&uv)
|
|
|
|
|
- indices = append(indices, uint32(nextidx))
|
|
|
|
|
|
|
+ positions.GetVector3(3*int(indicesOrig[0]), &vertex)
|
|
|
|
|
+ normals.GetVector3(3*int(indicesOrig[0]), &normal)
|
|
|
|
|
+ uvs.GetVector2(2*int(indicesOrig[0]), &uv)
|
|
|
|
|
+ positions.AppendVector3(&vertex)
|
|
|
|
|
+ normals.AppendVector3(&normal)
|
|
|
|
|
+ uvs.AppendVector2(&uv)
|
|
|
|
|
+ indicesOrig = append(indicesOrig, uint32(nextidx))
|
|
|
nextidx++
|
|
nextidx++
|
|
|
|
|
|
|
|
// Appends copy of second vertex (v1) USING LAST UV2
|
|
// Appends copy of second vertex (v1) USING LAST UV2
|
|
|
- Positions.GetVector3(3*int(indices[1]), &vertex)
|
|
|
|
|
- Normals.GetVector3(3*int(indices[1]), &normal)
|
|
|
|
|
- Positions.AppendVector3(&vertex)
|
|
|
|
|
- Normals.AppendVector3(&normal)
|
|
|
|
|
- Uvs.AppendVector2(&uv2)
|
|
|
|
|
- indices = append(indices, uint32(nextidx))
|
|
|
|
|
|
|
+ positions.GetVector3(3*int(indicesOrig[1]), &vertex)
|
|
|
|
|
+ normals.GetVector3(3*int(indicesOrig[1]), &normal)
|
|
|
|
|
+ positions.AppendVector3(&vertex)
|
|
|
|
|
+ normals.AppendVector3(&normal)
|
|
|
|
|
+ uvs.AppendVector2(&uv2)
|
|
|
|
|
+ indicesOrig = append(indicesOrig, uint32(nextidx))
|
|
|
nextidx++
|
|
nextidx++
|
|
|
|
|
|
|
|
- // Append faces indices
|
|
|
|
|
|
|
+ // Append faces indicesOrig
|
|
|
for x := 0; x < radialSegments; x++ {
|
|
for x := 0; x < radialSegments; x++ {
|
|
|
pos := 2 * x
|
|
pos := 2 * x
|
|
|
- i1 := indices[pos]
|
|
|
|
|
- i2 := indices[pos+1]
|
|
|
|
|
- i3 := indices[pos+3]
|
|
|
|
|
- Indices.Append(uint32(i1), uint32(i2), uint32(i3))
|
|
|
|
|
|
|
+ i1 := indicesOrig[pos]
|
|
|
|
|
+ i2 := indicesOrig[pos+1]
|
|
|
|
|
+ i3 := indicesOrig[pos+3]
|
|
|
|
|
+ indices.Append(uint32(i1), uint32(i2), uint32(i3))
|
|
|
}
|
|
}
|
|
|
// Second group is optional top cap of the cylinder
|
|
// Second group is optional top cap of the cylinder
|
|
|
- c.AddGroup(nextGroup, Indices.Size()-nextGroup, 1)
|
|
|
|
|
- nextGroup = Indices.Size()
|
|
|
|
|
|
|
+ c.AddGroup(nextGroup, indices.Size()-nextGroup, 1)
|
|
|
|
|
+ nextGroup = indices.Size()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Bottom cap
|
|
// Bottom cap
|
|
|
if bottom && radiusBottom > 0 {
|
|
if bottom && radiusBottom > 0 {
|
|
|
|
|
|
|
|
- // Array of vertex indices to build used to build the faces.
|
|
|
|
|
- indices := []uint32{}
|
|
|
|
|
- nextidx := Positions.Size() / 3
|
|
|
|
|
|
|
+ // Array of vertex indicesOrig to build used to build the faces.
|
|
|
|
|
+ indicesOrig := []uint32{}
|
|
|
|
|
+ nextidx := positions.Size() / 3
|
|
|
|
|
|
|
|
- // Appends top segments vertices and builds array of its indices
|
|
|
|
|
|
|
+ // Appends top segments vertices and builds array of its indicesOrig
|
|
|
var uv1, uv2, uv3 math32.Vector2
|
|
var uv1, uv2, uv3 math32.Vector2
|
|
|
for x := 0; x < radialSegments; x++ {
|
|
for x := 0; x < radialSegments; x++ {
|
|
|
- uv1 = uvs[heightSegments][x]
|
|
|
|
|
- uv2 = uvs[heightSegments][x+1]
|
|
|
|
|
|
|
+ uv1 = uvsOrig[heightSegments][x]
|
|
|
|
|
+ uv2 = uvsOrig[heightSegments][x+1]
|
|
|
uv3 = math32.Vector2{uv2.X, 1}
|
|
uv3 = math32.Vector2{uv2.X, 1}
|
|
|
// Appends CENTER with its own UV.
|
|
// Appends CENTER with its own UV.
|
|
|
- Positions.Append(0, float32(-heightHalf), 0)
|
|
|
|
|
- Normals.Append(0, -1, 0)
|
|
|
|
|
- Uvs.AppendVector2(&uv3)
|
|
|
|
|
- indices = append(indices, uint32(nextidx))
|
|
|
|
|
|
|
+ positions.Append(0, float32(-heightHalf), 0)
|
|
|
|
|
+ normals.Append(0, -1, 0)
|
|
|
|
|
+ uvs.AppendVector2(&uv3)
|
|
|
|
|
+ indicesOrig = append(indicesOrig, uint32(nextidx))
|
|
|
nextidx++
|
|
nextidx++
|
|
|
// Appends vertex
|
|
// Appends vertex
|
|
|
v := math32.Vector3{}
|
|
v := math32.Vector3{}
|
|
|
vi := vertices[heightSegments][x]
|
|
vi := vertices[heightSegments][x]
|
|
|
- Positions.GetVector3(3*vi, &v)
|
|
|
|
|
- Positions.AppendVector3(&v)
|
|
|
|
|
- Normals.Append(0, -1, 0)
|
|
|
|
|
- Uvs.AppendVector2(&uv1)
|
|
|
|
|
- indices = append(indices, uint32(nextidx))
|
|
|
|
|
|
|
+ positions.GetVector3(3*vi, &v)
|
|
|
|
|
+ positions.AppendVector3(&v)
|
|
|
|
|
+ normals.Append(0, -1, 0)
|
|
|
|
|
+ uvs.AppendVector2(&uv1)
|
|
|
|
|
+ indicesOrig = append(indicesOrig, uint32(nextidx))
|
|
|
nextidx++
|
|
nextidx++
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Appends copy of first vertex (center)
|
|
// Appends copy of first vertex (center)
|
|
|
var vertex, normal math32.Vector3
|
|
var vertex, normal math32.Vector3
|
|
|
var uv math32.Vector2
|
|
var uv math32.Vector2
|
|
|
- Positions.GetVector3(3*int(indices[0]), &vertex)
|
|
|
|
|
- Normals.GetVector3(3*int(indices[0]), &normal)
|
|
|
|
|
- Uvs.GetVector2(2*int(indices[0]), &uv)
|
|
|
|
|
- Positions.AppendVector3(&vertex)
|
|
|
|
|
- Normals.AppendVector3(&normal)
|
|
|
|
|
- Uvs.AppendVector2(&uv)
|
|
|
|
|
- indices = append(indices, uint32(nextidx))
|
|
|
|
|
|
|
+ positions.GetVector3(3*int(indicesOrig[0]), &vertex)
|
|
|
|
|
+ normals.GetVector3(3*int(indicesOrig[0]), &normal)
|
|
|
|
|
+ uvs.GetVector2(2*int(indicesOrig[0]), &uv)
|
|
|
|
|
+ positions.AppendVector3(&vertex)
|
|
|
|
|
+ normals.AppendVector3(&normal)
|
|
|
|
|
+ uvs.AppendVector2(&uv)
|
|
|
|
|
+ indicesOrig = append(indicesOrig, uint32(nextidx))
|
|
|
nextidx++
|
|
nextidx++
|
|
|
|
|
|
|
|
// Appends copy of second vertex (v1) USING LAST UV2
|
|
// Appends copy of second vertex (v1) USING LAST UV2
|
|
|
- Positions.GetVector3(3*int(indices[1]), &vertex)
|
|
|
|
|
- Normals.GetVector3(3*int(indices[1]), &normal)
|
|
|
|
|
- Positions.AppendVector3(&vertex)
|
|
|
|
|
- Normals.AppendVector3(&normal)
|
|
|
|
|
- Uvs.AppendVector2(&uv2)
|
|
|
|
|
- indices = append(indices, uint32(nextidx))
|
|
|
|
|
|
|
+ positions.GetVector3(3*int(indicesOrig[1]), &vertex)
|
|
|
|
|
+ normals.GetVector3(3*int(indicesOrig[1]), &normal)
|
|
|
|
|
+ positions.AppendVector3(&vertex)
|
|
|
|
|
+ normals.AppendVector3(&normal)
|
|
|
|
|
+ uvs.AppendVector2(&uv2)
|
|
|
|
|
+ indicesOrig = append(indicesOrig, uint32(nextidx))
|
|
|
nextidx++
|
|
nextidx++
|
|
|
|
|
|
|
|
- // Appends faces indices
|
|
|
|
|
|
|
+ // Appends faces indicesOrig
|
|
|
for x := 0; x < radialSegments; x++ {
|
|
for x := 0; x < radialSegments; x++ {
|
|
|
pos := 2 * x
|
|
pos := 2 * x
|
|
|
- i1 := indices[pos]
|
|
|
|
|
- i2 := indices[pos+3]
|
|
|
|
|
- i3 := indices[pos+1]
|
|
|
|
|
- Indices.Append(uint32(i1), uint32(i2), uint32(i3))
|
|
|
|
|
|
|
+ i1 := indicesOrig[pos]
|
|
|
|
|
+ i2 := indicesOrig[pos+3]
|
|
|
|
|
+ i3 := indicesOrig[pos+1]
|
|
|
|
|
+ indices.Append(uint32(i1), uint32(i2), uint32(i3))
|
|
|
}
|
|
}
|
|
|
// Third group is optional bottom cap of the cylinder
|
|
// Third group is optional bottom cap of the cylinder
|
|
|
- c.AddGroup(nextGroup, Indices.Size()-nextGroup, 2)
|
|
|
|
|
|
|
+ c.AddGroup(nextGroup, indices.Size()-nextGroup, 2)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- c.SetIndices(Indices)
|
|
|
|
|
- c.AddVBO(gls.NewVBO().AddAttrib(VertexPosition, 3).SetBuffer(Positions))
|
|
|
|
|
- c.AddVBO(gls.NewVBO().AddAttrib(VertexNormal, 3).SetBuffer(Normals))
|
|
|
|
|
- c.AddVBO(gls.NewVBO().AddAttrib(VertexTexcoord, 2).SetBuffer(Uvs))
|
|
|
|
|
|
|
+ c.SetIndices(indices)
|
|
|
|
|
+ c.AddVBO(gls.NewVBO(positions).AddAttrib(VertexPosition, 3))
|
|
|
|
|
+ c.AddVBO(gls.NewVBO(normals).AddAttrib(VertexNormal, 3))
|
|
|
|
|
+ c.AddVBO(gls.NewVBO(uvs).AddAttrib(VertexTexcoord, 2))
|
|
|
|
|
|
|
|
return c
|
|
return c
|
|
|
}
|
|
}
|