@@ 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<glow::Context>,
}
-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::<f32>() as i32, 0);
+ gl.vertex_attrib_pointer_f32(0, 2, glow::FLOAT, false, 4*size_of::<f32>() as i32, 0);
gl.enable_vertex_attrib_array(0);
+ gl.vertex_attrib_pointer_f32(1, 2, glow::FLOAT, false, 4*size_of::<f32>() as i32, 2*size_of::<f32>() 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);