summaryrefslogtreecommitdiff
path: root/webhogg/wasm/src/context/shader.rs
diff options
context:
space:
mode:
authornatrixaeria <janng@gmx.de>2019-06-14 17:20:01 +0200
committernatrixaeria <janng@gmx.de>2019-06-14 17:20:01 +0200
commit438825a7ce98a0cd455ff0adebd6d3cf8d3209be (patch)
tree30c4290562910c27c3a11a05344c6b236be62fbf /webhogg/wasm/src/context/shader.rs
parent4fd207e78452a9e282ef65fc9c3eaf8b19115956 (diff)
Draw a rectangle
Diffstat (limited to 'webhogg/wasm/src/context/shader.rs')
-rw-r--r--webhogg/wasm/src/context/shader.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/webhogg/wasm/src/context/shader.rs b/webhogg/wasm/src/context/shader.rs
new file mode 100644
index 0000000..9ccb9fc
--- /dev/null
+++ b/webhogg/wasm/src/context/shader.rs
@@ -0,0 +1,34 @@
+use crate::error::WasmError;
+use super::webgl;
+use super::webgl::{WebGl2, ShaderType};
+
+pub const MAIN_VERTEX_SHADER: &str = include_str!("main.vs");
+pub const MAIN_FRAGMENT_SHADER: &str = include_str!("main.fs");
+
+pub struct ShaderProgram {
+ program: webgl::WebGlProgram,
+}
+
+impl ShaderProgram {
+ pub fn from_sources(gl: &WebGl2, sources: &[(ShaderType, String)]) -> Result<Self, WasmError> {
+ let program = gl.create_program()
+ .map_err(|_| WasmError::Shader(format!("glCreateProgram failed ({})", gl.get_error())))?;
+ for (shader_type, source) in sources {
+ let shader = gl.create_shader(shader_type)
+ .map_err(|_| WasmError::Shader(format!("glCreateShader failed ({})", gl.get_error())))?;
+ gl.shader_source(&shader, source);
+ gl.compile_shader(&shader)
+ .map_err(|e| WasmError::Shader(format!("compile error in {} shader: {}", shader_type, e)))?;
+ gl.attach_shader(&program, &shader)
+ }
+ gl.link_program(&program)
+ .map_err(|e| WasmError::Shader(format!("linker error in program: {}", e)))?;
+ Ok(Self {
+ program
+ })
+ }
+
+ pub fn run(&self, gl: &WebGl2) {
+ gl.use_program(&self.program)
+ }
+}