This repository has been archived by the owner on May 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
220 lines (196 loc) · 4.71 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
package main
import (
"fmt"
"log"
"time"
tea "github.com/charmbracelet/bubbletea"
"golang.org/x/term"
)
type Render = struct {
img *RenderImg
// From 0 to 1
progress float32
stop bool
}
// Type to indicate there is a new frame that should be rendered
type NewFrameMsg struct{}
type Model struct {
// Data of the render of the current image
render *Render
scene Scene
cam Camera
mousex, mousey int
isClicking bool
}
func initialModel() *Model {
return &Model{
render: nil,
scene: Scene{
ambientLight: RGB{100, 100, 100},
backgroundColor: RGB{},
dirLight: DirLight{
dir: Vec3{-2, -1, 2}.Normalized(),
col: RGB{255, 255, 255},
},
spheres: []Sphere{
{
center: Vec3{-1.5, 1.2, 7},
radius: 1,
mat: Material{
color: RGB{255, 63, 63},
reflection: 0,
},
},
{
center: Vec3{1.5, 1.2, 5},
radius: 1,
mat: Material{
color: RGB{255, 63, 63},
reflection: 0.2,
},
},
},
planes: []Plane{
{
point: Vec3{0, 0, 0},
norm: Vec3Up,
mat: Material{
color: RGBFrom("#9090BA"),
reflection: 0.7,
},
},
},
},
cam: Camera{
origin: Vec3{0, 1.2, -1},
up: Vec3Up, //Vec3{-0.05, 1, 0.1}.Normalized(),
fw: Vec3Forward, //Vec3{-0.1, -0.2, 1}.Normalized(),
},
}
}
type retraceImageMsg struct{}
func RetraceImage() tea.Msg {
return retraceImageMsg{}
}
type checkRedrawMsg time.Time
func checkRedrawCmd() tea.Cmd {
return tea.Tick(time.Millisecond*10, func(t time.Time) tea.Msg {
return checkRedrawMsg(t)
})
}
func (m Model) Init() tea.Cmd {
return RetraceImage
}
func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
return m, tea.Quit
case "w":
m.cam.origin = m.cam.origin.Add(m.cam.fw.Scaled(0.5))
return m, m.RetraceImage()
case "a":
m.cam.origin = m.cam.origin.Add(m.cam.up.Cross(m.cam.fw).Scaled(-0.5))
return m, m.RetraceImage()
case "s":
m.cam.origin = m.cam.origin.Add(m.cam.fw.Scaled(-0.5))
return m, m.RetraceImage()
case "d":
m.cam.origin = m.cam.origin.Add(m.cam.up.Cross(m.cam.fw).Scaled(0.5))
return m, m.RetraceImage()
case "e":
m.cam.origin = m.cam.origin.Add(m.cam.up.Scaled(0.5))
return m, m.RetraceImage()
case "q":
m.cam.origin = m.cam.origin.Add(m.cam.up.Scaled(-0.5))
return m, m.RetraceImage()
}
case tea.MouseMsg:
switch msg.Action {
case tea.MouseActionPress:
m.isClicking = true
m.mousex = msg.X
m.mousey = msg.Y
return m, nil
case tea.MouseActionRelease:
m.isClicking = false
return m, nil
case tea.MouseActionMotion:
if !m.isClicking {
return m, nil
}
dx := msg.X - m.mousex
dy := msg.Y - m.mousey
m.cam.fw = m.cam.fw.RotatedAroundY(float64(dx) * 0.01)
m.cam.fw = m.cam.fw.RotatedAroundX(float64(dy) * 0.01)
// This is not really correct, we should rotate it around
// cam.up.Cross(cam.fw)
m.cam.up = m.cam.up.RotatedAroundX(float64(dy) * 0.01)
m.mousex = msg.X
m.mousey = msg.Y
return m, m.RetraceImage()
}
case tea.WindowSizeMsg, retraceImageMsg:
return m, m.RetraceImage()
case checkRedrawMsg:
if m.render.progress < 1.0 {
return m, checkRedrawCmd()
}
}
return m, nil
}
func (m *Model) View() string {
if m.render == nil {
return ""
}
return fmt.Sprintf(
"Size: %dx%d (%d pixels). Progress: %.2f%%\n%s",
m.render.img.width,
m.render.img.height,
m.render.img.width*m.render.img.height,
m.render.progress*100,
m.render.img.String(),
)
}
func (m *Model) RetraceImage() tea.Cmd {
// TODO: if there is an ongoing tracing, interrupt it. Current approach
// replaces ongoing tracing, but it keeps going in the background until
// it's done, which is a waste of resources and prevents from re-using
// the same RenderImg
if m.render != nil {
m.render.stop = true
}
width, height, err := term.GetSize(0)
if err != nil {
log.Fatalln("Error getting terminal size: ", err)
}
height -= 2
var img *RenderImg
if m.render != nil && m.render.img.width == width && m.render.img.height == height {
img = m.render.img
} else {
img = NewRenderImg(width, height, m.scene.backgroundColor)
}
render := Render{
img: img,
progress: 0,
}
m.render = &render
go renderScene(m.render, &m.scene, m.cam)
return checkRedrawCmd()
}
func main() {
// img := NewRenderImg(10, 5, RGB{})
// img.Set(5, 0, RGB{255, 0, 0})
// img.Set(2, 3, RGB{0, 255, 0})
// img.Set(9, 4, RGB{0, 0, 255})
// img.Set(0, 0, RGB{255, 255, 255})
// fmt.Println(img.String())
m := initialModel()
p := tea.NewProgram(m, tea.WithMouseAllMotion())
_, err := p.Run()
if err != nil {
log.Fatalf("Alas, there's been an error: %v", err)
}
}