grid_helper.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 util
  5. import (
  6. "github.com/g3n/engine/geometry"
  7. "github.com/g3n/engine/gls"
  8. "github.com/g3n/engine/graphic"
  9. "github.com/g3n/engine/material"
  10. "github.com/g3n/engine/math32"
  11. )
  12. // GridHelper is a visual representation of a grid.
  13. type GridHelper struct {
  14. graphic.Lines
  15. }
  16. // NewGridHelper creates and returns a pointer to a new grid help object
  17. // with the specified size and step.
  18. func NewGridHelper(size, step float32, color *math32.Color) *GridHelper {
  19. grid := new(GridHelper)
  20. half := size / 2
  21. positions := math32.NewArrayF32(0, 0)
  22. for i := -half; i <= half; i += step {
  23. positions.Append(
  24. -half, 0, i, color.R, color.G, color.B,
  25. half, 0, i, color.R, color.G, color.B,
  26. i, 0, -half, color.R, color.G, color.B,
  27. i, 0, half, color.R, color.G, color.B,
  28. )
  29. }
  30. // Creates geometry
  31. geom := geometry.NewGeometry()
  32. geom.AddVBO(
  33. gls.NewVBO(positions).
  34. AddAttrib(gls.VertexPosition).
  35. AddAttrib(gls.VertexColor),
  36. )
  37. // Creates material
  38. mat := material.NewBasic()
  39. // Initialize lines with the specified geometry and material
  40. grid.Lines.Init(geom, mat)
  41. return grid
  42. }