attribute_bindings.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 ATTRIBUTE_BINDINGS_HPP
00028 #define ATTRIBUTE_BINDINGS_HPP
00029 
00030 #include "renderstack/rs.h"
00031 #include "renderstack/rs_gl.h"
00032 #include <memory>
00033 #include <vector>
00034 #include <string>
00035 #include <map>
00036 
00037 namespace renderstack {
00038 
00039 struct attribute_mapping;
00040 struct attribute_binding;
00041 class attribute_mappings;
00042 class attribute_bindings;
00043 
00045 struct attribute_mapping
00046 {
00047     attribute_mapping(
00048         std::string const           &name_,
00049         enum vertex_attribute_usage usage_,
00050         int                         index_,         /*  0, 1, ...  */ 
00051         int                         dimension_      /*  1  -> float, 2 -> vec2, 3 -> vec3, 4 -> vec4  */ 
00052     )
00053     :   name        (name_)
00054     ,   usage       (usage_)
00055     ,   index       (index_)
00056     ,   dimension   (dimension_)
00057     {
00058     }
00059 
00060     std::string                 name;
00061     enum vertex_attribute_usage usage;
00062     int                         index;
00063     int                         dimension;
00064 };
00065 
00067 class attribute_mappings
00068 {
00069 public:
00070     void insert(
00071         std::string const           &name,          /*  name in source code  */ 
00072         enum vertex_attribute_usage usage,          /*  TODO replace with a string  */ 
00073         int                         index,
00074         int                         dimension
00075     )
00076     {
00077         std::tr1::shared_ptr<attribute_mapping> m(
00078             new attribute_mapping(name, usage, index, dimension)
00079         );
00080         m_mappings.push_back(m);
00081     }
00082 
00083     void bind_attributes(
00084         class attribute_bindings    &bindings,
00085         class program const         &program, 
00086         class mesh const            &mesh
00087     ) const;
00088 
00089 private:
00090     std::vector<std::tr1::shared_ptr<attribute_mapping> > m_mappings;
00091 };
00092 
00094 struct attribute_binding
00095 {
00096     attribute_binding(
00097         unsigned int    index_,
00098         int             size_,
00099         int             type_,
00100         unsigned char   normalized_,
00101         int             stride_,
00102         void const      *pointer_
00103     )
00104     :   index       (index_)
00105     ,   size        (size_)
00106     ,   type        (type_)
00107     ,   normalized  (normalized_)
00108     ,   stride      (stride_)
00109     ,   pointer     (pointer_)
00110     {
00111     }
00112 
00113     unsigned int    index;
00114     int             size;
00115     int             type;
00116     unsigned char   normalized;
00117     int             stride;
00118     void const      *pointer;
00119 };
00120 
00122 class attribute_bindings
00123 {
00124 public:
00125     void add(
00126         unsigned int    index,          /*  slot   */ 
00127         int             size,           /*  count  */ 
00128         int             type,
00129         unsigned char   normalized,
00130         int             stride,
00131         void const      *pointer
00132     )
00133     {
00134         attribute_binding binding(index, size, type, normalized, stride, pointer);
00135         m_bindings.push_back(binding);
00136     }
00137 
00138     void apply(bool enable)
00139     {
00140         for(
00141             std::vector<attribute_binding>::const_iterator i = m_bindings.begin();
00142             i != m_bindings.end();
00143             ++i
00144         )
00145         {
00146             attribute_binding const &binding = *i;
00147 
00148             if(enable == true)
00149             {
00150                 if(
00151                     (binding.type == GL_UNSIGNED_SHORT) ||
00152                     (binding.type == GL_UNSIGNED_INT)   
00153                 )
00154                 {
00155                     ::rs_gl_vertex_attrib_i_pointer(
00156                         binding.index,
00157                         binding.size,
00158                         binding.type,
00159                         binding.stride,
00160                         binding.pointer
00161                     );
00162                 }
00163                 else
00164                 {
00165                     ::rs_gl_vertex_attrib_pointer(
00166                         binding.index,
00167                         binding.size,
00168                         binding.type,
00169                         binding.normalized,
00170                         binding.stride,
00171                         binding.pointer
00172                     );
00173                 }
00174                 ::rs_gl_enable_vertex_attrib_array(binding.index);
00175             }
00176             else
00177             {
00178                 ::rs_gl_disable_vertex_attrib_array(binding.index);
00179             }
00180         }
00181     }
00182 
00183 private:
00184     std::vector<attribute_binding> m_bindings;
00185 };
00186 
00187 }
00188 
00189 #endif
00190 
Generated on Sun Apr 11 12:23:08 2010 for RenderStack by  doxygen 1.6.3