HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GlslRenderer.h
Go to the documentation of this file.
1 //
2 // TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3 // All rights reserved. See LICENSE.txt for license.
4 //
5 
6 #ifndef MATERIALX_GLSLRENDERER_H
7 #define MATERIALX_GLSLRENDERER_H
8 
9 /// @file
10 /// GLSL code renderer
11 
13 
16 
18 
20 
21 using GLContextPtr = std::shared_ptr<class GLContext>;
22 using SimpleWindowPtr = std::shared_ptr<class SimpleWindow>;
23 
24 /// Shared pointer to a GlslRenderer
25 using GlslRendererPtr = std::shared_ptr<class GlslRenderer>;
26 
27 /// @class GlslRenderer
28 /// Helper class for rendering generated GLSL code to produce images.
29 ///
30 /// There are two main interfaces which can be used. One which takes in a HwShader and one which
31 /// allows for explicit setting of shader stage code.
32 ///
33 /// The main services provided are:
34 /// - Validation: All shader stages are compiled and atteched to a GLSL shader program.
35 /// - Introspection: The compiled shader program is examined for uniforms and attributes.
36 /// - Binding: Uniforms and attributes which match the predefined variables generated the GLSL code generator
37 /// will have values assigned to this. This includes matrices, attribute streams, and textures.
38 /// - Rendering: The program with bound inputs will be used to drawing geometry to an offscreen buffer.
39 /// An interface is provided to save this offscreen buffer to disk using an externally defined image handler.
40 ///
42 {
43  public:
44  /// Create a GLSL renderer instance
45  static GlslRendererPtr create(unsigned int width = 512, unsigned int height = 512, Image::BaseType baseType = Image::BaseType::UINT8);
46 
47  /// Destructor
48  virtual ~GlslRenderer() { }
49 
50  /// @name Setup
51  /// @{
52 
53  /// Internal initialization of stages and OpenGL constructs
54  /// required for program validation and rendering.
55  /// An exception is thrown on failure.
56  /// The exception will contain a list of initialization errors.
57  void initialize() override;
58 
59  /// @}
60  /// @name Rendering
61  /// @{
62 
63  /// Create GLSL program based on an input shader
64  /// @param shader Input HwShader
65  void createProgram(ShaderPtr shader) override;
66 
67  /// Create GLSL program based on shader stage source code.
68  /// @param stages Map of name and source code for the shader stages.
69  void createProgram(const StageMap& stages) override;
70 
71  /// Validate inputs for the program
72  void validateInputs() override;
73 
74  /// Set the size of the rendered image
75  void setSize(unsigned int width, unsigned int height) override;
76 
77  /// Render the current program to an offscreen buffer.
78  void render() override;
79 
80  /// Render the current program in texture space to an off-screen buffer.
81  void renderTextureSpace(const Vector2& uvMin, const Vector2& uvMax);
82 
83  /// @}
84  /// @name Utilities
85  /// @{
86 
87  /// Capture the current contents of the off-screen hardware buffer as an image.
88  ImagePtr captureImage(ImagePtr image = nullptr) override;
89 
90  /// Return the GL frame buffer.
92  {
93  return _framebuffer;
94  }
95 
96  /// Return the GLSL program.
98  {
99  return _program;
100  }
101 
102  /// Submit geometry for a screen-space quad.
103  void drawScreenSpaceQuad(const Vector2& uvMin = Vector2(0.0f), const Vector2& uvMax = Vector2(1.0f));
104 
105  /// Sets the clear color
106  void setClearColor(const Color4& clearColor);
107 
108  /// @}
109 
110  protected:
111  GlslRenderer(unsigned int width, unsigned int height, Image::BaseType baseType);
112 
113  virtual void updateViewInformation();
114  virtual void updateWorldInformation();
115 
116  private:
117  GlslProgramPtr _program;
118 
119  GLFramebufferPtr _framebuffer;
120 
121  bool _initialized;
122 
123  const Vector3 _eye;
124  const Vector3 _center;
125  const Vector3 _up;
126  float _objectScale;
127 
128  SimpleWindowPtr _window;
129  GLContextPtr _context;
130  Color4 _clearColor;
131 };
132 
134 
135 #endif
GlslProgramPtr getProgram()
Return the GLSL program.
Definition: GlslRenderer.h:97
virtual void validateInputs()
Validate inputs for the program.
GLbitfield stages
Definition: glcorearb.h:1931
std::shared_ptr< GLFramebuffer > GLFramebufferPtr
Shared pointer to a GLFramebuffer.
Definition: GLFramebuffer.h:21
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:23
GLenum GLsizei GLenum GLenum const void * image
Definition: glew.h:4973
GLFramebufferPtr getFramebuffer() const
Return the GL frame buffer.
Definition: GlslRenderer.h:91
BaseType
Definition: Image.h:45
std::shared_ptr< class GlslProgram > GlslProgramPtr
Definition: GlslProgram.h:24
std::shared_ptr< class SimpleWindow > SimpleWindowPtr
SimpleWindow shared pointer.
Definition: GLContext.h:34
virtual void createProgram(ShaderPtr shader)
Create program based on an input shader.
virtual void setSize(unsigned int width, unsigned int height)
Set the size of the rendered image.
Definition: Types.h:282
virtual ~GlslRenderer()
Destructor.
Definition: GlslRenderer.h:48
#define MX_RENDERGLSL_API
Definition: Export.h:18
virtual void initialize()
Initialize the renderer.
GLint GLsizei width
Definition: glcorearb.h:103
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
GLuint shader
Definition: glcorearb.h:785
std::shared_ptr< class GlslRenderer > GlslRendererPtr
Shared pointer to a GlslRenderer.
Definition: GlslRenderer.h:25
Definition: Types.h:302
GLfloat f
Definition: glcorearb.h:1926
shared_ptr< Shader > ShaderPtr
Shared pointer to a Shader.
Definition: Library.h:34
virtual ImagePtr captureImage(ImagePtr image=nullptr)
Capture the current rendered output as an image.
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:24
shared_ptr< Image > ImagePtr
A shared pointer to an image.
Definition: Image.h:23
virtual void render()
Render the current program to produce an image.
std::shared_ptr< class GLContext > GLContextPtr
GLContext shared pointer.
Definition: GLContext.h:37