program.hpp

Go to the documentation of this file.
00001 /*
00002     RenderStack  Support library for OpenGL 3+
00003     Copyright (C) 2010  Timo Suoranta
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Lesser General Public
00007     License as published by the Free Software Foundation; either
00008     version 2.1 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00027 #ifndef PROGRAM_HPP
00028 #define PROGRAM_HPP
00029 
00030 #include "renderstack/rs.h"
00031 #include "renderstack/mesh.hpp"
00032 #include "renderstack/attribute_bindings.hpp"
00033 #include <vector>
00034 #include <exception>
00035 
00036 namespace renderstack {
00037 
00038 class program_attribute_not_found_exception
00039 :   public std::exception
00040 {
00041 };
00042 
00043 class uniform_not_found_exception
00044 :   public std::exception
00045 {
00046 };
00047 
00049 class uniform
00050 {
00051 public:
00052     uniform(){}
00053     uniform(std::string const &name, int gl_slot, int size, int gl_type_code)
00054     :   m_name        (name)
00055     ,   m_gl_slot     (gl_slot)
00056     ,   m_size        (size)
00057     ,   m_gl_type_code(gl_type_code)
00058     {
00059     }
00060 
00061     static void         print_source(std::string const &source);
00062 
00063     std::string const   &name       () const { return m_name; }
00064     int                 gl_slot     () const { return m_gl_slot; } 
00065     int                 size        () const { return m_size; }
00066     int                 gl_type_code() const { return m_gl_type_code; } 
00067 
00068 private:
00069     std::string m_name;
00070     int         m_gl_slot;
00071     int         m_size;
00072     int         m_gl_type_code;
00073 };
00074 
00076 class program_attribute
00077 {
00078 public:
00079     program_attribute(
00080         std::string const   &name, 
00081         int                 gl_slot, 
00082         int                 size, 
00083         int                 gl_type_code
00084     )
00085     :   m_name          (name)
00086     ,   m_gl_slot       (gl_slot)
00087     ,   m_size          (size)
00088     ,   m_gl_type_code  (gl_type_code)
00089     {
00090     }
00091 
00092 
00093     std::string const   &name       () const { return m_name; }
00094     int                 gl_slot     () const { return m_gl_slot; }
00095     int                 gl_type_code() const { return m_gl_type_code; } 
00096 
00097     void                debug() const;
00098 
00099 private:
00100     std::string m_name;
00101     int         m_gl_slot;
00102     int         m_size;
00103     int         m_gl_type_code;
00104 };
00105 
00107 class shader
00108 {
00109 public:
00110     shader(
00111         int                 shader_type,
00112         std::string const   &source
00113     );
00114 
00115     std::string const   &source() const { return m_source; }
00116     int                 gl_shader_object() const { return m_gl_shader_object; } 
00117 
00118     void                print_source() const;
00119 
00120 private:
00121     std::string m_source;
00122     int         m_gl_shader_object;
00123 };
00124 
00126 class program
00127 {
00128 public:
00129     program();
00130     program(std::string const &vertex_shader, std::string const &fragment_shader, enum mesh_mode mode);
00131 
00132     void    create ();
00133     void    set    (std::string const &vertex_shader, std::string const &fragment_shader, enum mesh_mode mode);
00134     void    link   ();
00135     int     gl_program_object() const { return m_gl_program_object; }
00136 
00137     void                            set_mesh_mode   (enum mesh_mode mode){ m_mesh_mode = mode; }
00138     enum mesh_mode                  mesh_mode       () const { return m_mesh_mode; }
00139     void                            use             () const;
00140     bool                            has_uniform     (std::string const &name) const;
00141     bool                            has_attribute   (std::string const &name) const;
00142     class uniform                   &uniform        (std::string const &name);
00143     class uniform const             &uniform        (std::string const &name) const;
00144     class program_attribute         &attribute      (std::string const &name);
00145     class program_attribute const   &attribute      (std::string const &name) const;
00146     void                            make_shader     (int shader_type, std::string const &source);
00147 
00148     class attribute_mappings        &attribute_mappings() { return m_attribute_mappings; }
00149     class attribute_mappings const  &attribute_mappings() const { return m_attribute_mappings; }
00150 
00151 private:
00152     void map_attributes();
00153     void map_uniforms();
00154 
00155 public:
00156     std::vector<class program_attribute>    m_attributes;
00157     std::vector<class uniform>              m_uniforms;
00158     std::vector<class shader>               m_shaders;
00159 
00160     int                                     m_gl_program_object;
00161     enum mesh_mode                          m_mesh_mode;
00162 
00163     class attribute_mappings                m_attribute_mappings;
00164 
00165 };
00166 
00167 }
00168 
00169 #endif
00170 
Generated on Sun Apr 11 12:23:08 2010 for RenderStack by  doxygen 1.6.3