lines.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. uniMVP 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.uniMVP.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. // Calculates model view projection matrix and updates uniform
  32. mw := l.MatrixWorld()
  33. var mvpm math32.Matrix4
  34. mvpm.MultiplyMatrices(&rinfo.ViewMatrix, &mw)
  35. mvpm.MultiplyMatrices(&rinfo.ProjMatrix, &mvpm)
  36. // Transfer mvpm uniform
  37. location := l.uniMVP.Location(gs)
  38. gs.UniformMatrix4fv(location, 1, false, &mvpm[0])
  39. }
  40. // Raycast satisfies the INode interface and checks the intersections
  41. // of this geometry with the specified raycaster
  42. func (l *Lines) Raycast(rc *core.Raycaster, intersects *[]core.Intersect) {
  43. lineRaycast(l, rc, intersects, 2)
  44. }
  45. // Internal function used by raycasting for Lines and LineStrip
  46. func lineRaycast(igr IGraphic, rc *core.Raycaster, intersects *[]core.Intersect, step int) {
  47. // Get the bounding sphere
  48. gr := igr.GetGraphic()
  49. geom := igr.GetGeometry()
  50. sphere := geom.BoundingSphere()
  51. // Transform bounding sphere from model to world coordinates and
  52. // checks intersection with raycaster
  53. matrixWorld := gr.MatrixWorld()
  54. sphere.ApplyMatrix4(&matrixWorld)
  55. if !rc.IsIntersectionSphere(&sphere) {
  56. return
  57. }
  58. // Copy ray and transform to model coordinates
  59. // This ray will will also be used to check intersects with
  60. // the geometry, as is much less expensive to transform the
  61. // ray to model coordinates than the geometry to world coordinates.
  62. var inverseMatrix math32.Matrix4
  63. var ray math32.Ray
  64. inverseMatrix.GetInverse(&matrixWorld)
  65. ray.Copy(&rc.Ray).ApplyMatrix4(&inverseMatrix)
  66. var vstart math32.Vector3
  67. var vend math32.Vector3
  68. var interSegment math32.Vector3
  69. var interRay math32.Vector3
  70. // Get geometry positions and indices buffers
  71. vboPos := geom.VBO("VertexPosition")
  72. if vboPos == nil {
  73. return
  74. }
  75. positions := vboPos.Buffer()
  76. indices := geom.Indices()
  77. precisionSq := rc.LinePrecision * rc.LinePrecision
  78. // Checks intersection with individual lines for indexed geometry
  79. if indices.Size() > 0 {
  80. for i := 0; i < indices.Size()-1; i += step {
  81. // Calculates distance from ray to this line segment
  82. a := indices[i]
  83. b := indices[i+1]
  84. positions.GetVector3(int(3*a), &vstart)
  85. positions.GetVector3(int(3*b), &vend)
  86. distSq := ray.DistanceSqToSegment(&vstart, &vend, &interRay, &interSegment)
  87. if distSq > precisionSq {
  88. continue
  89. }
  90. // Move back to world coordinates for distance calculation
  91. interRay.ApplyMatrix4(&matrixWorld)
  92. origin := rc.Ray.Origin()
  93. distance := origin.DistanceTo(&interRay)
  94. if distance < rc.Near || distance > rc.Far {
  95. continue
  96. }
  97. interSegment.ApplyMatrix4(&matrixWorld)
  98. *intersects = append(*intersects, core.Intersect{
  99. Distance: distance,
  100. Point: interSegment,
  101. Index: uint32(i),
  102. Object: igr,
  103. })
  104. }
  105. // Checks intersection with individual lines for NON indexed geometry
  106. } else {
  107. for i := 0; i < positions.Size()/3-1; i += step {
  108. positions.GetVector3(int(3*i), &vstart)
  109. positions.GetVector3(int(3*i+3), &vend)
  110. distSq := ray.DistanceSqToSegment(&vstart, &vend, &interRay, &interSegment)
  111. if distSq > precisionSq {
  112. continue
  113. }
  114. // Move back to world coordinates for distance calculation
  115. interRay.ApplyMatrix4(&matrixWorld)
  116. origin := rc.Ray.Origin()
  117. distance := origin.DistanceTo(&interRay)
  118. if distance < rc.Near || distance > rc.Far {
  119. continue
  120. }
  121. interSegment.ApplyMatrix4(&matrixWorld)
  122. *intersects = append(*intersects, core.Intersect{
  123. Distance: distance,
  124. Point: interSegment,
  125. Index: uint32(i),
  126. Object: igr,
  127. })
  128. }
  129. }
  130. }