Panda3D
shaderGenerator.h
1 // Filename: shaderGenerator.h
2 // Created by: jyelon (15Dec07)
3 // Updated by: weifengh, PandaSE(15Apr10)
4 //
5 ////////////////////////////////////////////////////////////////////
6 //
7 // PANDA 3D SOFTWARE
8 // Copyright (c) Carnegie Mellon University. All rights reserved.
9 //
10 // All use of this software is subject to the terms of the revised BSD
11 // license. You should have received a copy of this license along
12 // with this source code in a file named "LICENSE."
13 //
14 ////////////////////////////////////////////////////////////////////
15 
16 #ifndef SHADERGENERATOR_H
17 #define SHADERGENERATOR_H
18 
19 #include "pandabase.h"
20 #include "graphicsStateGuardianBase.h"
21 #include "graphicsOutputBase.h"
22 #include "nodePath.h"
23 #include "shaderAttrib.h"
24 #include "renderState.h"
25 #include "renderAttrib.h"
26 
27 class AmbientLight;
28 class DirectionalLight;
29 class PointLight;
30 class Spotlight;
31 class LightAttrib;
32 
33 ////////////////////////////////////////////////////////////////////
34 // Class : ShaderGenerator
35 // Description : The ShaderGenerator is a device that effectively
36 // replaces the classic fixed function pipeline with
37 // a 'next-gen' fixed function pipeline. The next-gen
38 // fixed function pipeline supports features like
39 // normal mapping, gloss mapping, cartoon lighting,
40 // and so forth. It works by automatically generating
41 // a shader from a given RenderState.
42 //
43 // Currently, there is one ShaderGenerator object per
44 // GraphicsStateGuardian. It is our intent that in
45 // time, people will write classes that derive from
46 // ShaderGenerator but which yield slightly different
47 // results.
48 //
49 // The ShaderGenerator owes its existence to the
50 // 'Bamboo Team' at Carnegie Mellon's Entertainment
51 // Technology Center. This is a group of students
52 // who, as a semester project, decided that next-gen
53 // graphics should be accessible to everyone, even if
54 // they don't know shader programming. The group
55 // consisted of:
56 //
57 // Aaron Lo, Programmer
58 // Heegun Lee, Programmer
59 // Erin Fernandez, Artist/Tester
60 // Joe Grubb, Artist/Tester
61 // Ivan Ortega, Technical Artist/Tester
62 //
63 // Thanks to them!
64 //
65 ////////////////////////////////////////////////////////////////////
66 class EXPCL_PANDA_PGRAPHNODES ShaderGenerator : public TypedReferenceCount {
67 PUBLISHED:
69  virtual ~ShaderGenerator();
70  virtual CPT(ShaderAttrib) synthesize_shader(const RenderState *rs);
71 
72 protected:
73  CPT(RenderAttrib) create_shader_attrib(const string &txt);
74  PT(Texture) update_shadow_buffer(NodePath light_np);
75  static const string combine_mode_as_string(CPT(TextureStage) stage,
76  TextureStage::CombineMode c_mode, bool alpha, short texindex);
77  static const string combine_source_as_string(CPT(TextureStage) stage,
78  short num, bool alpha, bool single_value, short texindex);
79  static const string texture_type_as_string(Texture::TextureType ttype);
80 
81  // Shader register allocation:
82 
83  int _vcregs_used;
84  int _fcregs_used;
85  int _vtregs_used;
86  int _ftregs_used;
87  void reset_register_allocator();
88  const char *alloc_vreg();
89  const char *alloc_freg();
90 
91  // RenderState analysis information. Created by analyze_renderstate:
92 
93  CPT(RenderState) _state;
94  Material *_material;
95  int _num_textures;
96 
97  pvector <AmbientLight *> _alights;
99  pvector <PointLight *> _plights;
100  pvector <Spotlight *> _slights;
101  pvector <NodePath> _alights_np;
102  pvector <NodePath> _dlights_np;
103  pvector <NodePath> _plights_np;
104  pvector <NodePath> _slights_np;
105 
106  bool _vertex_colors;
107  bool _flat_colors;
108 
109  bool _lighting;
110  bool _shadows;
111  bool _fog;
112 
113  bool _have_ambient;
114  bool _have_diffuse;
115  bool _have_emission;
116  bool _have_specular;
117 
118  bool _separate_ambient_diffuse;
119 
120  int _map_index_normal;
121  int _map_index_height;
122  int _map_index_glow;
123  int _map_index_gloss;
124  bool _map_height_in_alpha;
125 
126  bool _out_primary_glow;
127  bool _out_aux_normal;
128  bool _out_aux_glow;
129  bool _out_aux_any;
130 
131  bool _have_alpha_test;
132  bool _have_alpha_blend;
133  bool _calc_primary_alpha;
134  bool _subsume_alpha_test;
135  bool _disable_alpha_write;
136 
137  int _num_clip_planes;
138  bool _use_shadow_filter;
139 
140  bool _need_material_props;
141  bool _need_world_position;
142  bool _need_world_normal;
143  bool _need_eye_position;
144  bool _need_eye_normal;
145  bool _auto_normal_on;
146  bool _auto_glow_on;
147  bool _auto_gloss_on;
148  bool _auto_ramp_on;
149  bool _auto_shadow_on;
150 
151  void analyze_renderstate(const RenderState *rs);
152  void clear_analysis();
153 
154  // This is not a PT() to prevent a circular reference.
156  GraphicsOutputBase *_host;
157 
158 public:
159  static TypeHandle get_class_type() {
160  return _type_handle;
161  }
162  static void init_type() {
164  register_type(_type_handle, "ShaderGenerator",
165  TypedObject::get_class_type());
166  }
167  virtual TypeHandle get_type() const {
168  return get_class_type();
169  }
170  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
171 
172  private:
173  static TypeHandle _type_handle;
174 };
175 
176 #include "shaderGenerator.I"
177 
178 #endif // SHADERGENERATOR_H
A light shining from infinitely far away in a particular direction, like sunlight.
static void init_type()
This function is declared non-inline to work around a compiler bug in g++ 2.96.
Definition: typedObject.cxx:52
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:60
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:75
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
A light source that seems to illuminate all points in space at once.
Definition: ambientLight.h:29
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
The ShaderGenerator is a device that effectively replaces the classic fixed function pipeline with a ...
A light originating from a single point in space, and shining in a particular direction, with a cone-shaped falloff.
Definition: spotlight.h:37
Defines the way an object appears in the presence of lighting.
Definition: material.h:34
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:53
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
An abstract base class for GraphicsOutput, for all the usual reasons.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
Defines the properties of a named stage of the multitexture pipeline.
Definition: textureStage.h:38
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165
A light originating from a single point in space, and shining in all directions.
Definition: pointLight.h:27
Indicates which set of lights should be considered "on" to illuminate geometry at this level and belo...
Definition: lightAttrib.h:33