diff --git a/Cargo.lock b/Cargo.lock index ab3a5fa..55ccabe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,11 +1,35 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "addr2line" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b6a2d3371669ab3ca9797670853d61402b03d0b4b9ebf33d677dfa720203072" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e" + [[package]] name = "adler32" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" +[[package]] +name = "aho-corasick" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86" +dependencies = [ + "memchr", +] + [[package]] name = "approx" version = "0.3.2" @@ -27,6 +51,20 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" +[[package]] +name = "backtrace" +version = "0.3.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46254cf2fdcdf1badb5934448c1bcbe046a56537b3987d96c51a7afc5d03f293" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + [[package]] name = "bitflags" version = "1.2.1" @@ -59,7 +97,7 @@ checksum = "283944cdecc44bf0b8dd010ec9af888d3b4f142844fdbe026c20ef68148d6fe7" dependencies = [ "approx", "num-traits", - "rand", + "rand 0.6.5", ] [[package]] @@ -160,6 +198,7 @@ dependencies = [ "cgmath", "gl", "glfw", + "human-panic", "image", ] @@ -175,6 +214,17 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" +[[package]] +name = "getrandom" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "gif" version = "0.10.3" @@ -185,6 +235,12 @@ dependencies = [ "lzw", ] +[[package]] +name = "gimli" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aaf91faf136cb47367fa430cd46e37a788775e7fa104f8b4bcb3861dc389b724" + [[package]] name = "gl" version = "0.14.0" @@ -239,6 +295,21 @@ dependencies = [ "libc", ] +[[package]] +name = "human-panic" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39f357a500abcbd7c5f967c1d45c8838585b36743823b9d43488f24850534e36" +dependencies = [ + "backtrace", + "os_type", + "serde", + "serde_derive", + "termcolor", + "toml", + "uuid", +] + [[package]] name = "image" version = "0.22.5" @@ -323,6 +394,12 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" +[[package]] +name = "memchr" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" + [[package]] name = "memoffset" version = "0.5.5" @@ -332,15 +409,24 @@ dependencies = [ "autocfg 1.0.0", ] +[[package]] +name = "miniz_oxide" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be0f75932c1f6cfae3c04000e40114adf955636e19040f9c0a2c380702aa1c7f" +dependencies = [ + "adler", +] + [[package]] name = "num-derive" version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eafd0b45c5537c3ba526f79d3e75120036502bebacbb3f3220914067ce39dbf2" dependencies = [ - "proc-macro2", - "quote", - "syn", + "proc-macro2 0.4.30", + "quote 0.6.13", + "syn 0.15.44", ] [[package]] @@ -403,6 +489,21 @@ dependencies = [ "malloc_buf", ] +[[package]] +name = "object" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ab52be62400ca80aa00285d25253d7f7c437b7375c4de678f5405d3afe82ca5" + +[[package]] +name = "os_type" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7edc011af0ae98b7f88cf7e4a83b70a54a75d2b8cb013d6efd02e5956207e9eb" +dependencies = [ + "regex", +] + [[package]] name = "png" version = "0.15.3" @@ -415,13 +516,28 @@ dependencies = [ "inflate", ] +[[package]] +name = "ppv-lite86" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea" + [[package]] name = "proc-macro2" version = "0.4.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" dependencies = [ - "unicode-xid", + "unicode-xid 0.1.0", +] + +[[package]] +name = "proc-macro2" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04f5f085b5d71e2188cb8271e5da0161ad52c3f227a661a3c135fdf28e258b12" +dependencies = [ + "unicode-xid 0.2.1", ] [[package]] @@ -430,7 +546,16 @@ version = "0.6.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" dependencies = [ - "proc-macro2", + "proc-macro2 0.4.30", +] + +[[package]] +name = "quote" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +dependencies = [ + "proc-macro2 1.0.19", ] [[package]] @@ -441,9 +566,9 @@ checksum = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" dependencies = [ "autocfg 0.1.7", "libc", - "rand_chacha", + "rand_chacha 0.1.1", "rand_core 0.4.2", - "rand_hc", + "rand_hc 0.1.0", "rand_isaac", "rand_jitter", "rand_os", @@ -452,6 +577,19 @@ dependencies = [ "winapi", ] +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom", + "libc", + "rand_chacha 0.2.2", + "rand_core 0.5.1", + "rand_hc 0.2.0", +] + [[package]] name = "rand_chacha" version = "0.1.1" @@ -462,6 +600,16 @@ dependencies = [ "rand_core 0.3.1", ] +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core 0.5.1", +] + [[package]] name = "rand_core" version = "0.3.1" @@ -477,6 +625,15 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom", +] + [[package]] name = "rand_hc" version = "0.1.0" @@ -486,6 +643,15 @@ dependencies = [ "rand_core 0.3.1", ] +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core 0.5.1", +] + [[package]] name = "rand_isaac" version = "0.1.1" @@ -582,6 +748,30 @@ dependencies = [ "rand_core 0.3.1", ] +[[package]] +name = "regex" +version = "1.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", + "thread_local", +] + +[[package]] +name = "regex-syntax" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" + +[[package]] +name = "rustc-demangle" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" + [[package]] name = "scoped_threadpool" version = "0.1.9" @@ -609,15 +799,61 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" +[[package]] +name = "serde" +version = "1.0.115" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e54c9a88f2da7238af84b5101443f0c0d0a3bbdc455e34a5c9497b1903ed55d5" + +[[package]] +name = "serde_derive" +version = "1.0.115" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "609feed1d0a73cc36a0182a840a9b37b4a82f0b1150369f0536a9e3f2a31dc48" +dependencies = [ + "proc-macro2 1.0.19", + "quote 1.0.7", + "syn 1.0.38", +] + [[package]] name = "syn" version = "0.15.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" dependencies = [ - "proc-macro2", - "quote", - "unicode-xid", + "proc-macro2 0.4.30", + "quote 0.6.13", + "unicode-xid 0.1.0", +] + +[[package]] +name = "syn" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e69abc24912995b3038597a7a593be5053eb0fb44f3cc5beec0deb421790c1f4" +dependencies = [ + "proc-macro2 1.0.19", + "quote 1.0.7", + "unicode-xid 0.2.1", +] + +[[package]] +name = "termcolor" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "thread_local" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" +dependencies = [ + "lazy_static", ] [[package]] @@ -632,12 +868,42 @@ dependencies = [ "num-traits", ] +[[package]] +name = "toml" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a" +dependencies = [ + "serde", +] + [[package]] name = "unicode-xid" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + +[[package]] +name = "uuid" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" +dependencies = [ + "rand 0.7.3", +] + +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + [[package]] name = "winapi" version = "0.3.9" @@ -654,6 +920,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 19a0ede..97ac7c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,4 +19,5 @@ image = "0.22.5" # only needed from chapter 3 on # tobj = "0.1.6" # num = "0.2.0" -# rand = "0.5.5" \ No newline at end of file +# rand = "0.5.5" +human-panic = "1.0.3" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a72c684..e17236c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,22 +2,36 @@ extern crate gl; extern crate glfw; extern crate image; -use self::glfw::{Action, Context, Key}; use self::gl::types::*; +use self::glfw::{Action, Context, Key}; +use cgmath::prelude::*; +use cgmath::{perspective, vec3, Deg, Matrix4, Point3, Rad, Vector3}; +use human_panic::setup_panic; +use image::GenericImageView; use std::ffi::CString; use std::mem; use std::os::raw::c_void; use std::ptr; use std::sync::mpsc::Receiver; -use cgmath::prelude::*; -use cgmath::{perspective, vec3, Deg, Matrix4, Rad}; -use image::GenericImageView; - mod consts; mod macros; mod shader; +const CUBES_POS: [Vector3; 10] = [ + vec3(0.0, 0.0, 0.0), + vec3(2.0, 5.0, -15.0), + vec3(-1.5, -2.2, -2.5), + vec3(-3.8, -2.0, -12.3), + vec3(2.4, -0.4, -3.5), + vec3(-1.7, 3.0, -7.5), + vec3(1.3, -2.0, -2.5), + vec3(1.5, 2.0, -2.5), + vec3(1.5, 0.2, -1.5), + vec3(-1.3, 1.0, -1.5), +]; + pub fn main() { + setup_panic!(); // glfw: initialize and configure // ------------------------------ let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); @@ -56,68 +70,47 @@ pub fn main() { // ------------------------------------------------------------------ // HINT: type annotation is crucial since default for float literals is f64 let vertices: [f32; 180] = [ - -0.5, -0.5, -0.5, 0.0, 0.0, - 0.5, -0.5, -0.5, 1.0, 0.0, - 0.5, 0.5, -0.5, 1.0, 1.0, - 0.5, 0.5, -0.5, 1.0, 1.0, - -0.5, 0.5, -0.5, 0.0, 1.0, - -0.5, -0.5, -0.5, 0.0, 0.0, + -0.5, -0.5, -0.5, 0.0, 0.0, 0.5, -0.5, -0.5, 1.0, 0.0, 0.5, 0.5, -0.5, 1.0, 1.0, 0.5, + 0.5, -0.5, 1.0, 1.0, -0.5, 0.5, -0.5, 0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 0.0, -0.5, -0.5, + 0.5, 0.0, 0.0, 0.5, -0.5, 0.5, 1.0, 0.0, 0.5, 0.5, 0.5, 1.0, 1.0, 0.5, 0.5, 0.5, 1.0, + 1.0, -0.5, 0.5, 0.5, 0.0, 1.0, -0.5, -0.5, 0.5, 0.0, 0.0, -0.5, 0.5, 0.5, 1.0, 0.0, + -0.5, 0.5, -0.5, 1.0, 1.0, -0.5, -0.5, -0.5, 0.0, 1.0, -0.5, -0.5, -0.5, 0.0, 1.0, + -0.5, -0.5, 0.5, 0.0, 0.0, -0.5, 0.5, 0.5, 1.0, 0.0, 0.5, 0.5, 0.5, 1.0, 0.0, 0.5, 0.5, + -0.5, 1.0, 1.0, 0.5, -0.5, -0.5, 0.0, 1.0, 0.5, -0.5, -0.5, 0.0, 1.0, 0.5, -0.5, 0.5, + 0.0, 0.0, 0.5, 0.5, 0.5, 1.0, 0.0, -0.5, -0.5, -0.5, 0.0, 1.0, 0.5, -0.5, -0.5, 1.0, + 1.0, 0.5, -0.5, 0.5, 1.0, 0.0, 0.5, -0.5, 0.5, 1.0, 0.0, -0.5, -0.5, 0.5, 0.0, 0.0, + -0.5, -0.5, -0.5, 0.0, 1.0, -0.5, 0.5, -0.5, 0.0, 1.0, 0.5, 0.5, -0.5, 1.0, 1.0, 0.5, + 0.5, 0.5, 1.0, 0.0, 0.5, 0.5, 0.5, 1.0, 0.0, -0.5, 0.5, 0.5, 0.0, 0.0, -0.5, 0.5, -0.5, + 0.0, 1.0, + ]; + let (mut vbo, mut vao) = (0, 0); + gl::GenVertexArrays(1, &mut vao); + gl::GenBuffers(1, &mut vbo); - -0.5, -0.5, 0.5, 0.0, 0.0, - 0.5, -0.5, 0.5, 1.0, 0.0, - 0.5, 0.5, 0.5, 1.0, 1.0, - 0.5, 0.5, 0.5, 1.0, 1.0, - -0.5, 0.5, 0.5, 0.0, 1.0, - -0.5, -0.5, 0.5, 0.0, 0.0, + gl::BindVertexArray(vao); - -0.5, 0.5, 0.5, 1.0, 0.0, - -0.5, 0.5, -0.5, 1.0, 1.0, - -0.5, -0.5, -0.5, 0.0, 1.0, - -0.5, -0.5, -0.5, 0.0, 1.0, - -0.5, -0.5, 0.5, 0.0, 0.0, - -0.5, 0.5, 0.5, 1.0, 0.0, - - 0.5, 0.5, 0.5, 1.0, 0.0, - 0.5, 0.5, -0.5, 1.0, 1.0, - 0.5, -0.5, -0.5, 0.0, 1.0, - 0.5, -0.5, -0.5, 0.0, 1.0, - 0.5, -0.5, 0.5, 0.0, 0.0, - 0.5, 0.5, 0.5, 1.0, 0.0, - - -0.5, -0.5, -0.5, 0.0, 1.0, - 0.5, -0.5, -0.5, 1.0, 1.0, - 0.5, -0.5, 0.5, 1.0, 0.0, - 0.5, -0.5, 0.5, 1.0, 0.0, - -0.5, -0.5, 0.5, 0.0, 0.0, - -0.5, -0.5, -0.5, 0.0, 1.0, - - -0.5, 0.5, -0.5, 0.0, 1.0, - 0.5, 0.5, -0.5, 1.0, 1.0, - 0.5, 0.5, 0.5, 1.0, 0.0, - 0.5, 0.5, 0.5, 1.0, 0.0, - -0.5, 0.5, 0.5, 0.0, 0.0, - -0.5, 0.5, -0.5, 0.0, 1.0 - ]; - let (mut vbo, mut vao) = (0, 0); - gl::GenVertexArrays(1, &mut vao); - gl::GenBuffers(1, &mut vbo); - - gl::BindVertexArray(vao); - - gl::BindBuffer(gl::ARRAY_BUFFER, vbo); - gl::BufferData(gl::ARRAY_BUFFER, - (vertices.len() * mem::size_of::()) as GLsizeiptr, - &vertices[0] as *const f32 as *const c_void, - gl::STATIC_DRAW); - - let stride = 5 * mem::size_of::() as GLsizei; - // position attribute - gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, stride, ptr::null()); - gl::EnableVertexAttribArray(0); - // texture coord attribute - gl::VertexAttribPointer(1, 2, gl::FLOAT, gl::FALSE, stride, (3 * mem::size_of::()) as *const c_void); - gl::EnableVertexAttribArray(1); + gl::BindBuffer(gl::ARRAY_BUFFER, vbo); + gl::BufferData( + gl::ARRAY_BUFFER, + (vertices.len() * mem::size_of::()) as GLsizeiptr, + &vertices[0] as *const f32 as *const c_void, + gl::STATIC_DRAW, + ); + let stride = 5 * mem::size_of::() as GLsizei; + // position attribute + gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, stride, ptr::null()); + gl::EnableVertexAttribArray(0); + // texture coord attribute + gl::VertexAttribPointer( + 1, + 2, + gl::FLOAT, + gl::FALSE, + stride, + (3 * mem::size_of::()) as *const c_void, + ); + gl::EnableVertexAttribArray(1); // uncomment this call to draw in wireframe polygons. // gl::PolygonMode(gl::FRONT_AND_BACK, gl::LINE); @@ -133,7 +126,9 @@ pub fn main() { gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR as i32); gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::LINEAR as i32); // load image, create texture and generate mipmaps - let img = image::open("resources/garlic_dog_space.jpg").unwrap().flipv(); + let img = image::open("resources/garlic_dog_space.jpg") + .unwrap() + .flipv(); let data = img.raw_pixels(); // let data = flipped @@ -158,11 +153,11 @@ pub fn main() { let color_r = 0.188; let color_g = 0.22; let color_b = 0.235; - let mut view_modifier : f32 = 0.5; - let mut view_multiplier = 0.01; + let mut view_modifier: f32 = 0.5; + let mut view_multiplier = 0.03; while !window.should_close() { view_modifier = view_modifier + view_multiplier; - if view_modifier > 2.0 || view_modifier < 0.0 { + if view_modifier > 5.0 || view_modifier < 0.0 { view_multiplier = -view_multiplier; } // events @@ -180,10 +175,23 @@ pub fn main() { // create transformations // NOTE: cgmath requires axis vectors to be normalized! - let model: Matrix4 = Matrix4::from_axis_angle(vec3(0.5, 1.0, 0.0).normalize(), - Rad(glfw.get_time() as f32)); - let view: Matrix4 = Matrix4::from_translation(vec3(0., 0., -5.0+view_modifier)); - let projection: Matrix4 = perspective(Deg(45.0), consts::SCR_WIDTH as f32 / consts::SCR_HEIGHT as f32, 0.1, 100.0); + let model: Matrix4 = Matrix4::from_axis_angle( + vec3(0.5, 1.0, 0.0).normalize(), + Rad(glfw.get_time() as f32), + ); + // camera/view transformation + let radius: f32 = 10.0; + let time = glfw.get_time() as f32; + let cam_pos = Point3::new(time.sin() * radius, view_modifier, time.cos() * radius); + let view: Matrix4 = + Matrix4::look_at(cam_pos, Point3::new(0.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0)); + + let projection: Matrix4 = perspective( + Deg(45.0), + consts::SCR_WIDTH as f32 / consts::SCR_HEIGHT as f32, + 0.1, + 100.0, + ); // retrieve the matrix uniform locations let model_loc = gl::GetUniformLocation(shader_object.ID, c_str!("model").as_ptr()); let view_loc = gl::GetUniformLocation(shader_object.ID, c_str!("view").as_ptr()); @@ -194,7 +202,16 @@ pub fn main() { shader_object.setMat4(c_str!("projection"), &projection); gl::BindVertexArray(vao); - gl::DrawArrays(gl::TRIANGLES, 0, 36); + for (i, position) in CUBES_POS.iter().enumerate() { + // calculate the model matrix for each object and pass it to shader before drawing + let mut model: Matrix4 = Matrix4::from_translation(*position); + let angle = 20.0 * i as f32; + model = + model * Matrix4::from_axis_angle(vec3(1.0, 0.3, 0.5).normalize(), Deg(angle)); + shader_object.setMat4(c_str!("model"), &model); + + gl::DrawArrays(gl::TRIANGLES, 0, 36); + } } // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) diff --git a/src/shader.rs b/src/shader.rs index 6ec5d32..b60f493 100644 --- a/src/shader.rs +++ b/src/shader.rs @@ -114,12 +114,12 @@ impl Shader { /// utility function for checking shader compilation/linking errors. /// ------------------------------------------------------------------------ unsafe fn checkCompileErrors(&self, shader: u32, type_: &str) { - let mut success = gl::FALSE as GLint; + let mut is_success = gl::FALSE as GLint; let mut infoLog = Vec::with_capacity(1024); infoLog.set_len(1024 - 1); // subtract 1 to skip the trailing null character if type_ != "PROGRAM" { - gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut success); - if success != gl::TRUE as GLint { + gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut is_success); + if is_success != gl::TRUE as GLint { gl::GetShaderInfoLog( shader, 1024, @@ -134,8 +134,8 @@ impl Shader { ); } } else { - gl::GetProgramiv(shader, gl::LINK_STATUS, &mut success); - if success != gl::TRUE as GLint { + gl::GetProgramiv(shader, gl::LINK_STATUS, &mut is_success); + if is_success != gl::TRUE as GLint { gl::GetProgramInfoLog( shader, 1024,