I'm definitely doing something extremely rookie here, but I simply cannot draw a square with index buffers. I'm following the intructions in this video. But I just don't get the expected output.
I checked and rechecked my code a lot. Does anyone know what I might be missing? Here's my code:
extern crate gl;
extern crate sdl2;
use gl::types::*;
use std::ffi::CString;
use std::ptr;
use std::str;
// Shader sources
static VS_SRC: &'static str = "
#version 330 core
layout (location = 0) in vec4 Position;
void main()
{
gl_Position = Position;
}
";
static FS_SRC: &'static str = "
#version 330 core
layout(location = 0) out vec4 color;
void main()
{
color = vec4(0.2, 0.3, 0.8, 1.0);
}
";
fn compile_shader(src: &str, ty: GLenum) -> GLuint {
let shader;
unsafe {
shader = gl::CreateShader(ty);
// Attempt to compile the shader
let c_str = CString::new(src.as_bytes()).unwrap();
gl::ShaderSource(shader, 1, &c_str.as_ptr(), ptr::null());
gl::CompileShader(shader);
// Get the compile status
let mut status = gl::FALSE as GLint;
gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut status);
// Fail on error
if status != (gl::TRUE as GLint) {
let mut len = 0;
gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut len);
let mut buf = Vec::with_capacity(len as usize);
buf.set_len((len as usize) - 1); // subtract 1 to skip the trailing null character
gl::GetShaderInfoLog(
shader,
len,
ptr::null_mut(),
buf.as_mut_ptr() as *mut GLchar,
);
panic!(
"{}",
str::from_utf8(&buf)
.ok()
.expect("ShaderInfoLog not valid utf8")
);
}
}
shader
}
fn link_program(vs: GLuint, fs: GLuint) -> GLuint {
unsafe {
let program = gl::CreateProgram();
gl::AttachShader(program, vs);
gl::AttachShader(program, fs);
gl::LinkProgram(program);
// Get the link status
let mut status = gl::FALSE as GLint;
gl::GetProgramiv(program, gl::LINK_STATUS, &mut status);
// Fail on error
if status != (gl::TRUE as GLint) {
let mut len: GLint = 0;
gl::GetProgramiv(program, gl::INFO_LOG_LENGTH, &mut len);
let mut buf = Vec::with_capacity(len as usize);
buf.set_len((len as usize) - 1); // subtract 1 to skip the trailing null character
gl::GetProgramInfoLog(
program,
len,
ptr::null_mut(),
buf.as_mut_ptr() as *mut GLchar,
);
panic!(
"{}",
str::from_utf8(&buf)
.ok()
.expect("ProgramInfoLog not valid utf8")
);
}
program
}
}
fn main() {
let sdl = sdl2::init().unwrap();
let video_subsystem = sdl.video().unwrap();
let gl_attr = video_subsystem.gl_attr();
gl_attr.set_context_profile(sdl2::video::GLProfile::Core);
gl_attr.set_context_version(4, 1);
let window = video_subsystem
.window("Window", 500, 500)
.opengl()
.resizable()
.build()
.unwrap();
let _gl_context = window.gl_create_context().unwrap();
let _gl =
gl::load_with(|s| video_subsystem.gl_get_proc_address(s) as *const std::os::raw::c_void);
// Create GLSL shaders
let vs = compile_shader(VS_SRC, gl::VERTEX_SHADER);
let fs = compile_shader(FS_SRC, gl::FRAGMENT_SHADER);
let _program = link_program(vs, fs);
let positions: Vec<f32> = vec![-0.5, -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5];
let indices: [u8; 6] = [0, 1, 2, 2, 3, 0];
let mut triangle_buffer: gl::types::GLuint = 0;
unsafe {
gl::GenBuffers(1, &mut triangle_buffer);
gl::BindBuffer(gl::ARRAY_BUFFER, triangle_buffer);
gl::BufferData(
gl::ARRAY_BUFFER,
(8 * std::mem::size_of::<f32>()) as gl::types::GLsizeiptr,
positions.as_ptr() as *const gl::types::GLvoid,
gl::STATIC_DRAW,
);
gl::VertexAttribPointer(
0,
2,
gl::FLOAT,
gl::FALSE,
(2 * std::mem::size_of::<f32>()) as gl::types::GLint,
std::ptr::null(),
);
gl::EnableVertexAttribArray(0);
}
let mut ibo: gl::types::GLuint = 0;
unsafe {
gl::GenBuffers(1, &mut ibo);
gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, ibo);
gl::BufferData(
gl::ELEMENT_ARRAY_BUFFER,
(6 * std::mem::size_of::<u8>()) as gl::types::GLsizeiptr,
indices.as_ptr() as *const gl::types::GLvoid,
gl::STATIC_DRAW,
);
}
// set up shared state for window
let mut event_pump = sdl.event_pump().unwrap();
'main: loop {
for event in event_pump.poll_iter() {
match event {
sdl2::event::Event::Quit { .. } => break 'main,
_ => {}
}
}
unsafe {
// Clear the screen to black
gl::ClearColor(0.3, 0.3, 0.3, 1.0);
gl::Clear(gl::COLOR_BUFFER_BIT);
gl::BindVertexArray(triangle_buffer);
gl::DrawElements(
gl::TRIANGLES, // mode
6,
gl::TRIANGLES,
ptr::null(),
);
}
window.gl_swap_window();
}
}