-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawable.cpp
79 lines (64 loc) · 1.84 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
76
77
78
79
#include "drawable.h"
#include <iostream>
using namespace cs40;
Drawable::Drawable() : m_prog(NULL), m_vao(NULL), m_vbo(NULL),
m_color(0,0,0), m_displacement(0,0), m_visible(true) , m_center(0,0) {
}
Drawable::~Drawable(){
/* cleanup dynamically allocated base class objects */
if(m_vbo){
m_vbo->release();
delete m_vbo; m_vbo=NULL;
}
if(m_vao){
m_vao->release();
delete m_vao; m_vao=NULL;
}
}
bool Drawable::initVBO(){
bool ok=true; /* trust but verify */
if( ! m_vao ){
/* create VAO first */
m_vao = new QOpenGLVertexArrayObject();
ok = m_vao->create();
if (!ok) { return false; }
m_vao->bind();
}
if ( ! m_vbo ){
/* make new VBO ID, but don't allocate
* we don't know how much space to save anyways
* in the base class
*/
m_vbo = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
ok = m_vbo->create();
if (!ok) { return false; }
m_vbo->setUsagePattern(QOpenGLBuffer::DynamicDraw);
}
return ok;
}
void Drawable::move(float dx, float dy){
/* apply new shift to current displacement from
* original location */
vec2 shift(dx,dy);
m_displacement += shift;
}
void Drawable::drawHelper(GLenum mode, int count){
m_vao->bind();
m_vbo->bind();
m_prog->bind();
/* VAO should be remembering this. It is not */
m_prog->enableAttributeArray("vPosition");
m_prog->setAttributeBuffer("vPosition", GL_FLOAT, 0, 2, 0);
m_prog->setUniformValue("color", getColor());
m_prog->setUniformValue("displacement", m_displacement);
glDrawArrays(mode, 0 , count);
m_prog->release();
m_vbo->release();
m_vao->release();
}
void Drawable::setCenter(int x, int y){
m_center= vec2(x, y);
}
vec2 Drawable::getCenter(){
return m_center;
}