From d5706c8e307eeefbc90391bd7f421e4114d3b8cd Mon Sep 17 00:00:00 2001 From: ghostly_zsh Date: Thu, 20 Mar 2025 01:03:52 -0500 Subject: [PATCH] texture support --- crates/client/src/rendering/mod.rs | 28 ++++++++++++++++++------- crates/client/src/shaders/fragment.glsl | 7 +++++-- crates/client/src/shaders/vertex.glsl | 3 +++ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/crates/client/src/rendering/mod.rs b/crates/client/src/rendering/mod.rs index 553b9f9cca851aed4d710d39f8fa6ecbfa0055c0..245fb2df7065161bcd116f8938f1bd96c92eb28b 100644 --- a/crates/client/src/rendering/mod.rs +++ b/crates/client/src/rendering/mod.rs @@ -1,6 +1,6 @@ use std::num::NonZeroU32; -use glow::HasContext; +use glow::{HasContext, PixelUnpackData}; #[cfg(not(target_arch = "wasm32"))] use glutin::surface::{Surface, WindowSurface, GlSurface, SwapInterval}; #[cfg(not(target_arch = "wasm32"))] @@ -31,11 +31,11 @@ pub struct App { gl: Option, } -const VERTICES: [f32; 8] = [ - -1.0, -1.0, - 1.0, -1.0, - 1.0, 1.0, - -1.0, 1.0, +const VERTICES: [f32; 16] = [ + -1.0, -1.0, 0.0, 1.0, + 1.0, -1.0, 1.0, 1.0, + 1.0, 1.0, 1.0, 0.0, + -1.0, 1.0, 0.0, 0.0, ]; const INDICES: [u32; 6] = [ 0, 1, 2, @@ -139,14 +139,26 @@ impl ApplicationHandler for App { let element_buffer = gl.create_buffer().expect("Failed to create element buffer"); gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(element_buffer)); gl.buffer_data_u8_slice(glow::ARRAY_BUFFER, - std::slice::from_raw_parts(VERTICES.as_ptr() as *const u8, 8*4), + std::slice::from_raw_parts(VERTICES.as_ptr() as *const u8, size_of_val(&VERTICES)), glow::STATIC_DRAW); gl.buffer_data_u8_slice(glow::ELEMENT_ARRAY_BUFFER, std::slice::from_raw_parts(INDICES.as_ptr() as *const u8, 6*4), glow::STATIC_DRAW); - gl.vertex_attrib_pointer_f32(0, 2, glow::FLOAT, false, 2*std::mem::size_of::() as i32, 0); + gl.vertex_attrib_pointer_f32(0, 2, glow::FLOAT, false, 4*size_of::() as i32, 0); gl.enable_vertex_attrib_array(0); + gl.vertex_attrib_pointer_f32(1, 2, glow::FLOAT, false, 4*size_of::() as i32, 2*size_of::() as i32); + gl.enable_vertex_attrib_array(1); + + let texture = gl.create_texture().expect("Failed to create texture object"); + gl.active_texture(glow::TEXTURE0); + gl.bind_texture(glow::TEXTURE_2D, Some(texture)); + let image = image::load_from_memory(include_bytes!("../assets/happy-tree.png")).unwrap(); + let image = image.to_rgba8(); + gl.tex_image_2d(glow::TEXTURE_2D, 0, glow::RGBA as i32, + image.width() as i32, image.height() as i32, 0, glow::RGBA, + glow::UNSIGNED_BYTE, PixelUnpackData::Slice(Some(&image.into_raw()))); + gl.generate_mipmap(glow::TEXTURE_2D); gl.clear_color(1.0, 1.0, 1.0, 1.0); gl.viewport(0, 0, window.inner_size().width as i32, window.inner_size().height as i32); diff --git a/crates/client/src/shaders/fragment.glsl b/crates/client/src/shaders/fragment.glsl index 363adc73c9b8b48540e20b4c84bf7c11fa25c1d3..321831091406c5c711c30cc53cb29b2ee544c178 100644 --- a/crates/client/src/shaders/fragment.glsl +++ b/crates/client/src/shaders/fragment.glsl @@ -1,9 +1,12 @@ precision mediump float; in vec2 v_pos; +in vec2 v_texcoord; -out vec4 color; +out vec4 FragColor; + +uniform sampler2D sprite; void main() { - color = vec4((v_pos+1.0f)/2.0f, 0.5f, 1.0f); + FragColor = texture(sprite, v_texcoord); } diff --git a/crates/client/src/shaders/vertex.glsl b/crates/client/src/shaders/vertex.glsl index 21fc4129c43636abbf467b44fb62a23ba7a81473..0c787a293fe20cd5742973151443f05dd5c40bed 100644 --- a/crates/client/src/shaders/vertex.glsl +++ b/crates/client/src/shaders/vertex.glsl @@ -1,10 +1,13 @@ precision mediump float; in vec2 pos; +in vec2 texcoord; out vec2 v_pos; +out vec2 v_texcoord; void main() { v_pos = pos; + v_texcoord = texcoord; gl_Position = vec4(v_pos, 0.0, 1.0); }