renderstack::buffer Class Reference

wrapper for gl vertex and index buffers

#include <buffer.hpp>

Public Member Functions

 buffer (int gl_target, int gl_usage)
 buffer (int gl_target, int gl_usage, int gl_index_type, int gl_mode)
 ~buffer ()
void use () const
class vertex_formatvertex_format ()
class vertex_format const & vertex_format () const
int count () const
int gl_target () const
int gl_usage () const
unsigned int gl_buffer_object () const
int gl_index_type () const
int gl_mode () const
void begin_edit ()
void end_edit (uint32 end_index)
void set_index (uint32 index)
void put_float (vertex_attribute const &attribute, float x)
void put_vec2 (vertex_attribute const &attribute, vec2 const &v)
void put_vec2 (vertex_attribute const &attribute, float x, float y)
void put_vec3 (vertex_attribute const &attribute, vec3 const &v)
void put_vec3 (vertex_attribute const &attribute, float x, float y, float z)
void put_vec4 (vertex_attribute const &attribute, vec4 const &v)
void put_vec4 (vertex_attribute const &attribute, float x, float y, float z, float w)
void put_uint16 (vertex_attribute const &attribute, uint16 x)
void put_uint32 (vertex_attribute const &attribute, uint32 x)
void put_position (vec3 const &v, int index=0)
void put_position (float x, float y, float z, int index=0)
void put_normal (vec3 const &v, int index=0)
void put_normal (float x, float y, float z, int index=0)
void put_texcoord (vec2 const &v, int index=0)
void put_texcoord (float s, float t, int index=0)
void put_index (uint32 index_location, uint32 index)
void point (uint32 index0)
void line (uint32 index0, uint32 index1)
void triangle (uint32 index0, uint32 index1, uint32 index2)
void quad (uint32 index0, uint32 index1, uint32 index2, uint32 index3)

Detailed Description

Definition at line 67 of file buffer.hpp.


Constructor & Destructor Documentation

renderstack::buffer::buffer ( int  gl_target,
int  gl_usage 
)

Definition at line 37 of file buffer.cpp.

00041 :   m_gl_buffer_object  ((unsigned int)(~0))
00042 ,   m_gl_target         (gl_target)
00043 ,   m_gl_usage          (gl_usage)
00044 {
00045     ::rs_gl_gen_buffers(1, &m_gl_buffer_object);
00046 }

renderstack::buffer::buffer ( int  gl_target,
int  gl_usage,
int  gl_index_type,
int  gl_mode 
)

Definition at line 48 of file buffer.cpp.

00054 :   m_gl_buffer_object  ((unsigned int)(~0))
00055 ,   m_gl_target         (gl_target)
00056 ,   m_gl_usage          (gl_usage)
00057 ,   m_gl_index_type     (gl_index_type)
00058 ,   m_gl_mode           (gl_mode)
00059 {
00060     ::rs_gl_gen_buffers(1, &m_gl_buffer_object);
00061 }

renderstack::buffer::~buffer (  ) 

Definition at line 63 of file buffer.cpp.

00064 {
00065     ::GLboolean is_buffer = ::rs_gl_is_buffer(m_gl_buffer_object);
00066     if(is_buffer != GL_FALSE)
00067     {
00068         ::rs_gl_delete_buffers(1, &m_gl_buffer_object);
00069     }
00070     else
00071     {
00072         ::rs_log("glIsBuffer() == GL_FALSE\n");
00073     }
00074 }


Member Function Documentation

void renderstack::buffer::use (  )  const

Definition at line 128 of file buffer.cpp.

00129 {
00130     ::rs_gl_bind_buffer(
00131         gl_target(),
00132         gl_buffer_object()
00133     );
00134 }

class vertex_format& renderstack::buffer::vertex_format (  )  [inline]

Definition at line 76 of file buffer.hpp.

00076 { return m_vertex_format; }

class vertex_format const& renderstack::buffer::vertex_format (  )  const [inline]

Definition at line 77 of file buffer.hpp.

00077 { return m_vertex_format; }

int renderstack::buffer::count (  )  const [inline]

Definition at line 78 of file buffer.hpp.

