Panda3D
eggPolysetMaker.cxx
1 // Filename: eggPolysetMaker.cxx
2 // Created by: drose (20Jun01)
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 "eggPolysetMaker.h"
16 #include "eggPolygon.h"
17 
18 TypeHandle EggPolysetMaker::_type_handle;
19 
20 ////////////////////////////////////////////////////////////////////
21 // Function: EggPolysetMaker::Constructor
22 // Access: Public
23 // Description:
24 ////////////////////////////////////////////////////////////////////
25 EggPolysetMaker::
26 EggPolysetMaker() {
27  _properties = 0;
28 }
29 
30 ////////////////////////////////////////////////////////////////////
31 // Function: EggPolysetMaker::set_properties
32 // Access: Public
33 // Description: Sets the set of properties that determines which
34 // polygons are allowed to be grouped together into a
35 // single polyset. This is the bitwise 'or' of all the
36 // properties that matter. If this is 0, all polygons
37 // (within a given group) will be lumped into a common
38 // polyset regardless of their properties.
39 ////////////////////////////////////////////////////////////////////
41 set_properties(int properties) {
42  _properties = properties;
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: EggPolysetMaker::get_bin_number
47 // Access: Public, Virtual
48 // Description:
49 ////////////////////////////////////////////////////////////////////
50 int EggPolysetMaker::
51 get_bin_number(const EggNode *node) {
52  if (node->is_of_type(EggPolygon::get_class_type())) {
53  return (int)BN_polyset;
54  }
55 
56  return (int)BN_none;
57 }
58 
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function: EggPolysetMaker::sorts_less
62 // Access: Public, Virtual
63 // Description:
64 ////////////////////////////////////////////////////////////////////
66 sorts_less(int bin_number, const EggNode *a, const EggNode *b) {
67  nassertr((BinNumber)bin_number == BN_polyset, false);
68 
69  const EggPolygon *pa = DCAST(EggPolygon, a);
70  const EggPolygon *pb = DCAST(EggPolygon, b);
71 
72  if ((_properties & (P_has_texture | P_texture)) != 0) {
73  bool a_has_texture = (pa->get_num_textures() > 0);
74  bool b_has_texture = (pb->get_num_textures() > 0);
75  if (a_has_texture != b_has_texture) {
76  return ((int)a_has_texture < (int)b_has_texture);
77  }
78  }
79  if ((_properties & (P_texture)) != 0) {
80  int num_textures = min(pa->get_num_textures(), pb->get_num_textures());
81  for (int i = 0; i < num_textures; i++) {
82  EggTexture *a_texture = pa->get_texture(i);
83  EggTexture *b_texture = pb->get_texture(i);
84  if (a_texture != b_texture) {
85  return (a_texture->sorts_less_than(*b_texture, ~EggTexture::E_tref_name));
86  }
87  }
88  if (pa->get_num_textures() != pb->get_num_textures()) {
89  return (pa->get_num_textures() < pb->get_num_textures());
90  }
91  }
92  if ((_properties & (P_has_material | P_material)) != 0) {
93  if (pa->has_material() != pb->has_material()) {
94  return ((int)pa->has_material() < (int)pb->has_material());
95  }
96  }
97  if ((_properties & (P_material)) != 0) {
98  if (pa->has_material()) {
99  return (pa->get_material()->sorts_less_than(*pb->get_material(), ~EggMaterial::E_mref_name));
100  }
101  }
102  if ((_properties & (P_has_poly_color)) != 0) {
103  if (pa->has_color() != pb->has_color()) {
104  return ((int)pa->has_color() < (int)pb->has_color());
105  }
106  }
107  if ((_properties & (P_poly_color)) != 0) {
108  if (pa->get_color() != pb->get_color()) {
109  return (pa->get_color() < pb->get_color());
110  }
111  }
112  if ((_properties & (P_has_poly_normal)) != 0) {
113  if (pa->has_normal() != pb->has_normal()) {
114  return ((int)pa->has_normal() < (int)pb->has_normal());
115  }
116  }
117  if ((_properties & (P_has_vertex_normal)) != 0) {
118  bool pa_has_normal = pa->has_vertex_normal();
119  bool pb_has_normal = pb->has_vertex_normal();
120  if (pa_has_normal != pb_has_normal) {
121  return ((int)pa_has_normal < (int)pb_has_normal);
122  }
123  }
124  if ((_properties & (P_has_vertex_color)) != 0) {
125  bool pa_has_color = pa->has_vertex_color();
126  bool pb_has_color = pb->has_vertex_color();
127  if (pa_has_color != pb_has_color) {
128  return ((int)pa_has_color < (int)pb_has_color);
129  }
130  }
131  if ((_properties & (P_bface)) != 0) {
132  if (pa->get_bface_flag() != pb->get_bface_flag()) {
133  return ((int)pa->get_bface_flag() < (int)pb->get_bface_flag());
134  }
135  }
136 
137  return false;
138 }
void set_properties(int properties)
Sets the set of properties that determines which polygons are allowed to be grouped together into a s...
int get_num_textures() const
Returns the number of textures applied to the primitive.
Definition: eggPrimitive.I:218
Defines a texture map that may be applied to geometry.
Definition: eggTexture.h:33
bool has_vertex_color() const
Returns true if any vertex on the primitive has a specific color set, false otherwise.
EggTexture * get_texture() const
Returns the first texture on the primitive, if any, or NULL if there are no textures on the primitive...
Definition: eggPrimitive.I:182
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...
LColor get_color() const
Returns the color set on this particular attribute.
bool has_vertex_normal() const
Returns true if any vertex on the primitive has a specific normal set, false otherwise.
bool sorts_less_than(const EggTexture &other, int eq) const
An ordering operator to compare two textures for sorting order.
Definition: eggTexture.cxx:429
A single polygon.
Definition: eggPolygon.h:26
bool sorts_less_than(const EggMaterial &other, int eq) const
An ordering operator to compare two materials for sorting order.
EggMaterial * get_material() const
Returns a pointer to the applied material, or NULL if there is no material applied.
Definition: eggPrimitive.I:262
A base class for things that may be directly added into the egg hierarchy.
Definition: eggNode.h:38
bool is_of_type(TypeHandle handle) const
Returns true if the current object is or derives from the indicated type.
Definition: typedObject.I:63
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
bool get_bface_flag() const
Retrieves the backfacing flag of the polygon.
Definition: eggPrimitive.I:301
bool has_material() const
Returns true if the primitive is materiald (and get_material() will return a real pointer)...
Definition: eggPrimitive.I:275