From 9d66afaacaa5756d1f1e93f82569d55b474060c2 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Tue, 6 Aug 2024 12:30:08 +0700 Subject: [PATCH] Triangulation: Basic level with ability to place points --- triangulation.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 triangulation.py diff --git a/triangulation.py b/triangulation.py new file mode 100644 index 0000000..2d5b03f --- /dev/null +++ b/triangulation.py @@ -0,0 +1,73 @@ +import pyray as RL +from pyray import (Rectangle as Rect, Vector2 as Vec2, Vector3 as Vec3, Camera3D, BoundingBox) +import math +import pdb +import random +from typing import Optional, Tuple, List +from dataclasses import dataclass, field + +screen_width = 1280 +screen_height = 1024 +grid_slices = 100 +grid_spacing = 0.2 + +@dataclass +class World: + cam: Camera3D + floor_bb: BoundingBox + rotate_cam: bool = True + frame_count: int = 0 + vertices: List[Vec3] = field(default_factory=list) + +def init() -> World: + cam = Camera3D(Vec3(0, 10, 10), Vec3(0, 0, 0), Vec3(0, 1, 0), 45, RL.CAMERA_PERSPECTIVE) + half_grid = grid_slices * grid_spacing * 0.5 + floor_bb = BoundingBox(Vec3(-half_grid, -0.01, -half_grid), Vec3(half_grid, 0.01, half_grid)) + return World(cam, floor_bb) + +def player_input(w: World): + if RL.is_key_pressed(RL.KEY_SPACE): + w.rotate_cam = not w.rotate_cam + if RL.is_mouse_button_pressed(0): + mouse_pos = RL.get_mouse_position() + ray = RL.get_mouse_ray(mouse_pos, w.cam) + collision = RL.get_ray_collision_box(ray, w.floor_bb) + if collision.hit: + p = collision.point + w.vertices.append(Vec3(p.x, 0, p.z)) + +def update(w: World): + pass + +def draw_3d(w: World): + RL.draw_grid(grid_slices, grid_spacing) + RL.draw_bounding_box(w.floor_bb, RL.GREEN) + for vert in w.vertices: + RL.draw_sphere(vert, 0.1, RL.GREEN) + +def draw_2d(w: World): + pass + +RL.init_window(screen_width, screen_height, "Starter"); +RL.set_target_fps(60) + +w = init() +while not RL.window_should_close(): + player_input(w) + update(w) + + # Drawing + if w.rotate_cam: + RL.update_camera(w.cam, RL.CameraMode.CAMERA_ORBITAL) + RL.begin_drawing() + RL.clear_background(RL.WHITE) + + RL.begin_mode_3d(w.cam) + draw_3d(w) + RL.end_mode_3d() + + draw_2d(w) + + RL.end_drawing() + w.frame_count += 1 +