-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomMesh.cpp
223 lines (181 loc) · 5.95 KB
/
CustomMesh.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
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
#include "CustomMesh.h"
CustomMesh::CustomMesh(QObject *parent)
:QObject(parent)
{
this->inited = false;
this->m_vao = NULL;
this->m_transform = new Transform3D();
}
void CustomMesh::draw(QOpenGLShaderProgram* program)
{
if(this->inited == false)
return;
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
this->m_vao->bind();
if(this->m_textures.size() > 0)
{
CustomTexture* tex = this->m_textures[0];
tex->texture->bind(0);
program->setUniformValue("texture_diffuse",0);
}
f->glDrawArrays(this->m_mode, 0, this->m_count);
this->m_vao->release();
if(this->m_textures.size() > 0)
{
this->m_textures[0]->texture->release();
}
}
void CustomMesh::setDrawArrays(GLenum mode, int count)
{
this->m_mode = mode;
this->m_count = count;
}
QOpenGLVertexArrayObject *CustomMesh::vertexArrayObject()
{
if(this->m_vao == NULL)
this->m_vao = new QOpenGLVertexArrayObject();
return this->m_vao;
}
QOpenGLBuffer *CustomMesh::createBuffer(QOpenGLBuffer::UsagePattern hint)
{
QOpenGLBuffer buffer;
buffer.create();
buffer.setUsagePattern(hint);
this->m_buffer.push_back(buffer);
return &this->m_buffer[this->m_buffer.size() - 1];
}
std::vector<QOpenGLBuffer> *CustomMesh::buffers()
{
return &this->m_buffer;
}
///
/// \brief CustomMesh::createCube
/// \param position
/// \param size
///
void CustomMesh::createCube(QVector3D position, float size)
{
float halfsize = size / 2; // 半边的长度
float x = position.x();
float y = position.y();
float z = position.z();
// front
QVector3D FTR(halfsize + x, halfsize + y, halfsize + z);
QVector3D FTL(-halfsize + x, halfsize + y, halfsize + z);
QVector3D FBL(-halfsize + x, -halfsize + y, halfsize + z);
QVector3D FBR(halfsize + x, -halfsize + y, halfsize + z);
// back
QVector3D BTR(halfsize + x, halfsize + y, -halfsize + z);
QVector3D BTL(-halfsize + x, halfsize + y, -halfsize + z);
QVector3D BBL(-halfsize + x, -halfsize + y, -halfsize + z);
QVector3D BBR(halfsize + x, -halfsize + y, -halfsize + z);
// color
// Face 1 Front
this->m_vertexs.push_back(Vertex(FTR));
this->m_vertexs.push_back(Vertex(FTL));
this->m_vertexs.push_back(Vertex(FBL));
this->m_vertexs.push_back(Vertex(FBL));
this->m_vertexs.push_back(Vertex(FBR));
this->m_vertexs.push_back(Vertex(FTR));
// face 2 back
this->m_vertexs.push_back(Vertex(BBR));
this->m_vertexs.push_back(Vertex(BTL));
this->m_vertexs.push_back(Vertex(BTR));
this->m_vertexs.push_back(Vertex(BTL));
this->m_vertexs.push_back(Vertex(BBR));
this->m_vertexs.push_back(Vertex(BBL));
// face 3 top
this->m_vertexs.push_back(Vertex(FTR));
this->m_vertexs.push_back(Vertex(BTR));
this->m_vertexs.push_back(Vertex(BTL));
this->m_vertexs.push_back(Vertex(BTL));
this->m_vertexs.push_back(Vertex(FTL));
this->m_vertexs.push_back(Vertex(FTR));
// face 4 bottom
this->m_vertexs.push_back(Vertex(FBR));
this->m_vertexs.push_back(Vertex(FBL));
this->m_vertexs.push_back(Vertex(BBL));
this->m_vertexs.push_back(Vertex(BBL));
this->m_vertexs.push_back(Vertex(BBR));
this->m_vertexs.push_back(Vertex(FBR));
// face 5 left
this->m_vertexs.push_back(Vertex(FBL));
this->m_vertexs.push_back(Vertex(FTL));
this->m_vertexs.push_back(Vertex(BTL));
this->m_vertexs.push_back(Vertex(FBL));
this->m_vertexs.push_back(Vertex(BTL));
this->m_vertexs.push_back(Vertex(BBL));
// face 6 right
this->m_vertexs.push_back(Vertex(FTR));
this->m_vertexs.push_back(Vertex(FBR));
this->m_vertexs.push_back(Vertex(BBR));
this->m_vertexs.push_back(Vertex(BBR));
this->m_vertexs.push_back(Vertex(BTR));
this->m_vertexs.push_back(Vertex(FTR));
qDebug() <<"create vertex finished";
this->setDrawArrays(GL_TRIANGLES, this->m_vertexs.size());
QOpenGLFunctions *function = QOpenGLContext::currentContext()->functions();
this->m_vao = new QOpenGLVertexArrayObject();
this->m_vao->create();
this->m_vao->bind();
QOpenGLBuffer * buffer = this->createBuffer();
buffer->create();
buffer->bind();
buffer->allocate(this->m_vertexs.size() * sizeof(Vertex));
Vertex *data = static_cast<Vertex*>(buffer->map(QOpenGLBuffer::WriteOnly));
for(int i = 0; i < m_vertexs.size(); i++)
{
Vertex vertex;
QVector3D vec = m_vertexs[i].position();
vertex.setPosition(vec);
data[i] = vertex;
}
function->glVertexAttribPointer(
0,
Vertex::PositionTupleSize,
GL_FLOAT,
GL_FALSE,
Vertex::stride(),
(void*)Vertex::positionOffset());
function->glVertexAttribPointer(
1,
Vertex::ColorTupleSize,
GL_FLOAT,
GL_FALSE,
Vertex::stride(),
(void*)Vertex::colorOffset());
function->glVertexAttribPointer(
2,
Vertex::TexCoordsTupleSize,
GL_FLOAT,
GL_FALSE,
Vertex::stride(),
(void*)Vertex::texCoordsOffset());
function->glEnableVertexAttribArray(0);
function->glEnableVertexAttribArray(1);
function->glEnableVertexAttribArray(2);
buffer->unmap();
this->inited = true;
m_vao->release();
buffer->release();
qDebug() << "create cube finished";
}
Transform3D *CustomMesh::transform()
{
return this->m_transform;
}
// 设置transform
void CustomMesh::setTransform(Transform3D &transform)
{
this->m_transform->setTranslation(transform.translation());
this->m_transform->setRotation(transform.rotation());
this->m_transform->setScale(transform.scale());
}
std::vector<CustomTexture *> *CustomMesh::textures()
{
return &this->m_textures;
}
CustomTexture *CustomMesh::texture(int index)
{
return this->m_textures[index];
}