From c3683cb91a7142be405aa672fcbae4238a3bde72 Mon Sep 17 00:00:00 2001 From: tcmal Date: Sun, 25 Aug 2024 17:44:20 +0100 Subject: feat(render): texture store --- examples/render-quad/Cargo.toml | 2 +- examples/render-quad/data/test1.png | Bin 0 -> 486777 bytes examples/render-quad/data/test2.png | Bin 0 -> 375052 bytes examples/render-quad/src/main.rs | 37 ++++++++++++++++++++++++++++++------ 4 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 examples/render-quad/data/test1.png create mode 100644 examples/render-quad/data/test2.png (limited to 'examples/render-quad') 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 new file mode 100644 index 0000000..3d37758 Binary files /dev/null and b/examples/render-quad/data/test1.png differ diff --git a/examples/render-quad/data/test2.png b/examples/render-quad/data/test2.png new file mode 100644 index 0000000..f33cc3e Binary files /dev/null and b/examples/render-quad/data/test2.png differ 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 => { -- cgit v1.2.3