00078 { return m_count; }

int renderstack::buffer::gl_target (  )  const [inline]

Definition at line 79 of file buffer.hpp.

00079 { return m_gl_target; }

int renderstack::buffer::gl_usage (  )  const [inline]

Definition at line 80 of file buffer.hpp.

00080 { return m_gl_usage; }

unsigned int renderstack::buffer::gl_buffer_object (  )  const [inline]

Definition at line 81 of file buffer.hpp.

00081 { return m_gl_buffer_object; }

int renderstack::buffer::gl_index_type (  )  const [inline]

Definition at line 82 of file buffer.hpp.

00082 { return m_gl_index_type; }

int renderstack::buffer::gl_mode (  )  const [inline]

Definition at line 83 of file buffer.hpp.

00083 { return m_gl_mode; }

void renderstack::buffer::begin_edit (  ) 

Definition at line 76 of file buffer.cpp.

00077 {
00078     if(m_gl_target == GL_ARRAY_BUFFER)
00079     {
00080         m_stride = vertex_format().stride();
00081     }
00082     else if(m_gl_target == GL_ELEMENT_ARRAY_BUFFER)
00083     {
00084         m_stride = gl_size_of_type(gl_index_type());
00085     }
00086     else
00087     {
00088         throw invalid_buffer_gl_target_exception(m_gl_target);
00089     }
00090 
00091     if(m_stride == 0)
00092     {
00093         throw invalid_buffer_stride_exception();
00094     }
00095 
00096     m_current_index = 0;
00097 }

void renderstack::buffer::end_edit ( uint32  end_index  ) 

Definition at line 99 of file buffer.cpp.

00100 {
00101     uint32 current_size = end_index * m_stride;
00102 
00103     if(current_size == 0)
00104     {
00105         return;
00106     }
00107 
00108     m_count = end_index;
00109     m_size  = current_size;
00110 
00111     ::rs_gl_bind_buffer(
00112         gl_target(), 
00113         gl_buffer_object()
00114     );
00115 
00116     ::rs_gl_buffer_data(
00117         gl_target(),   /*  gl target       */ 
00118         current_size,  /*  length          */ 
00119         &m_data[0],    /*  data pointer    */ 
00120         gl_usage()     /*  gl usage        */ 
00121     );
00122 
00123     /*  This will free the memory  */ 
00124     std::vector<unsigned char> dummy;
00125     m_data = dummy;
00126 }

void renderstack::buffer::set_index ( uint32  index  ) 

Definition at line 137 of file buffer.cpp.

00138 {
00139     m_current_index = index;
00140 
00141     size_t offset = m_current_index * m_stride;
00142 
00143     /*  writing for example triangle writes 2 indices past current index  */ 
00144     if(offset + (4 * m_stride) > m_data.size())
00145     {
00146         size_t adjusted_capacity = m_data.size();
00147 
00148         /*  start with initial 32 kB  */ 
00149         if(adjusted_capacity == 0)
00150         {
00151             adjusted_capacity = 32 * 1024;
00152         }
00153 
00154         while(offset + (4 * m_stride) > adjusted_capacity)
00155         {
00156             adjusted_capacity *= 2;
00157         }
00158         if(adjusted_capacity != m_data.size())
00159         {
00160             m_data.resize(adjusted_capacity);
00161         }
00162     }
00163 }

void renderstack::buffer::put_float ( vertex_attribute const &  attribute,
float  x 
)

Definition at line 165 of file buffer.cpp.

00166 {
00167     size_t  offset   = m_current_index * m_stride + attribute.offset();
00168     float   *pointer = reinterpret_cast<float*>(&m_data[offset]);
00169 
00170     pointer[0] = x;
00171 }

void renderstack::buffer::put_vec2 ( vertex_attribute const &  attribute,
vec2 const &  v 
)

Definition at line 173 of file buffer.cpp.

00174 {
00175     size_t  offset   = m_current_index * m_stride + attribute.offset();
00176     float   *pointer = reinterpret_cast<float*>(&m_data[offset]);
00177 
00178     pointer[0] = v.x;
00179     pointer[1] = v.y;
00180 }

