DKL9 GitList
Repositories
DKL9 home
voxels
Code
Commits
Branches
Tags
Search
Tree:
17f6490
Branches
Tags
master
voxels
entity.py
Refactor WorldRegion and add crosshair
dkl9
commited
17f6490
at 2025-170 20:18:15
entity.py
Blame
History
Raw
from libs import * class Entity: def __init__(self, verts, col, pos, **kwargs): self.verts = vec(verts) self.col = vec(col) self.pos = vec(pos) self.draw_mode = kwargs.get("mode", GL_TRIANGLE_STRIP) self.hud = kwargs.get("hud", False) self.orient = np.identity(4, dtype=FLOAT) self.onclick = lambda s, b, n: print(f"clicked {s}:{n} with button {b}") self.vao = None self.vbos = [[None, 0], [None, 0]] def col_list(self): return np.repeat(vec([self.col]), len(self.verts), 0) def update_attr(self, i, l=None): if l is None: l = self.verts if i == 0 else self.col_list() if self.vao: glBindBuffer(GL_ARRAY_BUFFER, self.vbos[i][0]) if l.shape[0] != self.vbos[i][1]: glBufferData(GL_ARRAY_BUFFER, l.nbytes, l, GL_DYNAMIC_DRAW) self.vbos[i][1] = l.shape[0] else: glBufferSubData(GL_ARRAY_BUFFER, 0, l.nbytes, l) def build_vao(self): self.vao = glGenVertexArrays(1) glBindVertexArray(self.vao) for (i, l) in enumerate((self.verts, self.col_list())): self.vbos[i][0] = glGenBuffers(1) self.update_attr(i, l) glEnableVertexAttribArray(i) glVertexAttribPointer(i, l.shape[1], GL_FLOAT, GL_FALSE, 0, None) def translate(self): m = np.identity(4, dtype=FLOAT) m[:3, 3] = self.pos return m def rotate(self, theta, axis): x, y, z = axis c, s = np.cos(theta), np.sin(theta) t = 1 - c self.orient = vec([ [t*x*x + c, t*x*y - s*z, t*x*z + s*y, 0], [t*x*y + s*z, t*y*y + c, t*y*z - s*x, 0], [t*x*z - s*y, t*y*z + s*x, t*z*z + c, 0], [0, 0, 0, 1] ]) @ self.orient def click(self, button, face): self.onclick(self, button, face)