diff --git a/Box.glb b/Box.glb index 95ec886..95dc808 100644 Binary files a/Box.glb and b/Box.glb differ diff --git a/Cargo.lock b/Cargo.lock index 1bbd6de..2e19a67 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -74,6 +74,12 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff" +[[package]] +name = "bit_field" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcb6dd1c2376d2e096796e234a70e17e94cc2d5d54ff8ce42b28cef1d0d359a4" + [[package]] name = "bitflags" version = "1.3.2" @@ -275,6 +281,7 @@ version = "0.1.0" dependencies = [ "glium", "gltf", + "image 0.24.1", "mlua", "nalgebra-glm", ] @@ -288,6 +295,51 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aaa7bd5fb665c6864b5f963dd9097905c54125909c7aa94c9e18507cdbe6c53" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6455c0ca19f0d2fbf751b908d5c55c1f5cbc65e03c4225427254b46890bdde1e" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1145cf131a2c6ba0615079ab6a638f7e1973ac9c2634fcbeaaad6114246efe8c" +dependencies = [ + "autocfg", + "cfg-if 1.0.0", + "crossbeam-utils", + "lazy_static", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf124c720b7686e3c2663cf54062ab0f68a88af2fb6a030e87e30bf721fcb38" +dependencies = [ + "cfg-if 1.0.0", + "lazy_static", +] + [[package]] name = "cty" version = "0.2.2" @@ -339,6 +391,15 @@ dependencies = [ "byteorder", ] +[[package]] +name = "deflate" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c86f7e25f518f4b81808a2cf1c50996a61f5c2eb394b2393bd87f2a4780a432f" +dependencies = [ + "adler32", +] + [[package]] name = "dispatch" version = "0.2.0" @@ -366,6 +427,47 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +[[package]] +name = "exr" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14cc0e06fb5f67e5d6beadf3a382fec9baca1aa751c6d5368fdeee7e5932c215" +dependencies = [ + "bit_field", + "deflate 1.0.0", + "flume", + "half", + "inflate", + "lebe", + "smallvec", + "threadpool", +] + +[[package]] +name = "flate2" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39522e96686d38f4bc984b9198e3a0613264abaebaff2c5c918bfa6b6da09af" +dependencies = [ + "cfg-if 1.0.0", + "crc32fast", + "libc", + "miniz_oxide 0.5.1", +] + +[[package]] +name = "flume" +version = "0.10.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843c03199d0c0ca54bc1ea90ac0d507274c28abcc4f691ae8b4eaa375087c76a" +dependencies = [ + "futures-core", + "futures-sink", + "nanorand", + "pin-project", + "spin", +] + [[package]] name = "fnv" version = "1.0.7" @@ -387,6 +489,41 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" +[[package]] +name = "futures-core" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3" + +[[package]] +name = "futures-sink" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21163e139fa306126e6eedaf49ecdb4588f939600f0b1e770f4205ee4b7fa868" + +[[package]] +name = "getrandom" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad" +dependencies = [ + "cfg-if 1.0.0", + "js-sys", + "libc", + "wasi 0.10.2+wasi-snapshot-preview1", + "wasm-bindgen", +] + +[[package]] +name = "gif" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3a7187e78088aead22ceedeee99779455b23fc231fe13ec443f99bb71694e5b" +dependencies = [ + "color_quant", + "weezl", +] + [[package]] name = "gimli" version = "0.26.1" @@ -429,7 +566,7 @@ dependencies = [ "base64", "byteorder", "gltf-json", - "image", + "image 0.23.14", "lazy_static", ] @@ -529,6 +666,21 @@ dependencies = [ "gl_generator", ] +[[package]] +name = "half" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "ident_case" version = "1.0.1" @@ -544,11 +696,40 @@ dependencies = [ "bytemuck", "byteorder", "color_quant", - "jpeg-decoder", + "jpeg-decoder 0.1.22", "num-iter", "num-rational 0.3.2", "num-traits", - "png", + "png 0.16.8", +] + +[[package]] +name = "image" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db207d030ae38f1eb6f240d5a1c1c88ff422aa005d10f8c6c6fc5e75286ab30e" +dependencies = [ + "bytemuck", + "byteorder", + "color_quant", + "exr", + "gif", + "jpeg-decoder 0.2.4", + "num-iter", + "num-rational 0.4.0", + "num-traits", + "png 0.17.5", + "scoped_threadpool", + "tiff", +] + +[[package]] +name = "inflate" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cdb29978cc5797bd8dcc8e5bf7de604891df2a8dc576973d71a281e916db2ff" +dependencies = [ + "adler32", ] [[package]] @@ -596,6 +777,15 @@ version = "0.1.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "229d53d58899083193af11e15917b5640cd40b29ff475a1fe4ef725deb02d0f2" +[[package]] +name = "jpeg-decoder" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "744c24117572563a98a7e9168a5ac1ee4a1ca7f702211258797bbe0ed0346c3c" +dependencies = [ + "rayon", +] + [[package]] name = "js-sys" version = "0.3.56" @@ -617,6 +807,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "lebe" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7efd1d698db0759e6ef11a7cd44407407399a910c774dd804c64c032da7826ff" + [[package]] name = "libc" version = "0.2.121" @@ -719,6 +915,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "miniz_oxide" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2b29bd4bc3f33391105ebee3589c19197c4271e3e5a9ec9bfe8127eeff8f082" +dependencies = [ + "adler", +] + [[package]] name = "mio" version = "0.8.2" @@ -729,7 +934,7 @@ dependencies = [ "log", "miow", "ntapi", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "winapi", ] @@ -799,6 +1004,15 @@ dependencies = [ "simba", ] +[[package]] +name = "nanorand" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a51313c5820b0b02bd422f4b44776fbf47961755c74ce64afc73bfad10226c3" +dependencies = [ + "getrandom", +] + [[package]] name = "ndk" version = "0.5.0" @@ -945,6 +1159,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "num_enum" version = "0.5.7" @@ -1036,6 +1260,26 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" +[[package]] +name = "pin-project" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58ad3879ad3baf4e44784bc6a718a8698867bb991f8ce24d1bcbe2cfb4c3a75e" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "744b6f092ba29c3650faf274db506afd39944f48420f6c86b17cfe0ee1cb36bb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "pkg-config" version = "0.3.25" @@ -1050,10 +1294,22 @@ checksum = "3c3287920cb847dee3de33d301c463fba14dda99db24214ddf93f83d3021f4c6" dependencies = [ "bitflags", "crc32fast", - "deflate", + "deflate 0.8.6", "miniz_oxide 0.3.7", ] +[[package]] +name = "png" +version = "0.17.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc38c0ad57efb786dd57b9864e5b18bae478c00c824dc55a38bbc9da95dde3ba" +dependencies = [ + "bitflags", + "crc32fast", + "deflate 1.0.0", + "miniz_oxide 0.5.1", +] + [[package]] name = "proc-macro-crate" version = "1.1.3" @@ -1121,6 +1377,30 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" +[[package]] +name = "rayon" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd249e82c21598a9a426a4e00dd7adc1d640b22445ec8545feef801d1a74c221" +dependencies = [ + "autocfg", + "crossbeam-deque", + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f51245e1e62e1f1629cbfec37b5793bbabcaeb90f30e94d2ba03564687353e4" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "num_cpus", +] + [[package]] name = "redox_syscall" version = "0.2.13" @@ -1180,6 +1460,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" +[[package]] +name = "scoped_threadpool" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" + [[package]] name = "scopeguard" version = "1.1.0" @@ -1262,6 +1548,15 @@ dependencies = [ "wayland-protocols", ] +[[package]] +name = "spin" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c530c2b0d0bf8b69304b39fe2001993e267461948b890cd037d8ad4293fa1a0d" +dependencies = [ + "lock_api", +] + [[package]] name = "strsim" version = "0.10.0" @@ -1305,6 +1600,26 @@ dependencies = [ "syn", ] +[[package]] +name = "threadpool" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d050e60b33d41c19108b32cea32164033a9013fe3b46cbd4457559bfbf77afaa" +dependencies = [ + "num_cpus", +] + +[[package]] +name = "tiff" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cfada0986f446a770eca461e8c6566cb879682f7d687c8348aa0c857bd52286" +dependencies = [ + "flate2", + "jpeg-decoder 0.2.4", + "weezl", +] + [[package]] name = "toml" version = "0.5.8" @@ -1332,6 +1647,12 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -1485,6 +1806,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "weezl" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8b77fdfd5a253be4ab714e4ffa3c49caf146b4de743e97510c0656cf90f1e8e" + [[package]] name = "wide" version = "0.7.4" diff --git a/Cargo.toml b/Cargo.toml index d11c772..c32f0ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,6 @@ edition = "2021" glium = "0.31.0" nalgebra-glm = "0.16.0" +image = "0.24.1" gltf = { version = "1.0.0", features = ["utils"] } mlua = { version = "0.7.4", features = ["lua54", "macros"] } \ No newline at end of file diff --git a/container.png b/container.png new file mode 100644 index 0000000..22f9627 Binary files /dev/null and b/container.png differ diff --git a/main.lua b/main.lua index b6b5ddf..3567972 100644 --- a/main.lua +++ b/main.lua @@ -1,16 +1,24 @@ print("Hello from a lua script!") + +local roll_x = 0.0 + function couch.init() print("Hello from \"init\"") end function couch.update(delta) local transform = couch.root:find_node("cube"):get_transform() - transform.rotation[2] = transform.rotation[2] + delta + transform.rotation[2] = transform.rotation[2] + delta * roll_x transform.rotation[1] = transform.rotation[1] - delta * 0.3 couch.root:find_node("cube"):set_transform(transform) end -function couch.keyreleased(scancode) - print("Key was released") +function couch.keypressed(scancode) + if scancode == 30 then roll_x = 1.0 end + if scancode == 32 then roll_x = -1.0 end +end + +function couch.keyreleased(scancode) + if scancode == 30 or scancode == 32 then roll_x = 0.0 end end diff --git a/src/flat.frag b/src/flat.frag index bf77901..f0fcad9 100644 --- a/src/flat.frag +++ b/src/flat.frag @@ -1,10 +1,12 @@ #version 330 -in vec3 DIFFUSE; in vec3 NORMAL; +in vec2 UV; + +uniform sampler2D tex; out vec4 color; void main() { - color = vec4(DIFFUSE, 1.0); + color = texture(tex, UV); } diff --git a/src/flat.vert b/src/flat.vert index 20046be..220f68c 100644 --- a/src/flat.vert +++ b/src/flat.vert @@ -2,14 +2,16 @@ in vec3 position; in vec3 normal; +in vec2 uv; uniform mat4 MVP; +uniform sampler2D tex; -out vec3 DIFFUSE; out vec3 NORMAL; +out vec2 UV; void main() { gl_Position = MVP * vec4(position, 1.0); - DIFFUSE = vec3(1.0, 0.0, 0.0); NORMAL = normal; + UV = uv; } diff --git a/src/main.rs b/src/main.rs index 9efaa99..e8f83e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ fn main() { // Initial scene setup let mut root = root.borrow_mut(); - let mut cube = Mesh::new(Model::new(&display, "Box.glb")); + let mut cube = Mesh::new(Model::new(&display, "Box.glb", "container.png")); cube.set_transform(gameobject::Transform { position: glm::vec3(0.0, 0.0, -10.0), rotation: glm::vec3(0.0, 15.0_f32.to_radians(), 0.0), @@ -102,10 +102,7 @@ fn main() { let mvp = glm::rotate_y(&mvp, cube.get_transform().rotation.y); let mvp = glm::rotate_z(&mvp, cube.get_transform().rotation.z); - let uniforms = uniform! { - MVP: *mvp.as_ref(), - }; - cube.model.draw(&mut target, &program, &uniforms, ¶ms); + cube.model.draw(&mut target, &program, &mvp, ¶ms); target.finish().unwrap(); } diff --git a/src/model.rs b/src/model.rs index a1e7c49..dc4769d 100644 --- a/src/model.rs +++ b/src/model.rs @@ -1,22 +1,27 @@ extern crate gltf; +use nalgebra_glm as glm; + use glium::{ VertexBuffer, IndexBuffer }; +use glium::texture::SrgbTexture2d; #[derive(Copy, Clone)] struct Vertex { position: [f32; 3], normal: [f32; 3], + uv: [f32; 2], } -implement_vertex!(Vertex, position, normal); +implement_vertex!(Vertex, position, normal, uv); pub struct Model { vb: VertexBuffer, ib: IndexBuffer, + texture: SrgbTexture2d, } impl Model { - pub fn new(display: &glium::Display, file: &str) -> Self { + pub fn new(display: &glium::Display, file: &str, image_file: &str) -> Self { let (document, buffers, _images) = gltf::import(file).expect("Could not load gltf file"); let mesh = document.meshes().next().unwrap(); let primitive = mesh.primitives().next().unwrap(); @@ -25,23 +30,35 @@ impl Model { let mut vertices: Vec = Vec::new(); let positions = reader.read_positions().unwrap(); let normals = reader.read_normals().unwrap(); - for (position, normal) in positions.zip(normals) { - vertices.push(Vertex { position, normal }); + let uvs = reader.read_tex_coords(0).unwrap().into_f32(); + + for ((position, normal), uv) in positions.zip(normals).zip(uvs) { + vertices.push(Vertex { position, normal, uv}); } let vb = VertexBuffer::new(display, &vertices).unwrap(); let indices: Vec = reader.read_indices().unwrap().into_u32().collect(); let ib = IndexBuffer::new(display, glium::index::PrimitiveType::TrianglesList, &indices).unwrap(); - Model { vb, ib } + let image = image::io::Reader::open(image_file).unwrap().decode().unwrap().to_rgba8(); + let image_dimensions = image.dimensions(); + let image = glium::texture::RawImage2d::from_raw_rgba_reversed(&image.into_raw(), image_dimensions); + let texture = glium::texture::SrgbTexture2d::new(display, image).unwrap(); + + Model { vb, ib, texture } } pub fn draw(&self, target: &mut impl glium::Surface, program: &glium::Program, - uniforms: &impl glium::uniforms::Uniforms, + mvp: &glm::Mat4, params: &glium::DrawParameters ) { - target.draw(&self.vb, &self.ib, program, uniforms, params).unwrap() + let uniforms = uniform! { + MVP: *mvp.as_ref(), + tex: glium::uniforms::Sampler::new(&self.texture), + }; + + target.draw(&self.vb, &self.ib, program, &uniforms, params).unwrap() } } diff --git a/src/scripting/lua.rs b/src/scripting/lua.rs index 4cb4d92..12f7c7d 100644 --- a/src/scripting/lua.rs +++ b/src/scripting/lua.rs @@ -17,10 +17,12 @@ impl Lua { pub fn new(root: Rc>) -> Self { // Create a lua context, load std libraries let ctx = LuaContext::new(); + // Create the ``couch'' api let couch = ctx.create_table().unwrap(); couch.set("root", ctx.create_userdata(root).unwrap()).unwrap(); - // Hook in to globals + + // Hook into globals ctx.globals().set("couch", couch).unwrap(); let path = std::path::Path::new("main.lua"); let buf = std::fs::read(&path).expect("Could not find main.lua");