Skip to content

Commit

Permalink
Merge branch '0.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOnlyMrCat committed Jan 28, 2021
2 parents 238e135 + d669cc1 commit b936ac8
Show file tree
Hide file tree
Showing 15 changed files with 482 additions and 136 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "polystrip"
description = "A 2D accelerated graphics rendering library"
keywords = ["gamedev", "graphics", "rendering"]
categories = ["game-development", "graphics", "rendering::engine"]
version = "0.4.0"
version = "0.5.0"
authors = ["TheOnlyMrCat <[email protected]>"]
repository = "https://github.com/TheOnlyMrCat/polystrip"
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -31,6 +31,8 @@ gfx-backend-metal = { version = "0.6", optional = true }
gfx-backend-dx12 = { version = "0.6", optional = true }
gfx-backend-empty = { version = "0.6", optional = true }

cgmath = { version = "0.18", optional = true }

[dev-dependencies]
winit = "0.23"
image = "0.23"
39 changes: 27 additions & 12 deletions examples/geometry/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use polystrip::Renderer;
use polystrip::data::{GpuPos, Color};
use polystrip::vertex::{ColoredShape, ColorVertex};
use polystrip::data::{GpuVec3, Color};
use polystrip::vertex::{StrokedShape, ColoredShape, ColorVertex, Matrix4};

use winit::event::{Event, WindowEvent};
use winit::event_loop::{EventLoop, ControlFlow};
Expand All @@ -26,29 +26,44 @@ fn main() {
},
Event::MainEventsCleared => {
let mut frame = renderer.next_frame_clear(Color { r: 128, g: 128, b: 128, a: 255 });
frame.draw_stroked(StrokedShape {
vertices: &[
ColorVertex { position: GpuVec3 { x: 0.0, y: 0.5, h: 0.0 }, color: Color { r: 255, g: 255, b: 255, a: 255 }},
ColorVertex { position: GpuVec3 { x: -0.375, y: 0.125, h: 2.0 }, color: Color { r: 255, g: 255, b: 255, a: 255 }},
ColorVertex { position: GpuVec3 { x: -0.25, y: -0.5, h: 2.0 }, color: Color { r: 255, g: 255, b: 255, a: 255 }},
ColorVertex { position: GpuVec3 { x: 0.25, y: -0.5, h: 2.0 }, color: Color { r: 255, g: 255, b: 255, a: 255 }},
ColorVertex { position: GpuVec3 { x: 0.375, y: 0.125, h: 2.0 }, color: Color { r: 255, g: 255, b: 255, a: 255 }},
],
indices: &[
[0, 1], [0, 2], [0, 3], [0, 4],
[1, 2], [1, 3], [1, 4],
[2, 3], [2, 4],
[3, 4],
]
}, Matrix4::translate(0.5, 0.0));
frame.draw_colored(ColoredShape {
vertices: &[
ColorVertex { position: GpuPos { x: 0.5, y: 0.5 }, color: Color { r: 255, g: 0, b: 0, a: 255 }},
ColorVertex { position: GpuPos { x: 0.125, y: 0.125 }, color: Color { r: 255, g: 255, b: 0, a: 255 }},
ColorVertex { position: GpuPos { x: 0.25, y: -0.5 }, color: Color { r: 0, g: 255, b: 0, a: 255 }},
ColorVertex { position: GpuPos { x: 0.75, y: -0.5 }, color: Color { r: 0, g: 0, b: 255, a: 255 }},
ColorVertex { position: GpuPos { x: 0.875, y: 0.125 }, color: Color { r: 255, g: 0, b: 255, a: 255 }},
ColorVertex { position: GpuVec3 { x: 0.0, y: 0.5, h: 1.0 }, color: Color { r: 255, g: 0, b: 0, a: 255 }},
ColorVertex { position: GpuVec3 { x: -0.375, y: 0.125, h: 0.0 }, color: Color { r: 255, g: 255, b: 0, a: 255 }},
ColorVertex { position: GpuVec3 { x: -0.25, y: -0.5, h: 0.0 }, color: Color { r: 0, g: 255, b: 0, a: 255 }},
ColorVertex { position: GpuVec3 { x: 0.25, y: -0.5, h: 0.0 }, color: Color { r: 0, g: 0, b: 255, a: 255 }},
ColorVertex { position: GpuVec3 { x: 0.375, y: 0.125, h: 0.0 }, color: Color { r: 255, g: 0, b: 255, a: 255 }},
],
// Note the vertices are specified going counter-clockwise
indices: &[
[0, 1, 4],
[1, 2, 4],
[2, 3, 4],
]
});
}, Matrix4::translate(0.5, 0.0));
frame.draw_colored(ColoredShape {
vertices: &[
ColorVertex { position: GpuPos { x: -0.5, y: 0.5 }, color: Color { r: 255, g: 0, b: 0, a: 255 }},
ColorVertex { position: GpuPos { x: -0.75, y: -0.5 }, color: Color { r: 0, g: 255, b: 0, a: 255 }},
ColorVertex { position: GpuPos { x: -0.25, y: -0.5 }, color: Color { r: 0, g: 0, b: 255, a: 255 }},
ColorVertex { position: GpuVec3 { x: 0.0, y: 0.5, h: 0.0 }, color: Color { r: 255, g: 0, b: 0, a: 255 }},
ColorVertex { position: GpuVec3 { x: -0.25, y: -0.5, h: 0.0 }, color: Color { r: 0, g: 255, b: 0, a: 255 }},
ColorVertex { position: GpuVec3 { x: 0.25, y: -0.5, h: 0.0 }, color: Color { r: 0, g: 0, b: 255, a: 255 }},
],
indices: &[[0, 1, 2]]
});
}, Matrix4::translate(-0.5, 0.0));
},
_ => {}
}
Expand Down
14 changes: 7 additions & 7 deletions examples/textures/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use polystrip::Renderer;
use polystrip::data::{GpuPos, Color};
use polystrip::vertex::{TexturedShape, TextureVertex};
use polystrip::data::{GpuVec2, Color};
use polystrip::vertex::{TexturedShape, TextureVertex, Matrix4};

