grid.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 helper
  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. // Grid is a visual representation of a grid.
  13. type Grid struct {
  14. graphic.Lines
  15. }
  16. // NewGrid creates and returns a pointer to a new grid helper with the specified size and step.
  17. func NewGrid(size, step float32, color *math32.Color) *Grid {
  18. grid := new(Grid)
  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. // Create geometry
  30. geom := geometry.NewGeometry()
  31. geom.AddVBO(
  32. gls.NewVBO(positions).
  33. AddAttrib(gls.VertexPosition).
  34. AddAttrib(gls.VertexColor),
  35. )
  36. // Create material
  37. mat := material.NewBasic()
  38. // Initialize lines with the specified geometry and material
  39. grid.Lines.Init(geom, mat)
  40. return grid
  41. }