-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
171 lines (157 loc) · 4.85 KB
/
main.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
package main
import (
"./display"
"./objects"
"./objects/transformations"
"./objects/transformations/animations"
"./shaders"
"gonum.org/v1/gonum/mat"
"image"
"image/color"
"time"
)
//const w = 1024
//const h = 512
const fov = 30
func main() {
//defer profile.Start(profile.CPUProfile, profile.ProfilePath(".")).Stop()
imageChannel := make(chan *image.RGBA, 60)
myDisplay := display.CreateDisplay(imageChannel)
myScene := Scene{
//projection: mat.NewDense(4, 4, []float64{
// 1, 0, 0, 0,
// 0, 1, 0, 0,
// 0, 0, 1, 0,
// 0, 0, 0, 1,
//}),
projection: mat.NewDense(4, 4, []float64{
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, -1, 0,
}),
scale: mat.NewDense(4, 4, []float64{
768, 0, 0, 0,
0, -768, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
}),
translation: mat.NewDense(4, 4, []float64{
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
}),
camera: Camera{
mat.NewVecDense(3, []float64{
0,
0,
-1,
}),
},
shaders: []shaders.Shader{
shaders.ShadeFacesInt{Colors: []*color.RGBA{
//{255, 0, 0, 255},
//{0, 255, 0, 255},
//{0, 0, 255, 255},
{43, 142, 198, 255},
{41, 35, 114, 255},
{181, 23, 37, 255},
{33, 48, 52, 255},
{163, 183, 190, 255},
}},
//shaders.ShadeEdges{Color: &color.RGBA{R: 0, G: 0, B: 0, A: 255}},
},
nearClippingPlane: -1, farClippingPlane: -10,
viewType: PERSPECTIVE,
}
myScene.updater = myScene.SceneUpdater
//tri := objects.ReadFromObj("objs/tri.obj")
//tri.Transformations = append(tri.Transformations,
// (&animations.SingleAxisSpin{
// Axis: mat.NewVecDense(3, []float64{
// 1, 1, 0,
// }),
// Operator: 50,
// GetClock: func() float64 {
// return float64(time.Now().UnixNano()) / 1e9
// }}).GetTransformation)
//tri.Transformations = append(tri.Transformations, (&transformations.Translation{X: 0, Y: -2, Z: -6}).GetTransformation)
//myScene.objects = append(myScene.objects, tri)
obj := objects.ReadFromObj("objs/bmw2.obj")
//obj.Transformations = append(obj.Transformations, (&transformations.Translation{0, -3, 0}).GetTransformation)
obj.Transformations = append(obj.Transformations,
(&animations.SingleAxisSpin{
Axis: mat.NewVecDense(3, []float64{
0, 1, 0,
}),
Operator: 10,
GetClock: func() float64 {
return float64(time.Now().UnixNano()) / 1e9
}}).GetTransformation)
//obj.Transformations = append(obj.Transformations, (transformations.RotationFromEulerAngles(0, math.Pi/7, 0)).GetTransformation)
obj.Transformations = append(obj.Transformations, (&transformations.Translation{X: 0, Y: -2, Z: -6}).GetTransformation)
myScene.objects = append(myScene.objects, obj)
//obj := objects.ReadFromObj("objs/fox.obj")
//obj.Transformations = append(obj.Transformations, (&transformations.Translation{0, -2, -10}).GetTransformation)
//obj.Transformations = append(obj.Transformations, (transformations.RotationFromAxisAngle(mat.NewVecDense(3, []float64{0, 1, 0}), math.Pi / 2)).GetTransformation)
//obj.Transformations = append(obj.Transformations, (&transformations.Translation{10, 0, 0}).GetTransformation)
//myScene.objects = append(myScene.objects, obj)
//planet1 := objects.ReadFromObj("objs/icosphere.obj")
//planet1.Transformations = append(planet1.Transformations, (&animations.SingleAxisSpin{
// mat.NewVecDense(3, []float64{0, 1, 0}),
// 10,
// func() float64 {
// return float64(time.Now().UnixNano()) / 1e9
// },
//}).GetTransformation)
//planet1.Transformations = append(planet1.Transformations, (&transformations.Translation{0, 0, -2}).GetTransformation)
//planet1.Transformations = append(planet1.Transformations, (&animations.SingleAxisSpin{
// mat.NewVecDense(3, []float64{0, 1, 0}),
// 10,
// func() float64 {
// return float64(time.Now().UnixNano()) / 1e9
// },
//}).GetTransformation)
//myScene.objects = append(myScene.objects, planet1)
//output, _ := os.Create("output.png")
go func() {
var lastTime time.Time
for {
lastTime = time.Now()
w, h := myDisplay.GetDimensions()
frame := genBlankCanvas(w, h)
zBuffer := genBlankZBuffer(w, h)
// TODO this probably doesn't actually need to occur every frame
myScene.updater(float64(w), float64(h), fov)
myScene.drawObjects(shaders.Canvas{
Image: frame,
ZBuffer: zBuffer,
UseZBuffer: true,
})
imageChannel <- frame
myDisplay.Refresh()
myDisplay.SetFrametime(float64(time.Since(lastTime).Nanoseconds() / 1e6))
//fmt.Printf("%d\n", time.Since(lastTime).Nanoseconds() / 1e6)
//png.Encode(output, frame)
}
}()
myDisplay.Start()
}
func genBlankCanvas(w, h int) *image.RGBA {
canvas := image.NewRGBA(image.Rect(0, 0, w, h))
for i := range canvas.Pix {
canvas.Pix[i] = 255
}
return canvas
}
func genBlankZBuffer(w, h int) [][]float64 {
zBuffer := make([][]float64, w)
for c := 0; c < w; c++ {
zBuffer[c] = make([]float64, h)
for r := 0; r < h; r++ {
zBuffer[c][r] = -1
}
}
return zBuffer
}