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