MXVK Vulkan Framework 0.35.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
model Namespace Reference

Classes

class  ModelWindow

Functions

np.ndarray identity_matrix ()
np.ndarray look_at_matrix (tuple[float, float, float] eye, tuple[float, float, float] center, tuple[float, float, float] up)
int main ()
list[float] matrix_values (np.ndarray matrix)
np.ndarray normalize_vector (np.ndarray vector)
 parse_args ()
tuple[int, int] parse_resolution (str value)
np.ndarray perspective_matrix (float fov_radians, float aspect, float near_plane, float far_plane)
np.ndarray rotation_y_matrix (float angle)
np.ndarray scale_matrix (float x, float y, float z)
np.ndarray translation_matrix (float x, float y, float z)

Variables

 EXAMPLE_DIR = Path(__file__).resolve().parent

Function Documentation

◆ identity_matrix()

np.ndarray identity_matrix ( )

Definition at line 36 of file model.py.

36def identity_matrix() -> np.ndarray:
37 return np.eye(4, dtype=np.float32)
38

◆ look_at_matrix()

np.ndarray look_at_matrix ( tuple[float, float, float] eye,
tuple[float, float, float] center,
tuple[float, float, float] up )

Definition at line 57 of file model.py.

57def look_at_matrix(eye: tuple[float, float, float], center: tuple[float, float, float], up: tuple[float, float, float]) -> np.ndarray:
58 eye_vector = np.array(eye, dtype=np.float32)
59 center_vector = np.array(center, dtype=np.float32)
60 up_vector = np.array(up, dtype=np.float32)
61 forward = normalize_vector(center_vector - eye_vector)
62 side = normalize_vector(np.cross(forward, up_vector))
63 camera_up = np.cross(side, forward)
64 matrix = identity_matrix()
65 matrix[0, 0] = side[0]
66 matrix[0, 1] = side[1]
67 matrix[0, 2] = side[2]
68 matrix[0, 3] = -float(np.dot(side, eye_vector))
69 matrix[1, 0] = camera_up[0]
70 matrix[1, 1] = camera_up[1]
71 matrix[1, 2] = camera_up[2]
72 matrix[1, 3] = -float(np.dot(camera_up, eye_vector))
73 matrix[2, 0] = -forward[0]
74 matrix[2, 1] = -forward[1]
75 matrix[2, 2] = -forward[2]
76 matrix[2, 3] = float(np.dot(forward, eye_vector))
77 return matrix
78

◆ main()

int main ( void )

Definition at line 281 of file model.py.

281def main() -> int:
282 args = parse_args()
283 width, height = args.resolution
284 if (args.input_filename):
285 filename = args.input_filename
286 else:
287 filename = args.filename
288 window = None
289 try:
290 window = ModelWindow(filename, args.path, "MXVK Model Test", width, height, args.fullscreen, args.enable_vsync)
291 window.loop()
292
293 except mxvk.MXVKError as exception:
294 print(f"mxvk: Exception: {exception}", file=sys.stderr)
295 return 1
296 finally:
297 if (window is not None):
298 window.close()
299 return 0
300
Definition main.py:1

◆ matrix_values()

list[float] matrix_values ( np.ndarray matrix)

Definition at line 33 of file model.py.

33def matrix_values(matrix: np.ndarray) -> list[float]:
34 return matrix.astype(np.float32, copy=False).reshape(16).tolist()
35

◆ normalize_vector()

np.ndarray normalize_vector ( np.ndarray vector)

Definition at line 50 of file model.py.

50def normalize_vector(vector: np.ndarray) -> np.ndarray:
51 length = float(np.linalg.norm(vector))
52 if (length <= 0.0):
53 return vector
54
55 return vector / length
56

◆ parse_args()

parse_args ( )

Definition at line 271 of file model.py.

271def parse_args():
272 parser = argparse.ArgumentParser(description="MXVK textured Penguin Python example")
273 parser.add_argument("filename", nargs="?", default="", help="optional OBJ or MXMOD model path")
274 parser.add_argument("-i", "--input", dest="input_filename", default="", help="optional OBJ or MXMOD model path")
275 parser.add_argument("-p", "--path", default=str(EXAMPLE_DIR), help="resource root containing the data directory")
276 parser.add_argument("-r", "--resolution", type=parse_resolution, default=(1280, 720), metavar="WIDTHxHEIGHT", help="window resolution (default: 1280x720)")
277 parser.add_argument("-f", "--fullscreen", action="store_true", help="start fullscreen")
278 parser.add_argument("--enable-vsync", action="store_true", help="enable vertical synchronization")
279 return parser.parse_args()
280

◆ parse_resolution()

tuple[int, int] parse_resolution ( str value)

Definition at line 258 of file model.py.

258def parse_resolution(value: str) -> tuple[int, int]:
259 try:
260 width_text, height_text = value.lower().split("x", 1)
261 width = int(width_text)
262 height = int(height_text)
263
264 except ValueError as exception:
265 raise argparse.ArgumentTypeError("resolution must use WIDTHxHEIGHT, for example 1280x720") from exception
266
267 if (width <= 0 or height <= 0):
268 raise argparse.ArgumentTypeError("resolution width and height must be greater than zero")
269 return width, height
270

◆ perspective_matrix()

np.ndarray perspective_matrix ( float fov_radians,
float aspect,
float near_plane,
float far_plane )

Definition at line 79 of file model.py.

79def perspective_matrix(fov_radians: float, aspect: float, near_plane: float, far_plane: float) -> np.ndarray:
80 tangent = math.tan(fov_radians * 0.5)
81 matrix = np.zeros((4, 4), dtype=np.float32)
82 matrix[0, 0] = 1.0 / (aspect * tangent)
83 matrix[1, 1] = 1.0 / tangent
84 matrix[2, 2] = far_plane / (near_plane - far_plane)
85 matrix[2, 3] = (far_plane * near_plane) / (near_plane - far_plane)
86 matrix[3, 2] = -1.0
87 matrix[1, 1] *= -1.0
88 return matrix
89

◆ rotation_y_matrix()

np.ndarray rotation_y_matrix ( float angle)

Definition at line 39 of file model.py.

39def rotation_y_matrix(angle: float) -> np.ndarray:
40 cosine = math.cos(angle)
41 sine = math.sin(angle)
42 return np.array([[cosine, 0.0, sine, 0.0], [0.0, 1.0, 0.0, 0.0], [-sine, 0.0, cosine, 0.0], [0.0, 0.0, 0.0, 1.0]], dtype=np.float32)
43

◆ scale_matrix()

np.ndarray scale_matrix ( float x,
float y,
float z )

Definition at line 44 of file model.py.

44def scale_matrix(x: float, y: float, z: float) -> np.ndarray:
45 return np.array([[x, 0.0, 0.0, 0.0], [0.0, y, 0.0, 0.0], [0.0, 0.0, z, 0.0], [0.0, 0.0, 0.0, 1.0]], dtype=np.float32)
46

◆ translation_matrix()

np.ndarray translation_matrix ( float x,
float y,
float z )

Definition at line 47 of file model.py.

47def translation_matrix(x: float, y: float, z: float) -> np.ndarray:
48 return np.array([[1.0, 0.0, 0.0, x], [0.0, 1.0, 0.0, y], [0.0, 0.0, 1.0, z], [0.0, 0.0, 0.0, 1.0]], dtype=np.float32)
49

Variable Documentation

◆ EXAMPLE_DIR

model.EXAMPLE_DIR = Path(__file__).resolve().parent

Definition at line 31 of file model.py.