コード例 #1
0
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
}
コード例 #2
0
// 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
}