Panda3D
matrixLens.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 matrixLens.cxx
10  * @author drose
11  * @date 2001-12-12
12  */
13 
14 #include "matrixLens.h"
15 #include "indent.h"
16 #include "bamReader.h"
17 #include "bamWriter.h"
18 
19 TypeHandle MatrixLens::_type_handle;
20 
21 
22 /**
23  * Allocates a new Lens just like this one.
24  */
25 PT(Lens) MatrixLens::
26 make_copy() const {
27  return new MatrixLens(*this);
28 }
29 
30 /**
31  * Returns true if the lens represents a linear projection (e.g.
32  * PerspectiveLens, MatrixLens), and therefore there is a valid matrix
33  * returned by get_projection_mat(), or false otherwise.
34  */
35 bool MatrixLens::
36 is_linear() const {
37  return true;
38 }
39 
40 /**
41  *
42  */
43 void MatrixLens::
44 write(std::ostream &out, int indent_level) const {
45  indent(out, indent_level) << get_type() << ":\n";
46  get_projection_mat().write(out, indent_level + 2);
47 }
48 
49 /**
50  * Computes the complete transformation matrix from 3-d point to 2-d point, if
51  * the lens is linear.
52  */
53 void MatrixLens::
54 do_compute_projection_mat(Lens::CData *lens_cdata) {
55  lens_cdata->_projection_mat = do_get_lens_mat_inv(lens_cdata) * _user_mat * do_get_film_mat(lens_cdata);
56 
57  if (_ml_flags & MF_has_left_eye) {
58  lens_cdata->_projection_mat_left = do_get_lens_mat_inv(lens_cdata) * _left_eye_mat * do_get_film_mat(lens_cdata);
59  } else {
60  lens_cdata->_projection_mat_left = lens_cdata->_projection_mat;
61  }
62 
63  if (_ml_flags & MF_has_right_eye) {
64  lens_cdata->_projection_mat_right = do_get_lens_mat_inv(lens_cdata) * _right_eye_mat * do_get_film_mat(lens_cdata);
65  } else {
66  lens_cdata->_projection_mat_right = lens_cdata->_projection_mat;
67  }
68 
69  do_adjust_comp_flags(lens_cdata, CF_projection_mat_inv,
70  CF_projection_mat);
71 }
72 
73 /**
74  * Tells the BamReader how to create objects of type Lens.
75  */
76 void MatrixLens::
78  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
79 }
80 
81 /**
82  * Writes the contents of this object to the datagram for shipping out to a
83  * Bam file.
84  */
85 void MatrixLens::
87  Lens::write_datagram(manager, dg);
88 
89  if (manager->get_file_minor_ver() >= 41) {
90  dg.add_uint8(_ml_flags);
91  _user_mat.write_datagram(dg);
92 
93  if (_ml_flags & MF_has_left_eye) {
94  _left_eye_mat.write_datagram(dg);
95  }
96  if (_ml_flags & MF_has_right_eye) {
97  _left_eye_mat.write_datagram(dg);
98  }
99  }
100 }
101 
102 /**
103  * This function is called by the BamReader's factory when a new object of
104  * type Lens is encountered in the Bam file. It should create the Lens and
105  * extract its information from the file.
106  */
107 TypedWritable *MatrixLens::
108 make_from_bam(const FactoryParams &params) {
109  MatrixLens *lens = new MatrixLens;
110  DatagramIterator scan;
111  BamReader *manager;
112 
113  parse_params(params, scan, manager);
114  lens->fillin(scan, manager);
115 
116  return lens;
117 }
118 
119 /**
120  * This internal function is called by make_from_bam to read in all of the
121  * relevant data from the BamFile for the new MatrixLens.
122  */
123 void MatrixLens::
124 fillin(DatagramIterator &scan, BamReader *manager) {
125  Lens::fillin(scan, manager);
126 
127  if (manager->get_file_minor_ver() >= 41) {
128  _ml_flags = scan.get_uint8();
129 
130  _user_mat.read_datagram(scan);
131  if (_ml_flags & MF_has_left_eye) {
132  _left_eye_mat.read_datagram(scan);
133  }
134  if (_ml_flags & MF_has_right_eye) {
135  _right_eye_mat.read_datagram(scan);
136  }
137  }
138 }
uint8_t get_uint8()
Extracts an unsigned 8-bit integer.
A base class for any number of different kinds of lenses, linear and otherwise.
Definition: lens.h:41
A completely generic linear lens.
Definition: matrixLens.h:28
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
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being written.
Definition: bamWriter.I:59
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
static void register_with_read_factory()
Tells the BamReader how to create objects of type Lens.
Definition: matrixLens.cxx:77
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being read.
Definition: bamReader.I:83
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: matrixLens.cxx:86
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
PT(Lens) MatrixLens
Allocates a new Lens just like this one.
Definition: matrixLens.cxx:25
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: lens.cxx:1852
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
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.
const LMatrix4 & get_projection_mat(StereoChannel channel=SC_mono) const
Returns the complete transformation matrix from a 3-d point in space to a point on the film,...
Definition: lens.I:563
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
void add_uint8(uint8_t value)
Adds an unsigned 8-bit integer to the datagram.
Definition: datagram.I:50
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
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
virtual bool is_linear() const
Returns true if the lens represents a linear projection (e.g.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.