groverburger/g3d

Model directly from FFI data.

GoodClover opened this issue · 2 comments

I see that g3d has the capability to "compress" data using an FFI array rather than tables.
It would be great if this was exposed, so meshes could be generated directly to this format rather than requiring a table. (or, if this is already possible somehow, document it)

Creating the hundreds of tables needed for my meshes is very slow, and quickly eats up memory.

Copying the code for :compress, seems this is perfectly possible already. But still would be nice if it was exposed as a proper feature of the API.
Have to do a pre-run of my generation to get a vertex count, can't think of much of a way round that without a dynamic data structure. Luckily it's not run often and doesn't take too long anyways :)

function map:updatemesh()
	local vertcount = self:countverts()
	local data = love.data.newByteData(ffi.sizeof("struct vertex") * vertcount)
	local dp = ffi.cast("struct vertex *", data:getFFIPointer())
	
	-- Generate mesh into dp
	
	local model = g3d.newModel({{}}, terrain) -- {{}} is required here!
	self.model = model
	model.mesh:release()
	model.mesh = love.graphics.newMesh(model.vertexFormat, vertcount, "triangles")
	model.mesh:setVertices(data)
	model.mesh:setTexture(model.texture)
	model.verts = nil
end

I don't think I'll add this feature as it would make the codebase more complicated. One aspect of g3d that I try to preserve is the simplicity and readability of the codebase, trying to be as newbie friendly as possible.

If you would like to fork g3d and add this feature yourself, please do!