func prepareScene() { theTriangleBuf = glh.NewMeshBuffer( glh.RenderArrays, glh.NewIndexAttr(1, gl.UNSIGNED_INT, gl.STATIC_DRAW), glh.NewPositionAttr(3, gl.FLOAT, gl.STATIC_DRAW), ) theTriangleBuf.Add(triangleIdx, triangleVertex) }
// Create the glh.MeshBufer func createBuffer() *glh.MeshBuffer { if len(scene.Mesh[0].Colors) == 0 { // just add some colors to it. assimp.RandomColor(scene.Mesh[0]) } fmesh := assimp.NewFlatMesh(scene.Mesh[0]) println("FMesh vertex count: ", len(fmesh.Vertex)) var idxAttr *glh.Attr switch sz := len(fmesh.Index); { case sz < int(assimp.ByteSize): idxAttr = glh.NewIndexAttr(1, gl.UNSIGNED_BYTE, gl.STATIC_DRAW) case sz < int(assimp.ShortSize): idxAttr = glh.NewIndexAttr(1, gl.UNSIGNED_SHORT, gl.STATIC_DRAW) default: idxAttr = glh.NewIndexAttr(1, gl.UNSIGNED_INT, gl.STATIC_DRAW) } idxAttr = glh.NewIndexAttr(1, gl.UNSIGNED_INT, gl.STATIC_DRAW) // Create a mesh buffer with the given attributes. mb := glh.NewMeshBuffer( glh.RenderBuffered, // Indices. idxAttr, // Vertex positions have 3 components (x, y, z). glh.NewPositionAttr(3, gl.FLOAT, gl.STATIC_DRAW), // Colors have 4 components (r, g, b, a). glh.NewColorAttr(4, gl.FLOAT, gl.STATIC_DRAW), ) // Add the mesh to the buffer. mb.Add(fmesh.Index, fmesh.Vertex, fmesh.Color) return mb }
func createMeshBuffer() *glh.MeshBuffer { mb := glh.NewMeshBuffer( glh.RenderBuffered, glh.NewIndexAttr(1, gl.UNSIGNED_SHORT, gl.STATIC_DRAW), // 2 INTEGER values should be enough, but // just to keep things simpler // let's work with 3d vertex instead of 2d // it's easier to find help that way glh.NewPositionAttr(3, gl.FLOAT, gl.STATIC_DRAW), // At this moment, let's use colors instead of textures glh.NewColorAttr(3, gl.UNSIGNED_BYTE, gl.DYNAMIC_DRAW)) return mb }
// createBuffer creates a mesh buffer and fills it with data. func createBuffer() *glh.MeshBuffer { // Create a mesh buffer with the given attributes. mb := glh.NewMeshBuffer( glh.RenderBuffered, // 1 index per vertex. glh.NewIndexAttr(1, gl.UNSIGNED_SHORT, gl.STATIC_DRAW), // Vertex positions have 2 components (x, y). // These will never be changing, so mark them as static. glh.NewPositionAttr(2, gl.INT, gl.STATIC_DRAW), // Colors have 3 components (r, g, b). // These will be changing, so make them dynamic. glh.NewColorAttr(3, gl.UNSIGNED_BYTE, gl.DYNAMIC_DRAW), ) // Define components for a simple, coloured quad. idx := []uint16{0, 1, 2, 3} pos := []int32{0, 0, 0, 0, 0, 0, 0, 0} clr := []uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} for y := 0; y < Rows; y++ { py := int32(CellHeight * y) pos[1] = py + 1 pos[3] = py + 1 pos[5] = py + CellHeight pos[7] = py + CellHeight for x := 0; x < Cols; x++ { px := int32(CellWidth * x) pos[0] = px + 1 pos[2] = px + CellWidth pos[4] = px + CellWidth pos[6] = px + 1 palette := colors[rng.Int31n(6)] setColor(clr, palette[0], palette[1], palette[2]) mb.Add(idx, pos, clr) } } return mb }
func createBuffer() *glh.MeshBuffer { // We create as few vertices as possible. // Manually building a cube would require 24 vertices. Many of which // are duplicates. All we have to define here, is the 8 unique ones // necessary to construct each face of the cube. pos := []float32{ 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, } a := float32(0.02) // Each vertex comes with its own colour. clr := []float32{ 1, 0, 0, a, 0, 1, 0, a, 0, 0, 1, a, 1, 0, 1, a, 1, 1, 0, a, 0, 1, 1, a, 1, 1, 1, a, 0, 0, 0, a, } // These are the indices into the position and color lists. // They tell the GPU which position/color pair to use in order to construct // the whole cube. As can be seen, all elements are repeated multiple // times to create the correct layout. For large meshes with many duplicate // vertices, this can save a sizable amount of storage space. idx := []byte{ 0, 1, 2, 3, 4, 5, 6, 7, 3, 2, 5, 4, 7, 6, 1, 0, 2, 1, 6, 5, 0, 3, 4, 7, } // Create a mesh buffer with the given attributes. mb := glh.NewMeshBuffer( glh.RenderBuffered, // Indices. glh.NewIndexAttr(1, gl.UNSIGNED_BYTE, gl.STATIC_DRAW), // Vertex positions have 3 components (x, y, z). glh.NewPositionAttr(3, gl.FLOAT, gl.STATIC_DRAW), // Colors have 4 components (r, g, b, a). glh.NewColorAttr(4, gl.FLOAT, gl.STATIC_DRAW), ) // Add the mesh to the buffer. mb.Add(idx, pos, clr) return mb }