-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
161 lines (133 loc) · 4.07 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
local g = lovr.graphics
local width = 50
local height = 50
local vertices = {}
local previousPositions = {}
local connections = {}
local triangleIndices = {}
local points
function lovr.load()
local gridSize = lovr.headset.isPresent() and 2 or 2
local originY = lovr.headset.isPresent() and 1 or 0
-- TODO 2d table?
for j = 1, height do
local v = j / height
for i = 1, width do
local u = i / width
local x = (u - .5) * gridSize
local y = (v - .5) * gridSize + originY
local z = 0
table.insert(connections, { -1, -1, -1, -1 })
local n = #connections
local lastConnection = connections[n]
if j ~= height then
if i ~= 1 then lastConnection[1] = n - 1 end
if j ~= 1 then lastConnection[2] = n - width end
if i ~= width then lastConnection[3] = n + 1 end
if j ~= height then lastConnection[4] = n + width end
end
local c1, c2, c3, c4 = unpack(lastConnection)
table.insert(vertices, {
x, y, z,
x, y, z,
c1, c2, c3, c4,
.33 + u * .667, -v
})
end
end
local function index(x, y)
return x + (y - 1) * width
end
for j = 1, height - 1 do
for i = 1, width - 1 do
table.insert(triangleIndices, index(i, j))
table.insert(triangleIndices, index(i + 1, j))
table.insert(triangleIndices, index(i + 1, j + 1))
table.insert(triangleIndices, index(i, j))
table.insert(triangleIndices, index(i + 1, j + 1))
table.insert(triangleIndices, index(i, j + 1))
end
end
local texFormat = {
{ 'position', 'float', 3 }
}
tex_position = g.newBuffer(texFormat, #vertices, 'points')
updateShader = g.newShader('updateVert.glsl', nil, { 'tf_position', 'tf_prev_position' })
renderShader = g.newShader([[
in vec2 texCoord;
out vec2 TexCoord;
void main() {
TexCoord = texCoord;
gl_Position = lovrProjection * lovrTransform * vec4(position, 1.0);
}
]], [[
uniform sampler2D cloth;
in vec2 TexCoord;
void main() {
color = texture(cloth, TexCoord);
}
]])
local format = {
{ 'position', 'float', 3 },
{ 'previousPosition', 'float', 3 },
{ 'connection', 'int', 4 },
{ 'texCoord', 'float', 2 }
}
points = g.newBuffer(format, vertices, 'points')
points:setDrawMode('points')
texture = g.newTexture('cloth.jpg')
points:setTexture(texture)
g.setShader(renderShader)
controller = lovr.headset.getController('left')
updateShader:send('rayPosition', { controller:getPosition() })
end
function lovr.update(dt)
--for j = 1, 5 do
local positions = {}
for i = 1, #vertices do
local p = { unpack(vertices[i], 1, 3) }
table.insert(positions, p)
end
tex_position:setVertices(positions)
local texture = g.newTexture(tex_position)
texture:bind()
updateShader:send('timestep', dt)
updateShader:send('rayPosition', { controller:getPosition() })
updateShader:send('trigger', controller:getAxis('trigger'))
g.setShader(updateShader)
local data = points:feedback()
for i = 1, #data, 6 do
local x, y, z = data[i], data[i + 1], data[i + 2]
local px, py, pz = data[i + 3], data[i + 4], data[i + 5]
local vertexIndex = math.floor(i / 6) + 1
local p = vertices[vertexIndex]
vertices[vertexIndex] = {
x, y, z,
px, py, pz,
p[7], p[8], p[9], p[10],
p[11], p[12]
}
end
points:setVertices(vertices)
--end
g.setShader(renderShader)
end
function lovr.draw()
-- Draw triangles!
g.setShader(renderShader)
g.setColor(255, 255, 255)
points:setDrawMode('triangles')
points:setVertexMap(triangleIndices)
points:draw()
-- Clear depth buffer otherwise the points don't show up!
g.clear(false, true)
-- Draw points!
g.setPointSize(16)
g.setColor(255, 255, 255)
points:setDrawMode('points')
points:setVertexMap()
--points:draw()
local x, y, z = controller:getPosition()
g.setColor(255, 255, 255)
g.cube('line', x, y, z, .2)
end