This repository has been archived by the owner on Nov 25, 2021. It is now read-only.
/
backend.go
273 lines (223 loc) · 6.06 KB
/
backend.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
Copyright (C) 2016 Andreas T Jonsson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"image"
"image/color"
_ "image/png"
"log"
"net/http"
"os"
"runtime/pprof"
"time"
"golang.org/x/net/websocket"
"github.com/andreas-jonsson/octatron/trace"
)
var closeFrameErr = errors.New("close-frame")
var (
messageCodec = websocket.Codec{Marshal: nil, Unmarshal: unmarshalMessage}
streamCodec = websocket.Codec{Marshal: marshalData, Unmarshal: nil}
)
var cameraMatrix = [6]lookAtCamera{
// pos, at, up
{trace.Vec3{0, 0, 0}, trace.Vec3{-1, 0, 0}, trace.Vec3{0, 1, 0}}, // Right
{trace.Vec3{0, 0, 0}, trace.Vec3{1, 0, 0}, trace.Vec3{0, 1, 0}}, // Left
{trace.Vec3{0, 0, 0}, trace.Vec3{0, 1, 0}, trace.Vec3{0, 0, -1}}, // Up
{trace.Vec3{0, 0, 0}, trace.Vec3{0, -1, 0}, trace.Vec3{0, 0, 1}}, // Down
{trace.Vec3{0, 0, 0}, trace.Vec3{0, 0, 1}, trace.Vec3{0, 1, 0}}, // Backward
{trace.Vec3{0, 0, 0}, trace.Vec3{0, 0, -1}, trace.Vec3{0, 1, 0}}, // Forward
}
var loadedTree struct {
maxDepth int
tree trace.Octree
}
type (
setupMessage struct {
Resolution int `resolution`
ClearColor [4]byte `clear_color`
}
updateMessage struct {
Position [3]float32 `position`
}
lookAtCamera struct {
pos, at, up trace.Vec3
}
)
func (c *lookAtCamera) Up() trace.Vec3 {
return c.up
}
func (c *lookAtCamera) Position() trace.Vec3 {
return c.pos
}
func (c *lookAtCamera) LookAt() trace.Vec3 {
return c.at
}
func marshalData(v interface{}) ([]byte, byte, error) {
return v.([]byte), websocket.BinaryFrame, nil
}
func unmarshalMessage(data []byte, ty byte, v interface{}) error {
switch ty {
case websocket.CloseFrame:
return closeFrameErr
case websocket.TextFrame:
if err := json.Unmarshal(data, v); err != nil {
return err
}
return nil
default:
return errors.New("invalid frame type")
}
}
func loadTree(file string) error {
treeFp, err := os.Open(file)
if err != nil {
return err
}
defer treeFp.Close()
log.Println("loading octree:", file)
tree, vpa, err := trace.LoadOctree(treeFp)
if err != nil {
return err
}
loadedTree.maxDepth = trace.TreeWidthToDepth(vpa)
loadedTree.tree = tree
return nil
}
func renderServer(ws *websocket.Conn) {
addr := ws.RemoteAddr()
log.Println("new connection:", addr)
defer func() { log.Println(addr, "was disconnected") }()
// Setup watchdog.
shutdownWatch := make(chan struct{}, 1)
defer func() { shutdownWatch <- struct{}{} }()
go func() {
select {
case <-shutdownWatch:
case <-time.After(time.Duration(arguments.timeout) * time.Minute):
log.Println("session timeout")
ws.Close()
}
}()
var setup setupMessage
if err := messageCodec.Receive(ws, &setup); err != nil {
log.Println(err)
return
}
log.Println(setup)
if setup.Resolution < 16 || setup.Resolution > 4096 {
log.Println("invalid setup")
return
}
var (
surfaces [6]*image.RGBA
raytracers [6]*trace.Raytracer
cameras [6]lookAtCamera
rect = image.Rect(0, 0, setup.Resolution, setup.Resolution)
)
for i, _ := range surfaces {
surfaces[i] = image.NewRGBA(rect)
cfg := trace.Config{
FieldOfView: 1.55,
TreeScale: 1,
ViewDist: float32(arguments.viewDistance),
Images: [2]*image.RGBA{surfaces[i], nil},
Jitter: false,
MultiThreaded: true,
}
raytracers[i] = trace.NewRaytracer(cfg)
clear := setup.ClearColor
raytracers[i].SetClearColor(color.RGBA{clear[0], clear[1], clear[2], clear[3]})
cameras[i] = cameraMatrix[i]
}
updateChan := make(chan updateMessage, 2)
go func() {
var update updateMessage
for {
if err := messageCodec.Receive(ws, &update); err != nil {
log.Println(err)
return
}
// TODO Verify message.
updateChan <- update
}
}()
for {
update := <-updateChan
for i := 0; i < 6; i++ {
camera := cameras[i]
camera.pos = update.Position
camera.at[0] += camera.pos[0]
camera.at[1] += camera.pos[1]
camera.at[2] += camera.pos[2]
raytracers[i].Trace(&camera, loadedTree.tree, loadedTree.maxDepth)
}
for _, rt := range raytracers {
if err := streamCodec.Send(ws, rt.Image(0).Pix); err != nil {
log.Println(err)
return
}
}
}
}
var arguments struct {
web,
tree string
pprof bool
port,
timeout uint
viewDistance float64
}
func init() {
flag.Usage = func() {
fmt.Printf("Usage: program [options]\n\n")
flag.PrintDefaults()
}
flag.StringVar(&arguments.web, "web", "cmd/cm-raytracer/frontend", "web frontend location")
flag.StringVar(&arguments.tree, "tree", "tree.oct", "octree to serve clients")
flag.BoolVar(&arguments.pprof, "pprof", false, "enables cpu profiler and pprof over http, port 6060")
flag.UintVar(&arguments.port, "port", 8080, "server port")
flag.UintVar(&arguments.timeout, "timeout", 30, "max session length in minutes")
flag.Float64Var(&arguments.viewDistance, "dist", 1, "max view-distance")
}
func main() {
flag.Parse()
if arguments.pprof {
log.Println("pprof enabled")
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
fp, err := os.Create("backend.pprof")
if err != nil {
panic(err)
}
defer fp.Close()
pprof.StartCPUProfile(fp)
defer pprof.StopCPUProfile()
}
if err := loadTree(arguments.tree); err != nil {
log.Println(err)
os.Exit(-1)
}
http.Handle("/", http.FileServer(http.Dir(arguments.web)))
http.Handle("/render", websocket.Handler(renderServer))
log.Println("waiting for connections...")
if err := http.ListenAndServe(fmt.Sprintf(":%v", arguments.port), nil); err != nil {
log.Println(err)
os.Exit(-1)
}
}