This repository was archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathglobject.cpp
More file actions
273 lines (234 loc) · 7.91 KB
/
globject.cpp
File metadata and controls
273 lines (234 loc) · 7.91 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#define _USE_MATH_DEFINES
#include "globject.h"
#include <QOpenGLBuffer>
#include <QMatrix4x4>
#include <QPainter>
#include <QBitmap>
#include <math.h>
GLObject::GLObject(GLenum primitiveType,
QOpenGLFunctions_4_3_Core* functions,
QString id,
GLenum drawType)
: id_(id)
, vaoId_()
, textureId_(-1)
, text_()
, halfSize_()
, isVisible_(true)
, forceInvisible_(false)
, alreadyInObjectTree_(false)
, modelMatrix_()
, vertices_()
, texCoords_()
, velocity_()
, acceleration_()
, realPosition_()
, vboId_()
, vboTexCoordId_()
, color_()
, drawType_(drawType)
, orientation_(0)
, position_()
, primitiveType_(primitiveType)
, textObject_(nullptr)
, isInitialized_(false)
, lastVertexSize_(-1)
, objectType_(ObjectType::None)
, functions_(functions)
{
}
GLObject::~GLObject()
{
if (isInitialized_)
{
if (!texCoords_.isEmpty())
{
functions_->glDeleteBuffers(1, &vboTexCoordId_);
}
functions_->glDeleteBuffers(1, &vboId_);
functions_->glDeleteVertexArrays(1, &vaoId_);
}
}
// Qt provides classes like QVertexArrayObject, for example.
// However, they caused unexpected behaviour, thus plain OpenGL commands are used.
void
GLObject::Init()
{
if (isInitialized_)
{
return;
}
functions_->glGenVertexArrays(1, &vaoId_);
functions_->glBindVertexArray(vaoId_);
functions_->glGenBuffers(1, &vboId_);
UpdateVertexBuffer();
functions_->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
functions_->glEnableVertexAttribArray(0);
// Tex coord buffer
if (!texCoords_.isEmpty())
{
int elementSize = 2;
int sizeInBytes = elementSize * texCoords_.size() * sizeof(float);
functions_->glGenBuffers(1, &vboTexCoordId_);
functions_->glBindBuffer(GL_ARRAY_BUFFER, vboTexCoordId_);
functions_->glBufferData(GL_ARRAY_BUFFER, sizeInBytes, texCoords_.constData(), GL_STATIC_DRAW);
functions_->glVertexAttribPointer(1, elementSize, GL_FLOAT, GL_FALSE, 0, 0);
functions_->glEnableVertexAttribArray(1);
}
isInitialized_ = true;
}
void
GLObject::UpdateVertexBuffer()
{
int elementSize = 3;
int sizeInBytes = elementSize * vertices_.size() * sizeof(float);
functions_->glBindBuffer(GL_ARRAY_BUFFER, vboId_);
if (lastVertexSize_ == vertices_.size())
{
functions_->glBufferSubData(GL_ARRAY_BUFFER, 0, sizeInBytes, vertices_.constData());
}
else
{
// glBufferData deletes the previous buffer data
lastVertexSize_ = vertices_.size();
functions_->glBufferData(GL_ARRAY_BUFFER, sizeInBytes, vertices_.constData(), drawType_);
}
}
void
GLObject::SetText(QString text)
{
text_ = text;
QFont font("Arial", 100);
QFontMetrics fm(font);
QSize pixelSize = fm.size(Qt::TextSingleLine, text);
QImage image(pixelSize, QImage::Format_RGBA8888);
image.fill(Qt::transparent);
QPainter painter(&image);
painter.setRenderHint(QPainter::TextAntialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
painter.setRenderHint(QPainter::HighQualityAntialiasing, true);
painter.setPen(Qt::white);
painter.setFont(font);
painter.drawText(0, pixelSize.height() - 5, text);
// TODO: make size public such the text can be shifted appropriately
float halfLength = 1.0f;
float halfWidth = halfLength * pixelSize.width() / pixelSize.height();
textObject_ = std::make_shared<GLObject>(GL_QUADS, functions_);
textObject_->vertices_ << QVector3D(-halfLength, 0, halfWidth)
<< QVector3D(halfLength, 0, halfWidth)
<< QVector3D(halfLength, 0, -halfWidth)
<< QVector3D(-halfLength, 0, -halfWidth);
textObject_->texCoords_ << QVector2D(1, 1)
<< QVector2D(1, 0)
<< QVector2D(0, 0)
<< QVector2D(0, 1);
textObject_->Init();
textObject_->Translate(0, 0.01f, 0);
textObject_->halfSize_.setWidth(halfLength);
textObject_->halfSize_.setHeight(halfWidth);
textObject_->SetTexture(image, false);
}
void
GLObject::SetTexture(QImage image, bool generateMipMaps, GLint wrapping)
{
if (textureId_ > -1)
{
functions_->glDeleteTextures(1, &textureId_);
}
// glActiveTexture not needed atm
functions_->glGenTextures(1, &textureId_);
functions_->glBindTexture(GL_TEXTURE_2D, textureId_);
functions_->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
image.width(), image.height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, image.bits());
functions_->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapping);
functions_->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapping);
if (generateMipMaps)
{
functions_->glGenerateMipmap(GL_TEXTURE_2D);
functions_->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// TODO: A website stated that Mag filter could only use Nearest and Linear
functions_->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
else
{
functions_->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
functions_->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
// TODO: Will these result in deletion of the image?
image.detach();
}
void
GLObject::SetPosition(float x, float y, float z, bool updateMatrix)
{
SetPosition(QVector3D(x, y, z), updateMatrix);
}
void
GLObject::SetPosition(QVector3D position, bool updateMatrix)
{
position_ = position;
Update(updateMatrix);
}
void
GLObject::SetOrientation(float orientation, bool updateMatrix)
{
orientation_ = orientation;
Update(updateMatrix);
}
void
GLObject::Translate(float x, float y, float z, bool updateMatrix)
{
Translate(QVector3D(x, y, z), updateMatrix);
}
void
GLObject::Translate(QVector3D translation, bool updateMatrix)
{
position_ += translation;
Update(updateMatrix);
}
void
GLObject::RotateAroundYAxis(float rotation, bool updateMatrix)
{
orientation_ += rotation;
orientation_ = fmod(orientation_, 2 * M_PI);
Update(updateMatrix);
}
void GLObject::Update(bool updateMatrix)
{
if (!updateMatrix)
{
return;
}
//double cosr = 1;
//double sinr = 0;
double cosp = cos(orientation_);
double sinp = sin(orientation_);
//double cosy = 1;
//double siny = 0;
// TODO: Why has QMatrix3x3 not the same constructor?
// (QMatrix3x3 derives from QGenericMatrix, 4x4 does not)
/*
QMatrix4x4 rotX(1, 0, 0, 0,
0, cosr, -sinr, 0,
0, sinr, cosr, 0,
0, 0, 0, 1);
*/
QMatrix4x4 rotY(cosp, 0, sinp, 0,
0, 1, 0, 0,
-sinp, 0, cosp, 0,
0, 0, 0, 1);
/*
QMatrix4x4 rotZ(cosy, -siny, 0, 0,
siny, cosy, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
*/
// TODO: Correct order? Is this pre or post multiply?
QMatrix4x4 rotMatrix = rotY;// rotX * rotY * rotZ;
// TODO: Will this cause a memory leak?
modelMatrix_ = QMatrix4x4(rotMatrix(0, 0), rotMatrix(0, 1), rotMatrix(0, 2), position_.x(),
rotMatrix(1, 0), rotMatrix(1, 1), rotMatrix(1, 2), position_.y(),
rotMatrix(2, 0), rotMatrix(2, 1), rotMatrix(2, 2), position_.z(),
0, 0, 0, 1);
//modelMatrix = tempModelMatrix;
}