func octreeMesh(octree *gfx.Octant) *gfx.Mesh { m := new(gfx.Mesh) // Slice to zero. m.Vertices = m.Vertices[:0] m.Colors = m.Colors[:0] bci := -1 nextBC := func() gfx.Color { bci++ switch bci % 3 { case 0: return gfx.Color{1, 0, 0, 0} case 1: return gfx.Color{0, 1, 0, 0} case 2: return gfx.Color{0, 0, 1, 0} } panic("never here.") } var add func(o *gfx.Octant) add = func(o *gfx.Octant) { // Add vertices. before := len(m.Vertices) appendCube(m, float32(o.AABB.Size().X/2.0)) center := o.AABB.Center() for i, v := range m.Vertices[before:] { vert := v.Vec3() vert = vert.Add(center) m.Vertices[before+i] = gfx.ConvertVec3(vert) } fmt.Println("Octant", o.Depth, "-", len(o.Objects), "objects") for s := range o.Objects { before := len(m.Vertices) appendCube(m, float32(s.AABB().Size().X/2.0)) center := s.AABB().Center() //fmt.Println(center, s.AABB()) for i, v := range m.Vertices[before:] { vert := v.Vec3() vert = vert.Add(center) m.Vertices[before+i] = gfx.ConvertVec3(vert) } } for _, octant := range o.Octants { if octant != nil { add(octant) } } } add(octree) for _ = range m.Vertices { // Add barycentric coordinates. m.Colors = append(m.Colors, nextBC()) } return m }
func RTreeMesh(tree *rtree.Tree, level int) *gfx.Mesh { m := new(gfx.Mesh) bci := -1 nextBC := func() gfx.Color { bci++ switch bci % 3 { case 0: return gfx.Color{1, 0, 0, 0} case 1: return gfx.Color{0, 1, 0, 0} case 2: return gfx.Color{0, 0, 1, 0} } panic("never here.") } nodes := 0 var add func(n *rtree.Node) add = func(n *rtree.Node) { //if nodes > 100 { // return //} nodes++ if n == nil { return } if level == 0 || nodes == level { // Add vertices. m.Vertices = Rect3LineVerts(n.Bounds(), m.Vertices) } /*for _, s := range n.Objects { c := s.Bounds().Center() sz := 0.005 sb := math.Rect3{ Min: math.Vec3Zero.AddScalar(-sz).Add(c), Max: math.Vec3Zero.AddScalar(sz).Add(c), } m.Vertices = Rect3LineVerts(sb, m.Vertices) }*/ for _, c := range n.Children { add(c) } } add(tree.Root()) fmt.Println(tree.Count(), "objects", nodes, "nodes") for _ = range m.Vertices { // Add barycentric coordinates. m.Colors = append(m.Colors, nextBC()) } return m }
func appendCube(m *gfx.Mesh, scale float32) { addV := func(x, y, z float32) { m.Vertices = append(m.Vertices, gfx.Vec3{x, y, z}) } s := scale addV(-s, -s, -s) addV(-s, -s, s) addV(-s, s, s) addV(s, s, -s) addV(-s, -s, -s) addV(-s, s, -s) addV(s, -s, s) addV(-s, -s, -s) addV(s, -s, -s) addV(s, s, -s) addV(s, -s, -s) addV(-s, -s, -s) addV(-s, -s, -s) addV(-s, s, s) addV(-s, s, -s) addV(s, -s, s) addV(-s, -s, s) addV(-s, -s, -s) addV(-s, s, s) addV(-s, -s, s) addV(s, -s, s) addV(s, s, s) addV(s, -s, -s) addV(s, s, -s) addV(s, -s, -s) addV(s, s, s) addV(s, -s, s) addV(s, s, s) addV(s, s, -s) addV(-s, s, -s) addV(s, s, s) addV(-s, s, -s) addV(-s, s, s) addV(s, s, s) addV(-s, s, s) addV(s, -s, s) }
func NTreeMesh(tree *ntree.Tree, level int) *gfx.Mesh { m := new(gfx.Mesh) nodes := 0 var add func(n *ntree.Node) add = func(n *ntree.Node) { //if nodes > 100 { // return //} nodes++ if n == nil { return } if level == 0 || nodes == level { // Add vertices. m.Vertices = Rect3LineVerts(n.Bounds(), m.Vertices) } fmt.Println("node", nodes, len(n.Objects), "objects") for _, s := range n.Objects { c := s.Bounds().Center() sz := 0.005 sb := math.Rect3{ Min: math.Vec3Zero.AddScalar(-sz).Add(c), Max: math.Vec3Zero.AddScalar(sz).Add(c), } sb = s.Bounds() m.Vertices = Rect3LineVerts(sb, m.Vertices) } for _, c := range n.Children { add(c) } } add(tree.Root) fmt.Println(tree.Count(), "objects", nodes, "nodes\n\n\n") // Generate the barycentric coordinates. //m.GenerateBary() return m }