7个版本
0.3.2 | 2024年7月1日 |
---|---|
0.3.1 | 2024年6月27日 |
0.2.2 | 2024年5月10日 |
0.1.0 | 2024年4月18日 |
#115 在 图形API
217 每月下载量
在 2 crates 中使用
155KB
4K SLoC
glium-types
这是一个设计用于包含开始使用glium所需所有内容的crate。它包括
- 2-4维度的浮点数、整数、无符号整数和双精度向量
- 2-4维度的方阵
- 四元数
- 顶点类型:顶点、法线、顶点颜色和纹理坐标。
为了减少样板代码,它还包括
- 网格宏
- 预制的顶点着色器
- 3D绘图参数
- 调试茶壶
use std::{thread, time::{Duration, Instant}};
use glium::{backend::glutin::SimpleWindowBuilder, uniform, DrawParameters, Program, Surface};
use glium_types::{prelude::*, teapot};
use winit::event::{Event, WindowEvent};
fn main(){
let event_loop = winit::event_loop::EventLoop::new().unwrap();
event_loop.set_control_flow(winit::event_loop::ControlFlow::Poll);
let (window, display) = SimpleWindowBuilder::new().build(&event_loop);
let (indices, verts, norms) = mesh!(&display, &teapot::INDICES, &teapot::VERTICES, &teapot::NORMALS);
let program = Program::from_source(&display, shaders::VERTEX,
"#version 140
out vec4 colour;
in vec3 v_normal;
uniform vec3 light;
void main(){
colour = vec4(vec3(dot(normalize(v_normal), light)), 1.0);
}", None).unwrap();
let draw_parameters = DrawParameters{
//teapot uses clockwise culling. most other models use anti clockwise culling
backface_culling: glium::BackfaceCullingMode::CullClockwise,
..params::alias_3d()
};
let time = Instant::now();
event_loop.run(|event, target| {
match event {
Event::AboutToWait => {
let time = time.elapsed().as_secs_f32();
display.resize(window.inner_size().into());
let mut frame = display.draw();
let view = Mat4::view_matrix_3d(frame.get_dimensions(), 1.0, 1024.0, 0.1);
let camera = Mat4::from_pos(vec3(0.0, 0.0, 20.0));
// multiplying quaternions is equivelant to transformations,
// so the bellow code will rotate around the z axis then x and then y.
// this also works for matrices
let rot = Quaternion::from_y_rot(time)
* Quaternion::from_x_rot(time / 2.0)
* Quaternion::from_z_rot(time / 4.0);
//moves up 50.0 then scales and rotates.
let model = Mat4::from_rot(rot) * Mat4::from_scale(Vec3::splat(0.1))
* Mat4::from_pos(vec3(0.0, 50.0, 0.0));
println!("teapot origin at {:?}", vec4(0.0, 0.0, 0.0, 1.0).transform(&model).truncate());
//input for the vertex shader and our fragment shader.
let uniforms = uniform! {
view: view, model: model, camera: camera,
light: vec3(0.5, 1.0, -0.5).normalise()
};
frame.clear_color_and_depth((0.0, 0.0, 0.0, 0.0), 1.0);
frame.draw((&verts, &norms), &indices, &program, &uniforms, &draw_parameters).unwrap();
frame.finish().unwrap();
//throttle speed of rendering
thread::sleep(Duration::from_millis(16));
},
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => target.exit(),
_ => ()
}
}).unwrap();
}
依赖关系
~7–21MB
~331K SLoC