Panda3D
Loading...
Searching...
No Matches
eggBinner.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 eggBinner.cxx
10 * @author drose
11 * @date 2000-02-17
12 */
13
14#include "eggBinner.h"
15#include "eggRenderState.h"
16#include "eggPrimitive.h"
17#include "eggNurbsSurface.h"
18#include "eggNurbsCurve.h"
19#include "eggPatch.h"
20#include "eggSwitchCondition.h"
21#include "eggGroup.h"
22#include "dcast.h"
23
24/**
25 *
26 */
27EggBinner::
28EggBinner(EggLoader &loader) :
29 _loader(loader)
30{
31}
32
33/**
34 * May be overridden in derived classes to perform some setup work as each
35 * node is encountered. This will be called once for each node in the egg
36 * hierarchy.
37 */
39prepare_node(EggNode *node) {
40 if (node->is_of_type(EggPrimitive::get_class_type())) {
41 EggPrimitive *egg_prim = DCAST(EggPrimitive, node);
42 PT(EggRenderState) render_state = new EggRenderState(_loader);
43 render_state->fill_state(egg_prim);
44 egg_prim->set_user_data(render_state);
45 }
46}
47
48/**
49 *
50 */
51int EggBinner::
52get_bin_number(const EggNode *node) {
53 if (node->is_of_type(EggNurbsSurface::get_class_type())) {
54 return (int)BN_nurbs_surface;
55
56 } else if (node->is_of_type(EggNurbsCurve::get_class_type())) {
57 return (int)BN_nurbs_curve;
58
59 } else if (node->is_of_type(EggPatch::get_class_type())) {
60 return (int)BN_patches;
61
62 } else if (node->is_of_type(EggPrimitive::get_class_type())) {
63 return (int)BN_polyset;
64
65 } else if (node->is_of_type(EggGroup::get_class_type())) {
66 const EggGroup *group = DCAST(EggGroup, node);
67 if (group->has_lod()) {
68 return (int)BN_lod;
69 }
70 }
71
72 return (int)BN_none;
73}
74
75/**
76 * May be overridden in derived classes to define a name for each new bin,
77 * based on its bin number, and a sample child.
78 */
79std::string EggBinner::
80get_bin_name(int bin_number, const EggNode *child) {
81 if (bin_number == BN_polyset || bin_number == BN_patches) {
82 return DCAST(EggPrimitive, child)->get_sort_name();
83 }
84
85 return std::string();
86}
87
88/**
89 *
90 */
92sorts_less(int bin_number, const EggNode *a, const EggNode *b) {
93 switch (bin_number) {
94 case BN_polyset:
95 case BN_patches:
96 {
97 const EggPrimitive *pa, *pb;
98 DCAST_INTO_R(pa, a, false);
99 DCAST_INTO_R(pb, b, false);
100
101 // Different render states are binned separately.
102 const EggRenderState *rsa, *rsb;
103 DCAST_INTO_R(rsa, pa->get_user_data(EggRenderState::get_class_type()), false);
104 DCAST_INTO_R(rsb, pb->get_user_data(EggRenderState::get_class_type()), false);
105 int compare = rsa->compare_to(*rsb);
106 if (compare != 0) {
107 return (compare < 0);
108 }
109
110 if (bin_number == BN_patches) {
111 // For patches only, we group together patches of similar size.
112 const EggPatch *patch_a, *patch_b;
113 DCAST_INTO_R(patch_a, a, false);
114 DCAST_INTO_R(patch_b, b, false);
115 if (patch_a->size() != patch_b->size()) {
116 return patch_a->size() < patch_b->size();
117 }
118 }
119
120 // Also, if the primitive was given a name (that does not begin with a
121 // digit), it gets binned with similar-named primitives.
122 return pa->get_sort_name() < pb->get_sort_name();
123 }
124
125 case BN_lod:
126 {
127 const EggGroup *ga = DCAST(EggGroup, a);
128 const EggGroup *gb = DCAST(EggGroup, b);
129
130 const EggSwitchCondition &swa = ga->get_lod();
131 const EggSwitchCondition &swb = gb->get_lod();
132
133 // For now, this is the only kind of switch condition there is.
134 const EggSwitchConditionDistance &swda =
135 *DCAST(EggSwitchConditionDistance, &swa);
136 const EggSwitchConditionDistance &swdb =
137 *DCAST(EggSwitchConditionDistance, &swb);
138
139 // Group LOD nodes in order by switching center.
140 return (swda._center.compare_to(swdb._center) < 0);
141 }
142
143 case BN_nurbs_surface:
144 case BN_nurbs_curve:
145 // Nurbs curves and surfaces are always binned individually.
146 return a < b;
147
148 case BN_none:
149 break;
150 }
151
152 // Shouldn't get here.
153 return false;
154}
virtual std::string get_bin_name(int bin_number, const EggNode *child)
May be overridden in derived classes to define a name for each new bin, based on its bin number,...
Definition eggBinner.cxx:80
virtual void prepare_node(EggNode *node)
May be overridden in derived classes to perform some setup work as each node is encountered.
Definition eggBinner.cxx:39
virtual bool sorts_less(int bin_number, const EggNode *a, const EggNode *b)
May be overridden in derived classes to create additional bins within a particular bin number,...
Definition eggBinner.cxx:92
The main glue of the egg hierarchy, this corresponds to the <Group>, <Instance>, and <Joint> type nod...
Definition eggGroup.h:34
Converts an egg data structure, possibly read from an egg file but not necessarily,...
Definition eggLoader.h:67
A base class for things that may be directly added into the egg hierarchy.
Definition eggNode.h:36
EggUserData * get_user_data() const
Returns the user data pointer most recently stored on this object, or NULL if nothing was previously ...
Definition eggObject.cxx:84
void set_user_data(EggUserData *user_data)
Sets the user data associated with this object.
Definition eggObject.cxx:74
A single "patch", a special primitive to be rendered only with a tessellation shader.
Definition eggPatch.h:25
A base class for any of a number of kinds of geometry primitives: polygons, point lights,...
get_sort_name
Returns the name of the primitive for the purposes of sorting primitives into different groups,...
This class is used within this package only to record the render state that should be assigned to eac...
int compare_to(const EggRenderState &other) const
Provides a unique ordering for different EggRenderState objects, so that primitives of similar state ...
A SwitchCondition that switches the levels-of-detail based on distance from the camera's eyepoint.
This corresponds to a <SwitchCondition> entry within a group.
bool is_of_type(TypeHandle handle) const
Returns true if the current object is or derives from the indicated type.
Definition typedObject.I:28
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.