feat: implement Phase 1 foundation - triangle rendering
- voltex_math: Vec3 with arithmetic ops, dot, cross, length, normalize - voltex_platform: VoltexWindow (winit wrapper), InputState (keyboard/mouse), GameTimer (fixed timestep + variable render loop) - voltex_renderer: GpuContext (wgpu init), Vertex + buffer layout, WGSL shader, render pipeline - triangle example: colored triangle with ESC to exit All 13 tests passing. Window renders RGB triangle on dark background. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,184 @@
|
||||
// examples/triangle/src/main.rs
|
||||
fn main() {
|
||||
println!("Voltex Triangle Demo");
|
||||
use winit::{
|
||||
application::ApplicationHandler,
|
||||
event::WindowEvent,
|
||||
event_loop::{ActiveEventLoop, EventLoop},
|
||||
keyboard::{KeyCode, PhysicalKey},
|
||||
window::WindowId,
|
||||
};
|
||||
use voltex_platform::{VoltexWindow, WindowConfig, InputState, GameTimer};
|
||||
use voltex_renderer::{GpuContext, pipeline, vertex::Vertex};
|
||||
use wgpu::util::DeviceExt;
|
||||
|
||||
const TRIANGLE_VERTICES: &[Vertex] = &[
|
||||
Vertex { position: [0.0, 0.5, 0.0], color: [1.0, 0.0, 0.0] },
|
||||
Vertex { position: [-0.5, -0.5, 0.0], color: [0.0, 1.0, 0.0] },
|
||||
Vertex { position: [0.5, -0.5, 0.0], color: [0.0, 0.0, 1.0] },
|
||||
];
|
||||
|
||||
struct TriangleApp {
|
||||
state: Option<AppState>,
|
||||
}
|
||||
|
||||
struct AppState {
|
||||
window: VoltexWindow,
|
||||
gpu: GpuContext,
|
||||
pipeline: wgpu::RenderPipeline,
|
||||
vertex_buffer: wgpu::Buffer,
|
||||
input: InputState,
|
||||
timer: GameTimer,
|
||||
}
|
||||
|
||||
impl ApplicationHandler for TriangleApp {
|
||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||
let config = WindowConfig {
|
||||
title: "Voltex - Triangle".to_string(),
|
||||
width: 1280,
|
||||
height: 720,
|
||||
..Default::default()
|
||||
};
|
||||
let window = VoltexWindow::new(event_loop, &config);
|
||||
let gpu = GpuContext::new(window.handle.clone());
|
||||
let pipeline = pipeline::create_render_pipeline(&gpu.device, gpu.surface_format);
|
||||
let vertex_buffer = gpu.device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Triangle Vertex Buffer"),
|
||||
contents: bytemuck::cast_slice(TRIANGLE_VERTICES),
|
||||
usage: wgpu::BufferUsages::VERTEX,
|
||||
});
|
||||
|
||||
self.state = Some(AppState {
|
||||
window,
|
||||
gpu,
|
||||
pipeline,
|
||||
vertex_buffer,
|
||||
input: InputState::new(),
|
||||
timer: GameTimer::new(60),
|
||||
});
|
||||
}
|
||||
|
||||
fn window_event(
|
||||
&mut self,
|
||||
event_loop: &ActiveEventLoop,
|
||||
_window_id: WindowId,
|
||||
event: WindowEvent,
|
||||
) {
|
||||
let state = match &mut self.state {
|
||||
Some(s) => s,
|
||||
None => return,
|
||||
};
|
||||
|
||||
match event {
|
||||
WindowEvent::CloseRequested => event_loop.exit(),
|
||||
|
||||
WindowEvent::KeyboardInput {
|
||||
event: winit::event::KeyEvent {
|
||||
physical_key: PhysicalKey::Code(key_code),
|
||||
state: key_state,
|
||||
..
|
||||
},
|
||||
..
|
||||
} => {
|
||||
let pressed = key_state == winit::event::ElementState::Pressed;
|
||||
state.input.process_key(key_code, pressed);
|
||||
if key_code == KeyCode::Escape && pressed {
|
||||
event_loop.exit();
|
||||
}
|
||||
}
|
||||
|
||||
WindowEvent::Resized(size) => {
|
||||
state.gpu.resize(size.width, size.height);
|
||||
}
|
||||
|
||||
WindowEvent::CursorMoved { position, .. } => {
|
||||
state.input.process_mouse_move(position.x, position.y);
|
||||
}
|
||||
|
||||
WindowEvent::MouseInput { state: btn_state, button, .. } => {
|
||||
let pressed = btn_state == winit::event::ElementState::Pressed;
|
||||
state.input.process_mouse_button(button, pressed);
|
||||
}
|
||||
|
||||
WindowEvent::MouseWheel { delta, .. } => {
|
||||
let y = match delta {
|
||||
winit::event::MouseScrollDelta::LineDelta(_, y) => y,
|
||||
winit::event::MouseScrollDelta::PixelDelta(pos) => pos.y as f32,
|
||||
};
|
||||
state.input.process_scroll(y);
|
||||
}
|
||||
|
||||
WindowEvent::RedrawRequested => {
|
||||
state.timer.tick();
|
||||
state.input.begin_frame();
|
||||
|
||||
// Fixed update loop
|
||||
while state.timer.should_fixed_update() {
|
||||
let _fixed_dt = state.timer.fixed_dt();
|
||||
}
|
||||
|
||||
let output = match state.gpu.surface.get_current_texture() {
|
||||
Ok(t) => t,
|
||||
Err(wgpu::SurfaceError::Lost) => {
|
||||
let (w, h) = state.window.inner_size();
|
||||
state.gpu.resize(w, h);
|
||||
return;
|
||||
}
|
||||
Err(wgpu::SurfaceError::OutOfMemory) => {
|
||||
event_loop.exit();
|
||||
return;
|
||||
}
|
||||
Err(_) => return,
|
||||
};
|
||||
|
||||
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
let mut encoder = state.gpu.device.create_command_encoder(
|
||||
&wgpu::CommandEncoderDescriptor { label: Some("Render Encoder") },
|
||||
);
|
||||
|
||||
{
|
||||
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some("Render Pass"),
|
||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||
view: &view,
|
||||
resolve_target: None,
|
||||
depth_slice: None,
|
||||
ops: wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(wgpu::Color {
|
||||
r: 0.1,
|
||||
g: 0.1,
|
||||
b: 0.15,
|
||||
a: 1.0,
|
||||
}),
|
||||
store: wgpu::StoreOp::Store,
|
||||
},
|
||||
})],
|
||||
depth_stencil_attachment: None,
|
||||
occlusion_query_set: None,
|
||||
timestamp_writes: None,
|
||||
multiview_mask: None,
|
||||
});
|
||||
|
||||
render_pass.set_pipeline(&state.pipeline);
|
||||
render_pass.set_vertex_buffer(0, state.vertex_buffer.slice(..));
|
||||
render_pass.draw(0..3, 0..1);
|
||||
}
|
||||
|
||||
state.gpu.queue.submit(std::iter::once(encoder.finish()));
|
||||
output.present();
|
||||
}
|
||||
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
|
||||
if let Some(state) = &self.state {
|
||||
state.window.request_redraw();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
let event_loop = EventLoop::new().unwrap();
|
||||
let mut app = TriangleApp { state: None };
|
||||
event_loop.run_app(&mut app).unwrap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user