NewSegmentedPlane creates a segmented plane geometry with the specified width, height, and number of segments in each dimension (minimum 1 in each). The plane is generated centered in the XY plane with Z=0.
(width, height float32, widthSegments, heightSegments int)
| 18 | // NewSegmentedPlane creates a segmented plane geometry with the specified width, height, and number of |
| 19 | // segments in each dimension (minimum 1 in each). The plane is generated centered in the XY plane with Z=0. |
| 20 | func NewSegmentedPlane(width, height float32, widthSegments, heightSegments int) *Geometry { |
| 21 | |
| 22 | plane := NewGeometry() |
| 23 | |
| 24 | widthHalf := width / 2 |
| 25 | heightHalf := height / 2 |
| 26 | gridX := widthSegments |
| 27 | gridY := heightSegments |
| 28 | gridX1 := gridX + 1 |
| 29 | gridY1 := gridY + 1 |
| 30 | segmentWidth := width / float32(gridX) |
| 31 | segmentHeight := height / float32(gridY) |
| 32 | |
| 33 | // Create buffers |
| 34 | positions := math32.NewArrayF32(0, 16) |
| 35 | normals := math32.NewArrayF32(0, 16) |
| 36 | uvs := math32.NewArrayF32(0, 16) |
| 37 | indices := math32.NewArrayU32(0, 16) |
| 38 | |
| 39 | // Generate plane vertices, vertices normals and vertices texture mappings. |
| 40 | for iy := 0; iy < gridY1; iy++ { |
| 41 | y := float32(iy)*segmentHeight - heightHalf |
| 42 | for ix := 0; ix < gridX1; ix++ { |
| 43 | x := float32(ix)*segmentWidth - widthHalf |
| 44 | positions.Append(float32(x), float32(-y), 0) |
| 45 | normals.Append(0, 0, 1) |
| 46 | uvs.Append(float32(float64(ix)/float64(gridX)), float32(float64(1)-(float64(iy)/float64(gridY)))) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Generate plane vertices indices for the faces |
| 51 | for iy := 0; iy < gridY; iy++ { |
| 52 | for ix := 0; ix < gridX; ix++ { |
| 53 | a := ix + gridX1*iy |
| 54 | b := ix + gridX1*(iy+1) |
| 55 | c := (ix + 1) + gridX1*(iy+1) |
| 56 | d := (ix + 1) + gridX1*iy |
| 57 | indices.Append(uint32(a), uint32(b), uint32(d)) |
| 58 | indices.Append(uint32(b), uint32(c), uint32(d)) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | plane.SetIndices(indices) |
| 63 | plane.AddVBO(gls.NewVBO(positions).AddAttrib(gls.VertexPosition)) |
| 64 | plane.AddVBO(gls.NewVBO(normals).AddAttrib(gls.VertexNormal)) |
| 65 | plane.AddVBO(gls.NewVBO(uvs).AddAttrib(gls.VertexTexcoord)) |
| 66 | |
| 67 | // Update area |
| 68 | plane.area = width * height |
| 69 | plane.areaValid = true |
| 70 | |
| 71 | // Update volume |
| 72 | plane.volume = 0 |
| 73 | plane.volumeValid = true |
| 74 | |
| 75 | return plane |
| 76 | } |
no test coverage detected