MCPcopy
hub / github.com/g3n/engine / NewSegmentedPlane

Function NewSegmentedPlane

geometry/plane.go:20–76  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
20func 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}

Callers 1

NewPlaneFunction · 0.85

Calls 9

AppendMethod · 0.95
AppendMethod · 0.95
SetIndicesMethod · 0.95
AddVBOMethod · 0.95
NewArrayF32Function · 0.92
NewArrayU32Function · 0.92
NewVBOFunction · 0.92
NewGeometryFunction · 0.85
AddAttribMethod · 0.45

Tested by

no test coverage detected