OsgProgram.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013-2016, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_GRAPHICS_OSGPROGRAM_H
17 #define SURGSIM_GRAPHICS_OSGPROGRAM_H
18 
20 
21 #include <osg/Program>
22 #include <osg/StateSet>
23 
24 #include <string>
25 #include <memory>
26 #include <array>
27 
28 namespace SurgSim
29 {
30 namespace Framework
31 {
32 class ApplicationData;
33 }
34 
35 namespace Graphics
36 {
37 
43 class OsgProgram : public Program
44 {
45 public:
48  OsgProgram();
49 
50  bool hasVertexShader() const override;
51 
52  void clearVertexShader() override;
53 
54  bool loadVertexShader(const std::string& filePath) override;
55 
56  void setVertexShaderSource(const std::string& source) override;
57 
58  bool getVertexShaderSource(std::string* source) const override;
59 
60  bool hasGeometryShader() const override;
61 
62  void clearGeometryShader() override;
63 
64  bool loadGeometryShader(const std::string& filePath) override;
65 
66  void setGeometryShaderSource(const std::string& source) override;
67 
68  bool getGeometryShaderSource(std::string* source) const override;
69 
70  bool hasFragmentShader() const override;
71 
72  void clearFragmentShader() override;
73 
74  bool loadFragmentShader(const std::string& filePath) override;
75 
76  void setFragmentShaderSource(const std::string& source) override;
77 
78  bool getFragmentShaderSource(std::string* source) const override;
79 
80  void setGlobalScope(bool val) override;
81 
82  bool isGlobalScope() const override;
83 
85  osg::ref_ptr<osg::Program> getOsgProgram() const;
86 
89  void addToStateSet(osg::StateSet* stateSet);
90 
93  void removeFromStateSet(osg::StateSet* stateSet);
94 
95 private:
97  osg::ref_ptr<osg::Program> m_program;
98 
99  // Type of shader, internal use only
101  {
102  SHADER_TYPE_VERTEX = 0,
105  SHADER_TYPE_COUNT
106  };
107 
109  std::array<osg::ref_ptr<osg::Shader>, SHADER_TYPE_COUNT> m_osgShaders;
110 
114  bool hasShader(int shaderType) const;
115 
118  void clearShader(int shaderType);
119 
124  bool loadShaderSource(const std::string& filePath, int shaderType);
125 
129  virtual void setShaderSource(const std::string& source, int shaderType);
130 
133  virtual bool getShaderSource(int shaderType, std::string* source) const;
134 
138  osg::ref_ptr<osg::Shader> getOrCreateOsgShader(int shaderType);
139 
142 
143 };
144 
149 std::shared_ptr<SurgSim::Graphics::OsgProgram> loadProgram(const SurgSim::Framework::ApplicationData& data,
150  const std::string& name);
151 
157 std::shared_ptr<SurgSim::Graphics::OsgProgram> loadProgram(const SurgSim::Framework::ApplicationData& data,
158  const std::string& vertexShaderName, const std::string& fragmentShaderName);
159 
160 }; // namespace Graphics
161 
162 }; // namespace SurgSim
163 
164 #endif // SURGSIM_GRAPHICS_OSGPROGRAM_H
Definition: CompoundShapeToGraphics.cpp:29
Enable searching for files in a given list of paths, give access to the current directory and wrap bo...
Definition: ApplicationData.h:39
ShaderType
Definition: OsgProgram.h:100
OSG-based implementation of a graphics shader.
Definition: OsgProgram.h:43
osg::ref_ptr< osg::Program > m_program
OSG program attribute.
Definition: OsgProgram.h:97
Base class that defines the interface for graphics programs.
Definition: Program.h:39
std::array< osg::ref_ptr< osg::Shader >, SHADER_TYPE_COUNT > m_osgShaders
Storage of the osg objects.
Definition: OsgProgram.h:109
bool m_globalScope
Is the shader supposed to be used globally.
Definition: OsgProgram.h:141
std::shared_ptr< OsgProgram > loadProgram(const SurgSim::Framework::ApplicationData &data, const std::string &name)
Utility function, load a program from a set of shader files.
Definition: OsgProgram.cpp:217