Panda3D

fltTransformScale.cxx

00001 // Filename: fltTransformScale.cxx
00002 // Created by:  drose (30Aug00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "fltTransformScale.h"
00016 #include "fltRecordReader.h"
00017 #include "fltRecordWriter.h"
00018 
00019 TypeHandle FltTransformScale::_type_handle;
00020 
00021 ////////////////////////////////////////////////////////////////////
00022 //     Function: FltTransformScale::Constructor
00023 //       Access: Public
00024 //  Description:
00025 ////////////////////////////////////////////////////////////////////
00026 FltTransformScale::
00027 FltTransformScale(FltHeader *header) : FltTransformRecord(header) {
00028   _center.set(0.0, 0.0, 0.0);
00029   _scale.set(1.0, 1.0, 1.0);
00030 }
00031 
00032 ////////////////////////////////////////////////////////////////////
00033 //     Function: FltTransformScale::set
00034 //       Access: Public
00035 //  Description: Defines the scale.
00036 ////////////////////////////////////////////////////////////////////
00037 void FltTransformScale::
00038 set(const LPoint3d &center, const LVecBase3 &scale) {
00039   _center = center;
00040   _scale = scale;
00041 
00042   recompute_matrix();
00043 }
00044 
00045 ////////////////////////////////////////////////////////////////////
00046 //     Function: FltTransformScale::has_center
00047 //       Access: Public
00048 //  Description: Returns true if the center is specified, false if it
00049 //               is not.  For some reason, MultiGen stores large
00050 //               negative numbers in for the center if it is not
00051 //               specified.  It is unclear what the purpose of this
00052 //               is.
00053 ////////////////////////////////////////////////////////////////////
00054 bool FltTransformScale::
00055 has_center() const {
00056   return 
00057     _center[0] > -1e+08 && 
00058     _center[1] > -1e+08 && 
00059     _center[2] > -1e+08;
00060 }
00061 
00062 ////////////////////////////////////////////////////////////////////
00063 //     Function: FltTransformScale::get_center
00064 //       Access: Public
00065 //  Description:
00066 ////////////////////////////////////////////////////////////////////
00067 const LPoint3d &FltTransformScale::
00068 get_center() const {
00069   return _center;
00070 }
00071 
00072 ////////////////////////////////////////////////////////////////////
00073 //     Function: FltTransformScale::get_scale
00074 //       Access: Public
00075 //  Description:
00076 ////////////////////////////////////////////////////////////////////
00077 const LVecBase3 &FltTransformScale::
00078 get_scale() const {
00079   return _scale;
00080 }
00081 
00082 ////////////////////////////////////////////////////////////////////
00083 //     Function: FltTransformScale::recompute_matrix
00084 //       Access: Private
00085 //  Description:
00086 ////////////////////////////////////////////////////////////////////
00087 void FltTransformScale::
00088 recompute_matrix() {
00089   if (has_center()) {
00090     _matrix =
00091       LMatrix4d::translate_mat(-_center) *
00092       LMatrix4d::scale_mat(LCAST(double, _scale)) *
00093       LMatrix4d::translate_mat(_center);
00094   } else {
00095     _matrix =
00096       LMatrix4d::scale_mat(LCAST(double, _scale));
00097   }
00098 }
00099 
00100 ////////////////////////////////////////////////////////////////////
00101 //     Function: FltTransformScale::extract_record
00102 //       Access: Protected, Virtual
00103 //  Description: Fills in the information in this record based on the
00104 //               information given in the indicated datagram, whose
00105 //               opcode has already been read.  Returns true on
00106 //               success, false if the datagram is invalid.
00107 ////////////////////////////////////////////////////////////////////
00108 bool FltTransformScale::
00109 extract_record(FltRecordReader &reader) {
00110   if (!FltTransformRecord::extract_record(reader)) {
00111     return false;
00112   }
00113 
00114   nassertr(reader.get_opcode() == FO_scale, false);
00115   DatagramIterator &iterator = reader.get_iterator();
00116 
00117   iterator.skip_bytes(4);
00118 
00119   _center[0] = iterator.get_be_float64();
00120   _center[1] = iterator.get_be_float64();
00121   _center[2] = iterator.get_be_float64();
00122   _scale[0] = iterator.get_be_float32();
00123   _scale[1] = iterator.get_be_float32();
00124   _scale[2] = iterator.get_be_float32();
00125 
00126   iterator.skip_bytes(4);   // Undocumented additional padding.
00127 
00128   recompute_matrix();
00129 
00130   check_remaining_size(iterator);
00131   return true;
00132 }
00133 
00134 ////////////////////////////////////////////////////////////////////
00135 //     Function: FltTransformScale::build_record
00136 //       Access: Protected, Virtual
00137 //  Description: Fills up the current record on the FltRecordWriter with
00138 //               data for this record, but does not advance the
00139 //               writer.  Returns true on success, false if there is
00140 //               some error.
00141 ////////////////////////////////////////////////////////////////////
00142 bool FltTransformScale::
00143 build_record(FltRecordWriter &writer) const {
00144   if (!FltTransformRecord::build_record(writer)) {
00145     return false;
00146   }
00147 
00148   writer.set_opcode(FO_scale);
00149   Datagram &datagram = writer.update_datagram();
00150 
00151   datagram.pad_bytes(4);   // Undocumented additional padding.
00152 
00153   datagram.add_be_float64(_center[0]);
00154   datagram.add_be_float64(_center[1]);
00155   datagram.add_be_float64(_center[2]);
00156   datagram.add_be_float32(_scale[0]);
00157   datagram.add_be_float32(_scale[1]);
00158   datagram.add_be_float32(_scale[2]);
00159 
00160   datagram.pad_bytes(4);   // Undocumented additional padding.
00161 
00162   return true;
00163 }
00164 
 All Classes Functions Variables Enumerations