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

Last change on this file since 64 was 44, checked in by epyon, 12 years ago
  • context bugfixes, force apply state at creation and apply render state
  • window creates context
  • index buffer
  • vertex arrays (simulation of GL 3 functionality)
  • bugfixes
File size: 2.3 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, int size, void* data )
13        : vertex_buffer( hint, size ), m_name()
14{
15        if (data)
16        {
17                assign( data );
18        }
19}
20
21void gl_vertex_buffer::assign( void* data )
22{
23        glBindBuffer( GL_ARRAY_BUFFER, m_name.get_value() );
24        glBufferData( GL_ARRAY_BUFFER, m_size, data, buffer_hint_to_enum( m_hint ) );
25        glBindBuffer( GL_ARRAY_BUFFER, 0);
26}
27
28void gl_vertex_buffer::bind()
29{
30        glBindBuffer( GL_ARRAY_BUFFER, m_name.get_value() );
31}
32
33void gl_vertex_buffer::unbind()
34{
35        glBindBuffer( GL_ARRAY_BUFFER, 0 );
36}
37
38bool gl_vertex_buffer::is_valid() const
39{
40        return m_name.is_valid();
41}
42
43gl_index_buffer::gl_index_buffer( buffer_hint hint, int size, void* data )
44        : index_buffer( hint, size ), m_name()
45{
46        if (data)
47        {
48                assign( data );
49        }
50}
51
52void gl_index_buffer::assign( void* data )
53{
54        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_name.get_value() );
55        glBufferData( GL_ELEMENT_ARRAY_BUFFER, m_size, data, buffer_hint_to_enum( m_hint ) );
56        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0);
57}
58
59void gl_index_buffer::bind()
60{
61        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_name.get_value() );
62}
63
64void gl_index_buffer::unbind()
65{
66        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
67}
68
69bool gl_index_buffer::is_valid() const
70{
71        return m_name.is_valid();
72}
73
74gl_vertex_array::gl_vertex_array()
75{
76
77}
78
79void gl_vertex_array::bind()
80{
81        for ( vertex_buffer_attribute_map::iterator i = m_map.begin();  i != m_map.end(); ++i )
82        {
83                int location                = i->first;
84                vertex_buffer_attribute* va = i->second;
85                vertex_buffer*           vb = va->get_buffer();
86                glEnableVertexAttribArray( location );
87                vb->bind();
88                glVertexAttribPointer(
89                        location,
90                        va->get_components(),
91                        nv::type_to_gl_enum( va->get_datatype() ),
92                        GL_FALSE,
93                        va->get_stride(),
94                        (void*)va->get_offset()
95                        );
96                vb->unbind();
97        }
98
99}
100
101void gl_vertex_array::unbind()
102{
103        for ( vertex_buffer_attribute_map::iterator i = m_map.begin();  i != m_map.end(); ++i )
104        {
105                glDisableVertexAttribArray( i->first );
106        }
107}
Note: See TracBrowser for help on using the repository browser.