void renderstack::buffer::put_vec2 ( vertex_attribute const &  attribute,
float  x,
float  y 
)

Definition at line 182 of file buffer.cpp.

00183 {
00184     size_t  offset   = m_current_index * m_stride + attribute.offset();
00185     float   *pointer = reinterpret_cast<float*>(&m_data[offset]);
00186 
00187     pointer[0] = x;
00188     pointer[1] = y;
00189 }

void renderstack::buffer::put_vec3 ( vertex_attribute const &  attribute,
vec3 const &  v 
)

Definition at line 191 of file buffer.cpp.

00192 {
00193     size_t  offset   = m_current_index * m_stride + attribute.offset();
00194     float   *pointer = reinterpret_cast<float*>(&m_data[offset]);
00195 
00196     pointer[0] = v.x;
00197     pointer[1] = v.y;
00198     pointer[2] = v.z;
00199 }

void renderstack::buffer::put_vec3 ( vertex_attribute const &  attribute,
float  x,
float  y,
float  z 
)

Definition at line 201 of file buffer.cpp.

00202 {
00203     size_t  offset   = m_current_index * m_stride + attribute.offset();
00204     float   *pointer = reinterpret_cast<float*>(&m_data[offset]);
00205 
00206     pointer[0] = x;
00207     pointer[1] = y;
00208     pointer[2] = z;
00209 }

void renderstack::buffer::put_vec4 ( vertex_attribute const &  attribute,
vec4 const &  v 
)

Definition at line 211 of file buffer.cpp.

00212 {
00213     size_t  offset   = m_current_index * m_stride + attribute.offset();
00214     float   *pointer = reinterpret_cast<float*>(&m_data[offset]);
00215 
00216     pointer[0] = v.x;
00217     pointer[1] = v.y;
00218     pointer[2] = v.z;
00219     pointer[3] = v.w;
00220 }

void renderstack::buffer::put_vec4 ( vertex_attribute const &  attribute,
float  x,
float  y,
float  z,
float  w 
)

Definition at line 222 of file buffer.cpp.

00223 {
00224     size_t  offset   = m_current_index * m_stride + attribute.offset();
00225     float   *pointer = reinterpret_cast<float*>(&m_data[offset]);
00226 
00227     pointer[0] = x;
00228     pointer[1] = y;
00229     pointer[2] = z;
00230     pointer[3] = w;
00231 }

void renderstack::buffer::put_uint16 ( vertex_attribute const &  attribute,
uint16  x 
)

Definition at line 233 of file buffer.cpp.

00234 {
00235     size_t  offset   = m_current_index * m_stride + attribute.offset();
00236     uint16  *pointer = reinterpret_cast<uint16*>(&m_data[offset]);
00237 
00238     pointer[0] = x;
00239 }

void renderstack::buffer::put_uint32 ( vertex_attribute const &  attribute,
uint32  x 
)

Definition at line 241 of file buffer.cpp.

00242 {
00243     size_t  offset   = m_current_index * m_stride + attribute.offset();
00244     uint32  *pointer = reinterpret_cast<uint32*>(&m_data[offset]);
00245 
00246     pointer[0] = x;
00247 }

void renderstack::buffer::put_position ( vec3 const &  v,
int  index = 0 
)

Definition at line 249 of file buffer.cpp.

00250 {
00251     vertex_attribute const &attribute = vertex_format().attribute(
00252         vertex_attribute_usage_position,
00253         index
00254     );
00255 
00256     put_vec3(attribute, v);
00257 }

void renderstack::buffer::put_position ( float  x,
float  y,
float  z,
int  index = 0 
)

Definition at line 259 of file buffer.cpp.

00260 {
00261     vertex_attribute const &attribute = vertex_format().attribute(
00262         vertex_attribute_usage_position,
00263         index
00264     );
00265 
00266     put_vec3(attribute, x, y, z);
00267 }

void renderstack::buffer::put_normal ( vec3 const &  v,
int  index = 0 
)

Definition at line 269 of file buffer.cpp.

