-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.lua
97 lines (87 loc) · 2.71 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
class = require 'class'
--[[
local Material = require '3den.Material'
local LitMaterial = require '3den.LitMaterial'
local Shader = require '3den.Shader'
local MeshObject = require '3den.MeshObject'
local Camera = require '3den.Camera'
local Scene = require '3den.Scene'
local OBJLoader = require '3den.OBJLoader'
--]]
local Threeden = require '3den'
local tenball, light_ball, camera, scene, image_material, unlit_material, mesh
local fps
local sky_color = {0.2, 0.2, 0.2}
function love.load ()
love.window.setMode(800, 600, {
msaa = 16,
vsync = 0
})
love.mouse.setGrabbed (true)
love.mouse.setRelativeMode (true)
love.graphics.setDepthMode("lequal", true)
love.graphics.setMeshCullMode('back')
Threeden.Shader.init ()
image_material = Threeden.LitMaterial (
love.graphics.newImage ("textures/tenball.tga"),
nil,
nil,
{-3, 2, -1},
sky_color
)
unlit_material = Threeden.Material (Threeden.Shader.UNLIT,
{
image_texture = Threeden.Material.mapFromRGBA(1,1,1,1)
}
)
mesh = love.graphics.newMesh (Threeden.OBJLoader.VERTEX_FORMAT, Threeden.OBJLoader.load ("models/sphere_uv.obj"), "triangles")
tenball = Threeden.MeshObject ({0,0,-4}, {0, 0, 0}, {1,1,1}, mesh, image_material)
light_ball = Threeden.MeshObject ({-3,2,-1}, {0, 0, 0}, {0.2,0.2,0.2}, mesh, unlit_material)
camera = Threeden.Camera ({0,0,2}, {0, 0, 0})
scene = Threeden.Empty ()
scene:addChild (tenball)
scene:addChild (light_ball)
scene:addChild (camera)
end
function love.mousemoved (x, y, dx, dy, istouch)
camera.euler[2] = camera.euler[2] - 0.001 * dx
camera.euler[1] = camera.euler[1] - 0.001 * dy
end
function love.update (dt)
fps = math.floor(1 / dt)
tenball.euler[2] = tenball.euler[2] + math.pi * dt
tenball:updateMatrix ()
local x,y,z = 0,0,0
if love.keyboard.isDown('w') then
x = x+ -2 * math.sin (camera.euler[2]) * dt
z = z+ -2 * math.cos (camera.euler[2]) * dt
end
if love.keyboard.isDown('s') then
x = x+ 2 * math.sin (camera.euler[2]) * dt
z = z+ 2 * math.cos (camera.euler[2]) * dt
end
if love.keyboard.isDown('a') then
z = z+ 2 * math.sin (camera.euler[2]) * dt
x = x+ -2 * math.cos (camera.euler[2]) * dt
end
if love.keyboard.isDown('d') then
z = z+ -2 * math.sin (camera.euler[2]) * dt
x = x+ 2 * math.cos (camera.euler[2]) * dt
end
if love.keyboard.isDown('lshift') then
y = y+ -2 * dt
end
if love.keyboard.isDown('space') then
y = y+ 2 * dt
end
camera.position[1] = camera.position[1] + x
camera.position[2] = camera.position[2] + y
camera.position[3] = camera.position[3] + z
camera:updateMatrix ()
tenball.material.uniforms.light_position = light_ball.position
end
function love.draw ()
love.graphics.clear (unpack (sky_color))
love.graphics.print ("FPS: "..fps, 5, 5)
camera:drawObject (scene)
end