summaryrefslogtreecommitdiff
path: root/webhogg/wasm/src/context/shader.rs
diff options
context:
space:
mode:
authorDennis Kobert <d-kobert@web.de>2019-06-21 23:52:07 +0200
committerDennis Kobert <d-kobert@web.de>2019-06-21 23:52:07 +0200
commit4e0037169db5d0c7d824debedb5513b69676506a (patch)
tree82c56547fa70b499627236efa66c3bf7e5411ead /webhogg/wasm/src/context/shader.rs
parent36c89240a87ecb826cf09bc7b3069aa636c9f2f1 (diff)
parent031f63755aada2f1b51eb945fda2a18ad0d24aad (diff)
Merge branch 'wasm' into game_server_refactor
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)
+ }
+}