forked from cloudwu/lua-bgfx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
04-mesh.lua
80 lines (68 loc) · 1.68 KB
/
04-mesh.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
local ant = require "ant"
local util = require "ant.util"
local math3d = require "ant.math"
local bgfx = require "bgfx"
canvas = iup.canvas {}
dlg = iup.dialog {
canvas,
title = "04-mesh",
size = "HALFxHALF",
}
local ctx = {}
local time = 0
local function mainloop()
math3d.reset()
bgfx.touch(0)
time = time + 0.01
local timev = math3d.vector():pack(time)
bgfx.set_uniform(ctx.u_time, timev)
local mtx = math3d.matrix():rotmat(0, time)
bgfx.set_transform(mtx)
bgfx.set_state(ctx.state)
util.meshSubmit(ctx.mesh, 0, ctx.prog)
bgfx.frame()
end
local function init(canvas)
ant.init { nwh = iup.GetAttributeData(canvas,"HWND") }
bgfx.set_view_clear(0, "CD", 0x303030ff, 1, 0)
-- bgfx.set_debug "ST"
ctx.prog = util.programLoad("vs_mesh", "fs_mesh")
ctx.mesh = util.meshLoad "meshes/bunny.bin"
ctx.u_time = bgfx.create_uniform("u_time", "v4")
ctx.state = bgfx.make_state {
WRITE_MASK = "RGBAZ",
DEPTH_TEST = "LESS",
CULL = "CCW",
MSAA = true,
}
ant.mainloop(mainloop)
end
function canvas:resize_cb(w,h)
if init then
init(self)
init = nil
end
ctx.width = w
ctx.height = h
bgfx.reset(w,h, "v")
local viewmat = math3d.matrix "view"
local projmat = math3d.matrix "proj"
viewmat:lookatp( 0,1,-2.5, 0,1,0)
projmat:projmat(60, ctx.width/ctx.height, 0.1, 100)
bgfx.set_view_transform(0, viewmat, projmat)
bgfx.set_view_rect(0, 0, 0, ctx.width, ctx.height)
end
function canvas:action(x,y)
mainloop()
end
dlg:showxy(iup.CENTER,iup.CENTER)
dlg.usersize = nil
-- to be able to run this script inside another context
if (iup.MainLoopLevel()==0) then
iup.MainLoop()
iup.Close()
bgfx.destroy(ctx.u_time)
bgfx.destroy(ctx.prog)
util.meshUnload(ctx.mesh)
ant.shutdown()
end