mesh.hpp
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00027 #ifndef MESH_HPP
00028 #define MESH_HPP
00029 
00030 #include "renderstack/rs.h"
00031 #include "renderstack/buffer.hpp"
00032 #include "renderstack/attribute_bindings.hpp"
00033 #include <memory>
00034 #include <map>
00035 #include <exception>
00036 
00037 namespace renderstack {
00038 
00039 enum mesh_mode
00040 {
00041     mesh_mode_not_set = 0,
00042     mesh_mode_polygon_fill,
00043     mesh_mode_edge_lines,
00044     mesh_mode_corner_points,
00045     mesh_mode_corner_normals,
00046     mesh_mode_polygon_centroids,
00047     mesh_mode_count
00048 };
00049 
00050 class program;
00051 
00052 class index_buffer_not_found_exception
00053 :   public std::exception
00054 {
00055 public:
00056     index_buffer_not_found_exception(enum mesh_mode mode)
00057     :   m_mesh_mode(mode)
00058     {
00059     }
00060     enum mesh_mode mesh_mode() const { return m_mesh_mode; }
00061 
00062 private:
00063     enum mesh_mode m_mesh_mode;
00064 };
00065 
00067 class mesh
00068 {
00069 public:
00070     mesh(int draw_mode = GL_STATIC_DRAW)
00071     :   m_enabled_attribute_bindings(NULL)
00072     {
00073         m_vertex_buffer.reset(
00074             new buffer(GL_ARRAY_BUFFER, draw_mode)
00075         );
00076     }
00077     virtual ~mesh()
00078     {
00079     }
00080 
00081     void begin(
00082         class program const &program, 
00083         enum mesh_mode      mode = mesh_mode_not_set
00084     );
00085     void end();
00086 
00087     buffer  * const vertex_buffer  () const { return m_vertex_buffer.get(); } 
00088     buffer  *vertex_buffer  (){ return m_vertex_buffer.get(); } 
00089     buffer  *index_buffer   (enum mesh_mode mode);
00090     buffer  *find_or_create_index_buffer(
00091         enum mesh_mode  mode,
00092         int             gl_target, 
00093         int             gl_usage, 
00094         int             gl_index_type, 
00095         int             gl_mode
00096     );
00097 
00098 protected:
00099     void use(enum mesh_mode mode);
00100     void apply_attributes(class program const &program, bool enable);
00101 
00102 private:
00103     std::tr1::shared_ptr<buffer>                                m_vertex_buffer;
00104     std::map<enum mesh_mode, std::tr1::shared_ptr<buffer> >     m_index_buffers;
00105     std::map<int, std::tr1::shared_ptr<attribute_bindings> >    m_attribute_bindings;
00106     attribute_bindings                                          *m_enabled_attribute_bindings;
00107 };
00108 
00109 }
00110 
00111 #endif
00112