]> git.tdb.fi Git - libs/gl.git/blob - source/vertexsetup.cpp
547dc0967584f690e7c29f4e82baf564c32d1838
[libs/gl.git] / source / vertexsetup.cpp
1 #include <msp/core/raii.h>
2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_vertex_array_object.h>
4 #include <msp/gl/extensions/arb_vertex_attrib_binding.h>
5 #include <msp/gl/extensions/arb_vertex_buffer_object.h>
6 #include <msp/gl/extensions/arb_vertex_shader.h>
7 #include "buffer.h"
8 #include "gl.h"
9 #include "vertexarray.h"
10 #include "vertexsetup.h"
11
12 namespace Msp {
13 namespace GL {
14
15 VertexSetup::VertexSetup():
16         dirty(0),
17         array(0),
18         index_buffer(0)
19 {
20         static Require req(ARB_vertex_array_object);
21         if(ARB_direct_state_access)
22                 glCreateVertexArrays(1, &id);
23         else
24                 glGenVertexArrays(1, &id);
25 }
26
27 VertexSetup::~VertexSetup()
28 {
29         if(current()==this)
30                 unbind();
31         glDeleteVertexArrays(1, &id);
32 }
33
34 void VertexSetup::set_vertex_array(const VertexArray &a)
35 {
36         array = &a;
37         update(VERTEX_ARRAY);
38 }
39
40 void VertexSetup::set_index_buffer(const Buffer &ibuf)
41 {
42         index_buffer = &ibuf;
43         update(INDEX_BUFFER);
44 }
45
46 void VertexSetup::update(unsigned mask) const
47 {
48         static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
49         if(!direct && current()!=this)
50         {
51                 dirty |= mask;
52                 return;
53         }
54
55         if(mask&VERTEX_ARRAY)
56                 update_vertex_array(direct);
57
58         if(mask&INDEX_BUFFER)
59         {
60                 if(direct)
61                         glVertexArrayElementBuffer(id, index_buffer->get_id());
62                 else
63                         glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
64         }
65 }
66
67 void VertexSetup::update_vertex_array(bool direct) const
68 {
69         Conditional<Bind> bind_vbuf(!direct, array->get_buffer(), ARRAY_BUFFER);
70
71         const VertexFormat &fmt = array->get_format();
72         unsigned stride = get_stride(fmt)*sizeof(float);
73         if(direct)
74                 glVertexArrayVertexBuffer(id, 0, array->get_buffer()->get_id(), 0, stride);
75
76         unsigned offset = 0;
77         for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
78         {
79                 unsigned t = get_component_type(*c);
80                 if(t>=get_component_type(ATTRIB1))
81                         t -= get_component_type(ATTRIB1);
82                 unsigned sz = get_component_size(*c);
83                 if(direct)
84                 {
85                         if(*c==COLOR4_UBYTE)
86                                 glVertexArrayAttribFormat(id, t, 4, GL_UNSIGNED_BYTE, true, offset);
87                         else
88                                 glVertexArrayAttribFormat(id, t, sz, GL_FLOAT, false, offset);
89                         glVertexArrayAttribBinding(id, t, 0);
90                         glEnableVertexArrayAttrib(id, t);
91                 }
92                 else
93                 {
94                         if(*c==COLOR4_UBYTE)
95                                 glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
96                         else
97                                 glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
98                         glEnableVertexAttribArray(t);
99                 }
100                 offset += sz*sizeof(float);
101         }
102 }
103
104 void VertexSetup::bind() const
105 {
106         if(set_current(this))
107         {
108                 glBindVertexArray(id);
109                 if(dirty)
110                 {
111                         update(dirty);
112                         dirty = 0;
113                 }
114         }
115 }
116
117 void VertexSetup::unbind()
118 {
119         if(set_current(0))
120                 glBindVertexArray(0);
121 }
122
123 } // namespace GL
124 } // namespace Msp