-
Notifications
You must be signed in to change notification settings - Fork 4
/
raycasting_flat.go
executable file
·264 lines (228 loc) · 6.11 KB
/
raycasting_flat.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
// based on http://lodev.org/cgtutor/raycasting.html
package main
import (
"image"
"image/color"
"image/draw"
"log"
"math"
"os"
"runtime"
"github.com/qeedquan/go-media/math/ga"
"github.com/qeedquan/go-media/math/ga/vec2"
"github.com/qeedquan/go-media/sdl"
"github.com/qeedquan/go-media/sdl/sdlimage/sdlcolor"
)
const (
MAP_WIDTH = 24
MAP_HEIGHT = 24
)
var world = [MAP_WIDTH][MAP_HEIGHT]int{
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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0, 0, 0, 0, 0, 0, 1},
{1, 4, 0, 4, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 4, 0, 0, 0, 0, 5, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 4, 0, 4, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 4, 0, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
}
var (
pos = ga.Vec2d{22, 12}
dir = ga.Vec2d{-1, 0}
plane = ga.Vec2d{0, 0.66}
speed = 1.0
rot = math.Pi / 8
window *sdl.Window
renderer *sdl.Renderer
texture *sdl.Texture
canvas *image.RGBA
)
func main() {
runtime.LockOSThread()
log.SetFlags(0)
log.SetPrefix("")
initSDL()
for {
event()
blit()
}
}
func ck(err error) {
if err != nil {
log.Fatal(err)
}
}
func initSDL() {
err := sdl.Init(sdl.INIT_VIDEO)
ck(err)
w, h := 512, 384
window, renderer, err = sdl.CreateWindowAndRenderer(w, h, sdl.WINDOW_RESIZABLE)
ck(err)
resize(w, h)
window.SetTitle("Raycasting Untextured Walls")
}
func resize(w, h int) {
var err error
if texture != nil {
texture.Destroy()
}
texture, err = renderer.CreateTexture(sdl.PIXELFORMAT_ABGR8888, sdl.TEXTUREACCESS_STREAMING, w, h)
ck(err)
canvas = image.NewRGBA(image.Rect(0, 0, w, h))
}
func event() {
for {
ev := sdl.PollEvent()
if ev == nil {
break
}
switch ev := ev.(type) {
case sdl.QuitEvent:
os.Exit(0)
case sdl.KeyDownEvent:
switch ev.Sym {
case sdl.K_ESCAPE:
os.Exit(0)
case sdl.K_UP:
moveForward(dir.X, dir.Y, speed)
case sdl.K_DOWN:
moveForward(-dir.X, -dir.Y, speed)
case sdl.K_LEFT:
moveSide(rot)
case sdl.K_RIGHT:
moveSide(-rot)
}
case sdl.WindowEvent:
switch ev.Event {
case sdl.WINDOWEVENT_RESIZED:
resize(int(ev.Data[0]), int(ev.Data[1]))
}
}
}
}
func moveForward(dx, dy, speed float64) {
if world[int(pos.X+dx*speed)][int(pos.Y)] == 0 {
pos.X += dx * speed
}
if world[int(pos.X)][int(pos.Y+dy*speed)] == 0 {
pos.Y += dy * speed
}
}
func moveSide(rot float64) {
dir = vec2.Rotate(dir, rot)
plane = vec2.Rotate(plane, rot)
}
func blit() {
renderer.SetDrawColor(sdlcolor.Black)
renderer.Clear()
draw.Draw(canvas, canvas.Bounds(), image.NewUniform(color.RGBA{112, 112, 112, 255}), image.ZP, draw.Src)
blitWorld()
texture.Update(nil, canvas.Pix, canvas.Stride)
renderer.Copy(texture, nil, nil)
renderer.Present()
}
func blitWorld() {
w, h, err := renderer.OutputSize()
ck(err)
for x := 0; x < w; x++ {
camera := ga.Vec2d{2*float64(x)/float64(w) - 1, 0}
raypos := pos
raydir := vec2.Fmas(dir, plane, camera.X)
map_ := image.Pt(int(raypos.X), int(raypos.Y))
delta := ga.Vec2d{
math.Sqrt(1 + (raydir.Y*raydir.Y)/(raydir.X*raydir.X)),
math.Sqrt(1 + (raydir.X*raydir.X)/(raydir.Y*raydir.Y)),
}
var step, sidedist ga.Vec2d
if raydir.X < 0 {
step.X = -1
sidedist.X = (raypos.X - float64(map_.X)) * delta.X
} else {
step.X = 1
sidedist.X = (float64(map_.X) + 1 - raypos.X) * delta.X
}
if raydir.Y < 0 {
step.Y = -1
sidedist.Y = (raypos.Y - float64(map_.Y)) * delta.Y
} else {
step.Y = 1
sidedist.Y = (float64(map_.Y) + 1 - raypos.Y) * delta.Y
}
side := 0
for {
if sidedist.X < sidedist.Y {
sidedist.X += delta.X
map_.X = int(float64(map_.X) + step.X)
side = 0
} else {
sidedist.Y += delta.Y
map_.Y = int(float64(map_.Y) + step.Y)
side = 1
}
if world[map_.X][map_.Y] > 0 {
break
}
}
var perpdist float64
if side == 0 {
perpdist = (float64(map_.X) - raypos.X + (1-step.X)/2) / raydir.X
} else {
perpdist = (float64(map_.Y) - raypos.Y + (1-step.Y)/2) / raydir.Y
}
if perpdist <= 0 {
perpdist = 1e-2
}
height := int(float64(h) / perpdist)
start := -height/2 + h/2
if start < 0 {
start = 0
}
end := height/2 + h/2
if end >= h {
end = h - 1
}
var color sdl.Color
switch world[map_.X][map_.Y] {
case 1:
color = sdlcolor.Red
case 2:
color = sdlcolor.Green
case 3:
color = sdlcolor.Blue
case 4:
color = sdlcolor.White
default:
color = sdlcolor.Yellow
}
if side == 1 {
color = sdl.Color{color.R / 2, color.G / 2, color.B / 2, 255}
}
vline(x, 0, start, sdl.Color{55, 55, 55, 255})
vline(x, start, end, color)
}
}
func vline(x int, y0, y1 int, color sdl.Color) {
if y0 > y1 {
y0, y1 = y1, y0
}
for y := y0; y < y1; y++ {
canvas.Set(x, y, color)
}
}