mesh.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/mesh.hpp"
00028 #include "renderstack/program.hpp"
00029 
00030 namespace renderstack {
00031 
00032 buffer *mesh::index_buffer(enum mesh_mode mode)
00033 {
00034     std::map<
00035         enum mesh_mode, 
00036         std::tr1::shared_ptr<buffer>
00037     >::const_iterator i = m_index_buffers.find(mode);
00038 
00039     if(i != m_index_buffers.end())
00040     {
00041         std::tr1::shared_ptr<buffer> buffer = (*i).second;
00042         return buffer.get();
00043     }
00044 
00045     throw index_buffer_not_found_exception(mode);
00046 }
00047 
00048 buffer *mesh::find_or_create_index_buffer(
00049     enum mesh_mode  mode,
00050     int             gl_target, 
00051     int             gl_usage, 
00052     int             gl_index_type, 
00053     int             gl_mode
00054 )
00055 {
00056     std::map<
00057         enum mesh_mode, 
00058         std::tr1::shared_ptr<buffer>
00059     >::const_iterator i = m_index_buffers.find(mode);
00060 
00061     if(i != m_index_buffers.end())
00062     {
00063         std::tr1::shared_ptr<buffer> buffer = (*i).second;
00064         return buffer.get();
00065     }
00066 
00067     std::tr1::shared_ptr<buffer> b(
00068         new buffer(gl_target, gl_usage, gl_index_type, gl_mode)
00069     );
00070     m_index_buffers.insert(
00071         std::make_pair(
00072             mode, 
00073             b
00074         )
00075     );
00076     return b.get();
00077 }
00078 
00079 void mesh::apply_attributes(class program const &program, bool enable)
00080 {
00081     int gl_program_object = program.gl_program_object();
00082 
00083     std::map<int, std::tr1::shared_ptr<attribute_bindings> >::const_iterator i = m_attribute_bindings.find(gl_program_object);
00084     if(i != m_attribute_bindings.end())
00085     {
00086         std::tr1::shared_ptr<attribute_bindings> bindings = (*i).second;
00087         bindings->apply(enable);
00088         m_enabled_attribute_bindings = bindings.get();
00089     }
00090     else
00091     {
00092         std::tr1::shared_ptr<attribute_bindings> bindings(
00093             new attribute_bindings()
00094         );
00095         m_attribute_bindings.insert(
00096             std::make_pair(
00097                 gl_program_object,
00098                 bindings
00099             )
00100         );
00101         program.attribute_mappings().bind_attributes(
00102             *bindings,
00103             program,
00104             *this
00105         );
00106         bindings->apply(enable);
00107         m_enabled_attribute_bindings = bindings.get();
00108     }
00109 }
00110 
00111 void mesh::begin(
00112     class program const &program,
00113     enum mesh_mode      mode
00114 )
00115 {
00116     vertex_buffer()->use();
00117     if(mode == mesh_mode_not_set)
00118     {
00119         mode = program.mesh_mode();
00120     }
00121     index_buffer(mode)->use();
00122     apply_attributes(program, true);
00123 }
00124 
00125 void mesh::end()
00126 {
00127     if(m_enabled_attribute_bindings != NULL)
00128     {
00129         m_enabled_attribute_bindings->apply(false);
00130         m_enabled_attribute_bindings = NULL;
00131     }
00132 }
00133 
00134 }
00135 
Generated on Sun Apr 11 12:23:08 2010 for RenderStack by  doxygen 1.6.3