summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornatrixaeria <janng@gmx.de>2019-06-14 00:13:15 +0200
committernatrixaeria <janng@gmx.de>2019-06-14 00:13:15 +0200
commitb75597c140e9b4758a1ad803044dda96b403e1a5 (patch)
treed61eb5116cd45cd411b5e079d09fd07540c09042
parent03cedca74429c326fcca176576702873e32a8455 (diff)
Wrap WebGl2
-rw-r--r--webhogg/wasm/src/context/graphics.rs19
-rw-r--r--webhogg/wasm/src/context/mod.rs1
-rw-r--r--webhogg/wasm/src/context/webgl.rs26
3 files changed, 38 insertions, 8 deletions
diff --git a/webhogg/wasm/src/context/graphics.rs b/webhogg/wasm/src/context/graphics.rs
index b94441d..fa37e39 100644
--- a/webhogg/wasm/src/context/graphics.rs
+++ b/webhogg/wasm/src/context/graphics.rs
@@ -1,10 +1,12 @@
use log::*;
use crate::error::WasmError;
use wasm_bindgen::JsCast;
-use web_sys::WebGl2RenderingContext as Gl;
+use web_sys::WebGl2RenderingContext as GlContext;
+
+use super::webgl::{Color4, WebGl2};
pub struct GraphicsContext {
- gl: Gl,
+ gl: WebGl2,
frame_nr: u64,
}
@@ -16,26 +18,27 @@ impl GraphicsContext {
.ok_or_else(|| WasmError::WebGl2ContextCreation(
format!("context cration failed: getContext returned nothing")))?;
let context = context
- .dyn_into::<Gl>()
+ .dyn_into::<GlContext>()
.map_err(|_| WasmError::WebGl2ContextCreation(
format!("context object is not a context")))?;
+
+ let gl = WebGl2::from_context(context);
Ok(Self {
- gl: context,
- frame_nr: 0,
+ gl, frame_nr: 0,
})
}
pub fn update(&mut self) -> Result<(), WasmError> {
let light = 0.5;
+ let speed = 60.0;
- let a = (self.frame_nr as f32) / 60.0;
+ let a = (self.frame_nr as f32) / speed;
let a = f32::abs(f32::sin(a));
let b = f32::abs(f32::cos(a));
let (a, b) = (a * light, b * light);
- self.gl.clear_color(a, light - a, b, 1.0);
- self.gl.clear(Gl::COLOR_BUFFER_BIT);
+ self.gl.clear(Color4::new(a, light - a, b, 1.0));
self.frame_nr += 1;
diff --git a/webhogg/wasm/src/context/mod.rs b/webhogg/wasm/src/context/mod.rs
index 3e8261b..a581047 100644
--- a/webhogg/wasm/src/context/mod.rs
+++ b/webhogg/wasm/src/context/mod.rs
@@ -1,3 +1,4 @@
+mod webgl;
pub mod graphics;
pub mod logic;
diff --git a/webhogg/wasm/src/context/webgl.rs b/webhogg/wasm/src/context/webgl.rs
new file mode 100644
index 0000000..abecc6e
--- /dev/null
+++ b/webhogg/wasm/src/context/webgl.rs
@@ -0,0 +1,26 @@
+use web_sys::WebGl2RenderingContext as GlContext;
+
+pub struct Color4(f32, f32, f32, f32);
+
+impl Color4 {
+ pub fn new(r: f32, g: f32, b: f32, a: f32) -> Color4 {
+ Color4(r, g, b, a)
+ }
+}
+
+pub struct WebGl2 {
+ gl: GlContext,
+}
+
+impl WebGl2 {
+ pub fn from_context(context: GlContext) -> Self {
+ WebGl2 {
+ gl: context,
+ }
+ }
+
+ pub fn clear(&self, color: Color4) {
+ self.gl.clear_color(color.0, color.1, color.2, color.3);
+ self.gl.clear(GlContext::COLOR_BUFFER_BIT);
+ }
+}