00270 {
00271     vertex_attribute const &attribute = vertex_format().attribute(
00272         vertex_attribute_usage_normal,
00273         index
00274     );
00275 
00276     put_vec3(attribute, v);
00277 }

void renderstack::buffer::put_normal ( float  x,
float  y,
float  z,
int  index = 0 
)

Definition at line 279 of file buffer.cpp.

00280 {
00281     vertex_attribute const &attribute = vertex_format().attribute(
00282         vertex_attribute_usage_normal,
00283         index
00284     );
00285 
00286     put_vec3(attribute, x, y, z);
00287 }

void renderstack::buffer::put_texcoord ( vec2 const &  v,
int  index = 0 
)

Definition at line 289 of file buffer.cpp.

00290 {
00291     vertex_attribute const &attribute = vertex_format().attribute(
00292         vertex_attribute_usage_texcoord,
00293         index
00294     );
00295 
00296     put_vec2(attribute, v);
00297 }

void renderstack::buffer::put_texcoord ( float  s,
float  t,
int  index = 0 
)

Definition at line 299 of file buffer.cpp.

00300 {
00301     vertex_attribute const &attribute = vertex_format().attribute(
00302         vertex_attribute_usage_texcoord,
00303         index
00304     );
00305 
00306     put_vec2(attribute, s, t);
00307 }

void renderstack::buffer::put_index ( uint32  index_location,
uint32  index 
)

Definition at line 309 of file buffer.cpp.

00310 {
00311     switch(m_stride)
00312     {
00313         case 1:
00314         {
00315             size_t    offset   = index_location * m_stride;
00316             ::GLubyte *pointer = reinterpret_cast<::GLubyte*>(&m_data[offset]);
00317 
00318             if(
00319                 (index_value > 0xff) 
00320             )
00321             {
00322                 throw index_out_of_range_exception();
00323             }
00324 
00325             pointer[0] = static_cast<::GLubyte>(index_value & 0xff);
00326 
00327             break;
00328         }
00329         case 2:
00330         {
00331             size_t     offset   = index_location * m_stride;
00332             ::GLushort *pointer = reinterpret_cast<::GLushort*>(&m_data[offset]);
00333 
00334             if(
00335                 (index_value > 0xffff) 
00336             )
00337             {
00338                 throw index_out_of_range_exception();
00339             }
00340 
00341             pointer[0] = static_cast<::GLushort>(index_value & 0xffff);
00342 
00343             break;
00344         }
00345         case 4:
00346         {
00347             size_t   offset   = index_location * m_stride;
00348             ::GLuint *pointer = reinterpret_cast<::GLuint*>(&m_data[offset]);
00349 
00350             pointer[0] = index_value;
00351 
00352             break;
00353         }
00354         default:
00355         {
00356             throw invalid_buffer_stride_exception();
00357         }
00358     }
00359 }

void renderstack::buffer::point ( uint32  index0  ) 

Definition at line 361 of file buffer.cpp.

00362 {
00363     put_index(m_current_index, index0);
00364 }

void renderstack::buffer::line ( uint32  index0,
uint32  index1 
)

Definition at line 366 of file buffer.cpp.

00367 {
00368     put_index(m_current_index    , index0);
00369     put_index(m_current_index + 1, index1);
00370 }

void renderstack::buffer::triangle ( uint32  index0,
uint32  index1,
uint32  index2 
)

Definition at line 372 of file buffer.cpp.

00373 {
00374     put_index(m_current_index    , index0);
00375     put_index(m_current_index + 1, index1);
00376     put_index(m_current_index + 2, index2);
00377 }

void renderstack::buffer::quad ( uint32  index0,
uint32  index1,
uint32  index2,
uint32  index3 
)

Definition at line 379 of file buffer.cpp.

00380 {
00381     put_index(m_current_index    , index0);
00382     put_index(m_current_index + 1, index1);
00383     put_index(m_current_index + 2, index2);
00384     put_index(m_current_index + 3, index0);
00385     put_index(m_current_index + 4, index2);
00386     put_index(m_current_index + 5, index3);
00387 }


The documentation for this class was generated from the following files:
Generated on Sun Apr 11 12:23:11 2010 for RenderStack by  doxygen 1.6.3