Skip to main content

mesh_primitives/
mesh_primitives.rs

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