Ignore:
Timestamp:
08/07/14 19:06:34 (11 years ago)
Author:
epyon
Message:
  • buffers and vertex_arrays are now handle based
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gl/gl_context.cc

    r301 r302  
    1010#include "nv/gl/gl_device.hh"
    1111#include "nv/gl/gl_program.hh"
    12 #include "nv/gl/gl_vertex_buffer.hh"
    1312
    1413using namespace nv;
     
    3130}
    3231
    33 void nv::gl_context::bind( vertex_buffer* b )
    34 {
    35         GLuint id = static_cast< gl_vertex_buffer* >( b )->glid;
    36         glBindBuffer( GL_ARRAY_BUFFER, id );
    37 }
    38 
    39 void nv::gl_context::bind( index_buffer* b )
    40 {
    41         GLuint id = static_cast< gl_index_buffer* >( b )->glid;
    42         glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, id );
    43 }
    44 
    45 void nv::gl_context::bind( vertex_array* va )
    46 {
    47         for ( vertex_buffer_attribute_map::iterator i = va->m_map.begin();      i != va->m_map.end(); ++i )
    48         {
    49                 uint32 location             = static_cast<uint32>( i->first );
    50                 vertex_buffer_attribute* va = i->second;
    51                 vertex_buffer*           vb = va->get_buffer();
    52                 glEnableVertexAttribArray( location );
    53                 bind( vb );
    54                 glVertexAttribPointer(
    55                         location,
    56                         static_cast<GLint>( va->get_components() ),
    57                         nv::datatype_to_gl_enum( va->get_datatype() ),
    58                         GL_FALSE,
    59                         static_cast<GLsizei>( va->get_stride() ),
    60                         (void*)va->get_offset()
    61                         );
    62                 unbind( vb );
    63         }
    64 
    65         if ( va->m_index )
    66         {
    67                 bind( va->m_index );
     32void nv::gl_context::bind( buffer b )
     33{
     34        const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
     35        if ( info )
     36        {
     37                glBindBuffer( buffer_type_to_enum( info->type ), info->glid );
     38        }
     39}
     40
     41void nv::gl_context::bind( vertex_array va )
     42{
     43        vertex_array_info* info = get_vertex_array_info( va );
     44        if ( info )
     45        {
     46                for ( uint32 i = 0; i < info->count; ++i )
     47                {
     48                        const vertex_buffer_attribute& vba = info->attr[i];
     49                        uint32 location                    = static_cast<uint32>( vba.location );
     50                        glEnableVertexAttribArray( location );
     51                        bind( vba.vbuffer );
     52                        glVertexAttribPointer(
     53                                location,
     54                                static_cast<GLint>( vba.components ),
     55                                nv::datatype_to_gl_enum( vba.dtype ),
     56                                GL_FALSE,
     57                                static_cast<GLsizei>( vba.stride ),
     58                                (void*)vba.offset
     59                                );
     60                        unbind( vba.vbuffer );
     61                }
     62
     63                if ( info->index.is_valid() )
     64                {
     65                        bind( info->index );
     66                }
    6867        }
    6968}
     
    7473}
    7574
    76 void nv::gl_context::unbind( vertex_buffer* )
    77 {
    78         glBindBuffer( GL_ARRAY_BUFFER, 0 );
    79 }
    80 
    81 void nv::gl_context::unbind( index_buffer* )
    82 {
    83         glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
    84 }
    85 
    86 void nv::gl_context::unbind( vertex_array* va )
    87 {
    88         if ( va->m_index )
    89         {
    90                 unbind( va->m_index );
    91         }
    92 
    93         for ( vertex_buffer_attribute_map::iterator i = va->m_map.begin();      i != va->m_map.end(); ++i )
    94         {
    95                 glDisableVertexAttribArray( static_cast<uint32>( i->first ) );
     75void nv::gl_context::unbind( buffer b )
     76{
     77        const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
     78        if ( info )
     79        {
     80                glBindBuffer( buffer_type_to_enum( info->type ), 0 );
     81        }
     82}
     83
     84void nv::gl_context::unbind( vertex_array va )
     85{
     86        vertex_array_info* info = get_vertex_array_info( va );
     87        if ( info )
     88        {
     89                if ( info->index.is_valid() )
     90                {
     91                        unbind( info->index );
     92                }
     93
     94                for ( uint32 i = 0; i < info->count; ++i )
     95                {
     96                        glDisableVertexAttribArray( static_cast<uint32>( info->attr[i].location ) );
     97                }
    9698        }
    9799}
     
    110112}
    111113
    112 void gl_context::update( vertex_buffer* b, const void* data, size_t offset, size_t size )
    113 {
    114         bind( b );
    115         glBufferSubData( GL_ARRAY_BUFFER, (GLintptr)offset, (GLsizeiptr)size, data );
    116 }
    117 
    118 void gl_context::update( index_buffer* b, const void* data, size_t offset, size_t size )
    119 {
    120         bind( b );
    121         glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, (GLintptr)offset, (GLsizeiptr)size, data );
     114void gl_context::update( buffer b, const void* data, size_t offset, size_t size )
     115{
     116        const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
     117        if ( info )
     118        {
     119                bind( b );
     120                glBufferSubData( buffer_type_to_enum( info->type ), (GLintptr)offset, (GLsizeiptr)size, data );
     121        }
    122122}
    123123
     
    483483}
    484484
    485 void gl_context::draw( primitive prim, const render_state& rs, program* p, vertex_array* va, size_t count )
     485void gl_context::draw( primitive prim, const render_state& rs, program* p, vertex_array va, size_t count )
    486486{
    487487        apply_render_state( rs );
    488         if ( count > 0 )
     488        vertex_array_info* info = get_vertex_array_info( va );
     489        if ( count > 0 && info )
    489490        {
    490491                bind( p );
    491492                bind( va );
    492                 if ( va->has_index_buffer() )
    493                 {
    494                         glDrawElements( primitive_to_enum(prim), static_cast<GLsizei>( count ), datatype_to_gl_enum( va->get_index_buffer_type() ), 0 );
     493                if ( info->index.is_valid() )
     494                {
     495                        glDrawElements( primitive_to_enum(prim), static_cast<GLsizei>( count ), datatype_to_gl_enum( info->index_type ), 0 );
    495496                }
    496497                else
Note: See TracChangeset for help on using the changeset viewer.