-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.go
235 lines (213 loc) · 6.83 KB
/
scene.go
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
package main
import (
"./objects"
"./shaders"
"gonum.org/v1/gonum/mat"
"log"
"math"
"sync"
)
type Scene struct {
// TODO refactor type to func()*mat.Dense
projection, scale, translation *mat.Dense
objects []objects.VertexObject
shaders []shaders.Shader
camera Camera
updater func(width, height, fov float64)
nearClippingPlane, farClippingPlane float64
viewType ViewType
}
func (s Scene) addObject(o objects.VertexObject) {
s.objects = append(s.objects, o)
}
func (s Scene) drawObjects(canvas shaders.Canvas) {
screenspace := mat.NewDense(4, 4, nil)
fastDenseMatMul4x4By4x4(screenspace, s.translation, s.scale)
for _, object := range s.objects {
// FIXME z-normalization messing with faceVector?
objectMat := mat.NewDense(4, 4, []float64{
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
})
// FIXME don't do all these copies
// Could use .Product() but it seems like it kinda sucks and it's a pain in the butt to use
if len(object.Transformations) >= 2 {
a := mat.DenseCopyOf(objectMat)
b := mat.NewDense(4, 4, nil)
i := 0
for _, transformationMat := range object.Transformations {
if i%2 == 0 {
fastDenseMatMul4x4By4x4(b, transformationMat(), a)
} else {
fastDenseMatMul4x4By4x4(a, transformationMat(), b)
}
i++
}
if i%2 == 0 {
objectMat = a
} else {
objectMat = b
}
} else {
for _, transformationFunc := range object.Transformations {
fastDenseMatMul4x4By4x4(objectMat, transformationFunc(), mat.DenseCopyOf(objectMat))
}
}
switch s.viewType {
case ORTHOGRAPHIC:
case PERSPECTIVE:
fastDenseMatMul4x4By4x4(objectMat, s.projection, mat.DenseCopyOf(objectMat))
}
// Multithreading
var wg sync.WaitGroup
wg.Add(len(object.Faces))
for _, face := range object.Faces {
go func(face *objects.Triangle) {
defer wg.Done()
for i, vertex := range face.RawVertices {
fastDenseMatMul4x4By4x1(face.TransformedVertices[i], objectMat, vertex)
w := face.TransformedVertices[i].At(3, 0)
face.TransformedVertices[i].Set(0, 0, face.TransformedVertices[i].At(0, 0)/w)
face.TransformedVertices[i].Set(1, 0, face.TransformedVertices[i].At(1, 0)/w)
face.TransformedVertices[i].Set(2, 0, face.TransformedVertices[i].At(2, 0)/w)
}
if face.Visibility != objects.BOTH {
u := mat.NewDense(4, 1, nil)
fastDenseMatSub(u, face.TransformedVertices[1], face.TransformedVertices[0])
v := mat.NewDense(4, 1, nil)
fastDenseMatSub(v, face.TransformedVertices[2], face.TransformedVertices[1])
face.FaceNormal = mat.NewVecDense(3, []float64{
u.At(1, 0)*v.At(2, 0) - u.At(2, 0)*v.At(1, 0),
u.At(2, 0)*v.At(0, 0) - u.At(0, 0)*v.At(2, 0),
u.At(0, 0)*v.At(1, 0) - u.At(1, 0)*v.At(0, 0),
})
var faceVector *mat.VecDense
switch s.viewType {
case ORTHOGRAPHIC:
faceVector = mat.NewVecDense(3, []float64{
0, 0,
-(face.TransformedVertices[0].At(2, 0) + face.TransformedVertices[1].At(2, 0) + face.TransformedVertices[2].At(2, 0)) / 3,
})
case PERSPECTIVE:
faceVector = mat.NewVecDense(3, []float64{
(face.TransformedVertices[0].At(0, 0) + face.TransformedVertices[1].At(0, 0) + face.TransformedVertices[2].At(0, 0)) / 3,
(face.TransformedVertices[0].At(1, 0) + face.TransformedVertices[1].At(1, 0) + face.TransformedVertices[2].At(1, 0)) / 3,
(face.TransformedVertices[0].At(2, 0) + face.TransformedVertices[1].At(2, 0) + face.TransformedVertices[2].At(2, 0)) / 3,
})
}
if mat.Dot(face.FaceNormal, faceVector) < 0 {
if face.Visibility == objects.FRONT {
face.DoDraw = false
} else {
face.DoDraw = true
}
} else {
if face.Visibility == objects.BACK {
face.DoDraw = false
} else {
face.DoDraw = true
}
}
} else {
face.DoDraw = true
}
if face.DoDraw {
// We'll temporarily store copies of the vertices here
buffer := mat.NewDense(4, 1, nil)
for _, vertex := range face.TransformedVertices {
fastClone(buffer, vertex)
// FIXME remove copy
fastDenseMatMul4x4By4x1(vertex, screenspace, buffer)
w := vertex.At(3, 0)
vertex.Set(0, 0, vertex.At(0, 0)/w)
vertex.Set(1, 0, vertex.At(1, 0)/w)
vertex.Set(2, 0, vertex.At(2, 0)/w)
}
}
}(face)
}
wg.Wait()
// TODO don't draw if not within the clipping plane bounds
for _, shader := range s.shaders {
shader.Shade(object, canvas)
}
}
}
func (s Scene) PerspectiveTransformUpdate(width, height, fov float64) {
// TODO figure out where the clipping plane really should be
// Aspect ratio seems to break things for some reason, but everything works fine without it
//aspectRatio := width / height
top := math.Tan((fov/360*2*math.Pi)/2) * s.nearClippingPlane
bottom := -top
right := top
left := -top
s.projection.Set(0, 0, (2*s.nearClippingPlane)/(right-left))
s.projection.Set(0, 2, (right+left)/(right-left))
s.projection.Set(1, 1, (2*s.nearClippingPlane)/(top-bottom))
s.projection.Set(1, 2, (top+bottom)/(top-bottom))
s.projection.Set(2, 2, -(s.farClippingPlane+s.nearClippingPlane)/(s.farClippingPlane-s.nearClippingPlane))
s.projection.Set(2, 3, -(2*s.farClippingPlane*s.nearClippingPlane)/(s.farClippingPlane-s.nearClippingPlane))
}
func (s Scene) DisplayUpdate(width, height float64) {
// TODO set scale
s.translation.Set(0, 3, width/2)
s.translation.Set(1, 3, height/2)
}
func (s Scene) SceneUpdater(width, height, fov float64) {
s.PerspectiveTransformUpdate(width, height, fov)
s.DisplayUpdate(width, height)
}
// TODO improve with rawrowview?
// TODO improve with custom setter that doesn't check bounds?
func fastClone(receiver, a *mat.Dense) {
rLen, cLen := a.Dims()
for r := 0; r < rLen; r++ {
for c := 0; c < cLen; c++ {
receiver.Set(r, c, a.At(r, c))
}
}
}
func fastDenseMatSub(receiver, a, b *mat.Dense) {
if a == receiver || b == receiver {
log.Fatal("Receiver is also arg")
}
rLen, cLen := a.Dims()
for r := 0; r < rLen; r++ {
for c := 0; c < cLen; c++ {
receiver.Set(r, c, a.At(r, c)-b.At(r, c))
}
}
}
func fastDenseMatMul4x4By4x4(receiver, a, b *mat.Dense) {
if a == receiver || b == receiver {
log.Fatal("Receiver is also arg")
}
for r := 0; r <= 3; r++ {
for c := 0; c <= 3; c++ {
receiver.Set(r, c,
a.At(r, 0)*b.At(0, c)+
a.At(r, 1)*b.At(1, c)+
a.At(r, 2)*b.At(2, c)+
a.At(r, 3)*b.At(3, c))
}
}
}
func fastDenseMatMul4x4By4x1(receiver, a, b *mat.Dense) {
if a == receiver || b == receiver {
log.Fatal("Receiver is also arg")
}
for r := 0; r <= 3; r++ {
receiver.Set(r, 0,
a.At(r, 0)*b.At(0, 0)+
a.At(r, 1)*b.At(1, 0)+
a.At(r, 2)*b.At(2, 0)+
a.At(r, 3)*b.At(3, 0))
}
}
type ViewType uint8
const (
ORTHOGRAPHIC = 0
PERSPECTIVE = 1
)