use winit::event::{Event, WindowEvent};
use winit::event_loop::{EventLoop, ControlFlow};
Expand Down Expand Up @@ -31,16 +31,16 @@ fn main() {
let mut frame = renderer.next_frame_clear(Color { r: 128, g: 128, b: 128, a: 255 });
frame.draw_textured(TexturedShape {
vertices: &[
TextureVertex { position: frame.pixel(50, 50), tex_coords: GpuPos { x: 0.0, y: 0.0 } },
TextureVertex { position: frame.pixel(50, 150), tex_coords: GpuPos { x: 0.0, y: 1.0 } },
TextureVertex { position: frame.pixel(150, 150), tex_coords: GpuPos { x: 1.0, y: 1.0 } },
TextureVertex { position: frame.pixel(150, 50), tex_coords: GpuPos { x: 1.0, y: 0.0 } },
TextureVertex { position: frame.pixel(50, 50).with_height(0.0), tex_coords: GpuVec2 { x: 0.0, y: 0.0 } },
TextureVertex { position: frame.pixel(50, 150).with_height(0.0), tex_coords: GpuVec2 { x: 0.0, y: 1.0 } },
TextureVertex { position: frame.pixel(150, 150).with_height(0.0), tex_coords: GpuVec2 { x: 1.0, y: 1.0 } },
TextureVertex { position: frame.pixel(150, 50).with_height(0.0), tex_coords: GpuVec2 { x: 1.0, y: 0.0 } },
],
indices: &[
[0, 1, 3],
[1, 2, 3],
]
}, &sandstone);
}, &sandstone, Matrix4::identity());
},
_ => {}
}
Expand Down
49 changes: 44 additions & 5 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,59 @@
use std::fmt::Debug;

/// Coordinates in GPU space, represented with `f32`s.
/// 2D coordinates in GPU space, represented with `f32`s.
///
/// # Screen space
/// `(0.0, 0.0)` is the screen center. `(1.0, 1.0)` is the top-right corner.
/// `(-1.0, -1.0)` is the bottom-left corner.
///
/// # Texture space
/// `(0.0, 0.0)` is the top-left corner
/// `(1.0, 1.0)` is the bottom-right corner
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
pub struct GpuVec2 {
pub x: f32,
pub y: f32,
}

impl GpuVec2 {
pub fn new(x: f32, y: f32) -> GpuVec2 {
GpuVec2 { x, y }
}

pub fn with_height(self, h: f32) -> GpuVec3 {
GpuVec3 { x: self.x, y: self.y, h }
}
}

