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  {
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
SurgSim::Graphics::OsgProgram
OSG-based implementation of a graphics shader.
Definition: OsgProgram.h:43
SurgSim::Graphics::OsgProgram::getGeometryShaderSource
bool getGeometryShaderSource(std::string *source) const override
Gets the geometry shader source code.
Definition: OsgProgram.cpp:99
SurgSim::Graphics::OsgProgram::clearGeometryShader
void clearGeometryShader() override
Removes the geometry shader, returning that portion of the shader program to fixed-function.
Definition: OsgProgram.cpp:84
SurgSim::Graphics::OsgProgram::clearVertexShader
void clearVertexShader() override
Removes the vertex shader, returning that portion of the shader program to fixed-function.
Definition: OsgProgram.cpp:59
SurgSim::Graphics::OsgProgram::addToStateSet
void addToStateSet(osg::StateSet *stateSet)
Adds this shader to the OSG state set.
Definition: OsgProgram.cpp:37
SurgSim::Graphics::OsgProgram::SHADER_TYPE_COUNT
@ SHADER_TYPE_COUNT
Definition: OsgProgram.h:105
SurgSim::Graphics::OsgProgram::loadFragmentShader
bool loadFragmentShader(const std::string &filePath) override
Loads the fragment shader source code from a file.
Definition: OsgProgram.cpp:114
SurgSim::Graphics::OsgProgram::hasShader
bool hasShader(int shaderType) const
Check whether there is a shader in use for the given type.
Definition: OsgProgram.cpp:149
SurgSim::Graphics::OsgProgram::m_program
osg::ref_ptr< osg::Program > m_program
OSG program attribute.
Definition: OsgProgram.h:97
SurgSim::Graphics::OsgProgram::SHADER_TYPE_VERTEX
@ SHADER_TYPE_VERTEX
Definition: OsgProgram.h:102
SurgSim::Graphics::OsgProgram::SHADER_TYPE_FRAGMENT
@ SHADER_TYPE_FRAGMENT
Definition: OsgProgram.h:103
SurgSim::Graphics::OsgProgram::setShaderSource
virtual void setShaderSource(const std::string &source, int shaderType)
Set the shader source code.
Definition: OsgProgram.cpp:181
SurgSim::Graphics::OsgProgram::loadGeometryShader
bool loadGeometryShader(const std::string &filePath) override
Loads the geometry shader source code from a file.
Definition: OsgProgram.cpp:89
SurgSim::Graphics::OsgProgram::clearShader
void clearShader(int shaderType)
Removes the geometry shader, returning that portion of the shader program to fixed-function.
Definition: OsgProgram.cpp:159
SurgSim::Graphics::OsgProgram::hasFragmentShader
bool hasFragmentShader() const override
Definition: OsgProgram.cpp:104
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Graphics::OsgProgram::setVertexShaderSource
void setVertexShaderSource(const std::string &source) override
Set the vertex shader source code.
Definition: OsgProgram.cpp:69
SurgSim::Graphics::OsgProgram::clearFragmentShader
void clearFragmentShader() override
Definition: OsgProgram.cpp:109
SurgSim::Graphics::Program
Base class that defines the interface for graphics programs.
Definition: Program.h:39
SurgSim::Graphics::OsgProgram::m_globalScope
bool m_globalScope
Is the shader supposed to be used globally.
Definition: OsgProgram.h:141
SurgSim::Graphics::OsgProgram::ShaderType
ShaderType
Definition: OsgProgram.h:100
SurgSim::Graphics::OsgProgram::setFragmentShaderSource
void setFragmentShaderSource(const std::string &source) override
Set the fragment shader source code.
Definition: OsgProgram.cpp:119
SurgSim::Graphics::OsgProgram::OsgProgram
OsgProgram()
Constructor.
Definition: OsgProgram.cpp:30
SurgSim::Graphics::OsgProgram::getOrCreateOsgShader
osg::ref_ptr< osg::Shader > getOrCreateOsgShader(int shaderType)
Fetches the appropriate shader if it exists, creates it otherwise.
Definition: OsgProgram.cpp:201
SurgSim::Graphics::OsgProgram::loadShaderSource
bool loadShaderSource(const std::string &filePath, int shaderType)
Loads the shader source code from a file.
Definition: OsgProgram.cpp:168
SurgSim::Graphics::OsgProgram::hasVertexShader
bool hasVertexShader() const override
Definition: OsgProgram.cpp:54
SurgSim::Graphics::OsgProgram::loadVertexShader
bool loadVertexShader(const std::string &filePath) override
Loads the vertex shader source code from a file.
Definition: OsgProgram.cpp:64
SurgSim::Graphics::loadProgram
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
SurgSim::Graphics::OsgProgram::SHADER_TYPE_GEOMETRY
@ SHADER_TYPE_GEOMETRY
Definition: OsgProgram.h:104
SurgSim::Graphics::OsgProgram::setGlobalScope
void setGlobalScope(bool val) override
When this is set to true, this shader should be used instead of other shaders that might apply,...
Definition: OsgProgram.cpp:134
SurgSim::Graphics::OsgProgram::m_osgShaders
std::array< osg::ref_ptr< osg::Shader >, SHADER_TYPE_COUNT > m_osgShaders
Storage of the osg objects.
Definition: OsgProgram.h:109
string
string(TOUPPER ${DEVICE} DEVICE_UPPER_CASE) option(BUILD_DEVICE_$
Definition: CMakeLists.txt:38
Program.h
SurgSim::Graphics::OsgProgram::getShaderSource
virtual bool getShaderSource(int shaderType, std::string *source) const
Gets the shader source code.
Definition: OsgProgram.cpp:187
SurgSim::Graphics::OsgProgram::getFragmentShaderSource
bool getFragmentShaderSource(std::string *source) const override
Gets the fragment shader source code.
Definition: OsgProgram.cpp:124
SurgSim::Graphics::OsgProgram::getVertexShaderSource
bool getVertexShaderSource(std::string *source) const override
Gets the vertex shader source code.
Definition: OsgProgram.cpp:74
SurgSim::Graphics::OsgProgram::getOsgProgram
osg::ref_ptr< osg::Program > getOsgProgram() const
Definition: OsgProgram.cpp:129
SurgSim::Graphics::OsgProgram::isGlobalScope
bool isGlobalScope() const override
Query if this shader is of global scope.
Definition: OsgProgram.cpp:144
SurgSim::Graphics::OsgProgram::setGeometryShaderSource
void setGeometryShaderSource(const std::string &source) override
Set the geometry shader source code.
Definition: OsgProgram.cpp:94
SurgSim::Graphics::OsgProgram::hasGeometryShader
bool hasGeometryShader() const override
Definition: OsgProgram.cpp:79
SurgSim::Framework::ApplicationData
Enable searching for files in a given list of paths, give access to the current directory and wrap bo...
Definition: ApplicationData.h:39
SurgSim::Graphics::OsgProgram::removeFromStateSet
void removeFromStateSet(osg::StateSet *stateSet)
Removes this uniform from the OSG state set.
Definition: OsgProgram.cpp:46