lines.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 graphic
  5. import (
  6. "github.com/g3n/engine/core"
  7. "github.com/g3n/engine/geometry"
  8. "github.com/g3n/engine/gls"
  9. "github.com/g3n/engine/material"
  10. "github.com/g3n/engine/math32"
  11. )
  12. // Lines is a Graphic which is rendered as a collection of independent lines.
  13. type Lines struct {
  14. Graphic // Embedded graphic object
  15. uniMVPm gls.Uniform // Model view projection matrix uniform location cache
  16. }
  17. // Init initializes the Lines object and adds the specified material.
  18. func (l *Lines) Init(igeom geometry.IGeometry, imat material.IMaterial) {
  19. l.Graphic.Init(igeom, gls.LINES)
  20. l.AddMaterial(l, imat, 0, 0)
  21. l.uniMVPm.Init("MVP")
  22. }
  23. // NewLines returns a pointer to a new Lines object.
  24. func NewLines(igeom geometry.IGeometry, imat material.IMaterial) *Lines {
  25. l := new(Lines)
  26. l.Init(igeom, imat)
  27. return l
  28. }
  29. // RenderSetup is called by the engine before drawing this geometry.
  30. func (l *Lines) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo) {
  31. // Transfer model view projection matrix uniform
  32. mvpm := l.ModelViewProjectionMatrix()
  33. location := l.uniMVPm.Location(gs)
  34. gs.UniformMatrix4fv(location, 1, false, &mvpm[0])
  35. }
  36. // Raycast satisfies the INode interface and checks the intersections
  37. // of this geometry with the specified raycaster.
  38. func (l *Lines) Raycast(rc *core.Raycaster, intersects *[]core.Intersect) {
  39. lineRaycast(l, rc, intersects, 2)
  40. }
  41. // Internal function used by raycasting for Lines and LineStrip.
  42. func lineRaycast(igr IGraphic, rc *core.Raycaster, intersects *[]core.Intersect, step int) {
  43. // Get the bounding sphere
  44. gr := igr.GetGraphic()
  45. geom := igr.GetGeometry()
  46. sphere := geom.BoundingSphere()
  47. // Transform bounding sphere from model to world coordinates and
  48. // checks intersection with raycaster
  49. matrixWorld := gr.MatrixWorld()
  50. sphere.ApplyMatrix4(&matrixWorld)
  51. if !rc.IsIntersectionSphere(&sphere) {
  52. return
  53. }
  54. // Copy ray and transform to model coordinates
  55. // This ray will will also be used to check intersects with
  56. // the geometry, as is much less expensive to transform the
  57. // ray to model coordinates than the geometry to world coordinates.
  58. var inverseMatrix math32.Matrix4
  59. var ray math32.Ray
  60. inverseMatrix.GetInverse(&matrixWorld)
  61. ray.Copy(&rc.Ray).ApplyMatrix4(&inverseMatrix)
  62. var vstart math32.Vector3
  63. var vend math32.Vector3
  64. var interSegment math32.Vector3
  65. var interRay math32.Vector3
  66. // Get geometry positions and indices buffers
  67. vboPos := geom.VBO(geometry.VertexPosition)
  68. if vboPos == nil {
  69. return
  70. }
  71. positions := vboPos.Buffer()
  72. indices := geom.Indices()
  73. precisionSq := rc.LinePrecision * rc.LinePrecision
  74. // Checks intersection with individual lines for indexed geometry
  75. if indices.Size() > 0 {
  76. for i := 0; i < indices.Size()-1; i += step {
  77. // Calculates distance from ray to this line segment
  78. a := indices[i]
  79. b := indices[i+1]
  80. positions.GetVector3(int(3*a), &vstart)
  81. positions.GetVector3(int(3*b), &vend)
  82. distSq := ray.DistanceSqToSegment(&vstart, &vend, &interRay, &interSegment)
  83. if distSq > precisionSq {
  84. continue
  85. }
  86. // Move back to world coordinates for distance calculation
  87. interRay.ApplyMatrix4(&matrixWorld)
  88. origin := rc.Ray.Origin()
  89. distance := origin.DistanceTo(&interRay)
  90. if distance < rc.Near || distance > rc.Far {
  91. continue
  92. }
  93. interSegment.ApplyMatrix4(&matrixWorld)
  94. *intersects = append(*intersects, core.Intersect{
  95. Distance: distance,
  96. Point: interSegment,
  97. Index: uint32(i),
  98. Object: igr,
  99. })
  100. }
  101. // Checks intersection with individual lines for NON indexed geometry
  102. } else {
  103. for i := 0; i < positions.Size()/3-1; i += step {
  104. positions.GetVector3(int(3*i), &vstart)
  105. positions.GetVector3(int(3*i+3), &vend)
  106. distSq := ray.DistanceSqToSegment(&vstart, &vend, &interRay, &interSegment)
  107. if distSq > precisionSq {
  108. continue
  109. }
  110. // Move back to world coordinates for distance calculation
  111. interRay.ApplyMatrix4(&matrixWorld)
  112. origin := rc.Ray.Origin()
  113. distance := origin.DistanceTo(&interRay)
  114. if distance < rc.Near || distance > rc.Far {
  115. continue
  116. }
  117. interSegment.ApplyMatrix4(&matrixWorld)
  118. *intersects = append(*intersects, core.Intersect{
  119. Distance: distance,
  120. Point: interSegment,
  121. Index: uint32(i),
  122. Object: igr,
  123. })
  124. }
  125. }
  126. }