sphere.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package geometry
  5. import (
  6. "github.com/g3n/engine/gls"
  7. "github.com/g3n/engine/math32"
  8. "math"
  9. )
  10. type Sphere struct {
  11. Geometry
  12. Radius float64
  13. WidthSegments int
  14. HeightSegments int
  15. PhiStart float64
  16. PhiLength float64
  17. ThetaStart float64
  18. ThetaLength float64
  19. }
  20. func NewSphere(radius float64, widthSegments, heightSegments int, phiStart, phiLength, thetaStart, thetaLength float64) *Sphere {
  21. s := new(Sphere)
  22. s.Geometry.Init()
  23. s.Radius = radius
  24. s.WidthSegments = widthSegments
  25. s.HeightSegments = heightSegments
  26. s.PhiStart = phiStart
  27. s.PhiLength = phiLength
  28. s.ThetaStart = thetaStart
  29. thetaEnd := thetaStart + thetaLength
  30. vertexCount := (widthSegments + 1) * (heightSegments + 1)
  31. // Create buffers
  32. positions := math32.NewArrayF32(vertexCount*3, vertexCount*3)
  33. normals := math32.NewArrayF32(vertexCount*3, vertexCount*3)
  34. uvs := math32.NewArrayF32(vertexCount*2, vertexCount*2)
  35. indices := math32.NewArrayU32(0, vertexCount)
  36. index := 0
  37. vertices := make([][]uint32, 0)
  38. var normal math32.Vector3
  39. for y := 0; y <= heightSegments; y++ {
  40. verticesRow := make([]uint32, 0)
  41. v := float64(y) / float64(heightSegments)
  42. for x := 0; x <= widthSegments; x++ {
  43. u := float64(x) / float64(widthSegments)
  44. px := -radius * math.Cos(phiStart+u*phiLength) * math.Sin(thetaStart+v*thetaLength)
  45. py := radius * math.Cos(thetaStart+v*thetaLength)
  46. pz := radius * math.Sin(phiStart+u*phiLength) * math.Sin(thetaStart+v*thetaLength)
  47. normal.Set(float32(px), float32(py), float32(pz)).Normalize()
  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. for y := 0; y < heightSegments; y++ {
  57. for x := 0; x < widthSegments; x++ {
  58. v1 := vertices[y][x+1]
  59. v2 := vertices[y][x]
  60. v3 := vertices[y+1][x]
  61. v4 := vertices[y+1][x+1]
  62. if y != 0 || thetaStart > 0 {
  63. indices.Append(v1, v2, v4)
  64. }
  65. if y != heightSegments-1 || thetaEnd < math.Pi {
  66. indices.Append(v2, v3, v4)
  67. }
  68. }
  69. }
  70. s.SetIndices(indices)
  71. s.AddVBO(gls.NewVBO().AddAttrib("VertexPosition", 3).SetBuffer(positions))
  72. s.AddVBO(gls.NewVBO().AddAttrib("VertexNormal", 3).SetBuffer(normals))
  73. s.AddVBO(gls.NewVBO().AddAttrib("VertexTexcoord", 2).SetBuffer(uvs))
  74. //s.BoundingSphere = math32.NewSphere(math32.NewVector3(0, 0, 0), float32(radius))
  75. return s
  76. }