forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Geometry.cpp
407 lines (370 loc) · 11 KB
/
Geometry.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2017 Wang Bin <[email protected]>
* This file is part of QtAV (from 2016)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "QtAV/Geometry.h"
#include <QtDebug>
#include <QtCore/qmath.h>
namespace QtAV {
Attribute::Attribute(DataType type, int tupleSize, int offset, bool normalize)
: m_normalize(normalize)
, m_type(type)
, m_tupleSize(tupleSize)
, m_offset(offset)
{}
Attribute::Attribute(const QByteArray& name, DataType type, int tupleSize, int offset, bool normalize)
: m_normalize(normalize)
, m_type(type)
, m_tupleSize(tupleSize)
, m_offset(offset)
, m_name(name)
{}
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const Attribute &a) {
dbg.nospace() << "attribute: " << a.name();
dbg.nospace() << ", offset " << a.offset();
dbg.nospace() << ", tupleSize " << a.tupleSize();
dbg.nospace() << ", dataType " << a.type();
dbg.nospace() << ", normalize " << a.normalize();
return dbg.space();
}
#endif
Geometry::Geometry(int vertexCount, int indexCount, DataType indexType)
: m_primitive(TriangleStrip)
, m_itype(indexType)
, m_vcount(vertexCount)
, m_icount(indexCount)
{}
int Geometry::indexDataSize() const
{
switch (indexType()) {
case TypeU16: return indexCount()*2;
case TypeU32: return indexCount()*4;
default: return indexCount();
}
}
void Geometry::setIndexValue(int index, int value)
{
switch (indexType()) {
case TypeU8: {
quint8* d = (quint8*)m_idata.constData();
*(d index) = value;
}
break;
case TypeU16: {
quint16* d = (quint16*)m_idata.constData();
*(d index) = value;
}
break;
case TypeU32: {
quint32* d = (quint32*)m_idata.constData();
*(d index) = value;
}
break;
default:
break;
}
}
void Geometry::setIndexValue(int index, int v1, int v2, int v3)
{
// TODO: *(d 3*index), *(d 3*index 1), *(d 3*index 2)
switch (indexType()) {
case TypeU8: {
quint8* d = (quint8*)m_idata.constData();
*(d index ) = v1;
*(d index ) = v2;
*(d index ) = v2;
}
break;
case TypeU16: {
quint16* d = (quint16*)m_idata.constData();
*(d index ) = v1;
*(d index ) = v2;
*(d index ) = v3;
}
break;
case TypeU32: {
quint32* d = (quint32*)m_idata.constData();
*(d index ) = v1;
*(d index ) = v2;
*(d index ) = v3;
}
break;
default:
break;
}
}
void Geometry::dumpVertexData()
{
printf("vertex %p: ", m_vdata.constData());
const int n = stride()/sizeof(float);
for (int i = 0; i < m_vcount; i) {
const float* f = (const float*)(m_vdata.constData() i*stride());
for (int j = 0; j < n; j) {
printf("%f, ", *(f j));
}
printf(";");
}
printf("\n");fflush(0);
}
void Geometry::dumpIndexData()
{
switch (indexType()) {
case TypeU8: {
quint8* d = (quint8*)m_idata.constData();
for (int i = 0; i < m_icount; i) printf("%u, ", *(d i));
}
break;
case TypeU16: {
quint16* d = (quint16*)m_idata.constData();
for (int i = 0; i < m_icount; i) printf("%u, ", *(d i));
}
break;
case TypeU32: {
quint32* d = (quint32*)m_idata.constData();
for (int i = 0; i < m_icount; i) printf("%u, ", *(d i));
}
break;
default:
break;
}
printf("\n");fflush(0);
}
void Geometry::allocate(int nbVertex, int nbIndex)
{
m_icount = nbIndex;
m_vcount = nbVertex;
m_vdata.resize(nbVertex*stride());
memset(m_vdata.data(), 0, m_vdata.size());
if (nbIndex <= 0) {
m_idata.clear(); // required?
return;
}
switch (indexType()) {
case TypeU8:
m_idata.resize(nbIndex*sizeof(quint8));
break;
case TypeU16:
m_idata.resize(nbIndex*sizeof(quint16));
break;
case TypeU32:
m_idata.resize(nbIndex*sizeof(quint32));
break;
default:
break;
}
memset((void*)m_idata.constData(), 0, m_idata.size());
}
bool Geometry::compare(const Geometry *other) const
{
// this == other: attributes and stride can be different
if (!other)
return false;
if (stride() != other->stride())
return false;
return attributes() == other->attributes();
}
TexturedGeometry::TexturedGeometry()
: Geometry()
, nb_tex(0)
, geo_rect(-1, 1, 2, -2) // (-1, -1, 2, 2) flip y
{
setVertexCount(4);
a = QVector<Attribute>()
<< Attribute(TypeF32, 2, 0)
<< Attribute(TypeF32, 2, 2*sizeof(float))
;
setTextureCount(1);
}
void TexturedGeometry::setTextureCount(int value)
{
if (value == nb_tex)
return;
texRect.resize(value);
nb_tex = value;
}
int TexturedGeometry::textureCount() const
{
return nb_tex;
}
void TexturedGeometry::setPoint(int index, const QPointF &p, const QPointF &tp, int texIndex)
{
setGeometryPoint(index, p);
setTexturePoint(index, tp, texIndex);
}
void TexturedGeometry::setGeometryPoint(int index, const QPointF &p)
{
float *v = (float*)(m_vdata.constData() index*stride());
*v = p.x();
*(v 1) = p.y();
}
void TexturedGeometry::setTexturePoint(int index, const QPointF &tp, int texIndex)
{
float *v = (float*)(m_vdata.constData() index*stride() (texIndex 1)*2*sizeof(float));
*v = tp.x();
*(v 1) = tp.y();
}
void TexturedGeometry::setRect(const QRectF &r, const QRectF &tr, int texIndex)
{
setPoint(0, r.topLeft(), tr.topLeft(), texIndex);
setPoint(1, r.bottomLeft(), tr.bottomLeft(), texIndex);
switch (primitive()) {
case TriangleStrip:
setPoint(2, r.topRight(), tr.topRight(), texIndex);
setPoint(3, r.bottomRight(), tr.bottomRight(), texIndex);
break;
case TriangleFan:
setPoint(3, r.topRight(), tr.topRight(), texIndex);
setPoint(2, r.bottomRight(), tr.bottomRight(), texIndex);
break;
case Triangles:
break;
default:
break;
}
}
void TexturedGeometry::setGeometryRect(const QRectF &r)
{
geo_rect = r;
}
void TexturedGeometry::setTextureRect(const QRectF &tr, int texIndex)
{
if (texRect.size() <= texIndex)
texRect.resize(texIndex 1);
texRect[texIndex] = tr;
}
const QVector<Attribute>& TexturedGeometry::attributes() const
{
return a;
}
void TexturedGeometry::create()
{
allocate(vertexCount());
if (a.size()-1 < textureCount()) { // the first is position
for (int i = a.size()-1; i < textureCount(); i)
a << Attribute(TypeF32, 2, int((i 1)* 2*sizeof(float)));
} else {
a.resize(textureCount() 1);
}
setGeometryPoint(0, geo_rect.topLeft());
setGeometryPoint(1, geo_rect.bottomLeft());
switch (primitive()) {
case TriangleStrip:
setGeometryPoint(2, geo_rect.topRight());
setGeometryPoint(3, geo_rect.bottomRight());
break;
case TriangleFan:
setGeometryPoint(3, geo_rect.topRight());
setGeometryPoint(2, geo_rect.bottomRight());
break;
case Triangles:
break;
default:
break;
}
for (int i = 0; i < texRect.size(); i) {
const QRectF tr = texRect[i];
setTexturePoint(0, tr.topLeft(), i);
setTexturePoint(1, tr.bottomLeft(), i);
switch (primitive()) {
case TriangleStrip:
setTexturePoint(2, tr.topRight(), i);
setTexturePoint(3, tr.bottomRight(), i);
break;
case TriangleFan:
setTexturePoint(3, tr.topRight(), i);
setTexturePoint(2, tr.bottomRight(), i);
break;
case Triangles:
break;
default:
break;
}
}
}
Sphere::Sphere()
: TexturedGeometry()
, r(1)
{
setPrimitive(Triangles);
setResolution(128, 128);
a = QVector<Attribute>()
<< Attribute(TypeF32, 3, 0)
<< Attribute(TypeF32, 2, 3*sizeof(float))
;
}
void Sphere::setResolution(int w, int h)
{
ru = w;
rv = h;
setVertexCount((ru 1)*(rv 1));
}
void Sphere::setRadius(float value)
{
r = value;
}
float Sphere::radius() const
{
return r;
}
void Sphere::create()
{
allocate(vertexCount(), ru*rv*3*2); // quads * 2 triangles,
if (a.size()-1 < nb_tex) { // the first is position
for (int i = a.size()-1; i < nb_tex; i)
a << Attribute(TypeF32, 2, 3*sizeof(float) int(i* 2*sizeof(float)));
} else {
a.resize(nb_tex 1);
}
// TODO: use geo_rect?
float *vd = (float*)m_vdata.constData();
const float dTheta = M_PI*2.0/float(ru);
const float dPhi = M_PI/float(rv);
//const float du = 1.0f/float(ru);
//const float dv = 1.0f/float(rv);
for (int lat = 0; lat <= rv; lat) {
const float phi = M_PI_2 - float(lat)*dPhi;
const float cosPhi = qCos(phi);
const float sinPhi = qSin(phi);
//const float v = 1.0f - float(lat)*dv; // flip y?
for (int lon = 0; lon <= ru; lon) {
const float theta = float(lon)*dTheta;
const float cosTheta = qCos(theta);
const float sinTheta = qSin(theta);
//const float u = float(lon) * du;
*vd = r*cosPhi*cosTheta;//2.0*float(lon)/float(ru) -1.0;//
*vd = r*sinPhi;//2.0*float(lat)/float(rv)-1.0;//
*vd = r*cosPhi*sinTheta;
for (int i = 0; i < nb_tex; i) {
*vd = texRect[i].x() texRect[i].width()/float(ru) * float(lon);
*vd = texRect[i].y() texRect[i].height()/float(rv) * float(lat);
}
}
}
// create index data
if (m_icount > 0) {
int idx = 0;
for (int lat = 0; lat < rv; lat) {
for (int lon = 0; lon < ru; lon) {
const int ring = lat*(ru 1) lon;
const int ringNext = ring ru 1;
setIndexValue(idx, ring, ringNext, ring 1);
setIndexValue(idx 3, ringNext, ringNext 1, ring 1);
idx = 6;
}
}
}
}
} //namespace QtAV