2020-02-16 22:38:34 +00:00
|
|
|
use glam::{Mat4, Vec3, Vec4};
|
2020-01-08 23:58:36 +00:00
|
|
|
|
2020-01-06 01:25:17 +00:00
|
|
|
use sdl2::event::Event;
|
2020-01-18 05:18:08 +00:00
|
|
|
use sdl2::keyboard::{Keycode, Scancode};
|
2020-02-17 02:57:30 +00:00
|
|
|
use std::collections::*;
|
|
|
|
use std::iter::FromIterator;
|
2020-02-16 22:58:45 +00:00
|
|
|
use std::time::{Duration};
|
2020-01-06 01:25:17 +00:00
|
|
|
|
2020-02-16 23:24:33 +00:00
|
|
|
use opengl_rust::*;
|
2020-01-09 03:15:41 +00:00
|
|
|
|
2020-02-16 23:21:32 +00:00
|
|
|
use file::load_small_file;
|
2020-02-16 22:41:24 +00:00
|
|
|
use iqm::Model;
|
2020-02-16 22:56:35 +00:00
|
|
|
use shader::{ShaderProgram, ShaderObject};
|
2020-02-16 23:07:32 +00:00
|
|
|
use texture::Texture;
|
2020-02-16 22:58:45 +00:00
|
|
|
use timestep::TimeStep;
|
2020-01-09 00:18:12 +00:00
|
|
|
|
2020-02-16 22:56:35 +00:00
|
|
|
pub fn color_from_255 <V> (rgb: V) -> Vec3
|
|
|
|
where V: Into <Vec3>
|
|
|
|
{
|
|
|
|
let rgb: Vec3 = rgb.into ();
|
|
|
|
|
|
|
|
Vec3::from ((
|
|
|
|
rgb.x () / 255.0,
|
|
|
|
rgb.y () / 255.0,
|
|
|
|
rgb.z () / 255.0
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2020-01-26 00:41:59 +00:00
|
|
|
const KEY_LEFT: usize = 0;
|
|
|
|
const KEY_RIGHT: usize = KEY_LEFT + 1;
|
|
|
|
const KEY_UP: usize = KEY_RIGHT + 1;
|
|
|
|
const KEY_DOWN: usize = KEY_UP + 1;
|
|
|
|
|
|
|
|
struct ControllerState {
|
|
|
|
keys: Vec <bool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ControllerState {
|
|
|
|
pub fn from_sdl_keyboard (k: &sdl2::keyboard::KeyboardState) -> Self {
|
|
|
|
let f = |c| k.is_scancode_pressed (c);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
keys: vec! [
|
|
|
|
f (Scancode::Left),
|
|
|
|
f (Scancode::Right),
|
|
|
|
f (Scancode::Up),
|
|
|
|
f (Scancode::Down),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_pressed (&self, code: usize) -> bool {
|
|
|
|
self.keys [code]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WorldState {
|
|
|
|
azimuth: f32,
|
|
|
|
altitude: f32,
|
|
|
|
spin_speed: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WorldState {
|
|
|
|
pub fn new () -> Self {
|
|
|
|
Self {
|
|
|
|
azimuth: 0.0,
|
|
|
|
altitude: 0.0,
|
|
|
|
spin_speed: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn step (&mut self, controller: &ControllerState) {
|
|
|
|
const SPIN_RAMP_TIME: i32 = 30;
|
|
|
|
|
|
|
|
let spin_f = 4.0 * self.spin_speed as f32 / SPIN_RAMP_TIME as f32;
|
|
|
|
|
|
|
|
if controller.is_pressed (KEY_LEFT) {
|
|
|
|
self.spin_speed = std::cmp::min (self.spin_speed + 1, SPIN_RAMP_TIME);
|
|
|
|
self.azimuth += spin_f;
|
|
|
|
}
|
|
|
|
else if controller.is_pressed (KEY_RIGHT) {
|
|
|
|
self.spin_speed = std::cmp::min (self.spin_speed + 1, SPIN_RAMP_TIME);
|
|
|
|
self.azimuth -= spin_f;
|
|
|
|
}
|
|
|
|
else if controller.is_pressed (KEY_UP) {
|
|
|
|
self.spin_speed = std::cmp::min (self.spin_speed + 1, SPIN_RAMP_TIME);
|
|
|
|
self.altitude = f32::min (90.0, self.altitude + spin_f);
|
|
|
|
}
|
|
|
|
else if controller.is_pressed (KEY_DOWN) {
|
|
|
|
self.spin_speed = std::cmp::min (self.spin_speed + 1, SPIN_RAMP_TIME);
|
|
|
|
self.altitude = f32::max (-90.0, self.altitude - spin_f);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.spin_speed = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 02:57:30 +00:00
|
|
|
mod uniforms {
|
|
|
|
use iota::iota;
|
|
|
|
iota! {
|
|
|
|
pub const MVP: u32 = iota;
|
|
|
|
, OBJECT_SPACE_LIGHT
|
|
|
|
, ALBEDO
|
|
|
|
, MIN_ALBEDO
|
|
|
|
, MIN_BRIGHT
|
|
|
|
, TEXTURE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 01:25:17 +00:00
|
|
|
fn main () {
|
|
|
|
let sdl_context = sdl2::init ().unwrap ();
|
|
|
|
let video_subsystem = sdl_context.video ().unwrap ();
|
|
|
|
|
|
|
|
let window = video_subsystem.window ("OpenGL? In my Rust?", 1280, 720)
|
|
|
|
.position_centered ()
|
|
|
|
.opengl ()
|
|
|
|
.build ()
|
|
|
|
.unwrap ();
|
|
|
|
|
|
|
|
gl::load_with (|s| {
|
2020-02-17 02:09:13 +00:00
|
|
|
video_subsystem.gl_get_proc_address (s) as *const _
|
2020-01-06 01:25:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert! (gl::ClearColor::is_loaded ());
|
|
|
|
|
|
|
|
let gl_ctx = window.gl_create_context ().unwrap ();
|
|
|
|
|
2020-01-06 01:57:34 +00:00
|
|
|
window.gl_make_current (&gl_ctx).unwrap ();
|
|
|
|
|
2020-01-06 02:19:41 +00:00
|
|
|
// I love how with Rust I can throw unwrap ()s everywhere
|
|
|
|
// It's safer than C / C++'s default behavior of unchecked errors
|
|
|
|
// It's more programmer-friendly and explicit than C#'s unchecked
|
|
|
|
// exceptions that can appear almost anywhere at runtime with no
|
|
|
|
// compile-time warning
|
|
|
|
// And I'm still not actually checking errors - Just checkmarking
|
|
|
|
// that I know where they are.
|
|
|
|
|
2020-02-17 02:00:41 +00:00
|
|
|
let vert_shader = ShaderObject::from_file (gl::VERTEX_SHADER, "shaders/pumpkin-vert.glsl").unwrap ();
|
|
|
|
let frag_shader = ShaderObject::from_file (gl::FRAGMENT_SHADER, "shaders/pumpkin-frag.glsl").unwrap ();
|
2020-01-06 01:57:34 +00:00
|
|
|
|
2020-01-06 02:19:41 +00:00
|
|
|
let shader_program = ShaderProgram::new (&vert_shader, &frag_shader).unwrap ();
|
|
|
|
|
2020-02-17 02:57:30 +00:00
|
|
|
let uni_lookup: HashMap <_, &str> = HashMap::from_iter ({
|
|
|
|
use uniforms::*;
|
|
|
|
vec! [
|
|
|
|
(MVP, "mvp"),
|
|
|
|
(OBJECT_SPACE_LIGHT, "object_space_light"),
|
|
|
|
(ALBEDO, "albedo"),
|
|
|
|
(MIN_ALBEDO, "min_albedo"),
|
|
|
|
(MIN_BRIGHT, "min_bright"),
|
|
|
|
(TEXTURE, "texture"),
|
|
|
|
].into_iter ()
|
|
|
|
});
|
|
|
|
|
|
|
|
let unis = shader_program.get_uniform_locations (uni_lookup.values ().map (|s| *s));
|
|
|
|
|
|
|
|
let unis: HashMap <u32, i32> = HashMap::from_iter (
|
|
|
|
uni_lookup.iter ()
|
|
|
|
.map (|(key, name)| (*key, *unis.get (*name).unwrap ()))
|
|
|
|
);
|
2020-01-07 05:50:20 +00:00
|
|
|
|
2020-02-16 23:18:23 +00:00
|
|
|
let attrs = shader_program.get_attribute_locations (vec! [
|
2020-01-07 05:50:20 +00:00
|
|
|
"pos",
|
|
|
|
"uv",
|
2020-01-12 18:46:16 +00:00
|
|
|
"normal",
|
2020-02-16 23:18:23 +00:00
|
|
|
].into_iter ());
|
2020-01-07 05:50:20 +00:00
|
|
|
|
2020-02-16 23:07:32 +00:00
|
|
|
let texture = Texture::from_file ("sky.png");
|
|
|
|
texture.bind ();
|
2020-01-15 03:45:47 +00:00
|
|
|
|
2020-02-16 23:21:32 +00:00
|
|
|
let model_data = load_small_file ("pumpking.iqm", 1024 * 1024);
|
2020-02-16 22:41:24 +00:00
|
|
|
let model = Model::from_slice (&model_data [..]);
|
2020-01-09 03:55:44 +00:00
|
|
|
|
2020-02-17 01:22:05 +00:00
|
|
|
let renderable_model = glezz::RenderableModel::from_iqm (&model);
|
2020-02-17 00:32:52 +00:00
|
|
|
|
2020-02-16 23:21:32 +00:00
|
|
|
let sky_data = load_small_file ("sky-sphere.iqm", 1024 * 1024);
|
2020-02-16 22:41:24 +00:00
|
|
|
let sky_model = Model::from_slice (&sky_data [..]);
|
2020-01-15 03:45:47 +00:00
|
|
|
|
2020-02-17 01:53:50 +00:00
|
|
|
let renderable_sky = glezz::RenderableModel::from_iqm (&sky_model);
|
|
|
|
|
2020-02-16 23:49:38 +00:00
|
|
|
glezz::enable_vertex_attrib_array (attrs ["pos"]);
|
|
|
|
glezz::enable_vertex_attrib_array (attrs ["uv"]);
|
|
|
|
glezz::enable_vertex_attrib_array (attrs ["normal"]);
|
2020-02-16 23:32:35 +00:00
|
|
|
|
|
|
|
glezz::enable (gl::DEPTH_TEST);
|
2020-02-17 01:53:50 +00:00
|
|
|
glezz::enable (gl::TEXTURE_2D);
|
2020-01-06 02:19:41 +00:00
|
|
|
|
2020-01-26 00:54:16 +00:00
|
|
|
let mut time_step = TimeStep::new (60, 1000);
|
2020-01-26 00:41:59 +00:00
|
|
|
let mut state = WorldState::new ();
|
2020-01-12 18:46:16 +00:00
|
|
|
|
2020-01-06 01:25:17 +00:00
|
|
|
let mut event_pump = sdl_context.event_pump ().unwrap ();
|
|
|
|
'running: loop {
|
2020-01-26 00:54:16 +00:00
|
|
|
let frames_to_do = time_step.step ();
|
2020-01-12 18:46:16 +00:00
|
|
|
|
2020-01-26 00:41:59 +00:00
|
|
|
let controller = ControllerState::from_sdl_keyboard (&event_pump.keyboard_state ());
|
2020-01-18 05:18:08 +00:00
|
|
|
|
2020-01-26 00:54:16 +00:00
|
|
|
for _ in 0..frames_to_do {
|
|
|
|
state.step (&controller);
|
2020-01-12 18:46:16 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 01:57:34 +00:00
|
|
|
let _mouse = event_pump.mouse_state ();
|
2020-01-06 01:25:17 +00:00
|
|
|
|
|
|
|
for event in event_pump.poll_iter() {
|
|
|
|
match event {
|
|
|
|
Event::Quit {..} |
|
|
|
|
Event::KeyDown { keycode: Some (Keycode::Escape), .. } => {
|
|
|
|
break 'running
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.gl_make_current (&gl_ctx).unwrap ();
|
|
|
|
|
2020-01-26 00:41:59 +00:00
|
|
|
let longitude = state.azimuth.to_radians ();
|
|
|
|
let latitude = (state.altitude - 90.0).to_radians ();
|
2020-01-13 04:07:56 +00:00
|
|
|
|
2020-01-15 03:45:47 +00:00
|
|
|
let proj_mat = Mat4::perspective_rh_gl (30.0f32.to_radians (), 1280.0 / 720.0, 0.5, 500.0);
|
|
|
|
|
2020-01-12 18:46:16 +00:00
|
|
|
let model_mat =
|
2020-01-12 19:10:58 +00:00
|
|
|
Mat4::from_translation (Vec3::from ((0.0, 0.0, -2.7 * 0.5)))
|
2020-01-12 18:46:16 +00:00
|
|
|
;
|
|
|
|
|
2020-01-15 03:45:47 +00:00
|
|
|
let view_mat = proj_mat *
|
2020-01-13 04:07:56 +00:00
|
|
|
Mat4::from_translation (Vec3::from ((0.0, 0.0, -8.0))) *
|
2020-01-15 03:45:47 +00:00
|
|
|
Mat4::from_rotation_x (latitude) *
|
|
|
|
Mat4::from_rotation_z (longitude)
|
2020-01-13 04:07:56 +00:00
|
|
|
;
|
|
|
|
|
2020-01-15 03:45:47 +00:00
|
|
|
let mvp_mat = view_mat * model_mat;
|
|
|
|
|
|
|
|
let sky_mvp_mat = view_mat * Mat4::from_scale (Vec3::from ((16.0, 16.0, 16.0)));
|
|
|
|
|
2020-01-13 04:07:56 +00:00
|
|
|
let light = Vec3::from ((2.0, 0.0, 5.0)).normalize ();
|
|
|
|
let object_space_light = model_mat.inverse () * Vec4::from ((light.x (), light.y (), light.z (), 0.0));
|
2020-02-16 23:44:01 +00:00
|
|
|
let object_space_light = Vec3::from ((object_space_light.x (), object_space_light.y (), object_space_light.z ()));
|
2020-01-12 18:46:16 +00:00
|
|
|
|
2020-02-16 22:56:35 +00:00
|
|
|
let orange = color_from_255 ((255.0, 154.0, 0.0));
|
|
|
|
let green = color_from_255 ((14.0, 127.0, 24.0));
|
2020-01-15 03:45:47 +00:00
|
|
|
let white = Vec3::from ((1.0, 1.0, 1.0));
|
|
|
|
let black = Vec3::from ((0.0, 0.0, 0.0));
|
2020-01-12 18:46:16 +00:00
|
|
|
|
|
|
|
let orange = orange * orange;
|
|
|
|
let green = green * green;
|
|
|
|
|
2020-02-16 23:32:35 +00:00
|
|
|
glezz::clear_color (1.0f32, 1.0f32, 1.0f32, 1.0f32);
|
|
|
|
glezz::clear (gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
|
|
|
|
glezz::disable (gl::CULL_FACE);
|
|
|
|
|
2020-02-17 02:57:30 +00:00
|
|
|
{
|
|
|
|
use uniforms::*;
|
|
|
|
glezz::uniform_3fv (unis [&MIN_BRIGHT], &black);
|
|
|
|
glezz::uniform_3fv (unis [&MIN_ALBEDO], &white);
|
|
|
|
glezz::uniform_3fv (unis [&ALBEDO], &orange);
|
|
|
|
|
|
|
|
glezz::uniform_matrix_4fv (unis [&MVP], &mvp_mat);
|
|
|
|
glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &object_space_light);
|
2020-01-15 03:45:47 +00:00
|
|
|
|
2020-02-17 02:57:30 +00:00
|
|
|
renderable_model.draw (&attrs, 0);
|
|
|
|
|
|
|
|
glezz::uniform_3fv (unis [&ALBEDO], &green);
|
|
|
|
renderable_model.draw (&attrs, 1);
|
|
|
|
|
|
|
|
let draw_sky = true;
|
|
|
|
if draw_sky {
|
|
|
|
glezz::uniform_matrix_4fv (unis [&MVP], &sky_mvp_mat);
|
|
|
|
glezz::uniform_3fv (unis [&ALBEDO], &white);
|
|
|
|
glezz::uniform_3fv (unis [&MIN_BRIGHT], &white);
|
|
|
|
glezz::uniform_3fv (unis [&MIN_ALBEDO], &black);
|
|
|
|
glezz::uniform_1i (unis [&TEXTURE], 0);
|
|
|
|
|
|
|
|
renderable_sky.draw (&attrs, 0);
|
|
|
|
}
|
2020-01-06 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
window.gl_swap_window ();
|
|
|
|
|
2020-02-17 02:09:13 +00:00
|
|
|
std::thread::sleep (Duration::from_millis (15));
|
2020-01-06 01:25:17 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-08 23:58:36 +00:00
|
|
|
|
|
|
|
#[cfg (test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn iqm () {
|
2020-01-09 00:18:12 +00:00
|
|
|
|
2020-01-08 23:58:36 +00:00
|
|
|
}
|
|
|
|
}
|