opensurgsim
OsgProgram.h
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 
19 #include "SurgSim/Graphics/Program.h"
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 
38 SURGSIM_STATIC_REGISTRATION(OsgProgram);
39 
45 class OsgProgram : public Program
46 {
47 public:
50  OsgProgram();
51 
52  SURGSIM_CLASSNAME(SurgSim::Graphics::OsgProgram);
53 
54  bool hasVertexShader() const override;
55 
56  void clearVertexShader() override;
57 
58  bool loadVertexShader(const std::string& filePath) override;
59 
60  void setVertexShaderSource(const std::string& source) override;
61 
62  bool getVertexShaderSource(std::string* source) const override;
63 
64  bool hasGeometryShader() const override;
65 
66  void clearGeometryShader() override;
67 
68  bool loadGeometryShader(const std::string& filePath) override;
69 
70  void setGeometryShaderSource(const std::string& source) override;
71 
72  bool getGeometryShaderSource(std::string* source) const override;
73 
74  bool hasFragmentShader() const override;
75 
76  void clearFragmentShader() override;
77 
78  bool loadFragmentShader(const std::string& filePath) override;
79 
80  void setFragmentShaderSource(const std::string& source) override;
81 
82  bool getFragmentShaderSource(std::string* source) const override;
83 
84  void setGlobalScope(bool val) override;
85 
86  bool isGlobalScope() const override;
87 
89  osg::ref_ptr<osg::Program> getOsgProgram() const;
90 
93  void addToStateSet(osg::StateSet* stateSet);
94 
97  void removeFromStateSet(osg::StateSet* stateSet);
98 
99 private:
101  osg::ref_ptr<osg::Program> m_program;
102 
103  // Type of shader, internal use only
104  enum ShaderType
105  {
106  SHADER_TYPE_VERTEX = 0,
107  SHADER_TYPE_FRAGMENT,
108  SHADER_TYPE_GEOMETRY,
109  SHADER_TYPE_COUNT
110  };
111 
113  std::array<osg::ref_ptr<osg::Shader>, SHADER_TYPE_COUNT> m_osgShaders;
114 
118  bool hasShader(int shaderType) const;
119 
122  void clearShader(int shaderType);
123 
128  bool loadShaderSource(const std::string& filePath, int shaderType);
129 
133  virtual void setShaderSource(const std::string& source, int shaderType);
134 
137  virtual bool getShaderSource(int shaderType, std::string* source) const;
138 
142  osg::ref_ptr<osg::Shader> getOrCreateOsgShader(int shaderType);
143 
145  bool m_globalScope;
146 
147 protected:
148 
149  bool doLoad(const std::string& filePath) override;
150 
151 };
152 
157 std::shared_ptr<SurgSim::Graphics::OsgProgram> loadProgram(const SurgSim::Framework::ApplicationData& data,
158  const std::string& name);
159 
165 std::shared_ptr<SurgSim::Graphics::OsgProgram> loadProgram(const SurgSim::Framework::ApplicationData& data,
166  const std::string& vertexShaderName, const std::string& fragmentShaderName);
167 
168 }; // namespace Graphics
169 
170 }; // namespace SurgSim
171 
172 #endif // SURGSIM_GRAPHICS_OSGPROGRAM_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
Enable searching for files in a given list of paths, give access to the current directory and wrap bo...
Definition: ApplicationData.h:39
OSG-based implementation of a graphics shader.
Definition: OsgProgram.h:45
Base class that defines the interface for graphics programs.
Definition: Program.h:40