https://github.com/glium/glium/blob/master/book/tuto-07-shape.md
삼각형을 그리는 대신 더 복잡한 모양인 찻주전자를 표현
Utah teapot은 종종 그래픽 프로그래밍의 "hello world" 중 하나로 간주되는 유명한 3D 모델
teapot model data in rust
이 파일에는 3개의 배열이 존재
그래픽스에서 3D model 표현은 삼각형의 집합으로 표현하는데, vertex의 중복을 막기위해 index 배열을 사용하여 3D model을 표현
index 배열의 각 요소는 실제로 vertex 및 normal 배열의 인덱스
세개의 index가 하나의 삼각형을 표현
teapot을 사용하기위해 위 링크의 파일을 프로젝트에 추가하고 코드에 아래 코드를 사용
mod teapot;
데이터 로딩
let positions = glium::VertexBuffer::new(&display, &teapot::VERTICES).unwrap();
let normals = glium::VertexBuffer::new(&display, &teapot::NORMALS).unwrap();
let indices = glium::IndexBuffer::new(&display, glium::index::PrimitiveType::TrianglesList,
&teapot::INDICES).unwrap();
vertex shader 수정
#version 140
in vec3 position;
in vec3 normal;
uniform mat4 matrix;
void main() {
gl_Position = matrix * vec4(position, 1.0);
}
fragment shader 수정
#version 140
out vec4 color;
void main() {
color = vec4(1.0, 0.0, 0.0, 1.0);
}
drawing code 수정
let matrix = [
[0.01, 0.0, 0.0, 0.0],
[0.0, 0.01, 0.0, 0.0],
[0.0, 0.0, 0.01, 0.0],
[0.0, 0.0, 0.0, 1.0f32]
];
target.draw((&positions, &normals), &indices, &program, &uniform! { matrix: matrix },
&Default::default()).unwrap();
전체코드
#[macro_use]
extern crate glium;
mod teapot;
fn main() {
#[allow(unused_imports)]
use glium::{glutin, Surface};
let mut event_loop = glutin::event_loop::EventLoop::new();
let wb = glutin::window::WindowBuilder::new();
let cb = glutin::ContextBuilder::new();
let display = glium::Display::new(wb, cb, &event_loop).unwrap();
let positions = glium::VertexBuffer::new(&display, &teapot::VERTICES).unwrap();
let normals = glium::VertexBuffer::new(&display, &teapot::NORMALS).unwrap();
let indices = glium::IndexBuffer::new(
&display,
glium::index::PrimitiveType::TrianglesList,
&teapot::INDICES,
)
.unwrap();
let vertex_shader_src = r#"
#version 140
in vec3 position;
in vec3 normal;
uniform mat4 matrix;
void main() {
gl_Position = matrix * vec4(position, 1.0);
}
"#;
let fragment_shader_src = r#"
#version 140
out vec4 color;
void main() {
color = vec4(1.0, 0.0, 0.0, 1.0);
}
"#;
let program =
glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None)
.unwrap();
let mut t: f32 = -0.5;
event_loop.run(move |event, _, control_flow| {
match event {
glutin::event::Event::WindowEvent { event, .. } => match event {
glutin::event::WindowEvent::CloseRequested => {
*control_flow = glutin::event_loop::ControlFlow::Exit;
return;
}
_ => return,
},
glutin::event::Event::NewEvents(cause) => match cause {
glutin::event::StartCause::ResumeTimeReached { .. } => (),
glutin::event::StartCause::Init => (),
_ => return,
},
_ => return,
}
let next_frame_time =
std::time::Instant::now() + std::time::Duration::from_nanos(16_666_667);
*control_flow = glutin::event_loop::ControlFlow::WaitUntil(next_frame_time);
t += 0.002;
if t > 0.5 {
t = -0.5;
}
let uniforms = uniform! {
// matrix: [
// [ t.cos(), t.sin(), 0.0, 0.0],
// [-t.sin(), t.cos(), 0.0, 0.0],
// [0.0, 0.0, 1.0, 0.0],
// [0.0, 0.0, 0.0, 1.0f32],
// ]
matrix: [
[0.01, 0.0, 0.0, 0.0],
[0.0, 0.01, 0.0, 0.0],
[0.0, 0.0, 0.01, 0.0],
[0.0, 0.0, 0.0, 1.0f32]
]
};
let mut target = display.draw();
target.clear_color(0.3, 0.5, 0.7, 1.0);
target
.draw(
(&positions, &normals),
&indices,
&program,
&uniforms,
&Default::default(),
)
.unwrap();
target.finish().unwrap();
});
}