Changeset 299 for trunk/src/gl
- Timestamp:
- 08/07/14 10:10:24 (11 years ago)
- Location:
- trunk/src/gl
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/gl/gl_context.cc
r245 r299 8 8 #include "nv/lib/gl.hh" 9 9 #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" 10 13 11 14 using namespace nv; 15 16 void 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 23 void 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 30 void 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 36 void 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 42 void 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 68 void nv::gl_context::unbind( program* ) 69 { 70 glUseProgram( 0 ); 71 } 72 73 void nv::gl_context::unbind( vertex_buffer* ) 74 { 75 glBindBuffer( GL_ARRAY_BUFFER, 0 ); 76 } 77 78 void nv::gl_context::unbind( index_buffer* ) 79 { 80 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); 81 } 82 83 void 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 96 void 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 106 void 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 112 void 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 } 12 117 13 118 void gl_context::clear( const clear_state& cs ) … … 372 477 } 373 478 374 375 479 void gl_context::draw( primitive prim, const render_state& rs, program* p, vertex_array* va, size_t count ) 376 480 { … … 378 482 if ( count > 0 ) 379 483 { 380 p->bind();381 va->bind();484 bind( p ); 485 bind( va ); 382 486 if ( va->has_index_buffer() ) 383 487 { … … 388 492 glDrawArrays( primitive_to_enum(prim), 0, static_cast<GLsizei>( count ) ); 389 493 } 390 va->unbind();391 p->unbind();494 unbind( va ); 495 //unbind( p ); 392 496 } 393 497 } -
trunk/src/gl/gl_device.cc
r292 r299 67 67 vertex_array* gl_device::create_vertex_array() 68 68 { 69 return new gl_vertex_array();69 return new vertex_array(); 70 70 } 71 71 -
trunk/src/gl/gl_program.cc
r237 r299 125 125 load_uniforms(); 126 126 return true; 127 }128 129 void gl_program::bind()130 {131 glUseProgram( m_name.get_value() );132 update_uniforms();133 }134 135 void gl_program::unbind()136 {137 glUseProgram( 0 );138 127 } 139 128 -
trunk/src/gl/gl_texture2d.cc
r292 r299 31 31 if (data) 32 32 { 33 assign(data); 33 glBindTexture( GL_TEXTURE_2D, m_name.get_value() ); 34 glTexImage2D( GL_TEXTURE_2D, 0, (GLint)nv::image_format_to_enum(m_format.format), m_size.x, m_size.y, 0, nv::image_format_to_enum(m_format.format), nv::datatype_to_gl_enum(m_format.type), data ); 35 glBindTexture( GL_TEXTURE_2D, 0 ); 34 36 } 35 37 } 36 38 37 void nv::gl_texture2d::assign( void* data )38 {39 glBindTexture( GL_TEXTURE_2D, m_name.get_value() );40 glTexImage2D( GL_TEXTURE_2D, 0, (GLint)nv::image_format_to_enum(m_format.format), m_size.x, m_size.y, 0, nv::image_format_to_enum(m_format.format), nv::datatype_to_gl_enum(m_format.type), data );41 glBindTexture( GL_TEXTURE_2D, 0 );42 }43 44 void nv::gl_texture2d::bind( size_t slot )45 {46 glActiveTexture( GL_TEXTURE0 + static_cast< GLenum >( slot ) );47 glBindTexture( GL_TEXTURE_2D, m_name.get_value() );48 }49 50 void nv::gl_texture2d::unbind()51 {52 glBindTexture( GL_TEXTURE_2D, 0 );53 }54 55 bool nv::gl_texture2d::is_valid() const56 {57 return m_name.is_valid();58 } -
trunk/src/gl/gl_vertex_buffer.cc
r191 r299 13 13 : vertex_buffer( hint, size ), m_name() 14 14 { 15 bind();15 glBindBuffer( GL_ARRAY_BUFFER, m_name.get_value() ); 16 16 glBufferData( GL_ARRAY_BUFFER, (GLsizeiptr)m_size, data, buffer_hint_to_enum( m_hint ) ); 17 unbind();18 }19 20 void gl_vertex_buffer::update( const void* data, size_t offset, size_t size )21 {22 // IMPORTANT - THIS DOES NOT BIND, SHOULD IT?23 glBufferSubData( GL_ARRAY_BUFFER, (GLintptr)offset, (GLsizeiptr)size, data );24 }25 26 27 void gl_vertex_buffer::bind()28 {29 glBindBuffer( GL_ARRAY_BUFFER, m_name.get_value() );30 }31 32 void gl_vertex_buffer::unbind()33 {34 17 glBindBuffer( GL_ARRAY_BUFFER, 0 ); 35 }36 37 bool gl_vertex_buffer::is_valid() const38 {39 return m_name.is_valid();40 18 } 41 19 … … 43 21 : index_buffer( hint, size ), m_name() 44 22 { 45 bind();23 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_name.get_value() ); 46 24 glBufferData( GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)m_size, data, buffer_hint_to_enum( m_hint ) ); 47 unbind();48 }49 50 void gl_index_buffer::update( const void* data, size_t offset, size_t size )51 {52 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, (GLintptr)offset, (GLsizeiptr)size, data );53 }54 55 void gl_index_buffer::bind()56 {57 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_name.get_value() );58 }59 60 void gl_index_buffer::unbind()61 {62 25 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); 63 26 } 64 27 65 bool gl_index_buffer::is_valid() const66 {67 return m_name.is_valid();68 }69 70 gl_vertex_array::gl_vertex_array()71 {72 73 }74 75 void gl_vertex_array::bind()76 {77 for ( vertex_buffer_attribute_map::iterator i = m_map.begin(); i != m_map.end(); ++i )78 {79 uint32 location = static_cast<uint32>( i->first );80 vertex_buffer_attribute* va = i->second;81 vertex_buffer* vb = va->get_buffer();82 glEnableVertexAttribArray( location );83 vb->bind();84 glVertexAttribPointer(85 location,86 static_cast<GLint>( va->get_components() ),87 nv::datatype_to_gl_enum( va->get_datatype() ),88 GL_FALSE,89 static_cast<GLsizei>( va->get_stride() ),90 (void*)va->get_offset()91 );92 vb->unbind();93 }94 95 if ( m_index )96 {97 m_index->bind();98 }99 }100 101 void gl_vertex_array::unbind()102 {103 if ( m_index )104 {105 m_index->unbind();106 }107 108 for ( vertex_buffer_attribute_map::iterator i = m_map.begin(); i != m_map.end(); ++i )109 {110 glDisableVertexAttribArray( static_cast<uint32>( i->first ) );111 }112 }
Note: See TracChangeset
for help on using the changeset viewer.