-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.lua
131 lines (112 loc) · 3.09 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
--=========== Copyright © 2020, Planimeter, All rights reserved. ===========--
--
-- Purpose:
--
--==========================================================================--
local kazmath = require( "kazmath" )
local ffi = require( "ffi" )
local faces = {
"right",
"left",
"top",
"bottom",
"front",
"back"
}
local function getCubemap( filename, mipmapLevels )
local t = {}
for i = 0, mipmapLevels - 1 do
for j = 1, #faces do
local face = faces[ j ]
local filename = string.format( filename, face, i )
table.insert( t, { faces[ j ], filename } )
end
end
return t
end
function framework.load( arg )
-- Set glTF 2.0 physically-based rendering shader
framework.graphics.setShader( "gltfpbr" )
-- Create cube maps
diffuseCubemap = framework.graphics.newCubemap(
"diffuse",
getCubemap( "textures/papermill/diffuse/diffuse_%s_%u.jpg", 1 )
)
specularCubemap = framework.graphics.newCubemap(
"specular",
getCubemap( "textures/papermill/specular/specular_%s_%u.jpg", 10 )
)
-- View matrix
framework.graphics.lookAt(
0, 0, 4,
0, 0, 0,
0, 1, 0
)
-- Load scene
helmet = framework.graphics.newModel(
"models/DamagedHelmet/gltf/DamagedHelmet.gltf"
)
-- Set background color
framework.graphics.setBackgroundColor( { 51, 51, 51, 1 } )
-- Light
local rotation = math.rad( 75 )
local pitch = math.rad( 40 )
framework.graphics.setLightDirection( {
math.sin( rotation ) * math.cos( pitch ),
math.sin( pitch ),
math.cos( rotation ) * math.cos( pitch )
} )
end
function framework.update( dt )
end
--[[ ***** Mouse Controls ***** ]]
local mouseDown = false
local roll = math.pi
local pitch = 0
local translate = 4
function framework.draw()
local width, height = framework.graphics.getSize()
framework.graphics.setPerspectiveProjection( 45, width / height, 0.01, 100 )
-- set up the camera position and view matrix
framework.graphics.setCameraPosition( {
-translate * math.sin( roll ) * math.cos( -pitch ),
-translate * math.sin( -pitch ),
translate * math.cos( roll ) * math.cos( -pitch )
} )
-- Update view matrix
-- roll, pitch and translate are all globals.
local xRotation = ffi.new( "kmMat4" )
kazmath.kmMat4RotationY( xRotation, roll )
local yRotation = ffi.new( "kmMat4" )
kazmath.kmMat4RotationX( yRotation, pitch )
local mode = framework.graphics.getMatrixMode()
framework.graphics.setMatrixMode( "view" )
framework.graphics.push()
local mat4 = framework.graphics.getTransformation()
kazmath.kmMat4Multiply( mat4, yRotation, xRotation )
mat4.mat[14] = -translate
framework.graphics.draw( helmet )
framework.graphics.pop()
framework.graphics.setMatrixMode( mode )
end
function framework.mousepressed( x, y, button, istouch )
mouseDown = true
end
function framework.mousereleased( x, y, button, istouch )
mouseDown = false
end
function framework.mousemoved( x, y, dx, dy, istouch )
if ( not mouseDown ) then
return
end
roll = roll + ( dx / 100 )
pitch = pitch + ( dy / 100 )
end
local wheelSpeed = 1.04
function framework.wheelmoved( x, y )
if ( y > 0 ) then
translate = translate * wheelSpeed
else
translate = translate / wheelSpeed
end
end