forked from g3n/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.go
50 lines (41 loc) · 1.22 KB
/
grid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package helper
import (
"github.com/adamlenda/engine/geometry"
"github.com/adamlenda/engine/gls"
"github.com/adamlenda/engine/graphic"
"github.com/adamlenda/engine/material"
"github.com/adamlenda/engine/math32"
)
// Grid is a visual representation of a grid.
type Grid struct {
graphic.Lines
}
// NewGrid creates and returns a pointer to a new grid helper with the specified size and step.
func NewGrid(size, step float32, color *math32.Color) *Grid {
grid := new(Grid)
half := size / 2
positions := math32.NewArrayF32(0, 0)
for i := -half; i <= half; i += step {
positions.Append(
-half, 0, i, color.R, color.G, color.B,
half, 0, i, color.R, color.G, color.B,
i, 0, -half, color.R, color.G, color.B,
i, 0, half, color.R, color.G, color.B,
)
}
// Create geometry
geom := geometry.NewGeometry()
geom.AddVBO(
gls.NewVBO(positions).
AddAttrib(gls.VertexPosition).
AddAttrib(gls.VertexColor),
)
// Create material
mat := material.NewBasic()
// Initialize lines with the specified geometry and material
grid.Lines.Init(geom, mat)
return grid
}