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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#include "Mesh.hpp"
namespace renderer {
Mesh::Mesh () : mVAO( 0 ), mVBO( 0 ), mEBO( 0 ), mVertexCount( 0 ), mIndexCount( 0 ), mMode( GL_TRIANGLES ) {}
Mesh::~Mesh () {
if( mVBO ) {
glDeleteBuffers( 1, &mVBO );
}
if( mEBO ) {
glDeleteBuffers( 1, &mEBO );
}
if( mVAO ) {
glDeleteVertexArrays( 1, &mVAO );
}
}
bool Mesh::createFromPositions ( const std::vector<float>& positions, bool lines ) {
if( positions.empty() ) {
return false;
}
mMode = lines ? GL_LINES : GL_TRIANGLES;
mVertexCount = static_cast<GLsizei>( positions.size() / 3 );
glGenVertexArrays( 1, &mVAO );
glGenBuffers( 1, &mVBO );
glBindVertexArray( mVAO );
glBindBuffer( GL_ARRAY_BUFFER, mVBO );
glBufferData( GL_ARRAY_BUFFER, positions.size() * sizeof( float ), positions.data(), GL_STATIC_DRAW );
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof( float ), (void*)0 );
glBindVertexArray( 0 );
return true;
}
bool Mesh::createFromPosTex ( const std::vector<float>& data ) {
if( data.empty() )
return false;
const size_t strideFloats = 5;
mMode = GL_TRIANGLES;
mVertexCount = static_cast<GLsizei>( data.size() / strideFloats );
glGenVertexArrays( 1, &mVAO );
glGenBuffers( 1, &mVBO );
glBindVertexArray( mVAO );
glBindBuffer( GL_ARRAY_BUFFER, mVBO );
glBufferData( GL_ARRAY_BUFFER, data.size() * sizeof( float ), data.data(), GL_STATIC_DRAW );
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, strideFloats * sizeof( float ), (void*)0 );
glEnableVertexAttribArray( 1 );
glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, strideFloats * sizeof( float ),
(void*)( 3 * sizeof( float ) ) );
glBindVertexArray( 0 );
return true;
}
bool Mesh::createFromPosTexNormal ( const std::vector<float>& data ) {
if( data.empty() )
return false;
const size_t strideFloats = 8;
mMode = GL_TRIANGLES;
mVertexCount = static_cast<GLsizei>( data.size() / strideFloats );
glGenVertexArrays( 1, &mVAO );
glGenBuffers( 1, &mVBO );
glBindVertexArray( mVAO );
glBindBuffer( GL_ARRAY_BUFFER, mVBO );
glBufferData( GL_ARRAY_BUFFER, data.size() * sizeof( float ), data.data(), GL_STATIC_DRAW );
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, strideFloats * sizeof( float ), (void*)0 );
glEnableVertexAttribArray( 1 );
glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, strideFloats * sizeof( float ),
(void*)( 3 * sizeof( float ) ) );
glEnableVertexAttribArray( 2 );
glVertexAttribPointer( 2, 3, GL_FLOAT, GL_FALSE, strideFloats * sizeof( float ),
(void*)( 5 * sizeof( float ) ) );
glBindVertexArray( 0 );
return true;
}
bool Mesh::createFromPosTexNormalIndexed ( const std::vector<float>& data,
const std::vector<unsigned int>& indices ) {
if( data.empty() || indices.empty() )
return false;
const size_t strideFloats = 8;
mMode = GL_TRIANGLES;
mVertexCount = static_cast<GLsizei>( data.size() / strideFloats );
mIndexCount = static_cast<GLsizei>( indices.size() );
glGenVertexArrays( 1, &mVAO );
glGenBuffers( 1, &mVBO );
glGenBuffers( 1, &mEBO );
glBindVertexArray( mVAO );
glBindBuffer( GL_ARRAY_BUFFER, mVBO );
glBufferData( GL_ARRAY_BUFFER, data.size() * sizeof( float ), data.data(), GL_STATIC_DRAW );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mEBO );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof( unsigned int ), indices.data(),
GL_STATIC_DRAW );
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, strideFloats * sizeof( float ), (void*)0 );
glEnableVertexAttribArray( 1 );
glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, strideFloats * sizeof( float ),
(void*)( 3 * sizeof( float ) ) );
glEnableVertexAttribArray( 2 );
glVertexAttribPointer( 2, 3, GL_FLOAT, GL_FALSE, strideFloats * sizeof( float ),
(void*)( 5 * sizeof( float ) ) );
glBindVertexArray( 0 );
return true;
}
void Mesh::draw () const {
if( mVAO == 0 )
return;
glBindVertexArray( mVAO );
if( mEBO && mIndexCount > 0 ) {
glDrawElements( mMode, mIndexCount, GL_UNSIGNED_INT, 0 );
} else {
glDrawArrays( mMode, 0, mVertexCount );
}
glBindVertexArray( 0 );
}
} // namespace renderer
|