
이전 코드에 SQUARE 내용 수정
const SQUARE: [Vertex; 4] = [
Vertex { pos: [ 0.5, -0.5], color: WHITE },
Vertex { pos: [-0.5, -0.5], color: WHITE },
Vertex { pos: [-0.5, 0.5], color: WHITE },
Vertex { pos: [ 0.5, 0.5], color: WHITE },
];
index 배열 추가
const INDICES: &[u16] = &[0, 1, 2, 2, 3, 0];

버퍼생성 코드 수정
let (vertex_buffer, slice) = factory.create_vertex_buffer_with_slice(&SQUARE, INDICES);
전체 코드
#[macro_use]
extern crate gfx;
extern crate gfx_window_glutin;
extern crate glutin;
use gfx::traits::FactoryExt;
use gfx::Device;
use gfx_window_glutin as gfx_glutin;
pub type ColorFormat = gfx::format::Srgba8;
pub type DepthFormat = gfx::format::DepthStencil;
const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
const WHITE: [f32; 3] = [1.0, 1.0, 1.0];
gfx_defines! {
vertex Vertex {
pos: [f32; 2] = "a_Pos",
color: [f32; 3] = "a_Color",
}
pipeline pipe {
vbuf: gfx::VertexBuffer<Vertex> = (),
out: gfx::RenderTarget<ColorFormat> = "Target0",
}
}
pub fn main() {
const SQUARE: [Vertex; 4] = [
Vertex { pos: [ 0.5, -0.5], color: WHITE },
Vertex { pos: [-0.5, -0.5], color: WHITE },
Vertex { pos: [-0.5, 0.5], color: WHITE },
Vertex { pos: [ 0.5, 0.5], color: WHITE },
];
const INDICES: &[u16] = &[0, 1, 2, 2, 3, 0];
let vertex_shader_src = r#"
#version 150 core
in vec2 a_Pos;
in vec3 a_Color;
out vec4 v_Color;
void main() {
v_Color = vec4(a_Color, 1.0);
gl_Position = vec4(a_Pos, 0.0, 1.0);
}
"#;
let fragment_shader_src = r#"
#version 150 core
in vec4 v_Color;
out vec4 Target0;
void main() {
Target0 = v_Color;
}
"#;
let events_loop = glutin::EventsLoop::new();
let builder = glutin::WindowBuilder::new()
.with_title("Square Toy".to_string())
.with_dimensions(800, 800)
.with_vsync();
let (window, mut device, mut factory, mut main_color, mut main_depth) =
gfx_glutin::init::<ColorFormat, DepthFormat>(builder, &events_loop);
let mut encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();
let pso = factory.create_pipeline_simple(
vertex_shader_src.as_bytes(),
fragment_shader_src.as_bytes(),
pipe::new()
).unwrap();
let (vertex_buffer, slice) = factory.create_vertex_buffer_with_slice(&SQUARE, INDICES);
let mut data = pipe::Data {
vbuf: vertex_buffer,
out: main_color.clone()
};
let mut running = true;
while running {
events_loop.poll_events(
|glutin::Event::WindowEvent {
window_id: _,
event,
}| {
use glutin::WindowEvent::*;
match event {
KeyboardInput(_, _, Some(glutin::VirtualKeyCode::Escape), _) | Closed => {
running = false
}
Resized(_, _) => {
gfx_glutin::update_views(&window, &mut main_color, &mut main_depth);
}
_ => (),
}
},
);
encoder.clear(&main_color, BLACK);
encoder.draw(&slice, &pso, &data);
encoder.flush(&mut device);
window.swap_buffers().unwrap();
device.cleanup();
}
}
