Crates.io | rgl |
lib.rs | rgl |
version | 0.2.0 |
source | src |
created_at | 2018-08-18 21:02:46.412405 |
updated_at | 2018-08-19 06:57:35.68088 |
description | Safe thin wrapper for OpenGL using the gl-rs crate |
homepage | |
repository | https://github.com/Hopson97/rusty-gl |
max_upload_size | |
id | 80132 |
size | 24,154 |
Note: This is a work in progress.
Crate: https://crates.io/crates/rgl
Very thin wrapper over gl-rs, aiming to make code more "rust-like" and safer, while still allowing the control you have with classic OpenGL code.
VAO(GLuint)
to enforce correct OpenGL object type is passed in functionsCurrent tuple structs:
pub struct VBO(GLuint);
for vertex buffer objectspub struct VAO(GLuint);
for vertex array objectspub struct GLTexture(GLuint);
for texture objectspub struct GLShader(GLuint);
for programs/ shader objectThese structs have no implemention (assosiated function), they are just there to enforce that the correct OpenGL functions are passed into functions. For example:
let mut vao = gl_gen_vertex_array();
gl_bind_buffer(GLTarget::ArrayBuffer, vao);
would not work, as gl_bind_buffer
expects type struct VBO(GLuint)
, but vao is of type VAO(GLuint)
.
[dependancies]
rgl = "0.2.0"
gl = "0.6.0"
The gl-rs
create (gl
) is still needed for certain things such as window proc address and types (eg GLuint
, GLfloat
etc)
As mentioned above, this crate aims to be a more rust-like alternative to gl-rs.
For example, code from gl-rs such as:
let mut vao = 0;
let mut vbo = 0;
unsafe {
// Create Vertex Array Object
gl::GenVertexArrays(1, &mut vao);
gl::BindVertexArray(vao);
// Create a Vertex Buffer Object and copy the vertex data to it
gl::GenBuffers(1, &mut vbo);
gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
gl::BufferData(
gl::ARRAY_BUFFER,
(VERTEX_DATA.len() * mem::size_of::<GLfloat>()) as GLsizeiptr,
mem::transmute(&VERTEX_DATA[0]),
gl::STATIC_DRAW,
);
gl::EnableVertexAttribArray(0);
gl::VertexAttribPointer(
0,
2,
gl::FLOAT,
gl::FALSE as GLboolean,
0,
ptr::null(),
);
}
Is instead written like
//Create a vertex array object and a vertex buffer object
let mut vao = rgl::gen_vertex_array();
let mut vbo = rgl::gen_buffer();
//Generate and bind the VAO
rgl::gl_bind_vertex_array(vao);
//Generate and bind the VBO
rgl::gl_bind_buffer(rgl::Target::ArrayBuffer, vbo);
//Buffer the vertex data and tell OpenGL the structure
rgl::buffer_data(rgl::Target::ArrayBuffer, &VERTEX_DATA, rgl::Usage::StaticDraw);
rgl::enable_vertex_attrib_array(0);
rgl::vertex_attrib_pointer(0, 2, rgl::Type::Float, false, 0);
Changes include:
unsafe {...}
blocksstd::os::raw::c_void
, rusty-gl will do this for youstd::ffi::CString
, rusty-gl will do this for youExamples on using this crate can be found in the examples folder: https://github.com/Hopson97/rusty-gl/tree/master/Examples
Run the examples with cargo run
.