Explorar el Código

added BoundingBox to INode

Daniel Salvadori hace 7 años
padre
commit
f9fcf60e1c
Se han modificado 2 ficheros con 28 adiciones y 1 borrados
  1. 12 0
      core/node.go
  2. 16 1
      graphic/graphic.go

+ 12 - 0
core/node.go

@@ -16,6 +16,7 @@ type INode interface {
 	GetNode() *Node
 	UpdateMatrixWorld()
 	Raycast(*Raycaster, *[]Intersect)
+	BoundingBox() math32.Box3
 	Render(gs *gls.GLS)
 	Dispose()
 }
@@ -80,6 +81,17 @@ func (n *Node) GetNode() *Node {
 func (n *Node) Raycast(rc *Raycaster, intersects *[]Intersect) {
 }
 
+// BoundingBox satisfies the INode interface.
+func (n *Node) BoundingBox() math32.Box3 {
+
+	bbox := math32.Box3{n.position, n.position}
+	for _, inode := range n.Children() {
+		childBbox := inode.BoundingBox()
+		bbox.Union(&childBbox)
+	}
+	return bbox
+}
+
 // Render satisfies the INode interface.
 func (n *Node) Render(gs *gls.GLS) {
 }

+ 16 - 1
graphic/graphic.go

@@ -203,10 +203,25 @@ func (gr *Graphic) SetIGraphic(ig IGraphic) {
 	}
 }
 
+// BoundingBox recursively calculates and returns the bounding box
+// containing this node and all its children.
+func (gr *Graphic) BoundingBox() math32.Box3 {
+
+	geom := gr.igeom.GetGeometry()
+	bbox := geom.BoundingBox()
+	for _, inode := range gr.Children() {
+		childGraphic, ok := inode.(*Graphic)
+		if ok {
+			childBbox := childGraphic.BoundingBox()
+			bbox.Union(&childBbox)
+		}
+	}
+	return bbox
+}
+
 // CalculateMatrices calculates the model view and model view projection matrices.
 func (gr *Graphic) CalculateMatrices(gs *gls.GLS, rinfo *core.RenderInfo) {
 
-	// Calculate model view and model view projection matrices
 	mw := gr.MatrixWorld()
 	gr.mvm.MultiplyMatrices(&rinfo.ViewMatrix, &mw)
 	gr.mvpm.MultiplyMatrices(&rinfo.ProjMatrix, &gr.mvm)