-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
238 lines (199 loc) · 5.37 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
-- title: 3D
-- author: Tom Tkacz, github.com/TomTkacz
-- desc: 3D rendering
-- site: https://www.thomastkacz.com
-- license: MIT License
-- script: lua
include "include.Lookup"
include "include.Utils"
include "include.LoadObjects"
include "class.Camera"
include "class.Pos3D"
include "class.Pos2D"
include "class.Rot3D"
include "class.Dir3D"
include "class.Size2D"
include "class.Ray"
include "class.Matrix"
include "class.Matrix4D"
include "class.Object3D"
include "debug.Profiler"
-- GLOBAL VALUES --
SCREEN_WIDTH=240
SCREEN_HEIGHT=136
MAP_BASE_ADDRESS=0x8000
MAP_SIZE_BYTES=32640
PI=3.1415927
TWO_PI=6.2831854
PI_OVER_TWO=1.57079635
WORLD_ORIGIN=Pos3D(0,0,0)
Z_BUFFER={}
DEBUG=true
-- SCENE COMPONENTS --
camera=Camera( Pos3D(0,0,0), Rot3D(0,0,0), Dir3D(0,0,1) )
viewport={
size=Size2D(SCREEN_WIDTH,SCREEN_HEIGHT),
fov=90,
}
function viewport:updateFocalDist()
self._focalDist = self.size.w / ( 2*math.tan(math.rad(self.fov)/2) )
self._vfov = 2 * math.atan( self.size.h, (2*self._focalDist) ) -- in radians
Matrix4D.screenProjectionMatrix[1][1],Matrix4D.screenProjectionMatrix[2][2]=self._focalDist,self._focalDist
end
viewport:updateFocalDist()
light={
pos=Pos3D(-5,6,5)
}
gmouse={
sensitivity=70,
}
scene={
lights={},
loadedObjects={},
activeObjects={},
get = function(id)
return scene.activeObjects[id]
end
}
-- METHODS --
function initializeZBuffer()
local huge = math.huge
for col=1,SCREEN_WIDTH do
if not Z_BUFFER[col] then Z_BUFFER[col] = {} end
local zcol = Z_BUFFER[col]
for row=1,SCREEN_HEIGHT do
zcol[row] = huge
end
end
end
function calculateMeshOrigin(mesh)
local xAvg,yAvg,zAvg=0,0,0
for _,triangle in pairs(mesh.triangles) do
for _,vertex in pairs(triangle.vertices) do
xAvg=xAvg+vertex.x
yAvg=yAvg+vertex.y
zAvg=zAvg+vertex.z
end
end
xAvg=xAvg/mesh.numberOfTriangles
yAvg=yAvg/mesh.numberOfTriangles
zAvg=zAvg/mesh.numberOfTriangles
return Pos3D(xAvg,yAvg,zAvg)
end
function getMeshRelativeToOrigin(mesh,origin)
newmesh=mesh
for t,triangle in ipairs(newmesh.triangles) do
for v,vertex in ipairs(triangle) do
vPos=Pos3D(table.unpack(vertex))
vPos=vPos-origin
newmesh.triangles[t][v]={vPos.x,vPos.y,vPos.z}
end
end
return newmesh
end
function translate3D(pos,dir,dist)
local newX = pos.x+(dir.x*dist)
local newY = pos.y+(dir.y*dist)
local newZ = pos.z+(dir.z*dist)
return Pos3D(newX,newY,newZ)
end
function distBetween3DPoints(p1,p2)
local delta = p1-p2
return math.sqrt(delta:getDotProduct(delta))
end
function dirBetween3DPoints(p1,p2)
local dist = distBetween3DPoints(p1,p2)
local dx = p2.x-p1.x
local dy = p2.y-p1.y
local dz = p2.z-p1.z
return Dir3D(dx/dist,dy/dist,dz/dist)
end
function getSurfaceNormal(p1,p2,p3)
local a = p2 - p1
local b = p3 - p1
local nX = a.y * b.z - a.z * b.y
local nY = a.z * b.x - a.x * b.z
local nZ = a.x * b.y - a.y * b.x
return Dir3D(nX,nY,nZ)
end
function getTriangleCircumcenter(pA,pB,pC)
local faceNormal = getSurfaceNormal(pA,pB,pC)
local abMidpoint = (pA+pB)/2
local abPerpDir = dirBetween3DPoints(pA,pB)
abPerpDir:rotateAboutAxis(faceNormal,PI_OVER_TWO)
local bcMidpoint = (pB+pC)/2
local bcPerpDir = dirBetween3DPoints(pB,pC)
bcPerpDir:rotateAboutAxis(faceNormal,PI_OVER_TWO)
local a = 0
local lastDifference = math.huge
while a < 5 do
local left = ( abPerpDir:getCrossProduct(bcPerpDir) ) * a
local right = ( bcMidpoint - abMidpoint ):getCrossProduct(bcPerpDir)
local difference = (left-right):getMagnitude()
if difference > lastDifference then break end
lastDifference = difference
a=a+0.01
end
return translate3D(abMidpoint,abPerpDir,a)
end
function updateMouseInfo()
if gmouse.x==nil then gmouse.x=0 end
if gmouse.y==nil then gmouse.y=0 end
if gmouse.previous==nil then gmouse.previous={} end
gmouse.previous.x=gmouse.x
gmouse.previous.y=gmouse.y
gmouse.previous.down=mouseDown
gmouse.x,gmouse.y,gmouse.down=mouse()
gmouse.deltaX=gmouse.x-gmouse.previous.x
gmouse.deltaY=gmouse.previous.y-gmouse.y
end
function renderScreen()
initializeZBuffer()
for _,obj in pairs(scene.activeObjects) do
obj:render()
end
end
-- MAIN LOOP --
t=0
frameStartTimeMilliseconds=0
frameEndTimeMilliseconds=0
fpsInterval=5
currentFPS=0
function TIC()
updateMouseInfo()
if t==0 then
camera:updateVectors()
camera:initalizeClippingPlanes()
camera:updateClippingPlanes()
loadObjects()
cube=Object3D("mesh","mips",Pos3D(0,-1,5),Rot3D(0,0,0),Dir3D(0,0,1),0.2)
if DEBUG then profiler.start() end
end
cls(0)
if btn(0) then camera.pos=translate3D(camera.pos,camera.dir,0.1) end --forward
if btn(1) then camera.pos=translate3D(camera.pos,camera.dir,-0.1) end --backward
if btn(2) then camera.pos=translate3D(camera.pos,camera.horizontalVector,0.1) end --right
if btn(3) then camera.pos=translate3D(camera.pos,camera.horizontalVector,-0.1) end --left
if btn(4) then
scene.get(cube).rot:rotate(0,0.1,0)
end
if gmouse.down then
physicalSpace = (gmouse.deltaX/SCREEN_WIDTH)*viewport.size.w*(gmouse.sensitivity/100)
camera:rotate( Rot3D(0,2*PI*(physicalSpace/viewport.size.w),0) )
end
camera:updateVectors()
camera:updateClippingPlanes()
renderScreen()
if DEBUG and t==10 then
profiler.stop()
trace(profiler.report(20))
exit()
end
if t%fpsInterval==0 then
frameEndTimeMilliseconds=time()
currentFPS=fpsInterval/((frameEndTimeMilliseconds-frameStartTimeMilliseconds)/1000)
frameStartTimeMilliseconds=time()
end
print("FPS:"..round(currentFPS,2))
t=t+1
end