-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawable.cpp
75 lines (58 loc) · 1.47 KB
/
drawable.cpp
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
#include "drawable.h"
Drawable::Drawable( size_t nVBufferSize,
QString vertexShaderFile,
QString fragmentShaderFile)
: m_nVBufferSize(nVBufferSize)
, m_vertexShaderFile(vertexShaderFile)
, m_fragmentShaderFile(fragmentShaderFile)
{
}
QOpenGLBuffer& Drawable::VBuffer()
{
return m_vbuf;
}
QOpenGLVertexArrayObject& Drawable::VAO()
{
return m_vao;
}
VtxArray& Drawable::Vertices()
{
return m_vertices;
}
void Drawable::EnsureInit()
{
EnsureFree();
// m_vertices.resize(m_nVBufferSize);
// if (m_vertices.size() <= 0)
// {
// return;
// }
m_pShaderProgram = new QOpenGLShaderProgram();
m_pShaderProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, m_vertexShaderFile);
m_pShaderProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, m_fragmentShaderFile);
m_pShaderProgram->link();
m_pShaderProgram->bind();
m_vbuf.create();
m_vbuf.bind();
m_vbuf.setUsagePattern(QOpenGLBuffer::StaticDraw);
m_vbuf.allocate(m_vertices.data(), sizeof(Vertex) * m_nVBufferSize );
m_vao.create();
m_vao.bind();
m_vao.release();
m_vbuf.release();
m_pShaderProgram->release();
}
void Drawable::EnsureFree()
{
m_vao.destroy();
m_vbuf.destroy();
m_vertices.resize(0);
}
void Drawable::Paint()
{
m_pShaderProgram->bind();
m_vao.bind();
glDrawArrays(GL_TRIANGLES, 0, sizeof(Vertex));
m_vao.release();
m_pShaderProgram->release();
}