Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local vertices = {}
- -- Create a 101*101 grid of vertices covering a [-1;1]x[-1;1] rectangle
- for x = 0, 100 do
- for y = 0, 100 do
- vertices[x*101+y] = types.vertex(
- x/50-1, y/50-1, 0,
- x/100, y/100,
- 0, 0,
- argb8(x/100, y/100, 0, 1))
- end
- end
- local indices = {}
- -- Define a triangle list to fill the rectangle
- do
- local index = 0
- for x = 0, 99 do
- for y = 0, 99 do
- indices[index] = x*101+y
- indices[index+1] = (x+1)*101+y
- indices[index+2] = (x+1)*101+(y+1)
- indices[index+3] = x*101+y
- indices[index+4] = (x+1)*101+(y+1)
- indices[index+5] = x*101+(y+1)
- index = index + 6
- end
- end
- end
- -- Sends the vertex+index data to a MeshData object
- local function initmeshdata(meshdata)
- -- Call trylockback until success
- local mb = meshdata:lock()
- -- Create a DataBuffer object with sizeof(Vertex) * vertexcount bytes of data payload, the same amount of memory allocated and without a source buffer
- local vdb = databuffer:create(ffi.sizeof(types.vertex) * (#vertices+1), ffi.sizeof(types.vertex) * (#vertices+1), nil)
- -- Casts void* of the buffer to Vertex*
- local vptr = ffi.cast(types.pvertex, vdb:getdata())
- for i = 0, #vertices do
- vptr[i] = vertices[i]
- end
- mb:setvertexdata(vdb)
- local idb = databuffer:create(2 * (#indices+1), 2 * (#indices+1), nil)
- local iptr = ffi.cast('uint16_t*', idb:getdata())
- for i = 0, #indices do
- iptr[i] = indices[i]
- end
- mb:setindexdata(idb)
- -- Unlock the buffer and swap them immediately
- mb:release()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement