diff options
author | tcmal <me@aria.rip> | 2024-08-25 17:44:20 +0100 |
---|---|---|
committer | tcmal <me@aria.rip> | 2024-08-25 17:44:20 +0100 |
commit | c3683cb91a7142be405aa672fcbae4238a3bde72 (patch) | |
tree | 3069cd8aef8212e864181732a27534008f533d25 /examples | |
parent | 7cdb4bb159a7df88390e63c59f0d4c5538d7411b (diff) |
feat(render): texture store
Diffstat (limited to 'examples')
-rw-r--r-- | examples/render-quad/Cargo.toml | 2 | ||||
-rw-r--r-- | examples/render-quad/data/test1.png | bin | 0 -> 486777 bytes | |||
-rw-r--r-- | examples/render-quad/data/test2.png | bin | 0 -> 375052 bytes | |||
-rw-r--r-- | examples/render-quad/src/main.rs | 37 |
4 files changed, 32 insertions, 7 deletions
diff --git a/examples/render-quad/Cargo.toml b/examples/render-quad/Cargo.toml index 6f75543..7e9bc61 100644 --- a/examples/render-quad/Cargo.toml +++ b/examples/render-quad/Cargo.toml @@ -11,4 +11,4 @@ winit = "^0.21" log = "0.4.0" simple_logger = "1.0" rand = "0.7" - +image = "0.23.2" diff --git a/examples/render-quad/data/test1.png b/examples/render-quad/data/test1.png Binary files differnew file mode 100644 index 0000000..3d37758 --- /dev/null +++ b/examples/render-quad/data/test1.png diff --git a/examples/render-quad/data/test2.png b/examples/render-quad/data/test2.png Binary files differnew file mode 100644 index 0000000..f33cc3e --- /dev/null +++ b/examples/render-quad/data/test2.png diff --git a/examples/render-quad/src/main.rs b/examples/render-quad/src/main.rs index 9cc7310..8d6d439 100644 --- a/examples/render-quad/src/main.rs +++ b/examples/render-quad/src/main.rs @@ -21,9 +21,11 @@ extern crate stockton_render; extern crate winit; extern crate simple_logger; extern crate rand; +extern crate image; use stockton_render::draw::{RenderingContext, UVPoint}; use stockton_types::{Vector2, Vector3}; +use image::load_from_memory; use winit::{ event::{Event, WindowEvent}, @@ -36,19 +38,40 @@ fn main() { simple_logger::init().unwrap(); // Create the renderer. - let event_loop = EventLoop::new(); let window = WindowBuilder::new().build(&event_loop).unwrap(); let mut ctx = RenderingContext::new(&window).unwrap(); - ctx.vert_buffer[0] = UVPoint(Vector2::new(-0.5, -0.5), Vector3::new(1.0, 0.0, 0.0), Vector2::new(0.0, 0.0)); - ctx.vert_buffer[1] = UVPoint(Vector2::new(0.5, -0.5), Vector3::new(0.0, 1.0, 0.0), Vector2::new(1.0, 0.0)); - ctx.vert_buffer[2] = UVPoint(Vector2::new(0.5, 0.5), Vector3::new(0.0, 0.0, 1.0), Vector2::new(1.0, 1.0)); - ctx.vert_buffer[3] = UVPoint(Vector2::new(-0.5, 0.5), Vector3::new(1.0, 0.0, 1.0), Vector2::new(0.0, 1.0)); + // Load 2 test textures + ctx.add_texture( + load_from_memory(include_bytes!("../data/test1.png")) + .expect("Couldn't load test texture 1") + .into_rgba()) + .unwrap(); + ctx.add_texture( + load_from_memory(include_bytes!("../data/test2.png")) + .expect("Couldn't load test texture 2") + .into_rgba()) + .unwrap(); + + // First quad with test1 + ctx.vert_buffer[0] = UVPoint(Vector2::new(-1.0, -1.0), Vector3::new(1.0, 0.0, 0.0), Vector2::new(0.0, 0.0), 0); + ctx.vert_buffer[1] = UVPoint(Vector2::new(0.0, -1.0), Vector3::new(0.0, 1.0, 0.0), Vector2::new(1.0, 0.0), 0); + ctx.vert_buffer[2] = UVPoint(Vector2::new(0.0, 0.0), Vector3::new(0.0, 0.0, 1.0), Vector2::new(1.0, 1.0), 0); + ctx.vert_buffer[3] = UVPoint(Vector2::new(-1.0, 0.0), Vector3::new(1.0, 0.0, 1.0), Vector2::new(0.0, 1.0), 0); ctx.index_buffer[0] = (0, 1, 2); ctx.index_buffer[1] = (0, 2, 3); + // Second quad with test2 + ctx.vert_buffer[4] = UVPoint(Vector2::new(0.0, -1.0), Vector3::new(1.0, 0.0, 0.0), Vector2::new(0.0, 0.0), 1); + ctx.vert_buffer[5] = UVPoint(Vector2::new(1.0, -1.0), Vector3::new(0.0, 1.0, 0.0), Vector2::new(1.0, 0.0), 1); + ctx.vert_buffer[6] = UVPoint(Vector2::new(1.0, 0.0), Vector3::new(0.0, 0.0, 1.0), Vector2::new(1.0, 1.0), 1); + ctx.vert_buffer[7] = UVPoint(Vector2::new(0.0, 0.0), Vector3::new(1.0, 0.0, 1.0), Vector2::new(0.0, 1.0), 1); + + ctx.index_buffer[2] = (4, 5, 6); + ctx.index_buffer[3] = (4, 7, 6); + event_loop.run(move |event, _, flow| { *flow = ControlFlow::Poll; @@ -72,7 +95,9 @@ fn main() { let mouse_x: f32 = ((position.x / win_size.width as f64) * 2.0 - 1.0) as f32; let mouse_y: f32 = ((position.y / win_size.height as f64) * 2.0 - 1.0) as f32; - ctx.vert_buffer[0] = UVPoint(Vector2::new(mouse_x, mouse_y), Vector3::new(1.0, 0.0, 0.0), Vector2::new(0.0, 0.0)); + // Move a vertex from each quad + ctx.vert_buffer[2] = UVPoint(Vector2::new(mouse_x, mouse_y), Vector3::new(1.0, 0.0, 0.0), Vector2::new(1.0, 1.0), 0); + ctx.vert_buffer[7] = UVPoint(Vector2::new(mouse_x, mouse_y), Vector3::new(1.0, 0.0, 0.0), Vector2::new(0.0, 1.0), 1); } Event::MainEventsCleared => { |