To run the example in "example" folder, run: ``` cd ./example cargo web start --target-webasm ````
Example from https://www.tutorialspoint.com/webgl/webgldrawinga_triangle.htm in Rust
```rust let size = (800,600); let config = AppConfig::new("Test",size); let mut app = App::new(config);
let vertices: Vec
WebGLRenderingContext::loadwith(|name|app.getproc_address(name));
let gl = WebGLRenderingContext::new(app.canvas());
// Create an empty buffer object to store vertex buffer let vertexbuffer = gl.createbuffer();
// Bind appropriate array buffer to it gl.bindbuffer(BufferKind::Array, &vertexbuffer);
// Pass the vertex data to the buffer gl.bufferdata(BufferKind::Array, &vertices.intobytes(), DrawMode::Static);
// Unbind the buffer gl.unbind_buffer(BufferKind::Array);
// Create an empty buffer object to store Index buffer let indexbuffer = gl.createbuffer();
// Bind appropriate array buffer to it gl.bindbuffer(BufferKind::ElementArray, &indexbuffer);
// Pass the vertex data to the buffer gl.bufferdata( BufferKind::ElementArray, &indices.intobytes(), DrawMode::Static, );
// Unbind the buffer gl.unbind_buffer(BufferKind::ElementArray);
/================ Shaders ====================/
// Vertex shader source code let vertcode = "attribute vec3 coordinates; void main(void) { glPosition = vec4(coordinates, 1.0); }";
// Create a vertex shader object let vertshader = gl.createshader(ShaderKind::Vertex);
// Attach vertex shader source code gl.shadersource(&vertshader, vert_code);
// Compile the vertex shader gl.compileshader(&vertshader);
//fragment shader source code let fragcode = "void main(void) { glFragColor = vec4(1, 0.5, 0.0, 1); }"; // Create fragment shader object let fragshader = gl.createshader(ShaderKind::Fragment);
// Attach fragment shader source code gl.shadersource(&fragshader, frag_code);
// Compile the fragmentt shader gl.compileshader(&fragshader);
// Create a shader program object to store // the combined shader program let shaderprogram = gl.createprogram();
// Attach a vertex shader gl.attachshader(&shaderprogram, &vert_shader);
// Attach a fragment shader gl.attachshader(&shaderprogram, &frag_shader);
// Link both the programs gl.linkprogram(&shaderprogram);
// Use the combined shader program object gl.useprogram(&shaderprogram);
/======= Associating shaders to buffer objects =======/
// Bind vertex buffer object gl.bindbuffer(BufferKind::Array, &vertexbuffer);
// Bind index buffer object gl.bindbuffer(BufferKind::ElementArray, &indexbuffer);
// Get the attribute location let coord = gl.getattriblocation(&shader_program, "coordinates".into()) .unwrap();
// Point an attribute to the currently bound VBO gl.vertexattribpointer(coord, 3, DataType::Float, false, 0, 0);
// Enable the attribute gl.enablevertexattrib_array(coord);
/=========Drawing the triangle===========/
// Clear the canvas gl.clear_color(0.5, 0.5, 0.5, 0.9);
// Enable the depth test gl.enable(Flag::DepthTest);
// Clear the color buffer bit gl.clear(BufferBit::Color); gl.clear(BufferBit::Depth);
// Set the view port gl.viewport(0, 0, size.0, size.1);
app.run(move || { gl.clear(BufferBit::Color); gl.clear(BufferBit::Depth); gl.clearcolor(1.0, 1.0, 1.0, 1.0); gl.drawelements(Primitives::Triangles, count, DataType::U16, 0); }); ```