Retrieving vertex attribute locations
@@ -82,18 +82,21 @@ fn main() {
unsafe {
gl::BindVertexArray(vao);
gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
- gl::EnableVertexAttribArray(0); // this is "layout (location = 0)" in vertex shader
+ use std::ffi::{CString};
+ let pos_location = gl::GetAttribLocation(shader_program.id(), CString::new("Position").unwrap().as_ptr()) as gl::types::GLuint;
+ gl::EnableVertexAttribArray(pos_location);
gl::VertexAttribPointer(
- 0, // index of the generic vertex attribute ("layout (location = 0)")
+ pos_location, // index of the generic vertex attribute
3, // the number of components per generic vertex attribute
gl::FLOAT, // data type
gl::FALSE, // normalized (int-to-float conversion)
(6 * std::mem::size_of::<f32>()) as gl::types::GLint, // stride (byte offset between consecutive attributes)
std::ptr::null() // offset of the first component
);
- gl::EnableVertexAttribArray(1); // this is "layout (location = 0)" in vertex shader
+ let color_location = gl::GetAttribLocation(shader_program.id(), CString::new("Color").unwrap().as_ptr()) as gl::types::GLuint;
+ gl::EnableVertexAttribArray(color_location);
gl::VertexAttribPointer(
- 1, // index of the generic vertex attribute ("layout (location = 0)")
+ color_location, // index of the generic vertex attribute
3, // the number of components per generic vertex attribute
gl::FLOAT, // data type
gl::FALSE, // normalized (int-to-float conversion)
