-
Notifications
You must be signed in to change notification settings - Fork 0
/
vertex_array.h
92 lines (71 loc) · 1.51 KB
/
vertex_array.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#pragma once
#include <GL/glew.h>
#include <vector>
namespace gge {
struct vertex_flat
{
vertex_flat(float x, float y)
: pos { x, y }
{ }
GLfloat pos[2];
};
struct vertex_texuv
{
vertex_texuv(float x, float y, float u, float v)
: pos { x, y }
, texuv { u, v }
{ }
GLfloat pos[2];
GLfloat texuv[2];
};
namespace detail {
// RAII <3 <3 <3
template <typename VertexType>
struct client_state;
template <>
struct client_state<vertex_flat>
{
client_state(const vertex_flat *verts)
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, sizeof *verts, verts->pos);
}
~client_state()
{
glDisableClientState(GL_VERTEX_ARRAY);
}
};
template <>
struct client_state<vertex_texuv>
{
client_state(const vertex_texuv *verts)
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, sizeof *verts, verts->pos);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof *verts, verts->texuv);
}
~client_state()
{
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
};
} // detail
template <class Vertex>
class vertex_array : public std::vector<Vertex>
{
public:
vertex_array() = default;
vertex_array(std::initializer_list<Vertex> l)
: std::vector<Vertex>(l)
{ }
void draw(GLenum mode) const
{
detail::client_state<Vertex> state(&this->front());
glDrawArrays(mode, 0, this->size());
}
};
using vertex_array_flat = vertex_array<vertex_flat>;
using vertex_array_texuv = vertex_array<vertex_texuv>;
} // gge