Panda3D
material.h
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 material.h
10  * @author mike
11  * @date 1997-01-09
12  */
13 
14 #ifndef MATERIAL_H
15 #define MATERIAL_H
16 
17 #include "pandabase.h"
18 
20 #include "namable.h"
21 #include "luse.h"
22 #include "numeric_types.h"
23 #include "config_gobj.h"
25 
26 class FactoryParams;
27 
28 /**
29  * Defines the way an object appears in the presence of lighting. A material
30  * is only necessary if lighting is to be enabled; otherwise, the material
31  * isn't used.
32  *
33  * There are two workflows that are supported: the "classic" workflow of
34  * providing separate ambient, diffuse and specular colors, and the
35  * "metalness" workflow, in which a base color is specified along with a
36  * "metallic" value that indicates whether the material is a metal or a
37  * dielectric.
38  *
39  * The size of the specular highlight can be specified by either specifying
40  * the specular exponent (shininess) or by specifying a roughness value that
41  * in perceptually linear in the range of 0-1.
42  */
43 class EXPCL_PANDA_GOBJ Material : public TypedWritableReferenceCount, public Namable {
44 PUBLISHED:
45  INLINE explicit Material(const std::string &name = "");
46  INLINE Material(const Material &copy);
47  void operator = (const Material &copy);
48  INLINE ~Material();
49 
50  INLINE static Material *get_default();
51 
52  INLINE bool has_base_color() const;
53  INLINE const LColor &get_base_color() const;
54  void set_base_color(const LColor &color);
55  void clear_base_color();
56 
57  INLINE bool has_ambient() const;
58  INLINE const LColor &get_ambient() const;
59  void set_ambient(const LColor &color);
60  INLINE void clear_ambient();
61 
62  INLINE bool has_diffuse() const;
63  INLINE const LColor &get_diffuse() const;
64  void set_diffuse(const LColor &color);
65  INLINE void clear_diffuse();
66 
67  INLINE bool has_specular() const;
68  INLINE const LColor &get_specular() const;
69  void set_specular(const LColor &color);
70  void clear_specular();
71 
72  INLINE bool has_emission() const;
73  INLINE const LColor &get_emission() const;
74  void set_emission(const LColor &color);
75  INLINE void clear_emission();
76 
77  INLINE PN_stdfloat get_shininess() const;
78  void set_shininess(PN_stdfloat shininess);
79 
80  INLINE bool has_roughness() const;
81  PN_stdfloat get_roughness() const;
82  void set_roughness(PN_stdfloat roughness);
83 
84  INLINE bool has_metallic() const;
85  INLINE PN_stdfloat get_metallic() const;
86  void set_metallic(PN_stdfloat metallic);
87  void clear_metallic();
88 
89  INLINE bool has_refractive_index() const;
90  INLINE PN_stdfloat get_refractive_index() const;
91  void set_refractive_index(PN_stdfloat refractive_index);
92 
93  INLINE bool get_local() const;
94  INLINE void set_local(bool local);
95  INLINE bool get_twoside() const;
96  INLINE void set_twoside(bool twoside);
97 
98  INLINE bool operator == (const Material &other) const;
99  INLINE bool operator != (const Material &other) const;
100  INLINE bool operator < (const Material &other) const;
101 
102  int compare_to(const Material &other) const;
103 
104  void output(std::ostream &out) const;
105  void write(std::ostream &out, int indent) const;
106 
107  INLINE bool is_attrib_locked() const;
108  INLINE void set_attrib_lock();
109 
110 PUBLISHED:
111  MAKE_PROPERTY2(base_color, has_base_color, get_base_color,
112  set_base_color, clear_base_color);
113  MAKE_PROPERTY2(ambient, has_ambient, get_ambient,
114  set_ambient, clear_ambient);
115  MAKE_PROPERTY2(diffuse, has_diffuse, get_diffuse,
116  set_diffuse, clear_diffuse);
117  MAKE_PROPERTY2(specular, has_specular, get_specular,
118  set_specular, clear_specular);
119  MAKE_PROPERTY2(emission, has_emission, get_emission,
120  set_emission, clear_emission);
121 
122  MAKE_PROPERTY(shininess, get_shininess, set_shininess);
123  MAKE_PROPERTY(roughness, get_roughness, set_roughness);
124  MAKE_PROPERTY(metallic, get_metallic, set_metallic);
125  MAKE_PROPERTY(refractive_index, get_refractive_index,
126  set_refractive_index);
127 
128  MAKE_PROPERTY(local, get_local, set_local);
129  MAKE_PROPERTY(twoside, get_twoside, set_twoside);
130 
131 protected:
132  INLINE bool is_used_by_auto_shader() const;
133 
134 public:
135  INLINE void mark_used_by_auto_shader();
136  INLINE int get_flags() const;
137 
138  enum Flags {
139  F_ambient = 0x001,
140  F_diffuse = 0x002,
141  F_specular = 0x004,
142  F_emission = 0x008,
143  F_local = 0x010,
144  F_twoside = 0x020,
145  F_attrib_lock = 0x040,
146  F_roughness = 0x080,
147  F_metallic = 0x100,
148  F_base_color = 0x200,
149  F_refractive_index = 0x400,
150  F_used_by_auto_shader = 0x800,
151  };
152 
153 private:
154  LColor _base_color;
155  LColor _ambient;
156  LColor _diffuse;
157  LColor _specular;
158  LColor _emission;
159  PN_stdfloat _shininess;
160  PN_stdfloat _roughness;
161  PN_stdfloat _metallic;
162  PN_stdfloat _refractive_index;
163 
164  static PT(Material) _default;
165 
166  int _flags;
167 
168 public:
169  static void register_with_read_factory();
170  virtual void write_datagram(BamWriter *manager, Datagram &me);
171 
172 protected:
173  static TypedWritable *make_Material(const FactoryParams &params);
174  void fillin(DatagramIterator &scan, BamReader *manager);
175 
176 public:
177  static TypeHandle get_class_type() {
178  return _type_handle;
179  }
180  static void init_type() {
181  TypedWritableReferenceCount::init_type();
182  register_type(_type_handle, "Material",
183  TypedWritableReferenceCount::get_class_type());
184  }
185  virtual TypeHandle get_type() const {
186  return get_class_type();
187  }
188  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
189 
190 private:
191 
192  static TypeHandle _type_handle;
193 };
194 
195 INLINE std::ostream &operator << (std::ostream &out, const Material &m) {
196  m.output(out);
197  return out;
198 }
199 
200 #include "material.I"
201 
202 #endif
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
virtual void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is intended to be called by each class's make_from_bam() method to read in all...
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void output(std::ostream &out) const
Outputs the Namable.
Definition: namable.I:61
A base class for all things which can have a name.
Definition: namable.h:26
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A base class for things which need to inherit from both TypedWritable and from ReferenceCount.
Defines the way an object appears in the presence of lighting.
Definition: material.h:43
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An STL function object class, this is intended to be used on any ordered collection of class objects ...
Definition: stl_compares.h:73
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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