vertex_format.cpp

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 #include "renderstack/vertex_format.hpp"
00028 #include "renderstack/rs_gl.h"
00029 
00030 namespace renderstack {
00031 
00032 int gl_size_of_type(int gl_type_code)
00033 {
00034     switch(gl_type_code)
00035     {
00036         case GL_UNSIGNED_BYTE:  return 1;
00037         case GL_BYTE:           return 1;
00038         case GL_UNSIGNED_SHORT: return 2;
00039         case GL_SHORT:          return 2;
00040         case GL_UNSIGNED_INT:   return 4;
00041         case GL_INT:            return 4;
00042         case GL_FLOAT:          return 4;
00043         default: throw false;
00044     }
00045 }
00046 
00047 vertex_attribute::vertex_attribute(
00048     vertex_attribute_usage  usage, 
00049     int                     gl_type_code, 
00050     int                     index,
00051     int                     dimension
00052 )
00053 :   m_usage         (usage)
00054 ,   m_gl_type_code  (gl_type_code)
00055 ,   m_index         (index)
00056 ,   m_dimension     (dimension)
00057 ,   m_offset        (-1)
00058 {
00059 }
00060 
00061 vertex_attribute_usage vertex_attribute::usage() const
00062 {
00063     return m_usage;
00064 }
00065 
00066 int vertex_attribute::gl_type_code() const
00067 {
00068     return m_gl_type_code;
00069 }
00070 
00071 int vertex_attribute::index() const
00072 {
00073     return m_index;
00074 }
00075 
00076 int vertex_attribute::dimension() const
00077 {
00078     return m_dimension;
00079 }
00080 
00081 int vertex_attribute::offset() const
00082 {
00083     return m_offset;
00084 }
00085 
00086 void vertex_attribute::set_offset(int offset)
00087 {
00088     m_offset = offset;
00089 }
00090 
00091 
00092 vertex_format::vertex_format()
00093 :    m_stride(0)
00094 {
00095 }
00096 
00097 vertex_format::~vertex_format()
00098 {
00099 }
00100 
00101 void vertex_format::append(vertex_attribute &attribute)
00102 {
00103     int size = gl_size_of_type(attribute.gl_type_code()) * attribute.dimension();
00104 
00105     attribute.set_offset(m_stride);
00106     m_attributes.push_back(attribute);
00107 
00108     m_stride += size;
00109 }
00110 
00111 bool vertex_format::has_attribute(
00112     vertex_attribute_usage  usage, 
00113     int                     index
00114 ) const
00115 {
00116     for(
00117         std::vector<vertex_attribute>::const_iterator i = m_attributes.begin();
00118         i != m_attributes.end();
00119         ++i
00120     )
00121     {
00122         vertex_attribute const &attribute = *i;
00123 
00124         if(
00125             (attribute.usage() == usage) &&
00126             (attribute.index() == index)
00127         )
00128         {
00129             return true;
00130         }
00131     }
00132 
00133     return false;
00134 }
00135 
00136 vertex_attribute const &vertex_format::attribute(
00137     vertex_attribute_usage  usage, 
00138     int                     index
00139 ) const
00140 {
00141     for(
00142         std::vector<vertex_attribute>::const_iterator i = m_attributes.begin();
00143         i != m_attributes.end();
00144         ++i
00145     )
00146     {
00147         vertex_attribute const &attribute = *i;
00148 
00149         if(
00150             (attribute.usage() == usage) &&
00151             (attribute.index() == index)
00152         )
00153         {
00154             return attribute;
00155         }
00156     }
00157 
00158     throw vertex_attribute_not_found_exception(usage, index);
00159 }
00160 
00161 int vertex_format::stride() const
00162 {
00163     return m_stride;
00164 }
00165 
00166 }
00167 
Generated on Sun Apr 11 12:23:09 2010 for RenderStack by  doxygen 1.6.3