summaryrefslogtreecommitdiff
path: root/demo/vector/common.lua
blob: 4ae36f9a992df5124e6c3172c4b4dc5808644abf (plain)
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
honey.run = function()
	local gl = honey.gl
	local glfw = honey.glfw

	-- initialize opengl
	glfw.Init()
	--window.setHint(window.hintType.contextVersionMajor, 3)
	--window.setHint(window.hintType.contextVersionMinor, 3)
	
	local win = glfw.CreateWindow(640, 480, 'honey3d', glfw.monitor_NULL, glfw.window_NULL)
	glfw.MakeContextCurrent(win)
	glfw.win = win
	gl.InitGlad()
	gl.Enable(gl.DEPTH_TEST)

	if honey.init then
		honey.init()
	end

	glfw.SetFramebufferSizeCallback(win, function(_, width, height)
		if honey.windowSizeCallback then
			honey.windowSizeCallback(width, height)
		end
	end)

	local time = 0
	drawTime = 1/60
	while glfw.WindowShouldClose(win) == glfw.FALSE do
		local t = glfw.GetTime()
		local dt = t-time
		time = t

		honey.update(dt)
		glfw.PollEvents()

		if time > drawTime then
			if honey.clearColor then
				gl.ClearColor(
					honey.clearColor.r,
					honey.clearColor.g,
					honey.clearColor.b,
					1.0
				)
			else
				gl.ClearColor(0.2, 0.3, 0.3, 1.0)
			end
			gl.Clear(gl.COLOR_BUFFER_BIT + gl.DEPTH_BUFFER_BIT)
			honey.draw()
			glfw.SwapBuffers(win)
			drawTime = drawTime + 1/60
		end
	end

	--window.destroy(win)
	glfw.Terminate()
end


local set = honey.glm.vec3_set
local get = honey.glm.vec3_get
function honey.glm.setVector(vec, x, y, z)
	set(vec, 0, x)
	set(vec, 1, y)
	set(vec, 2, z)
end


function honey.glm.makeVector(x, y, z)
	local v = honey.glm.vec3()
	honey.glm.setVector(v, x, y, z)
	return v
end


function honey.glm.vec3_tostring(v)
	return string.format(
		'[%0.2f, %0.2f, %0.2f]',
		get(v, 0), get(v, 1), get(v, 2)
	)
end