-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMesh.py
More file actions
60 lines (53 loc) · 2.27 KB
/
Copy pathMesh.py
File metadata and controls
60 lines (53 loc) · 2.27 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
#!/usr/bin/env python3
# Mesh.py – robust, driver-safe upload
import numpy as np
import ctypes
from OpenGL.GL import *
class Mesh:
"""
Encapsulates a VAO/VBO mesh. Provide interleaved vertex data and attribute
layout, then call draw() to render.
"""
def __init__(self, vertices: np.ndarray, attribs: list[tuple]):
"""
vertices: flat numpy array of vertex data (dtype=np.float32).
attribs: list of attribute specs, each a tuple
(location, size, type, normalized, stride, offset)
"""
# ---- prepare data --------------------------------------------------
vertices = np.ascontiguousarray(vertices, dtype=np.float32)
self._vertices_ctypes = (ctypes.c_float * vertices.size).from_buffer(vertices)
self.vertex_count = vertices.nbytes // attribs[0][4] # bytes / stride
# ---- VAO -----------------------------------------------------------
self.vao = glGenVertexArrays(1)
glBindVertexArray(self.vao)
# ---- VBO -----------------------------------------------------------
self.vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
glBufferData(
GL_ARRAY_BUFFER,
vertices.nbytes,
self._vertices_ctypes, # typed buffer stays alive via self
GL_STATIC_DRAW
)
# ---- attribute layout ---------------------------------------------
for loc, size, typ, norm, stride, offset in attribs:
glEnableVertexAttribArray(loc)
glVertexAttribPointer(
loc, size, typ, norm, stride, ctypes.c_void_p(offset)
)
# ---- tidy up -------------------------------------------------------
glBindBuffer(GL_ARRAY_BUFFER, 0)
glBindVertexArray(0)
def draw(self, mode: int = GL_TRIANGLES) -> None:
"""Bind the VAO and issue a draw call (default: triangles)."""
glBindVertexArray(self.vao)
glDrawArrays(mode, 0, self.vertex_count)
glBindVertexArray(0)
def __del__(self):
"""Delete GL objects when the instance is garbage-collected."""
try:
glDeleteBuffers(1, [self.vbo])
glDeleteVertexArrays(1, [self.vao])
except Exception:
pass