Crates.io | glsmrs |
lib.rs | glsmrs |
version | 0.2.0 |
source | src |
created_at | 2021-01-16 04:45:09.640185 |
updated_at | 2022-06-14 12:15:16.481607 |
description | Opinionated thin wrapper over WebGL 1.0 API |
homepage | |
repository | https://github.com/wg-romank/glsmrs |
max_upload_size | |
id | 342664 |
size | 26,400 |
An opinionated wrapper for low-level WebGL API with intention to provide a bit of explicit state management with reasonable defaults.
Primary goals for this library is to support WebGL 1.0 so it is built on top of raw bindings from web-sys
crate.
shade
method for drawing.Get it from crates.io
[dependencies]
# ...
glsmrs = "0.2.0"
Import crate
use glsmrs as gl;
Create context, Ctx
is a wrapper that is using Rc
internally so you can clone it and pass around without worrying too much about lifetimes.
let ctx = gl::util::get_ctx("canvas-name", "webgl")?;
Create some mesh, like an RGB triangle
let vertices = vec![[0.5, -0.5], [0.0, 0.5], [-0.5, -0.5]];
let colors = vec![[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]];
let indices = [0, 1, 2];
let triangle = gl::mesh::Mesh::new(&ctx, &indices)?
.with_attribute::<gl::attributes::AttributeVector2>("position", &vertices)?
.with_attrubute::<gl::attributes::AttributeVector3>("color", &colors)?;
Define render target
let viewport = gl::texture::Viewport::new(720, 480);
let displayfb = gl::texture::EmptyFramebuffer::new(&ctx, viewport);
Create a program description
let program = gl::Program::new(
&ctx,
include_str!("../shaders/dummy.vert"),
include_str!("../shaders/dummy.frag"),
)?;
Run program on state supplying necessary inputs
let uniforms: HashMap<_, _> = vec![
("time", gl::UniformData::Scalar(time as f32)),
].into_iter().collect();
let pipeline = gl::Pipeline::new(&ctx);
pipeline.shade(
&program,
uniforms,
vec![&mut triangle],
&mut displayfb
)?;
For example project using this library check out [ https://github.com/wg-romank/wasm-game-of-life ] a tweaked version of original WASM tutorial that runs entierly on GPU.