Panda3D

orthographicLens.cxx

00001 // Filename: orthographicLens.cxx
00002 // Created by:  mike (18Feb99)
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 "orthographicLens.h"
00016 #include "indent.h"
00017 #include "bamReader.h"
00018 
00019 TypeHandle OrthographicLens::_type_handle;
00020 
00021 
00022 ////////////////////////////////////////////////////////////////////
00023 //     Function: OrthographicLens::make_copy
00024 //       Access: Public, Virtual
00025 //  Description: Allocates a new Lens just like this one.
00026 ////////////////////////////////////////////////////////////////////
00027 PT(Lens) OrthographicLens::
00028 make_copy() const {
00029   return new OrthographicLens(*this);
00030 }
00031 
00032 ////////////////////////////////////////////////////////////////////
00033 //     Function: OrthographicLens::is_linear
00034 //       Access: Published, Virtual
00035 //  Description: Returns true if the lens represents a linear
00036 //               projection (e.g. PerspectiveLens, OrthographicLens),
00037 //               and therefore there is a valid matrix returned by
00038 //               get_projection_mat(), or false otherwise.
00039 ////////////////////////////////////////////////////////////////////
00040 bool OrthographicLens::
00041 is_linear() const {
00042   return true;
00043 }
00044 
00045 ////////////////////////////////////////////////////////////////////
00046 //     Function: OrthographicLens::is_orthographic
00047 //       Access: Published, Virtual
00048 //  Description: Returns true if the lens represents a orthographic
00049 //               projection (i.e. it is a OrthographicLens), false
00050 //               otherwise.
00051 ////////////////////////////////////////////////////////////////////
00052 bool OrthographicLens::
00053 is_orthographic() const {
00054   return true;
00055 }
00056 
00057 ////////////////////////////////////////////////////////////////////
00058 //     Function: OrthographicLens::write
00059 //       Access: Public, Virtual
00060 //  Description: 
00061 ////////////////////////////////////////////////////////////////////
00062 void OrthographicLens::
00063 write(ostream &out, int indent_level) const {
00064   indent(out, indent_level) << get_type() << " film size = " << get_film_size() << "\n";
00065 }
00066 
00067 ////////////////////////////////////////////////////////////////////
00068 //     Function: OrthographicLens::do_compute_projection_mat
00069 //       Access: Protected, Virtual
00070 //  Description: Computes the complete transformation matrix from 3-d
00071 //               point to 2-d point, if the lens is linear.
00072 ////////////////////////////////////////////////////////////////////
00073 void OrthographicLens::
00074 do_compute_projection_mat(Lens::CData *lens_cdata) {
00075   CoordinateSystem cs = lens_cdata->_cs;
00076   if (cs == CS_default) {
00077     cs = get_default_coordinate_system();
00078   }
00079 
00080   PN_stdfloat a = 2.0f / (lens_cdata->_far_distance - lens_cdata->_near_distance);
00081   PN_stdfloat b = -(lens_cdata->_far_distance + lens_cdata->_near_distance) / (lens_cdata->_far_distance - lens_cdata->_near_distance);
00082 
00083   LMatrix4 canonical;
00084   switch (cs) {
00085   case CS_zup_right:
00086     canonical.set(1.0f,  0.0f,  0.0f,  0.0f,
00087                   0.0f,  0.0f,     a,  0.0f,
00088                   0.0f,  1.0f,  0.0f,  0.0f,
00089                   0.0f,  0.0f,     b,  1.0f);
00090     break;
00091 
00092   case CS_yup_right:
00093     canonical.set(1.0f,  0.0f,  0.0f,  0.0f,
00094                   0.0f,  1.0f,  0.0f,  0.0f,
00095                   0.0f,  0.0f,    -a,  0.0f,
00096                   0.0f,  0.0f,     b,  1.0f);
00097     break;
00098 
00099   case CS_zup_left:
00100     canonical.set(1.0f,  0.0f,  0.0f,  0.0f,
00101                   0.0f,  0.0f,    -a,  0.0f,
00102                   0.0f,  1.0f,  0.0f,  0.0f,
00103                   0.0f,  0.0f,     b,  1.0f);
00104     break;
00105 
00106   case CS_yup_left:
00107     canonical.set(1.0f,  0.0f,  0.0f,  0.0f,
00108                   0.0f,  1.0f,  0.0f,  0.0f,
00109                   0.0f,  0.0f,     a,  0.0f,
00110                   0.0f,  0.0f,     b,  1.0f);
00111     break;
00112 
00113   default:
00114     gobj_cat.error()
00115       << "Invalid coordinate system " << (int)cs << " in OrthographicLens!\n";
00116     canonical = LMatrix4::ident_mat();
00117   }
00118 
00119   lens_cdata->_projection_mat = do_get_lens_mat_inv(lens_cdata) * canonical * do_get_film_mat(lens_cdata);
00120   lens_cdata->_projection_mat_left = lens_cdata->_projection_mat_right = lens_cdata->_projection_mat;
00121 
00122   do_adjust_comp_flags(lens_cdata, 
00123                        CF_projection_mat_inv | CF_projection_mat_left_inv | CF_projection_mat_right_inv,
00124                        CF_projection_mat);
00125 }
00126 
00127 ////////////////////////////////////////////////////////////////////
00128 //     Function: OrthographicLens::register_with_read_factory
00129 //       Access: Public, Static
00130 //  Description: Tells the BamReader how to create objects of type
00131 //               Lens.
00132 ////////////////////////////////////////////////////////////////////
00133 void OrthographicLens::
00134 register_with_read_factory() {
00135   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00136 }
00137 
00138 ////////////////////////////////////////////////////////////////////
00139 //     Function: OrthographicLens::make_from_bam
00140 //       Access: Protected, Static
00141 //  Description: This function is called by the BamReader's factory
00142 //               when a new object of type Lens is encountered
00143 //               in the Bam file.  It should create the Lens
00144 //               and extract its information from the file.
00145 ////////////////////////////////////////////////////////////////////
00146 TypedWritable *OrthographicLens::
00147 make_from_bam(const FactoryParams &params) {
00148   OrthographicLens *lens = new OrthographicLens;
00149   DatagramIterator scan;
00150   BamReader *manager;
00151 
00152   parse_params(params, scan, manager);
00153   lens->fillin(scan, manager);
00154 
00155   return lens;
00156 }
 All Classes Functions Variables Enumerations