Browse Source

*** empty log message ***

David Rose 25 years ago
parent
commit
ef04bfc296
3 changed files with 237 additions and 4 deletions
  1. 56 0
      panda/src/sgmanip/nodePath.I
  2. 165 4
      panda/src/sgmanip/nodePath.cxx
  3. 16 0
      panda/src/sgmanip/nodePath.h

+ 56 - 0
panda/src/sgmanip/nodePath.I

@@ -740,6 +740,34 @@ heads_up(float x, float y, float z) {
   heads_up(LPoint3f(x, y, z));
   heads_up(LPoint3f(x, y, z));
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: NodePath::look_at_preserve_scale
+//       Access: Public
+//  Description: Functions like look_at(), but preforms additional
+//               work to preserve any scales that may already be
+//               present on the node.  Normally, look_at() blows away
+//               the scale because scale and rotation are represented
+//               in the same part of the matrix.
+////////////////////////////////////////////////////////////////////
+INLINE void NodePath::
+look_at_preserve_scale(float x, float y, float z) {
+  look_at_preserve_scale(LPoint3f(x, y, z));
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: NodePath::heads_up_preserve_scale
+//       Access: Public
+//  Description: Functions like heads_up(), but preforms additional
+//               work to preserve any scales that may already be
+//               present on the node.  Normally, heads_up() blows away
+//               the scale because scale and rotation are represented
+//               in the same part of the matrix.
+////////////////////////////////////////////////////////////////////
+INLINE void NodePath::
+heads_up_preserve_scale(float x, float y, float z) {
+  heads_up_preserve_scale(LPoint3f(x, y, z));
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: NodePath::print_pos
 //     Function: NodePath::print_pos
 //       Access: Public
 //       Access: Public
@@ -979,6 +1007,34 @@ heads_up(const NodePath &other, float x, float y, float z) {
   heads_up(other, LPoint3f(x, y, z));
   heads_up(other, LPoint3f(x, y, z));
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: NodePath::look_at_preserve_scale
+//       Access: Public
+//  Description: Functions like look_at(), but preforms additional
+//               work to preserve any scales that may already be
+//               present on the node.  Normally, look_at() blows away
+//               the scale because scale and rotation are represented
+//               in the same part of the matrix.
+////////////////////////////////////////////////////////////////////
+INLINE void NodePath::
+look_at_preserve_scale(const NodePath &other, float x, float y, float z) {
+  look_at_preserve_scale(other, LPoint3f(x, y, z));
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: NodePath::heads_up_preserve_scale
+//       Access: Public
+//  Description: Functions like heads_up(), but preforms additional
+//               work to preserve any scales that may already be
+//               present on the node.  Normally, heads_up() blows away
+//               the scale because scale and rotation are represented
+//               in the same part of the matrix.
+////////////////////////////////////////////////////////////////////
+INLINE void NodePath::
+heads_up_preserve_scale(const NodePath &other, float x, float y, float z) {
+  heads_up_preserve_scale(other, LPoint3f(x, y, z));
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: NodePath::print_pos
 //     Function: NodePath::print_pos
 //       Access: Public
 //       Access: Public

+ 165 - 4
panda/src/sgmanip/nodePath.cxx

@@ -1287,9 +1287,19 @@ LVecBase3f NodePath::
 get_scale() const {
 get_scale() const {
   nassertr(has_arcs(), LVecBase3f(1.0, 1.0, 1.0));
   nassertr(has_arcs(), LVecBase3f(1.0, 1.0, 1.0));
   LMatrix4f mat = get_mat();
   LMatrix4f mat = get_mat();
-  LVecBase3f scale, hpr, pos;
-  decompose_matrix(mat, scale, hpr, pos);
-  return scale;
+
+  // We decompose the scale directly instead of calling
+  // decompose_matrix(), since we don't care about the hpr and don't
+  // need to go through all the work of unrolling the axes.
+
+  // Extract the axes from the matrix.
+  LVector3f x, y, z;
+  x = mat.get_row3(0);
+  y = mat.get_row3(1);
+  z = mat.get_row3(2);
+
+  // Now return the lengths of these axes as the scale.
+  return LVecBase3f(length(x), length(y), length(z));
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -1425,7 +1435,8 @@ set_mat(const LMatrix4f &mat) {
 //  Description: Sets the transform on this NodePath so that it
 //  Description: Sets the transform on this NodePath so that it
 //               rotates to face the indicated point in space.  This
 //               rotates to face the indicated point in space.  This
 //               will overwrite any previously existing scale on the
 //               will overwrite any previously existing scale on the
-//               node, although it will preserve any translation.
+//               node, although it will preserve any translation.  See
+//               also look_at_preserve_scale().
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void NodePath::
 void NodePath::
 look_at(const LPoint3f &point, const LVector3f &up) {
 look_at(const LPoint3f &point, const LVector3f &up) {
@@ -1458,6 +1469,76 @@ heads_up(const LPoint3f &point, const LVector3f &up) {
   set_mat(mat);
   set_mat(mat);
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: NodePath::look_at_preserve_scale
+//       Access: Public
+//  Description: Functions like look_at(), but preforms additional
+//               work to preserve any scales that may already be
+//               present on the node.  Normally, look_at() blows away
+//               the scale because scale and rotation are represented
+//               in the same part of the matrix.
+////////////////////////////////////////////////////////////////////
+void NodePath::
+look_at_preserve_scale(const LPoint3f &point, const LVector3f &up) {
+  nassertv(has_arcs());
+
+  LMatrix4f mat = get_mat();
+
+  // Extract the axes from the matrix.
+  LVector3f x, y, z;
+  x = mat.get_row3(0);
+  y = mat.get_row3(1);
+  z = mat.get_row3(2);
+
+  // The lengths of the axes defines the scale.
+  LVecBase3f scale(length(x), length(y), length(z));
+  LPoint3f pos = mat.get_row3(3);
+
+  ::look_at(mat, point - pos, up);
+
+  // Now reapply the scale and position.
+  mat.set_row(0, mat.get_row3(0) * scale[0]);
+  mat.set_row(1, mat.get_row3(1) * scale[1]);
+  mat.set_row(2, mat.get_row3(2) * scale[2]);
+  mat.set_row(3, pos);
+  set_mat(mat);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: NodePath::heads_up_preserve_scale
+//       Access: Public
+//  Description: Functions like heads_up(), but preforms additional
+//               work to preserve any scales that may already be
+//               present on the node.  Normally, heads_up() blows away
+//               the scale because scale and rotation are represented
+//               in the same part of the matrix.
+////////////////////////////////////////////////////////////////////
+void NodePath::
+heads_up_preserve_scale(const LPoint3f &point, const LVector3f &up) {
+  nassertv(has_arcs());
+
+  LMatrix4f mat = get_mat();
+
+  // Extract the axes from the matrix.
+  LVector3f x, y, z;
+  x = mat.get_row3(0);
+  y = mat.get_row3(1);
+  z = mat.get_row3(2);
+
+  // The lengths of the axes defines the scale.
+  LVecBase3f scale(length(x), length(y), length(z));
+  LPoint3f pos = mat.get_row3(3);
+
+  ::heads_up(mat, point - pos, up);
+
+  // Now reapply the scale and position.
+  mat.set_row(0, mat.get_row3(0) * scale[0]);
+  mat.set_row(1, mat.get_row3(1) * scale[1]);
+  mat.set_row(2, mat.get_row3(2) * scale[2]);
+  mat.set_row(3, pos);
+  set_mat(mat);
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: NodePath::set_pos
 //     Function: NodePath::set_pos
 //       Access: Public
 //       Access: Public
@@ -1778,6 +1859,86 @@ heads_up(const NodePath &other, const LPoint3f &point, const LVector3f &up) {
   set_mat(mat);
   set_mat(mat);
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: NodePath::look_at_preserve_scale
+//       Access: Public
+//  Description: Functions like look_at(), but preforms additional
+//               work to preserve any scales that may already be
+//               present on the node.  Normally, look_at() blows away
+//               the scale because scale and rotation are represented
+//               in the same part of the matrix.
+////////////////////////////////////////////////////////////////////
+void NodePath::
+look_at_preserve_scale(const NodePath &other, const LPoint3f &point,
+		       const LVector3f &up) {
+  nassertv(has_arcs());
+
+  LMatrix4f mat = get_mat();
+
+  // Extract the axes from the matrix.
+  LVector3f x, y, z;
+  x = mat.get_row3(0);
+  y = mat.get_row3(1);
+  z = mat.get_row3(2);
+
+  // The lengths of the axes defines the scale.
+  LVecBase3f scale(length(x), length(y), length(z));
+  LPoint3f pos = mat.get_row3(3);
+
+  NodePath parent(*this);
+  parent.shorten(1);
+  LPoint3f rel_point = point * other.get_mat(parent);
+
+  ::look_at(mat, rel_point - pos, up);
+
+  // Now reapply the scale and position.
+  mat.set_row(0, mat.get_row3(0) * scale[0]);
+  mat.set_row(1, mat.get_row3(1) * scale[1]);
+  mat.set_row(2, mat.get_row3(2) * scale[2]);
+  mat.set_row(3, pos);
+  set_mat(mat);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: NodePath::heads_up_preserve_scale
+//       Access: Public
+//  Description: Functions like heads_up(), but preforms additional
+//               work to preserve any scales that may already be
+//               present on the node.  Normally, heads_up() blows away
+//               the scale because scale and rotation are represented
+//               in the same part of the matrix.
+////////////////////////////////////////////////////////////////////
+void NodePath::
+heads_up_preserve_scale(const NodePath &other, const LPoint3f &point,
+			const LVector3f &up) {
+  nassertv(has_arcs());
+
+  LMatrix4f mat = get_mat();
+
+  // Extract the axes from the matrix.
+  LVector3f x, y, z;
+  x = mat.get_row3(0);
+  y = mat.get_row3(1);
+  z = mat.get_row3(2);
+
+  // The lengths of the axes defines the scale.
+  LVecBase3f scale(length(x), length(y), length(z));
+  LPoint3f pos = mat.get_row3(3);
+
+  NodePath parent(*this);
+  parent.shorten(1);
+  LPoint3f rel_point = point * other.get_mat(parent);
+
+  ::heads_up(mat, rel_point - pos, up);
+
+  // Now reapply the scale and position.
+  mat.set_row(0, mat.get_row3(0) * scale[0]);
+  mat.set_row(1, mat.get_row3(1) * scale[1]);
+  mat.set_row(2, mat.get_row3(2) * scale[2]);
+  mat.set_row(3, pos);
+  set_mat(mat);
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: NodePath::set_color
 //     Function: NodePath::set_color
 //       Access: Public
 //       Access: Public

+ 16 - 0
panda/src/sgmanip/nodePath.h

@@ -306,6 +306,11 @@ PUBLISHED:
   INLINE void heads_up(float x, float y, float z);
   INLINE void heads_up(float x, float y, float z);
   void heads_up(const LPoint3f &point, const LVector3f &up = LVector3f::up());
   void heads_up(const LPoint3f &point, const LVector3f &up = LVector3f::up());
 
 
+  INLINE void look_at_preserve_scale(float x, float y, float z);
+  void look_at_preserve_scale(const LPoint3f &point, const LVector3f &up = LVector3f::up());
+  INLINE void heads_up_preserve_scale(float x, float y, float z);
+  void heads_up_preserve_scale(const LPoint3f &point, const LVector3f &up = LVector3f::up());
+
   INLINE void print_pos() const;
   INLINE void print_pos() const;
   INLINE void print_hpr() const;
   INLINE void print_hpr() const;
   INLINE void print_scale() const;
   INLINE void print_scale() const;
@@ -376,6 +381,17 @@ PUBLISHED:
 		const LPoint3f &point = LPoint3f(0.0, 0.0, 0.0),
 		const LPoint3f &point = LPoint3f(0.0, 0.0, 0.0),
 		const LVector3f &up = LVector3f::up());
 		const LVector3f &up = LVector3f::up());
 
 
+  INLINE void look_at_preserve_scale(const NodePath &other, 
+				     float x, float y, float z);
+  void look_at_preserve_scale(const NodePath &other, 
+			      const LPoint3f &point = LPoint3f(0.0, 0.0, 0.0),
+			      const LVector3f &up = LVector3f::up());
+  INLINE void heads_up_preserve_scale(const NodePath &other, 
+				      float x, float y, float z);
+  void heads_up_preserve_scale(const NodePath &other, 
+			       const LPoint3f &point = LPoint3f(0.0, 0.0, 0.0),
+			       const LVector3f &up = LVector3f::up());
+
   INLINE void print_pos(const NodePath &other) const;
   INLINE void print_pos(const NodePath &other) const;
   INLINE void print_hpr(const NodePath &other) const;
   INLINE void print_hpr(const NodePath &other) const;
   INLINE void print_scale(const NodePath &other) const;
   INLINE void print_scale(const NodePath &other) const;