Panda3D
orthographicLens.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file orthographicLens.cxx
10  * @author mike
11  * @date 1999-02-18
12  */
13 
14 #include "orthographicLens.h"
15 #include "indent.h"
16 #include "bamReader.h"
17 
18 TypeHandle OrthographicLens::_type_handle;
19 
20 
21 /**
22  * Allocates a new Lens just like this one.
23  */
24 PT(Lens) OrthographicLens::
25 make_copy() const {
26  return new OrthographicLens(*this);
27 }
28 
29 /**
30  * Returns true if the lens represents a linear projection (e.g.
31  * PerspectiveLens, OrthographicLens), and therefore there is a valid matrix
32  * returned by get_projection_mat(), or false otherwise.
33  */
35 is_linear() const {
36  return true;
37 }
38 
39 /**
40  * Returns true if the lens represents a orthographic projection (i.e. it is
41  * a OrthographicLens), false otherwise.
42  */
44 is_orthographic() const {
45  return true;
46 }
47 
48 /**
49  *
50  */
51 void OrthographicLens::
52 write(std::ostream &out, int indent_level) const {
53  indent(out, indent_level) << get_type() << " film size = " << get_film_size() << "\n";
54 }
55 
56 /**
57  * This is the generic implementation, which is based on do_extrude() and
58  * assumes a linear distribution of depth values between the near and far
59  * points.
60  */
61 bool OrthographicLens::
62 do_extrude_depth(const CData *cdata,
63  const LPoint3 &point2d, LPoint3 &point3d) const {
64  return do_extrude_depth_with_mat(cdata, point2d, point3d);
65 }
66 
67 /**
68  * Computes the complete transformation matrix from 3-d point to 2-d point, if
69  * the lens is linear.
70  */
71 void OrthographicLens::
72 do_compute_projection_mat(Lens::CData *lens_cdata) {
73  CoordinateSystem cs = lens_cdata->_cs;
74  if (cs == CS_default) {
75  cs = get_default_coordinate_system();
76  }
77 
78  PN_stdfloat a = 2.0f / (lens_cdata->_far_distance - lens_cdata->_near_distance);
79  PN_stdfloat b = -(lens_cdata->_far_distance + lens_cdata->_near_distance) / (lens_cdata->_far_distance - lens_cdata->_near_distance);
80 
81  LMatrix4 canonical;
82  switch (cs) {
83  case CS_zup_right:
84  canonical.set(1.0f, 0.0f, 0.0f, 0.0f,
85  0.0f, 0.0f, a, 0.0f,
86  0.0f, 1.0f, 0.0f, 0.0f,
87  0.0f, 0.0f, b, 1.0f);
88  break;
89 
90  case CS_yup_right:
91  canonical.set(1.0f, 0.0f, 0.0f, 0.0f,
92  0.0f, 1.0f, 0.0f, 0.0f,
93  0.0f, 0.0f, -a, 0.0f,
94  0.0f, 0.0f, b, 1.0f);
95  break;
96 
97  case CS_zup_left:
98  canonical.set(1.0f, 0.0f, 0.0f, 0.0f,
99  0.0f, 0.0f, -a, 0.0f,
100  0.0f, 1.0f, 0.0f, 0.0f,
101  0.0f, 0.0f, b, 1.0f);
102  break;
103 
104  case CS_yup_left:
105  canonical.set(1.0f, 0.0f, 0.0f, 0.0f,
106  0.0f, 1.0f, 0.0f, 0.0f,
107  0.0f, 0.0f, a, 0.0f,
108  0.0f, 0.0f, b, 1.0f);
109  break;
110 
111  default:
112  gobj_cat.error()
113  << "Invalid coordinate system " << (int)cs << " in OrthographicLens!\n";
114  canonical = LMatrix4::ident_mat();
115  }
116 
117  lens_cdata->_projection_mat = do_get_lens_mat_inv(lens_cdata) * canonical * do_get_film_mat(lens_cdata);
118  lens_cdata->_projection_mat_left = lens_cdata->_projection_mat_right = lens_cdata->_projection_mat;
119 
120  do_adjust_comp_flags(lens_cdata,
121  CF_projection_mat_inv | CF_projection_mat_left_inv | CF_projection_mat_right_inv,
122  CF_projection_mat);
123 }
124 
125 /**
126  * Tells the BamReader how to create objects of type Lens.
127  */
130  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
131 }
132 
133 /**
134  * This function is called by the BamReader's factory when a new object of
135  * type Lens is encountered in the Bam file. It should create the Lens and
136  * extract its information from the file.
137  */
138 TypedWritable *OrthographicLens::
139 make_from_bam(const FactoryParams &params) {
141  DatagramIterator scan;
142  BamReader *manager;
143 
144  parse_params(params, scan, manager);
145  lens->fillin(scan, manager);
146 
147  return lens;
148 }
virtual bool is_linear() const
Returns true if the lens represents a linear projection (e.g.
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:41
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual bool is_orthographic() const
Returns true if the lens represents a orthographic projection (i.e.
void parse_params(const FactoryParams &params, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
Definition: bamReader.I:275
get_film_size
Returns the horizontal and vertical film size of the virtual film.
Definition: lens.h:82
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
PT(Lens) OrthographicLens
Allocates a new Lens just like this one.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
void register_factory(TypeHandle handle, CreateFunc *func, void *user_data=nullptr)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:73
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An orthographic lens.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
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:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.