mesh_primitives/
mesh_primitives.rs1use astrelis_core::logging;
14use astrelis_render::{Color, GraphicsContext, RenderWindow, RenderWindowBuilder, wgpu};
15use astrelis_winit::{
16 FrameTime, WindowId,
17 app::{App, AppCtx, run_app},
18 event::EventBatch,
19 window::{WindowDescriptor, WinitPhysicalSize},
20};
21use std::sync::Arc;
22
23struct MeshPrimitivesDemo {
24 _context: Arc<GraphicsContext>,
25 window: RenderWindow,
26 window_id: WindowId,
27}
28
29fn main() {
30 logging::init();
31
32 run_app(|ctx| {
33 let graphics_ctx =
34 GraphicsContext::new_owned_sync().expect("Failed to create graphics context");
35
36 let window = ctx
37 .create_window(WindowDescriptor {
38 title: "Mesh Primitives Demo - Geometry API".to_string(),
39 size: Some(WinitPhysicalSize::new(1024.0, 768.0)),
40 ..Default::default()
41 })
42 .expect("Failed to create window");
43
44 let window = RenderWindowBuilder::new()
45 .color_format(wgpu::TextureFormat::Bgra8UnormSrgb)
46 .with_depth_default()
47 .build(window, graphics_ctx.clone())
48 .expect("Failed to create render window");
49
50 let window_id = window.id();
51
52 println!("\n═══════════════════════════════════════════════════════");
53 println!(" 📐 MESH PRIMITIVES DEMO - Geometry API");
54 println!("═══════════════════════════════════════════════════════");
55 println!("\n MESH API FEATURES:");
56 println!(" • MeshBuilder for custom geometry");
57 println!(" • Primitive generation (cube, sphere, plane, etc.)");
58 println!(" • Flexible vertex formats (Position, Normal, UV, Color)");
59 println!(" • Index buffer optimization");
60 println!(" • Instanced rendering support");
61 println!("\n EXAMPLE PRIMITIVES:");
62 println!(" • Cube - box with 24 vertices (6 faces × 4 vertices)");
63 println!(" • Sphere - tessellated sphere with UV mapping");
64 println!(" • Plane - quad with optional subdivisions");
65 println!(" • Cylinder - sides + caps");
66 println!(" • Custom - arbitrary vertex/index data");
67 println!("\n Mesh API Usage:");
68 println!(" let mesh = MeshBuilder::new()");
69 println!(" .with_positions(vertices)");
70 println!(" .with_normals(normals)");
71 println!(" .with_uvs(uvs)");
72 println!(" .with_indices(indices)");
73 println!(" .build(&ctx);");
74 println!(" mesh.draw(&mut pass);");
75 println!(" mesh.draw_instanced(&mut pass, instance_count);");
76 println!("═══════════════════════════════════════════════════════\n");
77
78 tracing::info!("Mesh primitives demo initialized");
79
80 Box::new(MeshPrimitivesDemo {
81 _context: graphics_ctx,
82 window,
83 window_id,
84 })
85 });
86}
87
88impl App for MeshPrimitivesDemo {
89 fn update(&mut self, _ctx: &mut AppCtx, _time: &FrameTime) {}
90
91 fn render(&mut self, _ctx: &mut AppCtx, window_id: WindowId, events: &mut EventBatch) {
92 if window_id != self.window_id {
93 return;
94 }
95
96 events.dispatch(|event| {
97 if let astrelis_winit::event::Event::WindowResized(size) = event {
98 self.window.resized(*size);
99 astrelis_winit::event::HandleStatus::consumed()
100 } else {
101 astrelis_winit::event::HandleStatus::ignored()
102 }
103 });
104
105 let Some(frame) = self.window.begin_frame() else {
106 return; };
108 {
109 let _pass = frame
110 .render_pass()
111 .clear_color(Color::from_rgb_u8(20, 30, 40))
112 .label("mesh_primitives_pass")
113 .build();
114 }
115 }
117}