Panda3D
geomPatches.cxx
1 // Filename: geomPatches.cxx
2 // Created by: drose (27Apr12)
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 "geomPatches.h"
16 #include "geomVertexRewriter.h"
17 #include "pStatTimer.h"
18 #include "bamReader.h"
19 #include "bamWriter.h"
20 #include "graphicsStateGuardianBase.h"
21 
22 TypeHandle GeomPatches::_type_handle;
23 
24 ////////////////////////////////////////////////////////////////////
25 // Function: GeomPatches::Constructor
26 // Access: Published
27 // Description: The number of vertices per patch must be specified to
28 // the GeomPatches constructor, and it may not be
29 // changed during the lifetime of the GeomPatches
30 // object. Create a new GeomPatches if you need to have
31 // a different value.
32 ////////////////////////////////////////////////////////////////////
34 GeomPatches(int num_vertices_per_patch, GeomPatches::UsageHint usage_hint) :
35  GeomPrimitive(usage_hint),
36  _num_vertices_per_patch(num_vertices_per_patch)
37 {
38 }
39 
40 ////////////////////////////////////////////////////////////////////
41 // Function: GeomPatches::Copy Constructor
42 // Access: Published
43 // Description:
44 ////////////////////////////////////////////////////////////////////
46 GeomPatches(const GeomPatches &copy) :
47  GeomPrimitive(copy),
48  _num_vertices_per_patch(copy._num_vertices_per_patch)
49 {
50 }
51 
52 ////////////////////////////////////////////////////////////////////
53 // Function: GeomPatches::Destructor
54 // Access: Published, Virtual
55 // Description:
56 ////////////////////////////////////////////////////////////////////
57 GeomPatches::
58 ~GeomPatches() {
59 }
60 
61 ////////////////////////////////////////////////////////////////////
62 // Function: GeomPatches::make_copy
63 // Access: Public, Virtual
64 // Description:
65 ////////////////////////////////////////////////////////////////////
66 PT(GeomPrimitive) GeomPatches::
67 make_copy() const {
68  return new GeomPatches(*this);
69 }
70 
71 ////////////////////////////////////////////////////////////////////
72 // Function: GeomPatches::get_primitive_type
73 // Access: Public, Virtual
74 // Description: Returns the fundamental rendering type of this
75 // primitive: whether it is points, lines, or polygons.
76 //
77 // This is used to set up the appropriate antialiasing
78 // settings when AntialiasAttrib::M_auto is in effect;
79 // it also implies the type of primitive that will be
80 // produced when decompose() is called.
81 ////////////////////////////////////////////////////////////////////
82 GeomPrimitive::PrimitiveType GeomPatches::
83 get_primitive_type() const {
84  return PT_patches;
85 }
86 
87 ////////////////////////////////////////////////////////////////////
88 // Function: GeomPatches::get_num_vertices_per_primitive
89 // Access: Public, Virtual
90 // Description: If the primitive type is a simple type in which all
91 // primitives have the same number of vertices, like
92 // patches, returns the number of vertices per
93 // primitive. If the primitive type is a more complex
94 // type in which different primitives might have
95 // different numbers of vertices, for instance a
96 // triangle strip, returns 0.
97 //
98 // In the case of GeomPatches, this returns the fixed
99 // number that was specified to the constructor.
100 ////////////////////////////////////////////////////////////////////
101 int GeomPatches::
103  return _num_vertices_per_patch;
104 }
105 
106 ////////////////////////////////////////////////////////////////////
107 // Function: GeomPatches::draw
108 // Access: Public, Virtual
109 // Description: Calls the appropriate method on the GSG to draw the
110 // primitive.
111 ////////////////////////////////////////////////////////////////////
112 bool GeomPatches::
114  bool force) const {
115  return gsg->draw_patches(reader, force);
116 }
117 
118 ////////////////////////////////////////////////////////////////////
119 // Function: GeomPatches::register_with_read_factory
120 // Access: Public, Static
121 // Description: Tells the BamReader how to create objects of type
122 // Geom.
123 ////////////////////////////////////////////////////////////////////
124 void GeomPatches::
125 register_with_read_factory() {
126  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
127 }
128 
129 ////////////////////////////////////////////////////////////////////
130 // Function: GeomPatches::write_datagram
131 // Access: Public, Virtual
132 // Description: Writes the contents of this object to the datagram
133 // for shipping out to a Bam file.
134 ////////////////////////////////////////////////////////////////////
135 void GeomPatches::
137  GeomPrimitive::write_datagram(manager, dg);
138  dg.add_uint16(_num_vertices_per_patch);
139 }
140 
141 ////////////////////////////////////////////////////////////////////
142 // Function: GeomPatches::fillin
143 // Access: Protected
144 // Description: This internal function is called by make_from_bam to
145 // read in all of the relevant data from the BamFile for
146 // the new GeomPatches.
147 ////////////////////////////////////////////////////////////////////
148 void GeomPatches::
149 fillin(DatagramIterator &scan, BamReader *manager) {
150  GeomPrimitive::fillin(scan, manager);
151  _num_vertices_per_patch = scan.get_uint16();
152 }
153 
154 ////////////////////////////////////////////////////////////////////
155 // Function: GeomPatches::make_from_bam
156 // Access: Protected, Static
157 // Description: This function is called by the BamReader's factory
158 // when a new object of type Geom is encountered
159 // in the Bam file. It should create the Geom
160 // and extract its information from the file.
161 ////////////////////////////////////////////////////////////////////
162 TypedWritable *GeomPatches::
163 make_from_bam(const FactoryParams &params) {
164  GeomPatches *object = new GeomPatches(0, UH_unspecified);
165  DatagramIterator scan;
166  BamReader *manager;
167 
168  parse_params(params, scan, manager);
169  object->fillin(scan, manager);
170 
171  return object;
172 }
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
This is an abstract base class for a family of classes that represent the fundamental geometry primit...
Definition: geomPrimitive.h:63
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
PN_uint16 get_uint16()
Extracts an unsigned 16-bit integer.
Defines a series of "patches", fixed-size groupings of vertices that must be processed by a tessellat...
Definition: geomPatches.h:27
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
virtual int get_num_vertices_per_primitive() const
If the primitive type is a simple type in which all primitives have the same number of vertices...
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
void add_uint16(PN_uint16 value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:181
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
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
Encapsulates the data from a GeomPrimitive, pre-fetched for one stage of the pipeline.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
GeomPatches(int num_vertices_per_patch, UsageHint usage_hint)
The number of vertices per patch must be specified to the GeomPatches constructor, and it may not be changed during the lifetime of the GeomPatches object.
Definition: geomPatches.cxx:34