summaryrefslogtreecommitdiff
path: root/demo/first_person/camera.lua
blob: 6898d2429e910538a71d854a363ecaabe7a49578 (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
local Camera = {}
local G = _G
local honey = honey
local math = math
local glm = honey.glm
setmetatable(Camera, {__index=_G})
setfenv(1, Camera)

local X = glm.makeVector(1, 0, 0)
local Y = glm.makeVector(0, 1, 0)
local Z = glm.makeVector(0, 0, 1)


--===== constructor =====--

function constructor(_, fov, ratio, near, far)
	local self = {}
	G.setmetatable(self, {__index=Camera})

	self.projection = glm.mat4()
	self.fov = fov
	self.near = near
	self.far = far
	glm.perspective(fov, ratio, near, far, self.projection)

	self.position = glm.vec3()
	self.forward = glm.vec3()
	self.up = Y
	self.right = glm.vec3()

	self.view = glm.mat4()
	glm.mat4_identity(self.view)

	return self
end
setmetatable(Camera, {__call=constructor})


--===== methods =====--

function translate(self, translation)
	local v = glm.vec3()
	glm.vec3_scale(self.forward, glm.vec3_get(translation, 0), v)
	glm.vec3_add(self.position, v, self.position)

	glm.vec3_scale(self.right, glm.vec3_get(translation, 2), v)
	glm.vec3_add(self.position, v, self.position)
end


function setAngles(self, yaw, pitch)
	local yawr = math.rad(yaw)
	local pitchr = math.rad(pitch)

	local x = math.cos(yawr) * math.cos(pitchr)
	local y = math.sin(pitchr)
	local z = math.sin(yawr) * math.cos(pitchr)

	glm.setVector(self.forward, x, y, z)
	glm.vec3_cross(self.forward, self.up, self.right)
	glm.vec3_normalize(self.forward)
	glm.vec3_normalize(self.right)

	G.print(yaw, pitch, glm.vec3_tostring(self.forward), glm.vec3_tostring(self.right))
end


function updateView(self)
	glm.look(self.position, self.forward, self.up, self.view)
end


function updateRatio(self, ratio)
	glm.perspective(self.fov, ratio, self.near, self.far, self.projection)
end


--===== fin =====--

return Camera