Explorar o código

wallet: fix mesh matrix transform calc

darkfi %!s(int64=2) %!d(string=hai) anos
pai
achega
c85e8e3f28
Modificáronse 2 ficheiros con 61 adicións e 8 borrados
  1. 39 3
      bin/darkwallet/src/app.rs
  2. 22 5
      bin/darkwallet/src/gfx2.rs

+ 39 - 3
bin/darkwallet/src/app.rs

@@ -215,7 +215,7 @@ impl App {
         let window_id = sg.lookup_node("/window").unwrap().id;
         sg.link(node_id, window_id).unwrap();
 
-        // Create a mesh
+        // Create a bg mesh
         let node_id = chatapp::create_mesh(&mut sg, "bg");
 
         let node = sg.get_node_mut(node_id).unwrap();
@@ -227,6 +227,40 @@ impl App {
         let code = vec![Op::LoadVar("lh".to_string())];
         prop.set_expr(3, code).unwrap();
 
+        // Setup the pimpl
+        let node_id = node.id;
+        let (x1, y1) = (0., 0.);
+        let (x2, y2) = (1., 1.);
+        let verts = vec![
+            // top left
+            Vertex { pos: [x1, y1], color: [0., 0., 0., 1.], uv: [0., 0.] },
+            // top right
+            Vertex { pos: [x2, y1], color: [0., 0., 0., 1.], uv: [1., 0.] },
+            // bottom left
+            Vertex { pos: [x1, y2], color: [0., 0., 0., 1.], uv: [0., 1.] },
+            // bottom right
+            Vertex { pos: [x2, y2], color: [0., 0., 0., 1.], uv: [1., 1.] },
+        ];
+        let indices = vec![0, 2, 1, 1, 2, 3];
+        drop(sg);
+        let pimpl =
+            Mesh::new(self.sg.clone(), node_id, self.render_api.clone(), verts, indices).await;
+        let mut sg = self.sg.lock().await;
+        let node = sg.get_node_mut(node_id).unwrap();
+        node.pimpl = pimpl;
+
+        sg.link(node_id, layer_node_id).unwrap();
+
+        // Create another mesh
+        let node_id = chatapp::create_mesh(&mut sg, "box");
+
+        let node = sg.get_node_mut(node_id).unwrap();
+        let prop = node.get_property("rect").unwrap();
+        prop.set_f32(0, 10.).unwrap();
+        prop.set_f32(1, 10.).unwrap();
+        prop.set_f32(2, 60.).unwrap();
+        prop.set_f32(3, 60.).unwrap();
+
         // Setup the pimpl
         let node_id = node.id;
         let (x1, y1) = (0., 0.);
@@ -731,9 +765,11 @@ impl Mesh {
         rect.x += parent_rect.x;
         rect.y += parent_rect.x;
 
+        let off_x = rect.x / parent_rect.w;
+        let off_y = rect.y / parent_rect.h;
         let scale_x = rect.w / parent_rect.w;
         let scale_y = rect.h / parent_rect.h;
-        let model = glam::Mat4::from_translation(glam::Vec3::new(rect.x, rect.y, 0.)) *
+        let model = glam::Mat4::from_translation(glam::Vec3::new(off_x, off_y, 0.)) *
             glam::Mat4::from_scale(glam::Vec3::new(scale_x, scale_y, 1.));
 
         Some((
@@ -742,7 +778,7 @@ impl Mesh {
                 self.dc_key,
                 DrawCall {
                     instrs: vec![
-                        DrawInstruction::ApplyMatrix(glam::Mat4::IDENTITY),
+                        DrawInstruction::ApplyMatrix(model),
                         DrawInstruction::Draw(mesh),
                     ],
                     dcs: vec![],

+ 22 - 5
bin/darkwallet/src/gfx2.rs

@@ -33,6 +33,8 @@ use crate::{
     shader,
 };
 
+const DEBUG_RENDER: bool = false;
+
 #[derive(Debug, SerialEncodable, SerialDecodable)]
 #[repr(C)]
 pub struct Vertex {
@@ -137,9 +139,13 @@ struct RenderContext<'a> {
 
 impl<'a> RenderContext<'a> {
     fn draw(&mut self) {
-        //debug!(target: "gfx", "RenderContext::draw()");
+        if DEBUG_RENDER {
+            debug!(target: "gfx", "RenderContext::draw()");
+        }
         self.draw_call(&self.draw_calls[&0], 0);
-        //debug!(target: "gfx", "RenderContext::draw() [DONE]");
+        if DEBUG_RENDER {
+            debug!(target: "gfx", "RenderContext::draw() [DONE]");
+        }
     }
 
     fn draw_call(&mut self, draw_call: &DrawCall, indent: u32) {
@@ -147,7 +153,9 @@ impl<'a> RenderContext<'a> {
         for instr in &draw_call.instrs {
             match instr {
                 DrawInstruction::ApplyViewport(view) => {
-                    //debug!(target: "gfx", "{}apply_viewport({:?})", ws, view);
+                    if DEBUG_RENDER {
+                        debug!(target: "gfx", "{}apply_viewport({:?})", ws, view);
+                    }
                     let (_, screen_height) = window::screen_size();
 
                     let view_x = view.x.round() as i32;
@@ -160,7 +168,14 @@ impl<'a> RenderContext<'a> {
                     self.ctx.apply_scissor_rect(view_x, view_y, view_w, view_h);
                 }
                 DrawInstruction::ApplyMatrix(model) => {
-                    //debug!(target: "gfx", "{}apply_matrix({:?})", ws, model);
+                    if DEBUG_RENDER {
+                        debug!(target: "gfx", "{}apply_matrix(", ws);
+                        debug!(target: "gfx", "{}    {:?}", ws, model.row(0).to_array());
+                        debug!(target: "gfx", "{}    {:?}", ws, model.row(1).to_array());
+                        debug!(target: "gfx", "{}    {:?}", ws, model.row(2).to_array());
+                        debug!(target: "gfx", "{}    {:?}", ws, model.row(3).to_array());
+                        debug!(target: "gfx", "{})", ws);
+                    }
                     let data: [u8; 64] = unsafe { std::mem::transmute_copy(model) };
                     self.uniforms_data[64..].copy_from_slice(&data);
                     self.ctx.apply_uniforms_from_bytes(
@@ -169,7 +184,9 @@ impl<'a> RenderContext<'a> {
                     );
                 }
                 DrawInstruction::Draw(mesh) => {
-                    //debug!(target: "gfx", "{}draw({:?})", ws, mesh);
+                    if DEBUG_RENDER {
+                        debug!(target: "gfx", "{}draw({:?})", ws, mesh);
+                    }
                     let texture = match mesh.texture {
                         Some(texture) => texture,
                         None => self.white_texture,