-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
353 lines (296 loc) · 8.34 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"runtime"
"runtime/pprof"
rl "github.com/gen2brain/raylib-go/raylib"
)
const (
downscaleFactor = 1
viewWidth = 800 / downscaleFactor
viewHeight = 600 / downscaleFactor
parallel = true
frameRate = 60
windowTitle = "gorender"
demoMode = true
)
func onOff(b bool) string {
if b {
return "ON"
}
return "OFF"
}
type options struct {
blockProfile string
cpuProfile string
memProfile string
trace string
}
func parseOptions() *options {
opts := &options{}
flag.StringVar(&opts.blockProfile, "blockprof", "", "write block profile to file")
flag.StringVar(&opts.cpuProfile, "cpuprof", "", "write cpu profile to file")
flag.StringVar(&opts.memProfile, "memprof", "", "write memory profile to file")
flag.StringVar(&opts.trace, "trace", "", "write trace to file")
flag.Parse()
return opts
}
func drawText(x, y int32, text string) {
rl.DrawText(text, x+1, y+1, 10, rl.Black)
rl.DrawText(text, x, y, 10, rl.White)
}
func main() {
opts := parseOptions()
if flag.NArg() == 0 {
log.Fatalf("usage: %s [options] filename.obj", os.Args[0])
}
if opts.blockProfile != "" {
runtime.SetBlockProfileRate(1)
f, err := os.Create(opts.blockProfile)
if err != nil {
log.Fatalf("failed to create block profile: %s", err)
}
defer func() {
_ = pprof.Lookup("block").WriteTo(f, 0)
_ = f.Close()
}()
}
if opts.cpuProfile != "" {
f, err := os.Create(opts.cpuProfile)
if err != nil {
log.Fatalf("failed to create CPU profile: %s", err)
}
defer func() {
_ = f.Close()
}()
if err := pprof.StartCPUProfile(f); err != nil {
log.Printf("[ERROR] failed to start CPU profile: %s", err)
}
defer pprof.StopCPUProfile()
}
if opts.memProfile != "" {
f, err := os.Create(opts.memProfile)
if err != nil {
log.Fatalf("failed to create memory profile: %s", err)
}
defer func() {
_ = f.Close()
}()
defer func() {
if err := pprof.WriteHeapProfile(f); err != nil {
log.Printf("[ERROR] failed to write memory profile: %s", err)
}
}()
}
if opts.trace != "" {
f, err := os.Create(opts.trace)
if err != nil {
log.Fatalf("failed to create trace file: %s", err)
}
defer func() {
_ = f.Close()
}()
if err := runtime.StartTrace(); err != nil {
log.Fatalf("failed to start trace: %s", err)
}
defer func() {
log.Printf("[INFO] stopping trace")
runtime.StopTrace()
log.Printf("[INFO] trace stopped")
}()
go func() {
for {
data := runtime.ReadTrace()
if data == nil {
break
}
if _, err := f.Write(data); err != nil {
log.Printf("[ERROR] failed to write trace: %s", err)
}
}
}()
}
fb := NewFrameBuffer(viewWidth, viewHeight)
renderer := NewRenderer(fb)
filename := flag.Arg(0)
var (
scene *Scene
err error
)
switch path.Ext(filename) {
case ".obj":
meshes, err := LoadMeshFile(filename, false)
if err != nil {
log.Fatalf("failed to load mesh file: %s", err)
}
scene = &Scene{}
for i := range meshes {
object := NewObject(meshes[i])
scene.Objects = append(scene.Objects, object)
}
case ".json":
scene, err = LoadSceneFile(filename)
if err != nil {
log.Fatalf("failed to load scene file: %s", err)
}
default:
log.Fatalf("unsupported file format: %s", path.Ext(filename))
}
var (
windowWidth = int32(fb.Width * downscaleFactor)
windowHeight = int32(fb.Height * downscaleFactor)
numVertices = scene.NumVertices()
numTriangles = scene.NumTriangles()
oumObjects = scene.NumObjects()
)
rl.SetTraceLogLevel(rl.LogError) // Make raylib less verbose
rl.InitWindow(windowWidth, windowHeight, windowTitle)
defer rl.CloseWindow()
rl.SetTargetFPS(frameRate)
renderTexture := rl.LoadRenderTexture(int32(fb.Width), int32(fb.Height))
defer rl.UnloadRenderTexture(renderTexture)
camera := &Camera{
Direction: Vec3{0, 0, -1},
Position: Vec3{0, 0, 5},
Up: Vec3{0, 1, 0},
}
triggerDraw := make(chan struct{})
frameReady := make(chan struct{})
go func() {
for {
<-triggerDraw
cameraCopy := *camera // to prevent updating camera mid-frame
renderer.Draw(scene.Objects, &cameraCopy)
frameReady <- struct{}{}
}
}()
triggerDraw <- struct{}{}
if !demoMode {
rl.DisableCursor()
}
var (
lastCursorX = rl.GetMouseX()
lastCursorY = rl.GetMouseY()
)
for !rl.WindowShouldClose() {
<-frameReady
fb.SwapBuffers()
framesPerSecond := int(rl.GetFPS())
trianglesPerFrame := renderer.TPF
trianglesPerSecond := (trianglesPerFrame * framesPerSecond) / 1000
triggerDraw <- struct{}{}
if demoMode {
for _, obj := range scene.Objects {
obj.Rotation.Y += 0.01
}
}
forward := camera.Direction.Normalize()
//forward.Y = 0 // Only move in the XZ plane
right := forward.CrossProduct(camera.Up).Normalize()
switch {
// WASD keys to move the camera
case rl.IsKeyDown(rl.KeyW):
camera.Position = camera.Position.Add(forward.Multiply(0.15))
case rl.IsKeyDown(rl.KeyS):
camera.Position = camera.Position.Sub(forward.Multiply(0.15))
case rl.IsKeyDown(rl.KeyA):
camera.Position = camera.Position.Sub(right.Multiply(0.15))
case rl.IsKeyDown(rl.KeyD):
camera.Position = camera.Position.Add(right.Multiply(0.15))
case rl.IsKeyDown(rl.KeyUp):
camera.Position.Y += 0.05
case rl.IsKeyDown(rl.KeyDown):
camera.Position.Y -= 0.05
// Render options
case rl.IsKeyPressed(rl.KeyB):
renderer.BackfaceCulling = !renderer.BackfaceCulling
case rl.IsKeyPressed(rl.KeyE):
renderer.ShowEdges = !renderer.ShowEdges
case rl.IsKeyPressed(rl.KeyF):
renderer.ShowFaces = !renderer.ShowFaces
case rl.IsKeyPressed(rl.KeyV):
renderer.ShowVertices = !renderer.ShowVertices
case rl.IsKeyPressed(rl.KeyL):
renderer.Lighting = !renderer.Lighting
case rl.IsKeyPressed(rl.KeyX):
renderer.DebugEnabled = !renderer.DebugEnabled
case rl.IsKeyPressed(rl.KeyC):
renderer.FrustumClipping = !renderer.FrustumClipping
case rl.IsKeyPressed(rl.KeyT):
renderer.ShowTextures = !renderer.ShowTextures
case rl.IsKeyPressed(rl.KeyI):
renderer.FlatShading = !renderer.FlatShading
}
if !demoMode {
cursorX := rl.GetMouseX()
cursorY := rl.GetMouseY()
deltaX := cursorX - lastCursorX
deltaY := cursorY - lastCursorY
if deltaX != 0 || deltaY != 0 {
yaw := -float32(deltaX) * 0.002
pitch := -float32(deltaY) * 0.002
yawQuaternion := NewQuaternionFromAxisAngle(camera.Up, yaw)
pitchQuaternion := NewQuaternionFromAxisAngle(right, pitch)
camera.Direction = yawQuaternion.Rotate(camera.Direction).Normalize()
camera.Direction = pitchQuaternion.Rotate(camera.Direction).Normalize()
}
lastCursorX = cursorX
lastCursorY = cursorY
}
// Copy the frame buffer to the render texture
rl.BeginTextureMode(renderTexture)
rl.UpdateTexture(renderTexture.Texture, fb.Pixels2)
rl.EndTextureMode()
// Draw the render texture to the screen
rl.BeginDrawing()
rl.DrawTexturePro(
renderTexture.Texture,
rl.NewRectangle(0, 0, float32(fb.Width), float32(fb.Height)),
rl.NewRectangle(0, 0, float32(fb.Width*downscaleFactor), float32(fb.Height*downscaleFactor)),
rl.NewVector2(0, 0),
0,
rl.White,
)
drawText(5, 5, fmt.Sprintf("%d fps / %dk tps", framesPerSecond, trianglesPerSecond))
drawText(5, 15, fmt.Sprintf("objects: %d", oumObjects))
drawText(5, 25, fmt.Sprintf("vertices: %d", numVertices))
drawText(5, 35, fmt.Sprintf("triangles: %d", numTriangles))
drawText(
5,
windowHeight-15,
fmt.Sprintf(
"[V]erticies: %s [E]dges: %s [F]aces: %s, [L]ights: %s, [B]ackface culling: %s, [C]lipping: %s, [T]extures: %s, Flat Shad[i]ng: %s",
onOff(renderer.ShowVertices),
onOff(renderer.ShowEdges),
onOff(renderer.ShowFaces),
onOff(renderer.Lighting),
onOff(renderer.BackfaceCulling),
onOff(renderer.FrustumClipping),
onOff(renderer.ShowTextures),
onOff(renderer.FlatShading),
),
)
for _, info := range renderer.DebugInfo {
rl.DrawText(info.Text, int32(info.X*downscaleFactor)+1, int32(info.Y*downscaleFactor)+1, 12, rl.Black)
rl.DrawText(info.Text, int32(info.X*downscaleFactor), int32(info.Y*downscaleFactor), 12, rl.Yellow)
}
drawText(
5,
windowHeight-35,
fmt.Sprintf(
"X=%.2f Y=%.2f Z=%.2f RX=%.2f RY=%.2f RZ=%.2f",
camera.Position.X,
camera.Position.Y,
camera.Position.Z,
camera.Direction.X,
camera.Direction.Y,
camera.Direction.Z,
),
)
rl.EndDrawing()
}
}