board.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 texture
  5. import (
  6. "github.com/g3n/engine/gls"
  7. "github.com/g3n/engine/math32"
  8. )
  9. // NewBoard creates and returns a pointer to a new checker board 2D texture.
  10. // A checker board texture contains up to 4 different colors arranged in
  11. // the following order:
  12. // +------+------+
  13. // | | |
  14. // | c3 | c4 |
  15. // | | |
  16. // +------+------+
  17. // | | |
  18. // | c1 | c2 | height (pixels)
  19. // | | |
  20. // +------+------+
  21. // width
  22. // (pixels)
  23. //
  24. func NewBoard(width, height int, c1, c2, c3, c4 *math32.Color, alpha float32) *Texture2D {
  25. // Generates texture data
  26. data := make([]float32, width*height*4*4)
  27. colorData := func(sx, sy int, c *math32.Color) {
  28. for y := sy; y < sy+height; y++ {
  29. for x := sx; x < sx+width; x++ {
  30. pos := (x + y*2*width) * 4
  31. data[pos] = c.R
  32. data[pos+1] = c.G
  33. data[pos+2] = c.B
  34. data[pos+3] = alpha
  35. }
  36. }
  37. }
  38. colorData(0, 0, c1)
  39. colorData(width, 0, c2)
  40. colorData(0, height, c3)
  41. colorData(width, height, c4)
  42. // Creates, initializes and returns board texture object
  43. return NewTexture2DFromData(width*2, height*2, gls.RGBA, gls.FLOAT, gls.RGBA8, data)
  44. }