-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraycasting.py
More file actions
188 lines (150 loc) · 6.87 KB
/
raycasting.py
File metadata and controls
188 lines (150 loc) · 6.87 KB
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
import numpy as np
from OpenGL.GL import *
from OpenGL.GL.shaders import compileProgram, compileShader
from settings import *
class RayCasting:
def __init__(self, game):
self.game = game
#configuração padrão do OpenGL
self.shader = self.createShader("shaders/frameBufferVertex.txt",
"shaders/frameBufferFragment.txt")
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glUseProgram(self.shader)
self.createQuad()
self.createColorBuffer()
self.colors = [
(0 << 24) + (0 << 16) + (128 << 8) + 255,
(0 << 24) + (128 << 16) + (0 << 8) + 255,
(0 << 24) + (128 << 16) + (128 << 8) + 255,
(128 << 24) + (0 << 16) + (0 << 8) + 255,
(128 << 24) + (0 << 16) + (128 << 8) + 255
]
def createQuad(self):
"""
Cria os vértices para 'quad'
"""
# x, y, z, s, t
self.vertices = np.array(
( 1.0, 1.0, 0.0, 0.0, 1.0, #top-right
-1.0, 1.0, 0.0, 0.0, 0.0, #top-left
-1.0, -1.0, 0.0, 1.0, 0.0, #bottom-left
-1.0, -1.0, 0.0, 1.0, 0.0, #bottom-left
1.0, -1.0, 0.0, 1.0, 1.0, #bottom-right
1.0, 1.0, 0.0, 0.0, 1.0), #top-right
dtype=np.float32
)
self.vertex_count = 6
self.vao = glGenVertexArrays(1)
glBindVertexArray(self.vao)
self.vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
glBufferData(GL_ARRAY_BUFFER, self.vertices.nbytes, self.vertices, GL_STATIC_DRAW)
glEnableVertexAttribArray(0)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 20, ctypes.c_void_p(0))
glEnableVertexAttribArray(1)
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 20, ctypes.c_void_p(12))
def createColorBuffer(self):
"""
Inicializa o 'frame buffer' customizado.
"""
self.colorBufferData = np.array(
[np.uint32((255<<16) + (255 << 8) + (255 << 0)) for pixel in range(WIDTH * HEIGHT)],
dtype=np.uint32
)
self.colorBuffer = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, self.colorBuffer)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,HEIGHT,WIDTH,0,GL_RGBA,
GL_UNSIGNED_INT_8_8_8_8,bytes(self.colorBufferData))
def createShader(self, vertexFilepath, fragmentFilepath):
with open(vertexFilepath,'r') as f:
vertex_src = f.readlines()
with open(fragmentFilepath,'r') as f:
fragment_src = f.readlines()
shader = compileProgram(compileShader(vertex_src, GL_VERTEX_SHADER),
compileShader(fragment_src, GL_FRAGMENT_SHADER))
return shader
def ray_cast(self):
glUseProgram(self.shader)
# Limpa a tela para um novo frame
self.clearScreen()
posX, posY = self.game.player.pos
dirX, dirY = PLAYER_DIR
planoX, planoY = PLANO
for rayIndex in range(WIDTH):
cameraX = 2.0 * rayIndex / WIDTH - 1.0 # máximo = 1.0, quando rayIndex é aprox. WIDTH
# mínimo = -1.0, quando rayIndex == 0
rayDirectionX = dirX + cameraX * planoX
rayDirectionY = dirY + cameraX * planoY
mapX, mapY = self.game.player.map_pos
#distância delta: distância até o próxima reta X (horizontal)
# ou Y (vertical) no quadriculado.
if np.abs(rayDirectionX) < 1e-8:
deltaDistX = 1e8
else:
deltaDistX = abs(1/rayDirectionX)
if np.abs(rayDirectionY) < 1e-8:
deltaDistY = 1e8
else:
deltaDistY = abs(1/rayDirectionY)
if (rayDirectionX < 0):
stepX = -1
sideDistX = (posX - mapX) * deltaDistX
else:
stepX = 1
sideDistX = (mapX + 1 - posX) * deltaDistX
if (rayDirectionY < 0):
stepY = -1
sideDistY = (posY - mapY) * deltaDistY
else:
stepY = 1
sideDistY = (mapY + 1 - posY) * deltaDistY
while True:
if (sideDistX < sideDistY):
sideDistX += deltaDistX
mapX += stepX
side = 0 # horizontal
else:
sideDistY += deltaDistY
mapY += stepY
side = 1 # vertical
if self.game.map.mini_map[mapY][mapX] != 0:
break
if side == 0:
distanceToCamera = np.abs(sideDistX - deltaDistX)
else:
distanceToCamera = np.abs(sideDistY - deltaDistY)
self.drawVerticalLine(
x = rayIndex,
height = (HEIGHT//2)/max(distanceToCamera,1e-8),
color = self.colors[self.game.map.mini_map[mapY][mapX] - 1]
)
self.updateScreen()
def clearScreen(self):
self.colorBufferData &= 0
self.colorBufferData |= ((16 << 24) + (32 << 16) + (64 << 8) + 255)
def drawVerticalLine(self, x, height, color):
lineHeight = min(int(height), HEIGHT//2)
wallTop = HEIGHT//2 - lineHeight + x * HEIGHT
wallBottom = HEIGHT//2 + lineHeight + x * HEIGHT
self.colorBufferData[wallTop:wallBottom] = color
def updateScreen(self):
glBindTexture(GL_TEXTURE_2D, self.colorBuffer)
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,HEIGHT,WIDTH,0,GL_RGBA,
GL_UNSIGNED_INT_8_8_8_8,bytes(self.colorBufferData))
glBindVertexArray(self.vao)
glDrawArrays(GL_TRIANGLES, 0, self.vertex_count)
def destroy(self):
glDeleteVertexArrays(1, (self.vao,))
glDeleteBuffers(1, (self.vbo,))
glDeleteTextures(1, (self.colorBuffer,))
glDeleteProgram(self.shader)
glDeleteVertexArrays(1, (self.vao,))
glDeleteBuffers(1, (self.vbo,))
glDeleteBuffers(self.shader)
def update(self):
self.ray_cast()