Added vertex data and sending it to the GPU
@@ -31,10 +31,7 @@ fn main() {
let _gl_context = window.gl_create_context().unwrap();
let _gl = gl::load_with(|s| video_ctx.gl_get_proc_address(s) as *const std::os::raw::c_void);
- unsafe {
- gl::Viewport(0, 0, 900, 700); // set viewport
- gl::ClearColor(0.3, 0.3, 0.5, 1.0);
- }
+ // set up shader program
use std::ffi::{CString};
let vert_shader = render_gl::Shader::from_vert_source(
@@ -51,8 +48,62 @@ fn main() {
shader_program.set_used();
+ // set up vertex buffer object
+ let vertices: Vec<f32> = vec![
+ -0.5, -0.5, 0.0,
+ 0.5, -0.5, 0.0,
+ 0.0, 0.5, 0.0
+ ];
+
+ let mut vbo: gl::types::GLuint = 0;
+ unsafe {
+ gl::GenBuffers(1, &mut vbo);
+ }
+
+ unsafe {
+ gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
+ gl::BufferData(
+ gl::ARRAY_BUFFER, // target
+ (vertices.len() * std::mem::size_of::<f32>()) as gl::types::GLsizeiptr, // size of data in bytes
+ vertices.as_ptr() as *const gl::types::GLvoid, // pointer to data
+ gl::STATIC_DRAW, // usage
+ );
+ gl::BindBuffer(gl::ARRAY_BUFFER, 0);
+ }
+
+ // set up vertex array object
+
+ let mut vao: gl::types::GLuint = 0;
+ unsafe {
+ gl::GenVertexArrays(1, &mut vao);
+ }
+
+ unsafe {
+ gl::BindVertexArray(vao);
+ gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
+ gl::EnableVertexAttribArray(0); // this is "layout (location = 0)" in vertex shader
+ gl::VertexAttribPointer(
+ 0, // index of the generic vertex attribute ("layout (location = 0)")
+ 3, // the number of components per generic vertex attribute
+ gl::FLOAT, // data type
+ gl::FALSE, // normalized (int-to-float conversion)
+ (3 * std::mem::size_of::<f32>()) as gl::types::GLint, // stride (byte offset between consecutive attributes)
+ std::ptr::null() // offset of the first component
+ );
+ gl::BindBuffer(gl::ARRAY_BUFFER, 0);
+ gl::BindVertexArray(0);
+ }
+
+ // set up shared state for window
+ unsafe {
+ gl::Viewport(0, 0, 900, 700); // set viewport
+ gl::ClearColor(0.3, 0.3, 0.5, 1.0);
+ }
+
+ // set up event handling
let mut events = ctx.event_pump().unwrap();
+ // main loop
let mut main_loop = || {
for event in events.poll_iter() {
match event {
@@ -67,6 +118,17 @@ fn main() {
gl::Clear(gl::COLOR_BUFFER_BIT);
}
+ // draw triangle
+ shader_program.set_used();
+ unsafe {
+ gl::BindVertexArray(vao);
+ gl::DrawArrays(
+ gl::TRIANGLES, // mode
+ 0, // starting index in the enabled arrays
+ 3 // number of indices to be rendered
+ );
+ }
+
window.gl_swap_window();
};
