NCBI C++ ToolKit
glshader.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_OPENGL___GL_SHADER__HPP
2 #define GUI_OPENGL___GL_SHADER__HPP
3 
4 /* $Id: glshader.hpp 38609 2017-06-02 13:12:34Z falkrb $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Bob Falk
30  *
31  * File Description:
32  *
33  */
34 
35 #include <gui/opengl.h>
36 #include <gui/gui.hpp>
37 
38 #include <gui/utils/vect2.hpp>
39 #include <gui/utils/vect3.hpp>
40 #include <gui/utils/vect4.hpp>
41 #include <gui/utils/matrix3.hpp>
42 #include <gui/utils/matrix4.hpp>
43 #include <gui/utils/rgba_color.hpp>
44 
45 #include <gui/opengl/gltypes.hpp>
46 #include <gui/opengl/globject.hpp>
47 
48 /** @addtogroup GUI_OPENGL
49  *
50  * @{
51  */
52 
54 
55 
56 //////////////////////////////////////////////////////////////////////////////
57 /// CGlShader
58 ///
59 /// A simple class to read in and create vertex and fragment shaders.
60 /// Note this also works (as default/built-in shader) if no shader is specified.
62 {
63 public:
65  : m_VertexShaderId(0)
66  , m_FragmentShaderId(0)
67  , m_ShaderProgramId(0)
68  , m_PrevProgramId(0) {}
69 
70  /// Free OpenGL resources (shader objects and program) and set ids to 0
71  virtual ~CGlShader();
72 
73  /// file_name should be the root file name of a vertex and a pixel shader with
74  /// the same base name, e.g. phong.vert and phong.frag.
75  GLuint CreateShader(const string& file_name);
76 
77  /// create shaders from passed in strings which have the shader contents.
78  GLuint CreateShader(const string& vertex_shader, const string& fragment_shader);
79 
80  /// Free OpenGL resources (shader objects and program) and set ids to 0
81  void Clear();
82 
83  /// Call glUseProgram to make m_ShaderProgramId the current shader
84  void MakeCurrent() const;
85 
86  /// Restore whatever the current shader was when MakeCurrent() was last called
87  void Release();
88 
89  /// Returns location ID for uniform var "name". Returns -1 if program
90  /// not valid or "name" not defined.
91  GLint GetUniformLocation(const GLchar *name);
92 
93  /// true if we have a non-default shader (but you can still call MakeCurrent when
94  /// m_ShaderProgramId is 0, you just get default shading).
95  bool IsValid() const { return (m_ShaderProgramId>0); }
96 
97  GLuint GetProgramId() const { return m_ShaderProgramId; }
98 
99  /// Return any logged errors from last call to create shaders
100  const string& GetLog() const { return m_CompileLinkLog; }
101 
102 protected:
103  string x_GetShaderInfoLog(GLuint obj) const;
104  string x_GetProgramInfoLog(GLuint obj) const;
105 
106  /// Shader IDs
109 
110  /// Shader program ID passed to glUseProgram()
112 
113  /// Updated everytime MakeCurrent is called so that the previous program
114  /// can be reset.
115  mutable GLuint m_PrevProgramId;
116 
117  /// Error log from last compile/link.
119 
120 };
121 
123 
124 /* @} */
125 
126 #endif // GUI_OPENGL___GL_SHADER__HPP
CGlObject Base class for OpenGl objects like buffers and models.
Definition: globject.hpp:55
CGlShader.
Definition: glshader.hpp:62
const char * file_name[]
GLuint m_VertexShaderId
Shader IDs.
Definition: glshader.hpp:107
GLuint GetProgramId() const
Definition: glshader.hpp:97
GLuint m_ShaderProgramId
Shader program ID passed to glUseProgram()
Definition: glshader.hpp:111
string m_CompileLinkLog
Error log from last compile/link.
Definition: glshader.hpp:118
bool IsValid() const
true if we have a non-default shader (but you can still call MakeCurrent when m_ShaderProgramId is 0,...
Definition: glshader.hpp:95
GLuint m_FragmentShaderId
Definition: glshader.hpp:108
GLuint m_PrevProgramId
Updated everytime MakeCurrent is called so that the previous program can be reset.
Definition: glshader.hpp:115
const string & GetLog() const
Return any logged errors from last call to create shaders.
Definition: glshader.hpp:100
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define NCBI_GUIOPENGL_EXPORT
Definition: gui_export.h:514
Standard mechanism to include OpenGL headers for all platforms.
Modified on Tue May 28 05:52:21 2024 by modify_doxy.py rev. 669887