/// 3D coordinates in GPU space, represented with `f32`s.
///
/// # Height
/// The third coordinate, `h`, is uncapped non-linear height, used to affect the render output.
/// Out of a set of shapes drawn at the same height, the one drawn last appears on top.
/// Out of a set of shapes drawn at different heights, however, the one with the greatest height appears on top.
///
/// Additionally, height interpolates linearly between vertices.
///
/// # Screen space
/// `(0.0, 0.0)` is the screen center. `(1.0, 1.0)` is the top-right corner.
/// `(-1.0, -1.0)` is the bottom-left corner.
///
/// # Texture space
/// `(0.0, 0.0)` is the top-left corner
/// `(1.0, 1.0)` is the bottom-right corner
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
pub struct GpuPos {
pub struct GpuVec3 {
pub x: f32,
pub y: f32,
pub h: f32,
}

impl GpuPos {
pub fn new(x: f32, y: f32) -> GpuPos {
GpuPos { x, y }
impl GpuVec3 {
pub fn new(x: f32, y: f32, h: f32) -> GpuVec3 {
GpuVec3 { x, y, h }
}
}

Expand Down
52 changes: 26 additions & 26 deletions src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! This module contains the [`FrameGeometryExt`](trait.FrameGeometryExt) trait, which adds numerous functions for simple rendering operations
//! where full description of the shape would be unnecessarily time-consuming.
use crate::data::{GpuPos, Color, Rect};
use crate::data::{GpuVec2, Color, Rect};
use crate::Frame;
use crate::Texture;
use crate::vertex::*;
Expand Down Expand Up @@ -44,60 +44,60 @@ impl<'a> FrameGeometryExt<'a> for Frame<'a> {
fn draw_rect(&mut self, bounds: Rect, color: Color) {
self.draw_colored(ColoredShape {
vertices: &[
ColorVertex { position: self.pixel(bounds.x, bounds.y), color },
ColorVertex { position: self.pixel(bounds.x, bounds.y + bounds.h), color },
ColorVertex { position: self.pixel(bounds.x + bounds.w, bounds.y + bounds.h), color },
ColorVertex { position: self.pixel(bounds.x + bounds.w, bounds.y), color }
ColorVertex { position: self.pixel(bounds.x, bounds.y).with_height(0.0), color },
ColorVertex { position: self.pixel(bounds.x, bounds.y + bounds.h).with_height(0.0), color },
ColorVertex { position: self.pixel(bounds.x + bounds.w, bounds.y + bounds.h).with_height(0.0), color },
ColorVertex { position: self.pixel(bounds.x + bounds.w, bounds.y).with_height(0.0), color }
],
indices: &QUAD_INCICES
});
}, Matrix4::identity());
}

fn draw_texture(&mut self, x: i32, y: i32, texture: &'a Texture) {
self.draw_textured(TexturedShape {
vertices: &[
TextureVertex { position: self.pixel(x, y), tex_coords: GpuPos { x: 0.0, y: 0.0 } },
TextureVertex { position: self.pixel(x, y + texture.height() as i32), tex_coords: GpuPos { x: 0.0, y: 1.0 } },
TextureVertex { position: self.pixel(x + texture.width() as i32, y + texture.height() as i32), tex_coords: GpuPos { x: 1.0, y: 1.0 } },
TextureVertex { position: self.pixel(x + texture.width() as i32, y), tex_coords: GpuPos { x: 1.0, y: 0.0 } }
TextureVertex { position: self.pixel(x, y).with_height(0.0), tex_coords: GpuVec2 { x: 0.0, y: 0.0 } },
TextureVertex { position: self.pixel(x, y + texture.height() as i32).with_height(0.0), tex_coords: GpuVec2 { x: 0.0, y: 1.0 } },
TextureVertex { position: self.pixel(x + texture.width() as i32, y + texture.height() as i32).with_height(0.0), tex_coords: GpuVec2 { x: 1.0, y: 1.0 } },
TextureVertex { position: self.pixel(x + texture.width() as i32, y).with_height(0.0), tex_coords: GpuVec2 { x: 1.0, y: 0.0 } }
],
indices: &QUAD_INCICES
}, texture);
}, texture, Matrix4::identity());
}

