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