Browse Source

wip: extrude()

David Rose 13 years ago
parent
commit
22534c9a07
2 changed files with 64 additions and 0 deletions
  1. 63 0
      panda/src/pnmimage/pfmFile.cxx
  2. 1 0
      panda/src/pnmimage/pfmFile.h

+ 63 - 0
panda/src/pnmimage/pfmFile.cxx

@@ -977,6 +977,69 @@ project(const Lens *lens) {
   }
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: PfmFile::extrude
+//       Access: Published
+//  Description: Converts each (u, v, depth) point of the Pfm file to
+//               an (x, y, z) point, by reversing project().  If the
+//               original file is only a 1-d file, assumes that it is
+//               a depth map with implicit (u, v) coordinates.
+////////////////////////////////////////////////////////////////////
+void PfmFile::
+extrude(const Lens *lens) {
+  nassertv(is_valid());
+
+  static LMatrix4f from_uv(2.0f, 0.0f, 0.0f, 0.0f,
+                           0.0f, 2.0f, 0.0f, 0.0f,
+                           0.0f, 0.0f, 2.0f, 0.0f,
+                           -1.0f, -1.0f, -1.0f, 1.0f);
+
+  PfmFile result;
+  result.clear(_x_size, _y_size, 3);
+  result.set_zero_special(true);
+
+  LPoint2 uv_scale(1.0, 1.0);
+  if (_x_size > 1) {
+    uv_scale[0] = 1.0 / PN_stdfloat(_x_size - 1);
+  }
+  if (_y_size > 1) {
+    uv_scale[1] = 1.0 / PN_stdfloat(_y_size - 1);
+  }
+
+  for (int yi = 0; yi < _y_size; ++yi) {
+    for (int xi = 0; xi < _x_size; ++xi) {
+      if (!has_point(xi, yi)) {
+        continue;
+      }
+      LPoint3 p;
+      if (_num_channels == 1) {
+        p.set((PN_stdfloat)xi * uv_scale[0],
+              (PN_stdfloat)yi * uv_scale[1],
+              (PN_stdfloat)get_point1(xi, yi));
+      } else {
+        p = LCAST(PN_stdfloat, get_point(xi, yi));
+      }
+
+      if (lens->is_linear()) {
+        lens->get_projection_mat_inv().xform_point_general_in_place(p);
+        result.set_point(xi, yi, p);
+
+      } else {
+        from_uv.xform_point_in_place(p);
+        LPoint3 near_point, far_point;
+        if (!lens->extrude(p, near_point, far_point)) {
+          continue;
+        }
+        
+        LPoint3 film = near_point + (far_point - near_point) * p[2];
+        result.set_point(xi, yi, film);
+      }
+    }
+  }
+
+  (*this) = result;
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: PfmFile::merge
 //       Access: Published

+ 1 - 0
panda/src/pnmimage/pfmFile.h

@@ -102,6 +102,7 @@ PUBLISHED:
   BLOCKING void xform(const LMatrix4f &transform);
   INLINE BLOCKING void xform(const LMatrix4d &transform);
   BLOCKING void project(const Lens *lens);
+  BLOCKING void extrude(const Lens *lens);
   BLOCKING void merge(const PfmFile &other);
   BLOCKING void copy_channel(int to_channel, const PfmFile &other, int from_channel);
   BLOCKING void apply_crop(int x_begin, int x_end, int y_begin, int y_end);