Repository navigation
Expand file tree
/
Copy pathsimpleobject3d.cpp
More file actions
146 lines (114 loc) · 4.02 KB
/
Copy pathsimpleobject3d.cpp
File metadata and controls
146 lines (114 loc) · 4.02 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
#include "simpleobject3d.h"
#include <QOpenGLTexture>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
SimpleObject3D::SimpleObject3D() :
m_vertexBuffer(QOpenGLBuffer::VertexBuffer),
m_indexBuffer(QOpenGLBuffer::IndexBuffer),
m_texture(nullptr),
m_scale(1.0f)
{
}
SimpleObject3D::SimpleObject3D(QVector<VertexData> &vertData,
const QVector<GLuint> &indexes, const QImage &texture) :
m_vertexBuffer(QOpenGLBuffer::VertexBuffer),
m_indexBuffer(QOpenGLBuffer::IndexBuffer),
m_texture(nullptr),
m_scale(1.0f)
{
init(vertData, indexes, texture);
}
SimpleObject3D::~SimpleObject3D()
{
if (m_vertexBuffer.isCreated())
m_vertexBuffer.destroy();
if (m_indexBuffer.isCreated())
m_indexBuffer.destroy();
if (m_texture!= nullptr && m_texture->isCreated())
m_texture->destroy();
}
void SimpleObject3D::draw(QOpenGLShaderProgram *shaderProgram, QOpenGLFunctions *functions)
{
QMatrix4x4 modelMatrix;
modelMatrix.setToIdentity();
modelMatrix.translate(m_translate);
modelMatrix.rotate(m_rotate);
modelMatrix.scale(m_scale);
modelMatrix = m_globalTransform * modelMatrix;
if (!m_vertexBuffer.isCreated() || !m_indexBuffer.isCreated())
return;
// Bind texture
m_texture->bind(0);
shaderProgram->setUniformValue("u_texture", 0);
// Set uniform values
shaderProgram->setUniformValue("u_modelMatrix", modelMatrix);
// Bind buffers
m_vertexBuffer.bind();
m_indexBuffer.bind();
int offset = 0;
// Set varying attribute "a_position"
int location = shaderProgram->attributeLocation("a_position");
shaderProgram->enableAttributeArray(location);
shaderProgram->setAttributeBuffer(location, GL_FLOAT, offset, 3, sizeof(VertexData));
offset += sizeof(QVector3D);
// Set varying attribute "a_textCoord"
location = shaderProgram->attributeLocation("a_textCoord");
shaderProgram->enableAttributeArray(location);
shaderProgram->setAttributeBuffer(location, GL_FLOAT, offset, 2, sizeof(VertexData));
offset += sizeof(QVector2D);
// Set varying attribute "a_normal"
location = shaderProgram->attributeLocation("a_normal");
shaderProgram->enableAttributeArray(location);
shaderProgram->setAttributeBuffer(location, GL_FLOAT, offset, 3, sizeof(VertexData));
// Draw
functions->glDrawElements(GL_TRIANGLES, m_indexBuffer.size(), GL_UNSIGNED_INT, 0);
// Release
m_indexBuffer.release();
m_vertexBuffer.release();
m_texture->release();
}
void SimpleObject3D::rotate(const QQuaternion &rotation)
{
m_rotate = rotation * m_rotate;
}
void SimpleObject3D::translate(const QVector3D &translation)
{
m_translate += translation;
}
void SimpleObject3D::scale(const float scaleKoef)
{
m_scale *= scaleKoef;
}
void SimpleObject3D::setGlobalTransform(const QMatrix4x4 &transformMatrix)
{
m_globalTransform = transformMatrix;
}
QVector3D SimpleObject3D::getPosition() const
{
return QVector3D(0, 0, 0);
}
void SimpleObject3D::init(QVector<VertexData> &vertData, const QVector<GLuint> &indexes, const QImage &texture)
{
if (m_vertexBuffer.isCreated())
m_vertexBuffer.destroy();
if (m_indexBuffer.isCreated())
m_indexBuffer.destroy();
if (m_texture!= nullptr && m_texture->isCreated()) {
delete m_texture;
m_texture = nullptr;
}
// Create and alloc memory for vertex buffer
m_vertexBuffer.create();
m_vertexBuffer.bind();
m_vertexBuffer.allocate(vertData.constData(), vertData.size() * sizeof(VertexData));
m_vertexBuffer.release();
// Create and alloc memory for index buffer
m_indexBuffer.create();
m_indexBuffer.bind();
m_indexBuffer.allocate(indexes.constData(), indexes.size() * sizeof(GLuint));
m_indexBuffer.release();
m_texture = new QOpenGLTexture(texture.mirrored());
m_texture->setMinificationFilter(QOpenGLTexture::Nearest);
m_texture->setMagnificationFilter(QOpenGLTexture::Linear);
m_texture->setWrapMode(QOpenGLTexture::Repeat);
}