adding in support for rotating textures, except missing y-inversion

This commit is contained in:
Dustin Carlino 2019-10-23 16:30:23 -07:00
parent d133dfd152
commit 17e02ebdf0
6 changed files with 31 additions and 8 deletions

View File

@ -1,3 +1,4 @@
use geom::Angle;
use serde_derive::{Deserialize, Serialize};
use std::fmt;
@ -8,8 +9,9 @@ pub enum Color {
// (The texture ID to pass to the shader, (texture width, height)). Tiles seamlessly through
// all of map-space.
TileTexture(f32, (f64, f64)),
// Stretches the entire texture to fit the entire polygon.
StretchTexture(f32),
// Stretches the entire texture to fit the entire polygon. Rotates from the center of the
// polygon. Not sure what this means for anything but circles right now.
StretchTexture(f32, Angle),
Hatching,
}
@ -20,7 +22,9 @@ impl fmt::Display for Color {
Color::TileTexture(id, (w, h)) => {
write!(f, "Color::TileTexture({}, width={}, height={})", id, w, h)
}
Color::StretchTexture(id) => write!(f, "Color::StretchTexture({})", id),
Color::StretchTexture(id, angle) => {
write!(f, "Color::StretchTexture({}, {})", id, angle)
}
Color::Hatching => write!(f, "Color::Hatching"),
}
}
@ -81,4 +85,11 @@ impl Color {
let b = usize::from_str_radix(&raw[5..7], 16).unwrap();
Color::rgb(r, g, b)
}
pub fn rotate(&self, angle: Angle) -> Color {
match self {
Color::StretchTexture(id, _) => Color::StretchTexture(*id, angle),
_ => unreachable!(),
}
}
}

View File

@ -430,11 +430,20 @@ impl<'a> Prerender<'a> {
let ty = pt.y() / tex_height;
[id, tx as f32, ty as f32, 0.0]
}
Color::StretchTexture(id) => {
Color::StretchTexture(id, angle) => {
// TODO Cache
let b = poly.get_bounds();
let tx = (pt.x() - b.min_x) / (b.max_x - b.min_x);
let ty = (pt.y() - b.min_y) / (b.max_y - b.min_y);
let center = poly.center();
let origin_pt = Pt2D::new(pt.x() - center.x(), pt.y() - center.y());
// TODO Needs Y-inversion!
let (sin, cos) = angle.normalized_radians().sin_cos();
let rot_pt = Pt2D::new(
center.x() + origin_pt.x() * cos - origin_pt.y() * sin,
center.y() + origin_pt.y() * cos + origin_pt.x() * sin,
);
let tx = (rot_pt.x() - b.min_x) / (b.max_x - b.min_x);
let ty = (rot_pt.y() - b.min_y) / (b.max_y - b.min_y);
[id, tx as f32, ty as f32, 0.0]
}
Color::Hatching => [10.0, 0.0, 0.0, 0.0],

View File

@ -3,6 +3,7 @@ use crate::{
Canvas, Color, GfxCtx, HorizontalAlignment, Line, Prerender, Text, UserInput, VerticalAlignment,
};
use abstutil::{elapsed_seconds, Timer, TimerSink};
use geom::Angle;
use glium_glyph::glyph_brush::rusttype::Font;
use glium_glyph::GlyphBrush;
use std::collections::VecDeque;
@ -70,7 +71,7 @@ impl<'a> EventCtx<'a> {
self.canvas.texture_lookups.insert(
filename.to_string(),
match tex_type {
TextureType::Stretch => Color::StretchTexture(idx as f32),
TextureType::Stretch => Color::StretchTexture(idx as f32, Angle::ZERO),
TextureType::Tile => {
Color::TileTexture(idx as f32, (f64::from(dims.0), f64::from(dims.1)))
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 49 KiB

View File

@ -35,7 +35,7 @@ impl DrawPedestrian {
if use_textures {
draw_default.push(
canvas.texture("assets/pedestrian.png"),
canvas.texture("assets/pedestrian.png").rotate(input.facing),
body_circle.to_polygon(),
);
} else {

View File

@ -8,6 +8,8 @@ use std::fmt;
pub struct Angle(f64);
impl Angle {
pub const ZERO: Angle = Angle(0.0);
pub(crate) fn new(rads: f64) -> Angle {
// Retain more precision for angles...
Angle((rads * 10_000_000.0).round() / 10_000_000.0)