cherish
ProgramEntity2D.h
1 #ifndef PROGRAMENTITY2D_H
2 #define PROGRAMENTITY2D_H
3 
4 #include <osg/Program>
5 #include <osg/StateSet>
6 #include <osg/observer_ptr>
7 #include <osg/Camera>
8 #include <osg/MatrixTransform>
9 
10 #include <QtGlobal>
11 #include <QDebug>
12 
17 class ProgramEntity2D : public osg::Program
18 {
19 public:
21 
22  virtual void initialize(osg::StateSet* state, osg::Camera* camera, osg::MatrixTransform* t);
23 
24  void updateTransform(osg::MatrixTransform* t);
25 
26  osg::MatrixTransform* getTransform() const;
27 
28  osg::Camera* getCamera() const;
29 
30 protected:
31  virtual bool addPresetShaders() = 0;
32  virtual bool addPresetUniforms() = 0;
33 
34  /* templated methods - write implementations in headers to avoid template declarations of inhereted classes */
35  template <typename T>
36  bool addUniform(const std::string& name, osg::Uniform::Type type, T* updateCallback = 0){
37  if (!m_state.get() || !updateCallback){
38  qCritical("State or callback is NULL");
39  return false;
40  }
41  osg::Uniform* uniform = m_state->getOrCreateUniform(name, type);
42  uniform->setUpdateCallback(updateCallback);
43 
44  return true;
45  }
46 
47  bool addUniformCanvasMatrix();
48 
49  template <typename T>
50  bool addUniform(const std::string& name, osg::Uniform::Type type, T value){
51  if (!m_state.get()){
52  qCritical("State is nULL");
53  return false;
54  }
55  osg::Uniform* uniform = m_state->getOrCreateUniform(name, type);
56  uniform->set(value);
57 
58  return true;
59  }
60 
61 protected:
62  osg::observer_ptr<osg::StateSet> m_state; // of entity::Canvas::m_geodeStrokes
63  osg::observer_ptr<osg::Camera> m_camera; // of GLWidget::getCamera()
64  osg::observer_ptr<osg::MatrixTransform> m_transform; // of Canvas::m_transform
65 };
66 
67 #endif // PROGRAMENTITY2D_H
A virtual class to be inhereted by program for stroke, polygon and other entities.
Definition: ProgramEntity2D.h:17