Open3D (C++ API)
ShaderWrapper.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <GL/glew.h>
31 
34 
35 namespace open3d {
36 namespace visualization {
37 
38 namespace glsl {
39 
41 public:
42  virtual ~ShaderWrapper() {}
43  ShaderWrapper(const ShaderWrapper &) = delete;
44  ShaderWrapper &operator=(const ShaderWrapper &) = delete;
45 
46 protected:
47  ShaderWrapper(const std::string &name) : shader_name_(name) {}
48 
49 public:
54  bool Render(const geometry::Geometry &geometry,
55  const RenderOption &option,
56  const ViewControl &view);
57 
60  void InvalidateGeometry();
61 
62  const std::string &GetShaderName() const { return shader_name_; }
63 
64  void PrintShaderWarning(const std::string &message) const;
65 
66 protected:
70  virtual bool Compile() = 0;
71 
75  virtual void Release() = 0;
76 
77  virtual bool BindGeometry(const geometry::Geometry &geometry,
78  const RenderOption &option,
79  const ViewControl &view) = 0;
80  virtual bool RenderGeometry(const geometry::Geometry &geometry,
81  const RenderOption &option,
82  const ViewControl &view) = 0;
83  virtual void UnbindGeometry() = 0;
84 
85 protected:
86  bool ValidateShader(GLuint shader_index);
87  bool ValidateProgram(GLuint program_index);
88  bool CompileShaders(const char *const vertex_shader_code,
89  const char *const geometry_shader_code,
90  const char *const fragment_shader_code);
91  void ReleaseProgram();
92 
93 protected:
97  GLuint program_;
98  GLenum draw_arrays_mode_ = GL_POINTS;
99  GLsizei draw_arrays_size_ = 0;
100  bool compiled_ = false;
101  bool bound_ = false;
102 
103  void SetShaderName(const std::string &shader_name) {
104  shader_name_ = shader_name;
105  }
106 
107 private:
108  std::string shader_name_ = "ShaderWrapper";
109 };
110 
111 } // namespace glsl
112 
113 } // namespace visualization
114 } // namespace open3d
bool ValidateShader(GLuint shader_index)
Definition: ShaderWrapper.cpp:139
ShaderWrapper(const ShaderWrapper &)=delete
GLuint geometry_shader_
Definition: ShaderWrapper.h:95
Definition: Geometry.h:32
void PrintShaderWarning(const std::string &message) const
Definition: ShaderWrapper.cpp:59
bool bound_
Definition: ShaderWrapper.h:101
GLuint vertex_shader_
Definition: ShaderWrapper.h:94
void ReleaseProgram()
Definition: ShaderWrapper.cpp:132
ShaderWrapper(const std::string &name)
Definition: ShaderWrapper.h:47
virtual ~ShaderWrapper()
Definition: ShaderWrapper.h:42
Definition: ViewControl.h:38
virtual bool BindGeometry(const geometry::Geometry &geometry, const RenderOption &option, const ViewControl &view)=0
void InvalidateGeometry()
Definition: ShaderWrapper.cpp:53
GLuint fragment_shader_
Definition: ShaderWrapper.h:96
ShaderWrapper & operator=(const ShaderWrapper &)=delete
bool CompileShaders(const char *const vertex_shader_code, const char *const geometry_shader_code, const char *const fragment_shader_code)
Definition: ShaderWrapper.cpp:64
virtual bool RenderGeometry(const geometry::Geometry &geometry, const RenderOption &option, const ViewControl &view)=0
Definition: RenderOption.h:36
GLenum draw_arrays_mode_
Definition: ShaderWrapper.h:98
void SetShaderName(const std::string &shader_name)
Definition: ShaderWrapper.h:103
bool compiled_
Definition: ShaderWrapper.h:100
Definition: PinholeCameraIntrinsic.cpp:33
std::string name
Definition: FilePCD.cpp:54
Definition: ShaderWrapper.h:40
const std::string & GetShaderName() const
Definition: ShaderWrapper.h:62
bool Render(const geometry::Geometry &geometry, const RenderOption &option, const ViewControl &view)
Definition: ShaderWrapper.cpp:37
GLsizei draw_arrays_size_
Definition: ShaderWrapper.h:99
GLuint program_
Definition: ShaderWrapper.h:97
bool ValidateProgram(GLuint program_index)
Definition: ShaderWrapper.cpp:156