grid_helper.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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(positions).
  33. AddAttrib(gls.VertexPosition, 3).
  34. AddAttrib(gls.VertexColor, 3),
  35. )
  36. // Creates material
  37. mat := material.NewBasic()
  38. // Initialize lines with the specified geometry and material
  39. grid.Lines.Init(geom, mat)
  40. return grid
  41. }