Panda3D
 All Classes Functions Variables Enumerations
orthographicLens.cxx
1 // Filename: orthographicLens.cxx
2 // Created by: mike (18Feb99)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "orthographicLens.h"
16 #include "indent.h"
17 #include "bamReader.h"
18 
19 TypeHandle OrthographicLens::_type_handle;
20 
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: OrthographicLens::make_copy
24 // Access: Public, Virtual
25 // Description: Allocates a new Lens just like this one.
26 ////////////////////////////////////////////////////////////////////
28 make_copy() const {
29  return new OrthographicLens(*this);
30 }
31 
32 ////////////////////////////////////////////////////////////////////
33 // Function: OrthographicLens::is_linear
34 // Access: Published, Virtual
35 // Description: Returns true if the lens represents a linear
36 // projection (e.g. PerspectiveLens, OrthographicLens),
37 // and therefore there is a valid matrix returned by
38 // get_projection_mat(), or false otherwise.
39 ////////////////////////////////////////////////////////////////////
41 is_linear() const {
42  return true;
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: OrthographicLens::is_orthographic
47 // Access: Published, Virtual
48 // Description: Returns true if the lens represents a orthographic
49 // projection (i.e. it is a OrthographicLens), false
50 // otherwise.
51 ////////////////////////////////////////////////////////////////////
53 is_orthographic() const {
54  return true;
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: OrthographicLens::write
59 // Access: Public, Virtual
60 // Description:
61 ////////////////////////////////////////////////////////////////////
62 void OrthographicLens::
63 write(ostream &out, int indent_level) const {
64  indent(out, indent_level) << get_type() << " film size = " << get_film_size() << "\n";
65 }
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function: OrthographicLens::do_extrude_depth
69 // Access: Protected, Virtual
70 // Description: This is the generic implementation, which is based on
71 // do_extrude() and assumes a linear distribution of
72 // depth values between the near and far points.
73 ////////////////////////////////////////////////////////////////////
74 bool OrthographicLens::
75 do_extrude_depth(const CData *cdata,
76  const LPoint3 &point2d, LPoint3 &point3d) const {
77  return do_extrude_depth_with_mat(cdata, point2d, point3d);
78 }
79 
80 ////////////////////////////////////////////////////////////////////
81 // Function: OrthographicLens::do_compute_projection_mat
82 // Access: Protected, Virtual
83 // Description: Computes the complete transformation matrix from 3-d
84 // point to 2-d point, if the lens is linear.
85 ////////////////////////////////////////////////////////////////////
86 void OrthographicLens::
87 do_compute_projection_mat(Lens::CData *lens_cdata) {
88  CoordinateSystem cs = lens_cdata->_cs;
89  if (cs == CS_default) {
90  cs = get_default_coordinate_system();
91  }
92 
93  PN_stdfloat a = 2.0f / (lens_cdata->_far_distance - lens_cdata->_near_distance);
94  PN_stdfloat b = -(lens_cdata->_far_distance + lens_cdata->_near_distance) / (lens_cdata->_far_distance - lens_cdata->_near_distance);
95 
96  LMatrix4 canonical;
97  switch (cs) {
98  case CS_zup_right:
99  canonical.set(1.0f, 0.0f, 0.0f, 0.0f,
100  0.0f, 0.0f, a, 0.0f,
101  0.0f, 1.0f, 0.0f, 0.0f,
102  0.0f, 0.0f, b, 1.0f);
103  break;
104 
105  case CS_yup_right:
106  canonical.set(1.0f, 0.0f, 0.0f, 0.0f,
107  0.0f, 1.0f, 0.0f, 0.0f,
108  0.0f, 0.0f, -a, 0.0f,
109  0.0f, 0.0f, b, 1.0f);
110  break;
111 
112  case CS_zup_left:
113  canonical.set(1.0f, 0.0f, 0.0f, 0.0f,
114  0.0f, 0.0f, -a, 0.0f,
115  0.0f, 1.0f, 0.0f, 0.0f,
116  0.0f, 0.0f, b, 1.0f);
117  break;
118 
119  case CS_yup_left:
120  canonical.set(1.0f, 0.0f, 0.0f, 0.0f,
121  0.0f, 1.0f, 0.0f, 0.0f,
122  0.0f, 0.0f, a, 0.0f,
123  0.0f, 0.0f, b, 1.0f);
124  break;
125 
126  default:
127  gobj_cat.error()
128  << "Invalid coordinate system " << (int)cs << " in OrthographicLens!\n";
129  canonical = LMatrix4::ident_mat();
130  }
131 
132  lens_cdata->_projection_mat = do_get_lens_mat_inv(lens_cdata) * canonical * do_get_film_mat(lens_cdata);
133  lens_cdata->_projection_mat_left = lens_cdata->_projection_mat_right = lens_cdata->_projection_mat;
134 
135  do_adjust_comp_flags(lens_cdata,
136  CF_projection_mat_inv | CF_projection_mat_left_inv | CF_projection_mat_right_inv,
137  CF_projection_mat);
138 }
139 
140 ////////////////////////////////////////////////////////////////////
141 // Function: OrthographicLens::register_with_read_factory
142 // Access: Public, Static
143 // Description: Tells the BamReader how to create objects of type
144 // Lens.
145 ////////////////////////////////////////////////////////////////////
148  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
149 }
150 
151 ////////////////////////////////////////////////////////////////////
152 // Function: OrthographicLens::make_from_bam
153 // Access: Protected, Static
154 // Description: This function is called by the BamReader's factory
155 // when a new object of type Lens is encountered
156 // in the Bam file. It should create the Lens
157 // and extract its information from the file.
158 ////////////////////////////////////////////////////////////////////
159 TypedWritable *OrthographicLens::
160 make_from_bam(const FactoryParams &params) {
162  DatagramIterator scan;
163  BamReader *manager;
164 
165  parse_params(params, scan, manager);
166  lens->fillin(scan, manager);
167 
168  return lens;
169 }
static const LMatrix4f & ident_mat()
Returns an identity matrix.
Definition: lmatrix.h:903
static void register_with_read_factory()
Tells the BamReader how to create objects of type Lens.
A base class for any number of different kinds of lenses, linear and otherwise.
Definition: lens.h:45
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
virtual bool is_linear() const
Returns true if the lens represents a linear projection (e.g.
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
An orthographic lens.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
const LVecBase2 & get_film_size() const
Returns the horizontal and vertical film size of the virtual film.
Definition: lens.I:291
A class to retrieve the individual data elements previously stored in a Datagram. ...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
virtual bool is_orthographic() const
Returns true if the lens represents a orthographic projection (i.e.