Ignore:
Timestamp:
08/07/14 10:10:24 (11 years ago)
Author:
epyon
Message:
  • all bind and update function for graphics objects are done via context (will simplify directx device, and allow for handles instead of objects)
File:
1 edited

Legend:

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

    r245 r299  
    88#include "nv/lib/gl.hh"
    99#include "nv/lib/sdl.hh"
     10#include "nv/gl/gl_texture2d.hh"
     11#include "nv/gl/gl_program.hh"
     12#include "nv/gl/gl_vertex_buffer.hh"
    1013
    1114using namespace nv;
     15
     16void gl_context::bind( texture2d* texture, texture_slot slot )
     17{
     18        GLuint id = static_cast< gl_texture2d* >( texture )->m_name.get_value();
     19        glActiveTexture( GL_TEXTURE0 + static_cast< GLenum >( slot ) );
     20        glBindTexture( GL_TEXTURE_2D, id );
     21}
     22
     23void nv::gl_context::bind( program* p )
     24{
     25        gl_program* glp = static_cast< gl_program* >( p );
     26        glUseProgram( glp->m_name.get_value() );
     27        glp->update_uniforms();
     28}
     29
     30void nv::gl_context::bind( vertex_buffer* b )
     31{
     32        GLuint id = static_cast< gl_vertex_buffer* >( b )->m_name.get_value();
     33        glBindBuffer( GL_ARRAY_BUFFER, id );
     34}
     35
     36void nv::gl_context::bind( index_buffer* b )
     37{
     38        GLuint id = static_cast< gl_index_buffer* >( b )->m_name.get_value();
     39        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, id );
     40}
     41
     42void nv::gl_context::bind( vertex_array* va )
     43{
     44        for ( vertex_buffer_attribute_map::iterator i = va->m_map.begin();      i != va->m_map.end(); ++i )
     45        {
     46                uint32 location             = static_cast<uint32>( i->first );
     47                vertex_buffer_attribute* va = i->second;
     48                vertex_buffer*           vb = va->get_buffer();
     49                glEnableVertexAttribArray( location );
     50                bind( vb );
     51                glVertexAttribPointer(
     52                        location,
     53                        static_cast<GLint>( va->get_components() ),
     54                        nv::datatype_to_gl_enum( va->get_datatype() ),
     55                        GL_FALSE,
     56                        static_cast<GLsizei>( va->get_stride() ),
     57                        (void*)va->get_offset()
     58                        );
     59                unbind( vb );
     60        }
     61
     62        if ( va->m_index )
     63        {
     64                bind( va->m_index );
     65        }
     66}
     67
     68void nv::gl_context::unbind( program* )
     69{
     70        glUseProgram( 0 );
     71}
     72
     73void nv::gl_context::unbind( vertex_buffer* )
     74{
     75        glBindBuffer( GL_ARRAY_BUFFER, 0 );
     76}
     77
     78void nv::gl_context::unbind( index_buffer* )
     79{
     80        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
     81}
     82
     83void nv::gl_context::unbind( vertex_array* va )
     84{
     85        if ( va->m_index )
     86        {
     87                unbind( va->m_index );
     88        }
     89
     90        for ( vertex_buffer_attribute_map::iterator i = va->m_map.begin();      i != va->m_map.end(); ++i )
     91        {
     92                glDisableVertexAttribArray( static_cast<uint32>( i->first ) );
     93        }
     94}
     95
     96void gl_context::update( texture2d* texture, void* data )
     97{
     98        GLuint id = static_cast< gl_texture2d* >( texture )->m_name.get_value();
     99        image_format format = texture->get_format();
     100        ivec2        size   = texture->get_size();
     101
     102        glBindTexture( GL_TEXTURE_2D, id );
     103        glTexImage2D( GL_TEXTURE_2D, 0, (GLint)nv::image_format_to_enum(format.format), size.x, size.y, 0, nv::image_format_to_enum(format.format), nv::datatype_to_gl_enum(format.type), data );
     104}
     105
     106void gl_context::update( vertex_buffer* b, const void* data, size_t offset, size_t size )
     107{
     108        bind( b );
     109        glBufferSubData( GL_ARRAY_BUFFER, (GLintptr)offset, (GLsizeiptr)size, data );
     110}
     111
     112void gl_context::update( index_buffer* b, const void* data, size_t offset, size_t size )
     113{
     114        bind( b );
     115        glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, (GLintptr)offset, (GLsizeiptr)size, data );
     116}
    12117
    13118void gl_context::clear( const clear_state& cs )
     
    372477}
    373478
    374 
    375479void gl_context::draw( primitive prim, const render_state& rs, program* p, vertex_array* va, size_t count )
    376480{
     
    378482        if ( count > 0 )
    379483        {
    380                 p->bind();
    381                 va->bind();
     484                bind( p );
     485                bind( va );
    382486                if ( va->has_index_buffer() )
    383487                {
     
    388492                        glDrawArrays( primitive_to_enum(prim), 0, static_cast<GLsizei>( count ) );
    389493                }
    390                 va->unbind();
    391                 p->unbind();
     494                unbind( va );
     495                //unbind( p );
    392496        }
    393497}
Note: See TracChangeset for help on using the changeset viewer.