2020-03-08 18:08:22 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate maplit;
|
|
|
|
|
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-03-07 02:23:23 +00:00
|
|
|
use std::str;
|
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-17 03:06:52 +00:00
|
|
|
use renderable_model::RenderableModel;
|
2020-02-16 22:56:35 +00:00
|
|
|
use shader::{ShaderProgram, ShaderObject};
|
2020-03-08 17:07:13 +00:00
|
|
|
use shader_closure::*;
|
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! {
|
2020-03-08 04:17:57 +00:00
|
|
|
pub const
|
|
|
|
MVP: u32 = iota;
|
|
|
|
, OBJECT_SPACE_LIGHT
|
|
|
|
, OBJECT_SPACE_SKY
|
|
|
|
, ALBEDO
|
|
|
|
, MIN_ALBEDO
|
|
|
|
, MIN_BRIGHT
|
|
|
|
, TEXTURE
|
2020-02-17 02:57:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 15:05:59 +00:00
|
|
|
fn make_object_space_vec (inverse_model_mat: &Mat4, world_space_vec: &Vec3)
|
|
|
|
-> Vec3
|
|
|
|
{
|
|
|
|
let v = world_space_vec;
|
|
|
|
let v4 = *inverse_model_mat * Vec4::from ((v.x (), v.y (), v.z (), 0.0));
|
|
|
|
Vec3::from ((v4.x (), v4.y (), v4.z ()))
|
|
|
|
}
|
|
|
|
|
2020-03-08 01:45:35 +00:00
|
|
|
fn renderable_from_iqm_file <P> (filename: P) -> RenderableModel
|
|
|
|
where P: AsRef <std::path::Path>
|
|
|
|
{
|
2020-03-08 02:32:17 +00:00
|
|
|
let data = load_small_file (filename, 1024 * 1024).unwrap ();
|
|
|
|
let model = Model::from_slice (&data).unwrap ();
|
2020-03-08 01:45:35 +00:00
|
|
|
|
|
|
|
RenderableModel::from_iqm (&model)
|
|
|
|
}
|
|
|
|
|
2020-03-08 05:26:11 +00:00
|
|
|
fn shader_from_files <P> (vert: P, frag: P) -> ShaderProgram
|
|
|
|
where P: AsRef <std::path::Path>
|
|
|
|
{
|
|
|
|
let vert_shader = ShaderObject::from_file (gl::VERTEX_SHADER, vert).unwrap ();
|
|
|
|
let frag_shader = ShaderObject::from_file (gl::FRAGMENT_SHADER, frag).unwrap ();
|
|
|
|
|
|
|
|
ShaderProgram::new (&vert_shader, &frag_shader).unwrap ()
|
|
|
|
}
|
|
|
|
|
2020-03-08 05:52:26 +00:00
|
|
|
struct Arrow {
|
|
|
|
origin: Vec3,
|
|
|
|
direction: Vec3,
|
|
|
|
}
|
|
|
|
|
2020-03-08 06:02:02 +00:00
|
|
|
struct RenderableArrow {
|
|
|
|
model_mat: Mat4,
|
|
|
|
inv_model_mat: Mat4,
|
|
|
|
}
|
|
|
|
|
2020-03-08 18:08:22 +00:00
|
|
|
#[derive (Copy, Clone)]
|
|
|
|
pub enum FrontFace {
|
|
|
|
Cw,
|
|
|
|
Ccw,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From <FrontFace> for u32 {
|
|
|
|
fn from (v: FrontFace) -> Self {
|
|
|
|
use FrontFace::*;
|
|
|
|
{
|
|
|
|
use gl::*;
|
|
|
|
match v {
|
|
|
|
Cw => CW,
|
|
|
|
Ccw => CCW,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Copy, Clone)]
|
|
|
|
pub enum StencilOp {
|
|
|
|
Keep,
|
|
|
|
Zero,
|
|
|
|
Replace,
|
|
|
|
Incr,
|
|
|
|
Decr,
|
|
|
|
Invert,
|
|
|
|
IncrWrap,
|
|
|
|
DecrWrap,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From <StencilOp> for u32 {
|
|
|
|
fn from (v: StencilOp) -> Self {
|
|
|
|
use StencilOp::*;
|
|
|
|
{
|
|
|
|
use gl::*;
|
|
|
|
match v {
|
|
|
|
Keep => KEEP,
|
|
|
|
Zero => ZERO,
|
|
|
|
Replace => REPLACE,
|
|
|
|
Incr => INCR,
|
|
|
|
Decr => DECR,
|
|
|
|
Invert => INVERT,
|
|
|
|
IncrWrap => INCR_WRAP,
|
|
|
|
DecrWrap => DECR_WRAP,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Copy, Clone)]
|
|
|
|
pub enum StencilFunc {
|
|
|
|
Never,
|
|
|
|
Always,
|
|
|
|
Less,
|
|
|
|
LessEqual,
|
|
|
|
Equal,
|
|
|
|
Greater,
|
|
|
|
GreaterEqual,
|
|
|
|
NotEqual,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From <StencilFunc> for u32 {
|
|
|
|
fn from (v: StencilFunc) -> Self {
|
|
|
|
use StencilFunc::*;
|
|
|
|
{
|
|
|
|
use gl::*;
|
|
|
|
match v {
|
|
|
|
Never => NEVER,
|
|
|
|
Always => ALWAYS,
|
|
|
|
Less => LESS,
|
|
|
|
LessEqual => LEQUAL,
|
|
|
|
Equal => EQUAL,
|
|
|
|
Greater => GREATER,
|
|
|
|
GreaterEqual => GEQUAL,
|
|
|
|
NotEqual => NOTEQUAL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct StencilOpState {
|
|
|
|
sfail: StencilOp,
|
|
|
|
dpfail: StencilOp,
|
|
|
|
dppass: StencilOp,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct StencilFuncState {
|
|
|
|
func: StencilFunc,
|
|
|
|
reference: i32,
|
|
|
|
mask: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct StencilState {
|
|
|
|
op: StencilOpState,
|
|
|
|
func: StencilFuncState,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Anything that's None is "unknown"
|
|
|
|
|
|
|
|
pub struct GlState {
|
|
|
|
flags: HashMap <u32, bool>,
|
|
|
|
front_face: Option <FrontFace>,
|
|
|
|
stencil: Option <StencilState>,
|
2020-03-08 18:21:32 +00:00
|
|
|
|
|
|
|
color_mask: Option <(u8, u8, u8, u8)>,
|
|
|
|
depth_mask: Option <u8>,
|
|
|
|
stencil_mask: Option <u32>,
|
2020-03-08 18:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Pass <'a> {
|
2020-03-08 18:21:32 +00:00
|
|
|
shader: Option <&'a ShaderClosure>,
|
2020-03-08 18:08:22 +00:00
|
|
|
|
|
|
|
// Anything that's None is "don't care"
|
|
|
|
|
|
|
|
required_state: GlState,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Pass <'_> {
|
|
|
|
pub fn apply_slow (&self) {
|
|
|
|
let state = &self.required_state;
|
|
|
|
|
|
|
|
for (flag, value) in state.flags.iter () {
|
|
|
|
if *value {
|
|
|
|
glezz::enable (*flag);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
glezz::disable (*flag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 18:21:32 +00:00
|
|
|
if let Some (v) = &state.front_face {
|
|
|
|
glezz::front_face ((*v).into ());
|
2020-03-08 18:08:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 18:21:32 +00:00
|
|
|
if let Some (v) = &state.stencil {
|
|
|
|
let func = &v.func;
|
|
|
|
let op = &v.op;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
gl::StencilFunc (
|
|
|
|
func.func.into (),
|
|
|
|
func.reference.into (),
|
|
|
|
func.mask.into ()
|
|
|
|
);
|
|
|
|
gl::StencilOp (
|
|
|
|
op.sfail.into (),
|
|
|
|
op.dpfail.into (),
|
|
|
|
op.dppass.into ()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some ((r, g, b, a)) = &state.color_mask {
|
|
|
|
unsafe {
|
|
|
|
gl::ColorMask (*r, *g, *b, *a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some (v) = &state.depth_mask {
|
|
|
|
unsafe {
|
|
|
|
gl::DepthMask (*v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some (v) = &state.stencil_mask {
|
|
|
|
unsafe {
|
|
|
|
gl::StencilMask (*v);
|
|
|
|
}
|
2020-03-08 18:08:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 18:33:57 +00:00
|
|
|
|
|
|
|
pub fn with <F> (&self, callback: F)
|
|
|
|
where F: Fn ()
|
|
|
|
{
|
|
|
|
self.apply_slow ();
|
|
|
|
callback ();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_shader <F> (&self, callback: F)
|
|
|
|
where F: Fn (BorrowedShaderVars)
|
|
|
|
{
|
|
|
|
if let Some (s) = self.shader {
|
|
|
|
s.with (|shader_vars| {
|
|
|
|
self.apply_slow ();
|
|
|
|
callback (shader_vars);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
panic! ("Called with_shader on a pass with no shader");
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 18:08:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
struct GameGraphics {
|
|
|
|
shader_diffuse: ShaderClosure,
|
|
|
|
shader_shadow: ShaderClosure,
|
2020-01-06 01:25:17 +00:00
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
mesh_pumpkin: RenderableModel,
|
|
|
|
mesh_sky: RenderableModel,
|
|
|
|
mesh_pitch: RenderableModel,
|
|
|
|
mesh_arrow: RenderableModel,
|
2020-03-08 05:06:19 +00:00
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
texture: Texture,
|
2020-03-07 03:36:05 +00:00
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
pitch_colors: Vec <Vec3>,
|
|
|
|
grass_index: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GameGraphics {
|
|
|
|
pub fn new () -> Self {
|
|
|
|
let uniform_names = {
|
|
|
|
use uniforms::*;
|
|
|
|
vec! [
|
|
|
|
(MVP, "uni_mvp"),
|
|
|
|
(OBJECT_SPACE_LIGHT, "uni_object_space_light"),
|
|
|
|
(OBJECT_SPACE_SKY, "uni_object_space_sky"),
|
|
|
|
(ALBEDO, "uni_albedo"),
|
|
|
|
(MIN_ALBEDO, "uni_min_albedo"),
|
|
|
|
(MIN_BRIGHT, "uni_min_bright"),
|
|
|
|
(TEXTURE, "uni_texture"),
|
|
|
|
]
|
|
|
|
};
|
2020-03-07 02:23:23 +00:00
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
let attr_names = {
|
|
|
|
use renderable_model::attributes::*;
|
|
|
|
vec! [
|
|
|
|
(POS, "attr_pos"),
|
|
|
|
(UV, "attr_uv"),
|
|
|
|
(NORMAL, "attr_normal"),
|
|
|
|
]
|
|
|
|
};
|
2020-01-12 18:46:16 +00:00
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
let shader_diffuse = ShaderClosure::new (shader_from_files ("shaders/pumpkin-vert.glsl", "shaders/pumpkin-frag.glsl"), &uniform_names, &attr_names);
|
|
|
|
let shader_shadow = ShaderClosure::new (shader_from_files ("shaders/shadow-vert.glsl", "shaders/shadow-frag.glsl"), &uniform_names, &attr_names);
|
2020-01-18 05:18:08 +00:00
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
shader_diffuse.with (|shader_vars| {
|
|
|
|
let attrs = shader_vars.attrs;
|
|
|
|
use renderable_model::attributes::*;
|
|
|
|
glezz::enable_vertex_attrib_array (attrs [POS]);
|
|
|
|
glezz::enable_vertex_attrib_array (attrs [UV]);
|
|
|
|
glezz::enable_vertex_attrib_array (attrs [NORMAL]);
|
|
|
|
});
|
2020-01-12 18:46:16 +00:00
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
let mesh_pumpkin = renderable_from_iqm_file ("pumpking.iqm");
|
|
|
|
let mesh_sky = renderable_from_iqm_file ("sky-sphere.iqm");
|
|
|
|
let mesh_pitch = renderable_from_iqm_file ("pitch.iqm");
|
|
|
|
let mesh_arrow = renderable_from_iqm_file ("arrow.iqm");
|
2020-01-06 01:25:17 +00:00
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
let texture = Texture::from_file ("sky.png");
|
|
|
|
texture.bind ();
|
|
|
|
|
|
|
|
let (pitch_colors, grass_index) = {
|
|
|
|
let silver = (255.0, 255.0, 255.0);
|
|
|
|
let wood = (133.0, 76.0, 48.0);
|
|
|
|
|
|
|
|
let color_lookup: HashMap <&str, _> = HashMap::from_iter (vec! [
|
|
|
|
("GoalN1", silver),
|
|
|
|
("GoalN2", silver),
|
|
|
|
("GoalN3", silver),
|
|
|
|
|
|
|
|
("GoalS1", silver),
|
|
|
|
("GoalS2", silver),
|
|
|
|
("GoalS3", silver),
|
|
|
|
|
|
|
|
("TowerNW", wood),
|
|
|
|
("TowerNE", wood),
|
|
|
|
("TowerSW", wood),
|
|
|
|
("TowerSE", wood),
|
|
|
|
|
|
|
|
("Wall", wood),
|
|
|
|
|
|
|
|
("Grass", (52.0, 101.0, 36.0)),
|
|
|
|
].into_iter ());
|
|
|
|
|
|
|
|
let mut grass_index = None;
|
|
|
|
let colors: Vec <_> = (0..mesh_pitch.meshes.len ()).map (|i| {
|
|
|
|
let name = str::from_utf8 (&mesh_pitch.meshes [i].name).unwrap ();
|
|
|
|
if name == "Grass" {
|
|
|
|
grass_index = Some (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
match color_lookup.get (name) {
|
|
|
|
Some (t) => color_from_255 (*t),
|
|
|
|
_ => (0.0, 0.0, 0.0).into (),
|
|
|
|
}
|
|
|
|
}).collect ();
|
|
|
|
|
|
|
|
(colors, grass_index.unwrap ())
|
|
|
|
};
|
|
|
|
|
|
|
|
Self {
|
|
|
|
shader_diffuse,
|
|
|
|
shader_shadow,
|
|
|
|
|
|
|
|
mesh_pumpkin,
|
|
|
|
mesh_sky,
|
|
|
|
mesh_pitch,
|
|
|
|
mesh_arrow,
|
|
|
|
|
|
|
|
texture,
|
|
|
|
|
|
|
|
pitch_colors,
|
|
|
|
grass_index,
|
2020-01-06 01:25:17 +00:00
|
|
|
}
|
2020-03-08 17:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn draw (
|
|
|
|
&self,
|
|
|
|
state: &WorldState,
|
|
|
|
arrows: &[RenderableArrow]
|
|
|
|
)
|
|
|
|
{
|
|
|
|
let magenta = color_from_255 ((255.0, 0.0, 255.0));
|
|
|
|
let orange = color_from_255 ((210.0, 125.0, 44.0));
|
|
|
|
let green = color_from_255 ((52.0, 101.0, 36.0));
|
|
|
|
let white = color_from_255 ((255.0, 255.0, 255.0));
|
|
|
|
let _off_white = color_from_255 ((222.0, 238.0, 214.0));
|
|
|
|
let black = color_from_255 ((0.0, 0.0, 0.0));
|
|
|
|
let _off_black = color_from_255 ((20.0, 12.0, 28.0));
|
2020-01-06 01:25:17 +00:00
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
let pumpkin_colors = vec! [
|
|
|
|
orange,
|
|
|
|
green,
|
|
|
|
];
|
2020-01-06 01:25:17 +00:00
|
|
|
|
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-03-07 14:16:51 +00:00
|
|
|
let shadow_mat = {
|
2020-03-07 03:36:05 +00:00
|
|
|
let mut mat = Mat4::identity ();
|
2020-03-08 05:52:26 +00:00
|
|
|
mat.set_z_axis ((-0.25, 0.125, 0.0, 0.0).into ());
|
2020-03-07 14:16:51 +00:00
|
|
|
mat
|
2020-03-07 03:36:05 +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) *
|
2020-03-07 03:36:05 +00:00
|
|
|
Mat4::from_rotation_z (longitude) *
|
|
|
|
Mat4::from_translation ((0.0, 0.0, -2.7 * 0.5).into ())
|
2020-01-13 04:07:56 +00:00
|
|
|
;
|
|
|
|
|
2020-03-07 03:36:05 +00:00
|
|
|
let sky_mvp_mat = view_mat * Mat4::from_scale ((16.0, 16.0, 16.0).into ());
|
2020-01-15 03:45:47 +00:00
|
|
|
|
2020-03-08 05:52:26 +00:00
|
|
|
let light = Vec3::from ((0.25, -0.125, 1.0)).normalize ();
|
2020-01-12 18:46:16 +00:00
|
|
|
|
2020-03-08 18:08:22 +00:00
|
|
|
let passes = vec! [
|
2020-03-08 18:21:32 +00:00
|
|
|
// Clear everything
|
|
|
|
Pass {
|
|
|
|
shader: None,
|
|
|
|
required_state: GlState {
|
|
|
|
flags: hashmap! {},
|
|
|
|
front_face: None,
|
|
|
|
stencil: None,
|
|
|
|
color_mask: Some ((1, 1, 1, 1)),
|
|
|
|
depth_mask: Some (1),
|
|
|
|
stencil_mask: Some (255),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Draw world
|
2020-03-08 18:08:22 +00:00
|
|
|
Pass {
|
2020-03-08 18:21:32 +00:00
|
|
|
shader: Some (&self.shader_diffuse),
|
2020-03-08 18:08:22 +00:00
|
|
|
required_state: GlState {
|
|
|
|
flags: hashmap! {
|
2020-03-08 18:33:57 +00:00
|
|
|
gl::CULL_FACE => true,
|
2020-03-08 18:21:32 +00:00
|
|
|
gl::DEPTH_TEST => true,
|
|
|
|
gl::TEXTURE_2D => true,
|
2020-03-08 18:08:22 +00:00
|
|
|
gl::STENCIL_TEST => false,
|
|
|
|
},
|
|
|
|
front_face: Some (FrontFace::Cw),
|
|
|
|
stencil: None,
|
2020-03-08 18:21:32 +00:00
|
|
|
color_mask: Some ((1, 1, 1, 1)),
|
|
|
|
depth_mask: Some (1),
|
|
|
|
stencil_mask: Some (0),
|
2020-03-08 18:08:22 +00:00
|
|
|
},
|
|
|
|
},
|
2020-03-08 18:21:32 +00:00
|
|
|
// Write shadows into stencil buffer
|
2020-03-08 18:08:22 +00:00
|
|
|
Pass {
|
2020-03-08 18:21:32 +00:00
|
|
|
shader: Some (&self.shader_shadow),
|
2020-03-08 18:08:22 +00:00
|
|
|
required_state: GlState {
|
|
|
|
flags: hashmap! {
|
2020-03-08 18:33:57 +00:00
|
|
|
gl::CULL_FACE => true,
|
2020-03-08 18:21:32 +00:00
|
|
|
gl::DEPTH_TEST => true,
|
2020-03-08 18:08:22 +00:00
|
|
|
gl::STENCIL_TEST => true,
|
|
|
|
},
|
|
|
|
front_face: Some (FrontFace::Ccw),
|
|
|
|
stencil: Some (StencilState {
|
|
|
|
func: StencilFuncState {
|
|
|
|
func: StencilFunc::Always,
|
|
|
|
reference: 1,
|
|
|
|
mask: 1,
|
|
|
|
},
|
|
|
|
op: StencilOpState {
|
|
|
|
sfail: StencilOp::Keep,
|
|
|
|
dpfail: StencilOp::Keep,
|
|
|
|
dppass: StencilOp::Replace,
|
|
|
|
},
|
|
|
|
}),
|
2020-03-08 18:21:32 +00:00
|
|
|
color_mask: Some ((0, 0, 0, 0)),
|
|
|
|
depth_mask: Some (0),
|
|
|
|
stencil_mask: Some (255),
|
2020-03-08 18:08:22 +00:00
|
|
|
},
|
|
|
|
},
|
2020-03-08 18:21:32 +00:00
|
|
|
// Draw lit ground
|
2020-03-08 18:08:22 +00:00
|
|
|
Pass {
|
2020-03-08 18:21:32 +00:00
|
|
|
shader: Some (&self.shader_diffuse),
|
2020-03-08 18:08:22 +00:00
|
|
|
required_state: GlState {
|
|
|
|
flags: hashmap! {
|
2020-03-08 18:33:57 +00:00
|
|
|
gl::CULL_FACE => true,
|
2020-03-08 18:21:32 +00:00
|
|
|
gl::DEPTH_TEST => true,
|
|
|
|
gl::TEXTURE_2D => true,
|
2020-03-08 18:08:22 +00:00
|
|
|
gl::STENCIL_TEST => true,
|
|
|
|
},
|
|
|
|
front_face: Some (FrontFace::Cw),
|
|
|
|
stencil: Some (StencilState {
|
|
|
|
func: StencilFuncState {
|
|
|
|
func: StencilFunc::NotEqual,
|
|
|
|
reference: 0,
|
|
|
|
mask: 1,
|
|
|
|
},
|
|
|
|
op: StencilOpState {
|
|
|
|
sfail: StencilOp::Keep,
|
|
|
|
dpfail: StencilOp::Keep,
|
|
|
|
dppass: StencilOp::Keep,
|
|
|
|
},
|
|
|
|
}),
|
2020-03-08 18:21:32 +00:00
|
|
|
color_mask: Some ((1, 1, 1, 1)),
|
|
|
|
depth_mask: Some (1),
|
|
|
|
stencil_mask: Some (0),
|
2020-03-08 18:08:22 +00:00
|
|
|
},
|
|
|
|
},
|
2020-03-08 18:21:32 +00:00
|
|
|
// Draw unlit ground
|
2020-03-08 18:08:22 +00:00
|
|
|
Pass {
|
2020-03-08 18:21:32 +00:00
|
|
|
shader: Some (&self.shader_diffuse),
|
2020-03-08 18:08:22 +00:00
|
|
|
required_state: GlState {
|
|
|
|
flags: hashmap! {
|
2020-03-08 18:33:57 +00:00
|
|
|
gl::CULL_FACE => true,
|
2020-03-08 18:21:32 +00:00
|
|
|
gl::DEPTH_TEST => true,
|
|
|
|
gl::TEXTURE_2D => true,
|
2020-03-08 18:08:22 +00:00
|
|
|
gl::STENCIL_TEST => true,
|
|
|
|
},
|
|
|
|
front_face: Some (FrontFace::Cw),
|
|
|
|
stencil: Some (StencilState {
|
|
|
|
func: StencilFuncState {
|
|
|
|
func: StencilFunc::Equal,
|
|
|
|
reference: 0,
|
|
|
|
mask: 1,
|
|
|
|
},
|
|
|
|
op: StencilOpState {
|
|
|
|
sfail: StencilOp::Keep,
|
|
|
|
dpfail: StencilOp::Keep,
|
|
|
|
dppass: StencilOp::Keep,
|
|
|
|
},
|
|
|
|
}),
|
2020-03-08 18:21:32 +00:00
|
|
|
color_mask: Some ((1, 1, 1, 1)),
|
|
|
|
depth_mask: Some (1),
|
|
|
|
stencil_mask: Some (0),
|
2020-03-08 18:08:22 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-03-08 18:33:57 +00:00
|
|
|
passes [0].with (|| {
|
|
|
|
glezz::clear_color (1.0f32, 0.0f32, 1.0f32, 1.0f32);
|
|
|
|
glezz::clear (gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT | gl::STENCIL_BUFFER_BIT);
|
|
|
|
});
|
2020-02-16 23:32:35 +00:00
|
|
|
|
2020-03-07 14:16:51 +00:00
|
|
|
let pumpkin_model_mat =
|
|
|
|
Mat4::from_translation ((0.0, 0.0, 2.7 * 0.5).into ()) *
|
|
|
|
Mat4::from_scale ((0.125, 0.125, 0.125).into ()) *
|
|
|
|
Mat4::from_translation ((0.0, 0.0, -2.7 * 0.5).into ())
|
|
|
|
;
|
|
|
|
|
|
|
|
let world_model_mat = Mat4::identity ();
|
2020-03-08 18:33:57 +00:00
|
|
|
let inverse_pumpkin = pumpkin_model_mat.inverse ();
|
2020-03-07 14:16:51 +00:00
|
|
|
|
2020-03-08 02:53:02 +00:00
|
|
|
use uniforms::*;
|
|
|
|
|
2020-03-08 18:33:57 +00:00
|
|
|
// Draw the world except the ground plane
|
|
|
|
passes [1].with_shader (|shader_vars| {
|
2020-03-08 02:53:02 +00:00
|
|
|
let unis = shader_vars.unis;
|
|
|
|
let attrs = shader_vars.attrs;
|
2020-03-07 02:23:23 +00:00
|
|
|
|
2020-03-07 14:16:51 +00:00
|
|
|
let mvp = view_mat * pumpkin_model_mat;
|
|
|
|
glezz::uniform_matrix_4fv (unis [&MVP], &mvp);
|
|
|
|
|
2020-03-07 15:05:59 +00:00
|
|
|
let object_space_light = make_object_space_vec (&inverse_pumpkin, &light);
|
|
|
|
|
|
|
|
let object_space_sky = make_object_space_vec (&inverse_pumpkin, &Vec3::from ((0.0, 0.0, 1.0)));
|
|
|
|
|
|
|
|
glezz::uniform_3fv (unis [&MIN_BRIGHT], &black);
|
|
|
|
glezz::uniform_3fv (unis [&MIN_ALBEDO], &white);
|
2020-02-17 02:57:30 +00:00
|
|
|
glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &object_space_light);
|
2020-03-07 15:05:59 +00:00
|
|
|
glezz::uniform_3fv (unis [&OBJECT_SPACE_SKY], &object_space_sky);
|
2020-01-15 03:45:47 +00:00
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
self.mesh_pumpkin.draw_all (attrs, |i| {
|
2020-03-08 01:59:44 +00:00
|
|
|
glezz::uniform_3fv (unis [&ALBEDO], &pumpkin_colors [i]);
|
|
|
|
true
|
|
|
|
});
|
2020-02-17 02:57:30 +00:00
|
|
|
|
2020-03-07 14:16:51 +00:00
|
|
|
let mvp = view_mat * world_model_mat;
|
|
|
|
glezz::uniform_matrix_4fv (unis [&MVP], &mvp);
|
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
self.mesh_pitch.draw_all (attrs, |i| {
|
|
|
|
glezz::uniform_3fv (unis [&ALBEDO], &self.pitch_colors [i]);
|
|
|
|
i != self.grass_index
|
2020-03-08 01:59:44 +00:00
|
|
|
});
|
2020-02-17 02:57:30 +00:00
|
|
|
|
2020-03-08 05:52:26 +00:00
|
|
|
glezz::uniform_3fv (unis [&ALBEDO], &magenta);
|
2020-03-08 17:20:49 +00:00
|
|
|
for arrow in arrows.iter () {
|
2020-03-08 06:02:02 +00:00
|
|
|
let mvp = view_mat * arrow.model_mat;
|
2020-03-08 05:52:26 +00:00
|
|
|
|
|
|
|
glezz::uniform_matrix_4fv (unis [&MVP], &mvp);
|
2020-03-08 06:02:02 +00:00
|
|
|
let object_space_light = make_object_space_vec (&arrow.inv_model_mat, &light);
|
|
|
|
let object_space_sky = make_object_space_vec (&arrow.inv_model_mat, &Vec3::from ((0.0, 0.0, 1.0)));
|
|
|
|
glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &object_space_light);
|
|
|
|
glezz::uniform_3fv (unis [&OBJECT_SPACE_SKY], &object_space_sky);
|
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
self.mesh_arrow.draw_all (attrs, |_| true);
|
2020-03-08 05:52:26 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 02:57:30 +00:00
|
|
|
let draw_sky = true;
|
|
|
|
if draw_sky {
|
2020-03-08 17:20:49 +00:00
|
|
|
self.texture.bind ();
|
2020-02-17 02:57:30 +00:00
|
|
|
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);
|
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
self.mesh_sky.draw_all (attrs, |_| true);
|
2020-02-17 02:57:30 +00:00
|
|
|
}
|
2020-03-08 05:52:26 +00:00
|
|
|
});
|
|
|
|
|
2020-03-08 18:33:57 +00:00
|
|
|
// Draw shadows into stencil buffer
|
|
|
|
passes [2].with_shader (|shader_vars| {
|
2020-03-08 05:52:26 +00:00
|
|
|
let unis = shader_vars.unis;
|
|
|
|
let attrs = shader_vars.attrs;
|
2020-03-07 03:36:05 +00:00
|
|
|
|
2020-03-07 14:16:51 +00:00
|
|
|
let view_mat = view_mat * shadow_mat;
|
|
|
|
|
|
|
|
let mvp = view_mat * pumpkin_model_mat;
|
2020-03-08 02:53:02 +00:00
|
|
|
glezz::uniform_matrix_4fv (unis [&MVP], &mvp);
|
2020-03-07 14:16:51 +00:00
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
self.mesh_pumpkin.draw_all (attrs, |_| true);
|
2020-03-07 03:36:05 +00:00
|
|
|
|
2020-03-07 14:16:51 +00:00
|
|
|
let mvp = view_mat * world_model_mat;
|
2020-03-08 02:53:02 +00:00
|
|
|
glezz::uniform_matrix_4fv (unis [&MVP], &mvp);
|
2020-03-07 14:16:51 +00:00
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
self.mesh_pitch.draw_all (attrs, |i| i != self.grass_index);
|
2020-03-07 15:05:59 +00:00
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
for arrow in arrows.iter () {
|
|
|
|
let mvp = view_mat * arrow.model_mat;
|
2020-03-08 05:52:26 +00:00
|
|
|
|
|
|
|
glezz::uniform_matrix_4fv (unis [&MVP], &mvp);
|
2020-03-08 17:20:49 +00:00
|
|
|
self.mesh_arrow.draw_all (attrs, |_| true);
|
2020-03-07 15:05:59 +00:00
|
|
|
}
|
2020-03-08 02:53:02 +00:00
|
|
|
});
|
|
|
|
|
2020-03-08 18:33:57 +00:00
|
|
|
let object_space_light = make_object_space_vec (&inverse_pumpkin, &light);
|
|
|
|
|
|
|
|
// Draw unlit ground
|
|
|
|
passes [3].with_shader (|shader_vars| {
|
2020-03-08 02:53:02 +00:00
|
|
|
let unis = shader_vars.unis;
|
|
|
|
let attrs = shader_vars.attrs;
|
|
|
|
|
2020-03-07 15:05:59 +00:00
|
|
|
let inverse_pumpkin = pumpkin_model_mat.inverse ();
|
|
|
|
|
|
|
|
let object_space_sky = make_object_space_vec (&inverse_pumpkin, &Vec3::from ((0.0, 0.0, 1.0)));
|
|
|
|
|
|
|
|
glezz::uniform_3fv (unis [&MIN_BRIGHT], &black);
|
|
|
|
glezz::uniform_3fv (unis [&MIN_ALBEDO], &white);
|
|
|
|
glezz::uniform_3fv (unis [&OBJECT_SPACE_SKY], &object_space_sky);
|
|
|
|
|
|
|
|
let mvp = view_mat * world_model_mat;
|
|
|
|
glezz::uniform_matrix_4fv (unis [&MVP], &mvp);
|
|
|
|
|
2020-03-08 17:20:49 +00:00
|
|
|
glezz::uniform_3fv (unis [&ALBEDO], &self.pitch_colors [self.grass_index]);
|
2020-03-07 15:05:59 +00:00
|
|
|
|
|
|
|
glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &Vec3::from ((0.0, 0.0, 0.0)));
|
2020-03-08 17:20:49 +00:00
|
|
|
self.mesh_pitch.draw (attrs, self.grass_index);
|
2020-03-08 18:33:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Draw lit ground
|
|
|
|
passes [4].with_shader (|shader_vars| {
|
|
|
|
let unis = shader_vars.unis;
|
|
|
|
let attrs = shader_vars.attrs;
|
2020-03-08 18:08:22 +00:00
|
|
|
|
2020-03-07 15:05:59 +00:00
|
|
|
glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &object_space_light);
|
2020-03-08 17:20:49 +00:00
|
|
|
self.mesh_pitch.draw (attrs, self.grass_index);
|
2020-03-08 02:53:02 +00:00
|
|
|
});
|
2020-03-08 17:20:49 +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| {
|
|
|
|
video_subsystem.gl_get_proc_address (s) as *const _
|
|
|
|
});
|
|
|
|
|
|
|
|
assert! (gl::ClearColor::is_loaded ());
|
|
|
|
|
|
|
|
let gl_ctx = window.gl_create_context ().unwrap ();
|
|
|
|
|
|
|
|
window.gl_make_current (&gl_ctx).unwrap ();
|
|
|
|
|
|
|
|
let mut time_step = TimeStep::new (60, 1000);
|
|
|
|
let mut state = WorldState::new ();
|
|
|
|
|
|
|
|
let graphics = GameGraphics::new ();
|
|
|
|
|
|
|
|
let mut event_pump = sdl_context.event_pump ().unwrap ();
|
|
|
|
'running: loop {
|
|
|
|
let frames_to_do = time_step.step ();
|
|
|
|
|
|
|
|
let controller = ControllerState::from_sdl_keyboard (&event_pump.keyboard_state ());
|
|
|
|
|
|
|
|
for _ in 0..frames_to_do {
|
|
|
|
state.step (&controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
let _mouse = event_pump.mouse_state ();
|
|
|
|
|
|
|
|
for event in event_pump.poll_iter() {
|
|
|
|
match event {
|
|
|
|
Event::Quit {..} |
|
|
|
|
Event::KeyDown { keycode: Some (Keycode::Escape), .. } => {
|
|
|
|
break 'running
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let arrows = vec![
|
|
|
|
Arrow {
|
|
|
|
origin: (0.0, 0.0, 1.0).into (),
|
|
|
|
direction: (0.0, 0.0, 1.0).into (),
|
|
|
|
},
|
|
|
|
Arrow {
|
|
|
|
origin: (1.0, 0.0, 1.0).into (),
|
|
|
|
direction: (1.0, 0.0, 0.0).into (),
|
|
|
|
},
|
|
|
|
Arrow {
|
|
|
|
origin: (0.0, 1.0, 1.0).into (),
|
|
|
|
direction: (0.0, 1.0, 0.0).into (),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let renderable_arrows: Vec <_> = arrows.iter ().map (|arrow| {
|
|
|
|
let d = arrow.direction;
|
|
|
|
|
|
|
|
let up: Vec3 = if d.z () > 0.5 {
|
|
|
|
(-1.0, 0.0, 0.0)
|
|
|
|
}
|
|
|
|
else if d.z () < -0.5 {
|
|
|
|
(-1.0, 0.0, 0.0)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
(0.0, 0.0, 1.0)
|
|
|
|
}.into ();
|
|
|
|
|
|
|
|
// These are probably all fucked
|
|
|
|
|
|
|
|
let left = d.cross (up);
|
|
|
|
let up = d.cross (left);
|
|
|
|
|
|
|
|
let mut dir_mat = Mat4::identity ();
|
|
|
|
|
|
|
|
dir_mat.set_x_axis ((left.x (), left.y (), left.z (), 0.0).into ());
|
|
|
|
dir_mat.set_y_axis ((up.x (), up.y (), up.z (), 0.0).into ());
|
|
|
|
dir_mat.set_z_axis ((d.x (), d.y (), d.z (), 0.0).into ());
|
|
|
|
|
|
|
|
let model_mat =
|
|
|
|
Mat4::from_translation (arrow.origin) *
|
|
|
|
Mat4::from_scale ((0.125, 0.125, 0.125).into ()) *
|
|
|
|
dir_mat;
|
|
|
|
|
|
|
|
let inv_model_mat = model_mat.inverse ();
|
|
|
|
|
|
|
|
RenderableArrow {
|
|
|
|
model_mat,
|
|
|
|
inv_model_mat,
|
|
|
|
}
|
|
|
|
}).collect ();
|
|
|
|
|
|
|
|
window.gl_make_current (&gl_ctx).unwrap ();
|
|
|
|
|
|
|
|
graphics.draw (&state, &renderable_arrows);
|
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]
|
2020-03-08 18:08:22 +00:00
|
|
|
pub fn sizes () {
|
|
|
|
use std::mem;
|
2020-01-09 00:18:12 +00:00
|
|
|
|
2020-03-08 18:08:22 +00:00
|
|
|
assert_eq! (8, mem::size_of::<Option <u32>>());
|
|
|
|
assert_eq! (1, mem::size_of::<FrontFace>());
|
|
|
|
assert_eq! (1, mem::size_of::<Option <FrontFace>>());
|
2020-01-08 23:58:36 +00:00
|
|
|
}
|
|
|
|
}
|