Choreonoid  1.8
ShaderPrograms.h
Go to the documentation of this file.
1 
5 #ifndef CNOID_BASE_SHADER_PROGRAMS_H
6 #define CNOID_BASE_SHADER_PROGRAMS_H
7 
8 #include "glcore.h"
9 #include <cnoid/EigenTypes>
10 #include <initializer_list>
11 #include "exportdecl.h"
12 
13 namespace cnoid {
14 
15 class GLSLProgram;
16 class SgCamera;
17 class SgLight;
18 class SgMaterial;
19 class SgFog;
20 
21 class CNOID_EXPORT ShaderProgram
22 {
23  ShaderProgram(const ShaderProgram&) = delete;
24 
25 public:
26  virtual ~ShaderProgram();
27 
28  GLSLProgram& glslProgram(){ return *glslProgram_; }
29  virtual void initialize();
30  virtual void release();
31  virtual void activate();
32  virtual void deactivate();
33 
34  bool isActive() const;
35 
43  virtual void setTransform(const Matrix4& PV, const Isometry3& V, const Affine3& M, const Matrix4* L = nullptr);
44 
45  virtual void setMaterial(const SgMaterial* material);
46  virtual void setVertexColorEnabled(bool on);
47 
48  enum Capability {
49  NoCapability = 0,
50  Lighting = 1,
51  Transparency = 2
52  };
53 
54  int capabilities() const { return capabilities_; }
55  bool hasCapability(int capability) const { return capabilities_ & capability; }
56 
57 protected:
58  ShaderProgram() = default;
59 
60  struct ShaderSource {
61  const char* filename;
63  };
64  ShaderProgram(std::initializer_list<ShaderSource> sources);
65 
66  void setCapability(int capability) { capabilities_ |= capability; }
67 
68 private:
69  GLSLProgram* glslProgram_;
70  int capabilities_;
71 
72  class Impl;
73  Impl* impl;
74 };
75 
76 
77 class CNOID_EXPORT NolightingProgram : public ShaderProgram
78 {
79  NolightingProgram(const NolightingProgram&) = delete;
80 
81 public:
84  virtual void initialize() override;
85  virtual void setTransform(const Matrix4& PV, const Isometry3& V, const Affine3& M, const Matrix4* L) override;
86 
87 protected:
88  NolightingProgram(std::initializer_list<ShaderSource> sources);
89 
90 private:
91  class Impl;
92  Impl* impl;
93 };
94 
95 
96 class CNOID_EXPORT SolidColorProgram : public NolightingProgram
97 {
98  SolidColorProgram(const SolidColorProgram&) = delete;
99 
100 public:
103 
104  virtual void initialize() override;
105  virtual void activate() override;
106  virtual void setColor(const Vector3f& color);
107  virtual void setMaterial(const SgMaterial* material) override;
108  virtual void setPointSize(float s);
109 
110  void setColorChangable(bool on);
111  bool isColorChangable() const;
112  void resetColor(const Vector3f& color);
113 
114 protected:
115  SolidColorProgram(std::initializer_list<ShaderSource> sources);
116 
117 private:
118  class Impl;
119  Impl* impl;
120 };
121 
122 
123 class CNOID_EXPORT SolidColorExProgram : public SolidColorProgram
124 {
125  SolidColorExProgram(const SolidColorExProgram&) = delete;
126 
127 public:
130 
131  virtual void initialize() override;
132  virtual void activate() override;
133  virtual void setColor(const Vector3f& color) override;
134  virtual void setMaterial(const SgMaterial* material) override;
135  virtual void setVertexColorEnabled(bool on) override;
136 
137 protected:
138  SolidColorExProgram(std::initializer_list<ShaderSource> sources);
139 
140 private:
141  class Impl;
142  Impl* impl;
143 };
144 
145 
146 class CNOID_EXPORT ThickLineProgram : public SolidColorExProgram
147 {
148  ThickLineProgram(const ThickLineProgram&) = delete;
149 
150 public:
152  ~ThickLineProgram();
153 
154  virtual void initialize() override;
155  virtual void activate() override;
156 
157  void setViewportSize(int width, int height);
158  void setLineWidth(float width);
159 
160 private:
161  class Impl;
162  Impl* impl;
163 };
164 
165 
166 class CNOID_EXPORT SolidPointProgram : public SolidColorProgram
167 {
168  SolidPointProgram(const SolidPointProgram&) = delete;
169 
170 public:
173 
174  virtual void initialize() override;
175  virtual void activate() override;
176  virtual void deactivate() override;
177  virtual void setTransform(const Matrix4& PV, const Isometry3& V, const Affine3& M, const Matrix4* L) override;
178 
179  void setProjectionMatrix(const Matrix4& P);
180  void setViewportSize(int width, int height);
181 
182 private:
183  class Impl;
184  Impl* impl;
185 };
186 
187 
191 class CNOID_EXPORT OutlineProgram : public SolidColorProgram
192 {
193  OutlineProgram(const OutlineProgram&) = delete;
194 
195 public:
196  OutlineProgram();
197  virtual void initialize() override;
198  virtual void setTransform(const Matrix4& PV, const Isometry3& V, const Affine3& M, const Matrix4* L) override;
199  void setLineWidth(float width);
200 
201 private:
202  GLint normalMatrixLocation;
203 };
204 
205 
206 class CNOID_EXPORT LightingProgram : public ShaderProgram
207 {
208  LightingProgram() = default;
209  LightingProgram(const LightingProgram&) = delete;
210 
211 public:
212  virtual int maxNumLights() const = 0;
213  virtual bool setLight(
214  int index, const SgLight* light, const Isometry3& T, const Isometry3& view, bool shadowCasting) = 0;
215  virtual void setNumLights(int n) = 0;
216  virtual void setFog(const SgFog* fog);
217 
218 protected:
219  LightingProgram(std::initializer_list<ShaderSource> sources);
220 };
221 
222 
223 class CNOID_EXPORT MinimumLightingProgram : public LightingProgram
224 {
226 
227 public:
230 
231  virtual void initialize() override;
232  virtual void activate() override;
233  virtual void setTransform(const Matrix4& PV, const Isometry3& V, const Affine3& M, const Matrix4* L) override;
234  virtual int maxNumLights() const override;
235  virtual bool setLight(
236  int index, const SgLight* light, const Isometry3& T, const Isometry3& view, bool shadowCasting) override;
237  virtual void setNumLights(int n) override;
238  virtual void setMaterial(const SgMaterial* material) override;
239 
240 private:
241  class Impl;
242  Impl* impl;
243 };
244 
245 
246 class CNOID_EXPORT BasicLightingProgram : public LightingProgram
247 {
249 
250 public:
251  virtual void initialize() override;
252  virtual int maxNumLights() const override;
253  virtual bool setLight(
254  int index, const SgLight* light, const Isometry3& T, const Isometry3& view, bool shadowCasting) override;
255  virtual void setNumLights(int n) override;
256  virtual void setFog(const SgFog* fog) override;
257 
258 protected:
259  BasicLightingProgram(std::initializer_list<ShaderSource> sources);
261 
262 private:
263  class Impl;
264  Impl* impl;
265 };
266 
267 
269 {
271 
272 protected:
273  MaterialLightingProgram(std::initializer_list<ShaderSource> sources);
275 
276 public:
277  virtual void initialize() override;
278  virtual void activate() override;
279  virtual void setMaterial(const SgMaterial* material) override;
280  virtual void setVertexColorEnabled(bool on) override;
281  void setColorTextureIndex(int textureIndex);
282  int colorTextureIndex() const;
283  void setTextureEnabled(bool on);
284  void setMinimumTransparency(float t);
285 
286 private:
287  class Impl;
288  Impl* impl;
289 };
290 
291 
292 class ShadowMapProgram;
293 
295 {
296  FullLightingProgram(const FullLightingProgram&) = delete;
297 
298 public:
300  FullLightingProgram(std::initializer_list<ShaderSource> sources);
302 
303  void setDefaultFramebufferObject(GLuint id);
304  GLuint defaultFramebufferObject() const;
305  void setViewportSize(int width, int height);
306 
307  virtual void initialize() override;
308  virtual void release() override;
309  virtual void activate() override;
310  virtual bool setLight(
311  int index, const SgLight* light, const Isometry3& T, const Isometry3& view, bool shadowCasting) override;
312  virtual void setTransform(const Matrix4& PV, const Isometry3& V, const Affine3& M, const Matrix4* L) override;
313 
314  void enableWireframe(const Vector4f& color, float width);
315  void disableWireframe();
316  bool isWireframeEnabled() const;
317 
318  void activateShadowMapGenerationPass(int shadowIndex);
320 
321  void setShadowMapTextureTopIndex(int textureIndex);
322  int maxNumShadows() const;
323  void setNumShadows(int n);
325  void getShadowMapSize(int& width, int& height) const;
327  void setShadowMapViewProjection(const Matrix4& PV);
328  void setShadowAntiAliasingEnabled(bool on);
329  bool isShadowAntiAliasingEnabled() const;
330 
331 private:
332  class Impl;
333  Impl* impl;
334  friend class ShadowMapProgram;
335 };
336 
337 
339 {
340  ShadowMapProgram(const ShadowMapProgram&) = delete;
341 
342 public:
344  virtual void initialize() override;
345  virtual void activate() override;
347  virtual void deactivate() override;
348 
349 private:
350  FullLightingProgram* mainProgram;
351 };
352 
353 }
354 
355 #endif
cnoid::ShaderProgram::hasCapability
bool hasCapability(int capability) const
Definition: ShaderPrograms.h:55
cnoid::ShadowMapProgram
Definition: ShaderPrograms.h:338
cnoid::FullLightingProgram::activate
virtual void activate() override
Definition: ShaderPrograms.cpp:1295
cnoid::ShaderProgram::setCapability
void setCapability(int capability)
Definition: ShaderPrograms.h:66
cnoid::FullLightingProgram
Definition: ShaderPrograms.h:294
cnoid::BasicLightingProgram
Definition: ShaderPrograms.h:246
cnoid::Isometry3
Eigen::Isometry3d Isometry3
Definition: EigenTypes.h:73
cnoid::FullLightingProgram::setShadowAntiAliasingEnabled
void setShadowAntiAliasingEnabled(bool on)
Definition: ShaderPrograms.cpp:1503
cnoid::FullLightingProgram::isShadowAntiAliasingEnabled
bool isShadowAntiAliasingEnabled() const
Definition: ShaderPrograms.cpp:1509
cnoid::SgCamera
Definition: SceneCameras.h:14
cnoid::FullLightingProgram::release
virtual void release() override
Definition: ShaderPrograms.cpp:1282
cnoid::FullLightingProgram::setTransform
virtual void setTransform(const Matrix4 &PV, const Isometry3 &V, const Affine3 &M, const Matrix4 *L) override
Definition: ShaderPrograms.cpp:1334
cnoid::FullLightingProgram::setViewportSize
void setViewportSize(int width, int height)
Definition: ShaderPrograms.cpp:1418
cnoid::MaterialLightingProgram::setColorTextureIndex
void setColorTextureIndex(int textureIndex)
Definition: ShaderPrograms.cpp:979
cnoid::ShaderProgram::capabilities
int capabilities() const
Definition: ShaderPrograms.h:54
cnoid::MaterialLightingProgram::setMaterial
virtual void setMaterial(const SgMaterial *material) override
Definition: ShaderPrograms.cpp:1033
cnoid::SgLight
Definition: SceneLights.h:14
cnoid::ThickLineProgram
Definition: ShaderPrograms.h:146
cnoid::MaterialLightingProgram::setVertexColorEnabled
virtual void setVertexColorEnabled(bool on) override
Definition: ShaderPrograms.cpp:1091
cnoid::OutlineProgram
Definition: ShaderPrograms.h:191
cnoid::MaterialLightingProgram::setMinimumTransparency
void setMinimumTransparency(float t)
Definition: ShaderPrograms.cpp:1109
cnoid::ShadowMapProgram::initializeShadowMapBuffer
void initializeShadowMapBuffer()
Definition: ShaderPrograms.cpp:1528
cnoid::FullLightingProgram::setLight
virtual bool setLight(int index, const SgLight *light, const Isometry3 &T, const Isometry3 &view, bool shadowCasting) override
Definition: ShaderPrograms.cpp:1317
cnoid::FullLightingProgram::activateMainRenderingPass
void activateMainRenderingPass()
Definition: ShaderPrograms.cpp:1436
cnoid::FullLightingProgram::getShadowMapCamera
SgCamera * getShadowMapCamera(SgLight *light, Isometry3 &io_T)
Definition: ShaderPrograms.cpp:1472
cnoid::MaterialLightingProgram::initialize
virtual void initialize() override
Definition: ShaderPrograms.cpp:991
cnoid::MaterialLightingProgram
Definition: ShaderPrograms.h:268
cnoid::Matrix4
Eigen::Matrix4d Matrix4
Definition: EigenTypes.h:58
cnoid::FullLightingProgram::setShadowMapViewProjection
void setShadowMapViewProjection(const Matrix4 &PV)
Definition: ShaderPrograms.cpp:1497
cnoid::SolidColorProgram
Definition: ShaderPrograms.h:96
cnoid::ShaderProgram::ShaderSource::shaderType
int shaderType
Definition: ShaderPrograms.h:62
cnoid::FullLightingProgram::setNumShadows
void setNumShadows(int n)
Definition: ShaderPrograms.cpp:1453
cnoid::ShaderProgram::glslProgram
GLSLProgram & glslProgram()
Definition: ShaderPrograms.h:28
cnoid::SgFog
Definition: SceneEffects.h:69
cnoid::ShaderProgram::ShaderSource
Definition: ShaderPrograms.h:60
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::ShadowMapProgram::activate
virtual void activate() override
Definition: ShaderPrograms.cpp:1546
cnoid::MaterialLightingProgram::setTextureEnabled
void setTextureEnabled(bool on)
Definition: ShaderPrograms.cpp:1100
cnoid::SolidPointProgram
Definition: ShaderPrograms.h:166
cnoid::SolidColorExProgram
Definition: ShaderPrograms.h:123
cnoid::FullLightingProgram::defaultFramebufferObject
GLuint defaultFramebufferObject() const
Definition: ShaderPrograms.cpp:1174
cnoid::FullLightingProgram::disableWireframe
void disableWireframe()
Definition: ShaderPrograms.cpp:1378
cnoid::MaterialLightingProgram::~MaterialLightingProgram
~MaterialLightingProgram()
Definition: ShaderPrograms.cpp:973
cnoid::MaterialLightingProgram::colorTextureIndex
int colorTextureIndex() const
Definition: ShaderPrograms.cpp:985
cnoid::ShadowMapProgram::initialize
virtual void initialize() override
Definition: ShaderPrograms.cpp:1522
cnoid::FullLightingProgram::shadowMapProgram
ShadowMapProgram * shadowMapProgram()
Definition: ShaderPrograms.cpp:1459
cnoid::ShaderProgram::ShaderSource::filename
const char * filename
Definition: ShaderPrograms.h:61
cnoid::FullLightingProgram::initialize
virtual void initialize() override
Definition: ShaderPrograms.cpp:1186
cnoid::FullLightingProgram::FullLightingProgram
FullLightingProgram()
Definition: ShaderPrograms.cpp:1115
cnoid::MaterialLightingProgram::activate
virtual void activate() override
Definition: ShaderPrograms.cpp:1026
cnoid::FullLightingProgram::isWireframeEnabled
bool isWireframeEnabled() const
Definition: ShaderPrograms.cpp:1389
cnoid::GLSLProgram
Definition: GLSLProgram.h:17
cnoid::ShadowMapProgram::deactivate
virtual void deactivate() override
Definition: ShaderPrograms.cpp:1555
cnoid::FullLightingProgram::enableWireframe
void enableWireframe(const Vector4f &color, float width)
Definition: ShaderPrograms.cpp:1365
cnoid::FullLightingProgram::activateShadowMapGenerationPass
void activateShadowMapGenerationPass(int shadowIndex)
Definition: ShaderPrograms.cpp:1426
cnoid::NolightingProgram
Definition: ShaderPrograms.h:77
cnoid::ShaderProgram::Capability
Capability
Definition: ShaderPrograms.h:48
cnoid::LightingProgram
Definition: ShaderPrograms.h:206
cnoid::FullLightingProgram::setShadowMapTextureTopIndex
void setShadowMapTextureTopIndex(int textureIndex)
Definition: ShaderPrograms.cpp:1180
cnoid::SgMaterial
Definition: SceneDrawables.h:18
cnoid::Affine3
Eigen::Affine3d Affine3
Definition: EigenTypes.h:62
cnoid::FullLightingProgram::getShadowMapSize
void getShadowMapSize(int &width, int &height) const
Definition: ShaderPrograms.cpp:1465
cnoid::FullLightingProgram::~FullLightingProgram
~FullLightingProgram()
Definition: ShaderPrograms.cpp:1162
cnoid::ShaderProgram
Definition: ShaderPrograms.h:21
cnoid::FullLightingProgram::setDefaultFramebufferObject
void setDefaultFramebufferObject(GLuint id)
Definition: ShaderPrograms.cpp:1168
cnoid::FullLightingProgram::maxNumShadows
int maxNumShadows() const
Definition: ShaderPrograms.cpp:1447
cnoid::MinimumLightingProgram
Definition: ShaderPrograms.h:223