use std::mem;
use std::ffi::CString;
use std::num::NonZeroU32;
use glutin::prelude::*;
use shade::cvmath::*;
#[derive(Copy, Clone, Default, dataview::Pod)]
#[repr(C)]
struct TriangleVertex {
position: Vec2f,
color: [u8; 4],
}
unsafe impl shade::TVertex for TriangleVertex {
const LAYOUT: &'static shade::VertexLayout = &shade::VertexLayout {
size: mem::size_of::<TriangleVertex>() as u16,
alignment: mem::align_of::<TriangleVertex>() as u16,
attributes: &[
shade::VertexAttribute {
name: "aPos",
format: shade::VertexAttributeFormat::F32v2,
offset: dataview::offset_of!(TriangleVertex.position) as u16,
},
shade::VertexAttribute {
name: "aColor",
format: shade::VertexAttributeFormat::U8Normv4,
offset: dataview::offset_of!(TriangleVertex.color) as u16,
},
],
};
}
const FRAGMENT_SHADER: &str = r#"
#version 330 core
out vec4 o_fragColor;
in vec4 v_color;
void main() {
float levels = 10.0;
vec3 qColor = floor(v_color.rgb * levels) / (levels - 1.0);
o_fragColor = vec4(qColor, v_color.a);
}
"#;
const VERTEX_SHADER: &str = r#"
#version 330 core
in vec2 aPos;
in vec4 aColor;
out vec4 v_color;
void main() {
v_color = aColor;
gl_Position = vec4(aPos, 0.0, 1.0);
}
"#;
struct GlWindow {
size: winit::dpi::PhysicalSize<u32>,
window: winit::window::Window,
surface: glutin::surface::Surface<glutin::surface::WindowSurface>,
context: glutin::context::PossiblyCurrentContext,
}
impl GlWindow {
fn new(
event_loop: &winit::event_loop::ActiveEventLoop,
size: winit::dpi::PhysicalSize<u32>,
) -> GlWindow {
use glutin::config::ConfigTemplateBuilder;
use glutin::context::{ContextApi, ContextAttributesBuilder, Version};
use glutin::display::GetGlDisplay;
use glutin::surface::{SurfaceAttributesBuilder, WindowSurface};
use raw_window_handle::HasWindowHandle;
let template_builder = ConfigTemplateBuilder::new()
.with_alpha_size(8)
.with_multisampling(4);
let window_attributes = winit::window::WindowAttributes::default()
.with_inner_size(size);
let config_picker = |configs: Box<dyn Iterator<Item = glutin::config::Config> + '_>| {
configs
.filter(|c| c.srgb_capable())
.max_by_key(|c| c.num_samples())
.expect("No GL configs found")
};
let (window, gl_config) = glutin_winit::DisplayBuilder::new()
.with_window_attributes(Some(window_attributes))
.build(event_loop, template_builder, config_picker)
.expect("Failed DisplayBuilder.build");
let window = window.expect("DisplayBuilder did not build a Window");
let raw_window_handle = window
.window_handle()
.expect("Failed Window.window_handle")
.as_raw();
let context_attributes = ContextAttributesBuilder::new()
.with_context_api(ContextApi::OpenGl(Some(Version::new(3, 3))))
.build(Some(raw_window_handle));
let gl_display = gl_config.display();
let not_current = unsafe { gl_display.create_context(&gl_config, &context_attributes) }
.expect("Failed Display.create_context");
let surface_attributes_builder = SurfaceAttributesBuilder::<WindowSurface>::new()
.with_srgb(Some(true));
let surface_attributes = surface_attributes_builder.build(
raw_window_handle,
NonZeroU32::new(size.width.max(1)).unwrap(),
NonZeroU32::new(size.height.max(1)).unwrap(),
);
let surface = unsafe { gl_display.create_window_surface(&gl_config, &surface_attributes) }
.expect("Failed Display.create_window_surface");
let context = not_current.make_current(&surface)
.expect("Failed NotCurrentContext.make_current");
shade::gl::capi::load_with(|s| {
let c = CString::new(s).unwrap();
gl_display.get_proc_address(&c)
});
GlWindow { size, window, surface, context }
}
fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
let width = NonZeroU32::new(new_size.width.max(1)).unwrap();
let height = NonZeroU32::new(new_size.height.max(1)).unwrap();
self.size = new_size;
self.surface.resize(&self.context, width, height);
}
}
struct TriangleDemo {
vertices: shade::VertexBuffer,
shader: shade::ShaderProgram,
}
impl TriangleDemo {
fn new(g: &mut shade::Graphics) -> TriangleDemo {
let vertices = g.vertex_buffer(&[
TriangleVertex { position: Vec2(0.0, 0.5), color: [255, 0, 0, 255] },
TriangleVertex { position: Vec2(-0.5, -0.5), color: [0, 255, 0, 255] },
TriangleVertex { position: Vec2(0.5, -0.5), color: [0, 0, 255, 255] },
],
shade::BufferUsage::Static,
);
let shader = g.shader_compile(VERTEX_SHADER, FRAGMENT_SHADER);
TriangleDemo { vertices, shader }
}
fn draw(&mut self, g: &mut shade::Graphics, viewport: Bounds2i) {
g.begin(&shade::BeginArgs::BackBuffer { viewport });
shade::clear!(g, color: Vec4(0.2, 0.5, 0.2, 1.0));
g.draw(&shade::DrawArgs {
scissor: None,
blend_mode: shade::BlendMode::Solid,
depth_test: None,
cull_mode: None,
mask: shade::DrawMask::COLOR,
prim_type: shade::PrimType::Triangles,
shader: self.shader,
vertices: &[shade::DrawVertexBuffer {
buffer: self.vertices,
divisor: shade::VertexDivisor::PerVertex,
}],
uniforms: &[],
vertex_start: 0,
vertex_end: 3,
instances: -1,
});
g.end();
}
}
struct App {
window: GlWindow,
opengl: shade::gl::GlGraphics,
demo: TriangleDemo,
}
impl App {
fn new(event_loop: &winit::event_loop::ActiveEventLoop, size: winit::dpi::PhysicalSize<u32>) -> Box<App> {
let window = GlWindow::new(event_loop, size);
let mut opengl = shade::gl::GlGraphics::new(shade::gl::GlConfig { srgb: true });
let demo = TriangleDemo::new(opengl.as_graphics());
Box::new(App { window, opengl, demo })
}
fn draw(&mut self) {
let viewport = Bounds2::c(0, 0, self.window.size.width as i32, self.window.size.height as i32);
self.demo.draw(self.opengl.as_graphics(), viewport);
}
}
fn main() {
let event_loop = winit::event_loop::EventLoop::new().expect("Failed to create event loop");
let size = winit::dpi::PhysicalSize::new(800, 600);
let mut app: Option<Box<App>> = None;
#[allow(deprecated)]
let _ = event_loop.run(move |event, event_loop| {
use winit::event::{Event, WindowEvent};
match event {
Event::Resumed => {
if app.is_none() {
app = Some(App::new(event_loop, size));
}
}
Event::WindowEvent { event, .. } => match event {
WindowEvent::Resized(new_size) => {
if let Some(app) = app.as_deref_mut() {
app.window.resize(new_size);
}
}
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::RedrawRequested => {
if let Some(app) = app.as_deref_mut() {
app.draw();
app.window.surface.swap_buffers(&app.window.context).unwrap();
}
}
_ => {}
},
Event::AboutToWait => {
if let Some(app) = app.as_deref() {
app.window.window.request_redraw();
}
}
_ => {}
}
});
}