source: trunk/src/gl/gl_vertex_buffer.cc @ 233

Last change on this file since 233 was 191, checked in by epyon, 12 years ago
  • formats - md3_animation works!
File size: 2.6 KB
Line 
1// Copyright (C) 2012-2013 Kornel Kisielewicz
2// This file is part of NV Libraries.
3// For conditions of distribution and use, see copyright notice in nv.hh
4
5#include "nv/gl/gl_vertex_buffer.hh"
6
7#include "nv/lib/gl.hh"
8#include "nv/gl/gl_enum.hh"
9
10using namespace nv;
11
12gl_vertex_buffer::gl_vertex_buffer( buffer_hint hint, size_t size, const void* data )
13        : vertex_buffer( hint, size ), m_name()
14{
15        bind();
16        glBufferData( GL_ARRAY_BUFFER, (GLsizeiptr)m_size, data, buffer_hint_to_enum( m_hint ) );
17        unbind();
18}
19
20void 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
27void gl_vertex_buffer::bind()
28{
29        glBindBuffer( GL_ARRAY_BUFFER, m_name.get_value() );
30}
31
32void gl_vertex_buffer::unbind()
33{
34        glBindBuffer( GL_ARRAY_BUFFER, 0 );
35}
36
37bool gl_vertex_buffer::is_valid() const
38{
39        return m_name.is_valid();
40}
41
42gl_index_buffer::gl_index_buffer( buffer_hint hint, size_t size, const void* data )
43        : index_buffer( hint, size ), m_name()
44{
45        bind();
46        glBufferData( GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)m_size, data, buffer_hint_to_enum( m_hint ) );
47        unbind();
48}
49
50void 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
55void gl_index_buffer::bind()
56{
57        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_name.get_value() );
58}
59
60void gl_index_buffer::unbind()
61{
62        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
63}
64
65bool gl_index_buffer::is_valid() const
66{
67        return m_name.is_valid();
68}
69
70gl_vertex_array::gl_vertex_array()
71{
72
73}
74
75void 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
101void 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 TracBrowser for help on using the repository browser.