func MarchingCubes(volume Volume, extents math3d.Vector, cubeSize float32) *render.Mesh { defer util.EndTiming(util.StartTiming("MarchingCubes(%v, %v, %v)", volume, extents, cubeSize)) var verticies []math3d.Vector var x, y, z float32 if cubeSize <= 0 { panic("Cube size was 0 or negative!") } // FIXME, seed at the start of the game r := rand.New(rand.NewSource(100)) finalMesh := new(render.Mesh) for x = 0; x < extents.X; x += cubeSize { for y = 0; y < extents.Y; y += cubeSize { for z = 0; z < extents.Z; z += cubeSize { verticies = marchCube(volume, x, y, z, cubeSize) for _, vertex := range verticies { finalMesh.VertexList = append(finalMesh.VertexList, vertex.X) finalMesh.VertexList = append(finalMesh.VertexList, vertex.Y) finalMesh.VertexList = append(finalMesh.VertexList, vertex.Z) finalMesh.ColorList = append(finalMesh.ColorList, r.Float32()) finalMesh.ColorList = append(finalMesh.ColorList, r.Float32()) finalMesh.ColorList = append(finalMesh.ColorList, r.Float32()) } } } } return finalMesh }
func (self *OpenGLRenderer) LoadMesh(mesh *render.Mesh) { if len(mesh.VertexList) == 0 { log.Println("WARNING Stopping load of mesh [", mesh.Name, "] with no verticies") return } vertexArrayObj := gl.GenVertexArray() vertexArrayObj.Bind() vertexBuffer := gl.GenBuffer() vertexBuffer.Bind(gl.ARRAY_BUFFER) gl.BufferData(gl.ARRAY_BUFFER, len(mesh.VertexList)*4, mesh.VertexList, gl.STATIC_DRAW) attribLoc := gl.AttribLocation(0) attribLoc.EnableArray() attribLoc.AttribPointer(3, gl.FLOAT, false, 0, nil) if len(mesh.UVList) > 0 { uvBuffer := gl.GenBuffer() uvBuffer.Bind(gl.ARRAY_BUFFER) gl.BufferData(gl.ARRAY_BUFFER, len(mesh.UVList)*4, mesh.UVList, gl.STATIC_DRAW) attribLoc := gl.AttribLocation(1) attribLoc.EnableArray() attribLoc.AttribPointer(2, gl.FLOAT, false, 0, nil) mesh.UVBuffer = uvBuffer } else if len(mesh.ColorList) > 0 { colorBuffer := gl.GenBuffer() colorBuffer.Bind(gl.ARRAY_BUFFER) gl.BufferData(gl.ARRAY_BUFFER, len(mesh.ColorList)*4, mesh.ColorList, gl.STATIC_DRAW) attribLoc := gl.AttribLocation(1) attribLoc.EnableArray() attribLoc.AttribPointer(3, gl.FLOAT, false, 0, nil) mesh.ColorBuffer = colorBuffer } if len(mesh.IndexList) > 0 { indexBuffer := gl.GenBuffer() indexBuffer.Bind(gl.ELEMENT_ARRAY_BUFFER) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(mesh.IndexList)*4, mesh.IndexList, gl.STATIC_DRAW) mesh.IndexBuffer = indexBuffer } mesh.VertexArrayObj = vertexArrayObj mesh.VertexBuffer = vertexBuffer }
// UnloadMesh clears out all of the buffers and the VAO for the given mesh func (self *OpenGLRenderer) UnloadMesh(mesh *render.Mesh) { if mesh.VertexArrayObj == nil { log.Println("WARNING Stopping unload of mesh [", mesh.Name, "] which seems to be already unloaded") return } self.deleteBuffer(mesh.VertexBuffer) mesh.VertexBuffer = nil self.deleteBuffer(mesh.ColorBuffer) mesh.ColorBuffer = nil self.deleteBuffer(mesh.UVBuffer) mesh.UVBuffer = nil self.deleteBuffer(mesh.IndexBuffer) mesh.IndexBuffer = nil mesh.VertexArrayObj.(gl.VertexArray).Delete() mesh.VertexArrayObj = nil }