fn draw_texture_scaled(&mut self, destination: Rect, texture: &'a Texture) {
self.draw_textured(TexturedShape {
vertices: &[
TextureVertex { position: self.pixel(destination.x, destination.y), tex_coords: GpuPos { x: 0.0, y: 0.0 } },
TextureVertex { position: self.pixel(destination.x, destination.y + destination.h), tex_coords: GpuPos { x: 0.0, y: 1.0 } },
TextureVertex { position: self.pixel(destination.x + destination.w, destination.y + destination.h), tex_coords: GpuPos { x: 1.0, y: 1.0 } },
TextureVertex { position: self.pixel(destination.x + destination.w, destination.y), tex_coords: GpuPos { x: 1.0, y: 0.0 } }
TextureVertex { position: self.pixel(destination.x, destination.y).with_height(0.0), tex_coords: GpuVec2 { x: 0.0, y: 0.0 } },
TextureVertex { position: self.pixel(destination.x, destination.y + destination.h).with_height(0.0), tex_coords: GpuVec2 { x: 0.0, y: 1.0 } },
TextureVertex { position: self.pixel(destination.x + destination.w, destination.y + destination.h).with_height(0.0), tex_coords: GpuVec2 { x: 1.0, y: 1.0 } },
TextureVertex { position: self.pixel(destination.x + destination.w, destination.y).with_height(0.0), tex_coords: GpuVec2 { x: 1.0, y: 0.0 } }
],
indices: &QUAD_INCICES
}, texture);
}, texture, Matrix4::identity());
}

fn draw_texture_cropped(&mut self, source: Rect, x: i32, y: i32, texture: &'a Texture) {
self.draw_textured(TexturedShape {
vertices: &[
TextureVertex { position: self.pixel(x, y), tex_coords: texture.pixel(source.x, source.y) },
TextureVertex { position: self.pixel(x, y + texture.height() as i32 - source.h), tex_coords: texture.pixel(source.x, source.y + source.h) },
TextureVertex { position: self.pixel(x + texture.width() as i32 - source.w, y + texture.height() as i32 - source.h), tex_coords: texture.pixel(source.x + source.w, source.y + source.h) },
TextureVertex { position: self.pixel(x + texture.width() as i32 - source.w, y), tex_coords: texture.pixel(source.x + source.w, source.y) }
TextureVertex { position: self.pixel(x, y).with_height(0.0), tex_coords: texture.pixel(source.x, source.y) },
TextureVertex { position: self.pixel(x, y + texture.height() as i32 - source.h).with_height(0.0), tex_coords: texture.pixel(source.x, source.y + source.h) },
TextureVertex { position: self.pixel(x + texture.width() as i32 - source.w, y + texture.height() as i32 - source.h).with_height(0.0), tex_coords: texture.pixel(source.x + source.w, source.y + source.h) },
TextureVertex { position: self.pixel(x + texture.width() as i32 - source.w, y).with_height(0.0), tex_coords: texture.pixel(source.x + source.w, source.y) }
],
indices: &QUAD_INCICES
}, texture);
}, texture, Matrix4::identity());
}

fn draw_texture_cropped_scaled(&mut self, source: Rect, destination: Rect, texture: &'a Texture) {
self.draw_textured(TexturedShape {
vertices: &[
TextureVertex { position: self.pixel(destination.x, destination.y), tex_coords: texture.pixel(source.x, source.y) },
TextureVertex { position: self.pixel(destination.x, destination.y + destination.h), tex_coords: texture.pixel(source.x, source.y + source.h) },
TextureVertex { position: self.pixel(destination.x + destination.w, destination.y + destination.h), tex_coords: texture.pixel(source.x + source.w, source.y + source.h) },
TextureVertex { position: self.pixel(destination.x + destination.w, destination.y), tex_coords: texture.pixel(source.x + source.w, source.y) }
TextureVertex { position: self.pixel(destination.x, destination.y).with_height(0.0), tex_coords: texture.pixel(source.x, source.y) },
TextureVertex { position: self.pixel(destination.x, destination.y + destination.h).with_height(0.0), tex_coords: texture.pixel(source.x, source.y + source.h) },
TextureVertex { position: self.pixel(destination.x + destination.w, destination.y + destination.h).with_height(0.0), tex_coords: texture.pixel(source.x + source.w, source.y + source.h) },
TextureVertex { position: self.pixel(destination.x + destination.w, destination.y).with_height(0.0), tex_coords: texture.pixel(source.x + source.w, source.y) }
],
indices: &QUAD_INCICES
}, texture);
}, texture, Matrix4::identity());
}
}
Loading

0 comments on commit b936ac8

Please sign in to comment.