Panda3D
cullableObject.I
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 cullableObject.I
10  * @author drose
11  * @date 2002-03-04
12  */
13 
14 /**
15  * Creates an empty CullableObject whose pointers can be filled in later.
16  */
17 INLINE CullableObject::
19 #ifdef DO_MEMORY_USAGE
20  MemoryUsage::record_pointer(this, get_class_type());
21 #endif
22 }
23 
24 /**
25  * Creates a CullableObject based the indicated geom, with the indicated
26  * render state and transform.
27  */
28 INLINE CullableObject::
29 CullableObject(CPT(Geom) geom, CPT(RenderState) state,
30  CPT(TransformState) internal_transform) :
31  _geom(std::move(geom)),
32  _state(std::move(state)),
33  _internal_transform(std::move(internal_transform))
34 {
35 #ifdef DO_MEMORY_USAGE
36  MemoryUsage::record_pointer(this, get_class_type());
37 #endif
38 }
39 
40 /**
41  * Copies the CullableObject.
42  */
43 INLINE CullableObject::
45  _geom(copy._geom),
46  _munged_data(copy._munged_data),
47  _state(copy._state),
48  _internal_transform(copy._internal_transform)
49 {
50 #ifdef DO_MEMORY_USAGE
51  MemoryUsage::record_pointer(this, get_class_type());
52 #endif
53 }
54 
55 /**
56  * Copies the CullableObject.
57  */
58 INLINE void CullableObject::
60  _geom = copy._geom;
61  _munged_data = copy._munged_data;
62  _state = copy._state;
63  _internal_transform = copy._internal_transform;
64  _draw_callback = copy._draw_callback;
65 }
66 
67 /**
68  * Draws the cullable object on the GSG immediately, in the GSG's current
69  * state. This should only be called from the draw thread.
70  */
71 INLINE void CullableObject::
72 draw(GraphicsStateGuardianBase *gsg, bool force, Thread *current_thread) {
73  if (_draw_callback != nullptr) {
74  // It has a callback associated.
75  gsg->clear_before_callback();
76  gsg->set_state_and_transform(_state, _internal_transform);
77  GeomDrawCallbackData cbdata(this, gsg, force);
78  _draw_callback->do_callback(&cbdata);
79  if (cbdata.get_lost_state()) {
80  // Tell the GSG to forget its state.
81  gsg->clear_state_and_transform();
82  }
83  // Now the callback has taken care of drawing.
84  } else {
85  nassertv(_geom != nullptr);
86  gsg->set_state_and_transform(_state, _internal_transform);
87  draw_inline(gsg, force, current_thread);
88  }
89 }
90 
91 /**
92  * Returns true if all the data necessary to render this object is currently
93  * resident in memory. If this returns false, the data will be brought back
94  * into memory shortly; try again later.
95  */
96 INLINE bool CullableObject::
98  bool resident = true;
99  if (!_geom->request_resident()) {
100  resident = false;
101  }
102  if (!_munged_data->request_resident()) {
103  resident = false;
104  }
105  return resident;
106 }
107 
108 
109 /**
110  * Specifies a CallbackObject that will be responsible for drawing this
111  * object.
112  */
113 INLINE void CullableObject::
115  _draw_callback = draw_callback;
116 }
117 
118 /**
119  * Flushes the PStatCollectors used during traversal.
120  */
121 INLINE void CullableObject::
123  _sw_sprites_pcollector.flush_level();
124 }
125 
126 /**
127  * Draws the cullable object on the GSG immediately, in the GSG's current
128  * state. This should only be called from the draw thread. Assumes the GSG
129  * has already been set to the appropriate state.
130  */
131 INLINE void CullableObject::
132 draw_inline(GraphicsStateGuardianBase *gsg, bool force, Thread *current_thread) {
133  _geom->draw(gsg, _munged_data, force, current_thread);
134 }
135 
136 /**
137  * Invokes the draw callback, assuming one is set. Crashes if not.
138  */
139 INLINE void CullableObject::
140 draw_callback(GraphicsStateGuardianBase *gsg, bool force, Thread *current_thread) {
141  gsg->clear_before_callback();
142  gsg->set_state_and_transform(_state, _internal_transform);
143  GeomDrawCallbackData cbdata(this, gsg, force);
144  _draw_callback->do_callback(&cbdata);
145  if (cbdata.get_lost_state()) {
146  // Tell the GSG to forget its state.
147  gsg->clear_state_and_transform();
148  }
149 }
150 
151 /**
152  *
153  */
154 INLINE CullableObject::SortPoints::
155 SortPoints(const CullableObject::PointData *array) :
156  _array(array)
157 {
158 }
159 
160 /**
161  * Orders the points from back-to-front for correct transparency sorting in
162  * munge_points_to_quads
163  */
164 INLINE bool CullableObject::SortPoints::
165 operator () (unsigned short a, unsigned short b) const {
166  return _array[a]._dist > _array[b]._dist;
167 }
168 
169 /**
170  *
171  */
172 INLINE bool CullableObject::SourceFormat::
173 operator < (const CullableObject::SourceFormat &other) const {
174  if (_format != other._format) {
175  return _format < other._format;
176  }
177  if (_sprite_texcoord != other._sprite_texcoord) {
178  return (int)_sprite_texcoord < (int)other._sprite_texcoord;
179  }
180  if (_retransform_sprites != other._retransform_sprites) {
181  return (int)_retransform_sprites < (int)other._retransform_sprites;
182  }
183  return false;
184 }
This specialization on CallbackData is passed when the callback is initiated from deep within the dra...
static void flush_level()
Flushes the PStatCollectors used during traversal.
Indicates a coordinate-system transform on vertices.
void operator=(const CullableObject &copy)
Copies the CullableObject.
void draw_callback(GraphicsStateGuardianBase *gsg, bool force, Thread *current_thread)
Invokes the draw callback, assuming one is set.
bool request_resident() const
Returns true if all the data necessary to render this object is currently resident in memory.
void draw_inline(GraphicsStateGuardianBase *gsg, bool force, Thread *current_thread)
Draws the cullable object on the GSG immediately, in the GSG's current state.
void draw(GraphicsStateGuardianBase *gsg, bool force, Thread *current_thread)
Draws the cullable object on the GSG immediately, in the GSG's current state.
The smallest atom of cull.
CullableObject()
Creates an empty CullableObject whose pointers can be filled in later.
A container for geometry primitives.
Definition: geom.h:54
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:47
This is a generic object that can be assigned to a callback at various points in the rendering proces...
void set_draw_callback(CallbackObject *draw_callback)
Specifies a CallbackObject that will be responsible for drawing this object.
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
A thread; that is, a lightweight process.
Definition: thread.h:46
bool get_lost_state() const
Returns the lost_state flag.
static void record_pointer(ReferenceCount *ptr)
Indicates that the given pointer has been recently allocated.
Definition: memoryUsage.I:32