Advertisement
Delfigamer

scripts/index-test/page.lua (excerpt)

Sep 20th, 2015
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.49 KB | None | 0 0
  1. local vertices = {}
  2. -- Create a 101*101 grid of vertices covering a [-1;1]x[-1;1] rectangle
  3. for x = 0, 100 do
  4.     for y = 0, 100 do
  5.         vertices[x*101+y] = types.vertex(
  6.             x/50-1, y/50-1, 0,
  7.             x/100, y/100,
  8.             0, 0,
  9.             argb8(x/100, y/100, 0, 1))
  10.     end
  11. end
  12.  
  13. local indices = {}
  14. -- Define a triangle list to fill the rectangle
  15. do
  16.     local index = 0
  17.     for x = 0, 99 do
  18.         for y = 0, 99 do
  19.             indices[index] = x*101+y
  20.             indices[index+1] = (x+1)*101+y
  21.             indices[index+2] = (x+1)*101+(y+1)
  22.             indices[index+3] = x*101+y
  23.             indices[index+4] = (x+1)*101+(y+1)
  24.             indices[index+5] = x*101+(y+1)
  25.             index = index + 6
  26.         end
  27.     end
  28. end
  29.  
  30. -- Sends the vertex+index data to a MeshData object
  31. local function initmeshdata(meshdata)
  32.     -- Call trylockback until success
  33.     local mb = meshdata:lock()
  34.     -- Create a DataBuffer object with sizeof(Vertex) * vertexcount bytes of data payload, the same amount of memory allocated and without a source buffer
  35.     local vdb = databuffer:create(ffi.sizeof(types.vertex) * (#vertices+1), ffi.sizeof(types.vertex) * (#vertices+1), nil)
  36.     -- Casts void* of the buffer to Vertex*
  37.     local vptr = ffi.cast(types.pvertex, vdb:getdata())
  38.     for i = 0, #vertices do
  39.         vptr[i] = vertices[i]
  40.     end
  41.     mb:setvertexdata(vdb)
  42.     local idb = databuffer:create(2 * (#indices+1), 2 * (#indices+1), nil)
  43.     local iptr = ffi.cast('uint16_t*', idb:getdata())
  44.     for i = 0, #indices do
  45.         iptr[i] = indices[i]
  46.     end
  47.     mb:setindexdata(idb)
  48.     -- Unlock the buffer and swap them immediately
  49.     mb:release()
  50. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement