summaryrefslogtreecommitdiffstats
path: root/glow/src/triangle/solid.rs
diff options
context:
space:
mode:
Diffstat (limited to 'glow/src/triangle/solid.rs')
-rw-r--r--glow/src/triangle/solid.rs86
1 files changed, 86 insertions, 0 deletions
diff --git a/glow/src/triangle/solid.rs b/glow/src/triangle/solid.rs
new file mode 100644
index 00000000..5ba7f91c
--- /dev/null
+++ b/glow/src/triangle/solid.rs
@@ -0,0 +1,86 @@
+use crate::program::Version;
+use crate::triangle::{set_transform, simple_triangle_program};
+use crate::Color;
+use glow::{Context, HasContext, NativeProgram};
+use iced_graphics::Transformation;
+
+#[derive(Debug)]
+pub struct SolidProgram {
+ program: <Context as HasContext>::Program,
+ uniform_data: SolidUniformData,
+}
+
+#[derive(Debug)]
+struct SolidUniformData {
+ pub color: Color,
+ pub color_location: <Context as HasContext>::UniformLocation,
+ pub transform: Transformation,
+ pub transform_location: <Context as HasContext>::UniformLocation,
+}
+
+impl SolidUniformData {
+ fn new(gl: &Context, program: NativeProgram) -> Self {
+ Self {
+ color: Color::TRANSPARENT,
+ color_location: unsafe {
+ gl.get_uniform_location(program, "color")
+ }
+ .expect("Solid - Get color."),
+ transform: Transformation::identity(),
+ transform_location: unsafe {
+ gl.get_uniform_location(program, "u_Transform")
+ }
+ .expect("Solid - Get u_Transform."),
+ }
+ }
+}
+
+impl SolidProgram {
+ pub fn new(gl: &Context, shader_version: &Version) -> Self {
+ let program = simple_triangle_program(
+ gl,
+ shader_version,
+ include_str!("../shader/common/triangle.frag"),
+ );
+
+ Self {
+ program,
+ uniform_data: SolidUniformData::new(gl, program),
+ }
+ }
+
+ pub fn write_uniforms(
+ &mut self,
+ gl: &Context,
+ color: &Color,
+ transform: &Transformation,
+ ) {
+ if transform != &self.uniform_data.transform {
+ set_transform(gl, self.uniform_data.transform_location, *transform)
+ }
+
+ if color != &self.uniform_data.color {
+ unsafe {
+ gl.uniform_4_f32(
+ Some(&self.uniform_data.color_location),
+ color.r,
+ color.g,
+ color.b,
+ color.a,
+ );
+ }
+
+ self.uniform_data.color = *color;
+ }
+ }
+
+ pub fn use_program(
+ &mut self,
+ gl: &Context,
+ color: &Color,
+ transform: &Transformation,
+ ) {
+ unsafe { gl.use_program(Some(self.program)) }
+ self.write_uniforms(gl, color, transform)
+ }
+}