summaryrefslogtreecommitdiffstats
path: root/src/plugins/sceneformats/3ds/qgl3dsmesh.cpp
blob: 03f8b51e929ac3b89117f28be82b4124b0c01d57 (plain)
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qgl3dsmesh.h"
#include "qglmaterialcollection.h"
#include "qglsection_p.h"
#include "qarray.h"
#include "qvector_utils_p.h"

#include <lib3ds/mesh.h>
#include <lib3ds/material.h>
#include <lib3ds/vector.h>
#include <lib3ds/types.h>

#include <QtGui/qmatrix4x4.h>
#include <QtCore/qmath.h>

// Faceted meshes look terrible when they have more than a small number
// of faces.  Usually if large meshes are faceted its some kind of error
// in the model, or in importing of the model by lib3ds.  Force on
// smoothing when greater than this many faces are detected.
#define FACETED_THRESHOLD 1000

// Two faces that have an angle between their plane vectors with a cosine
// less than this are judged to form a sharp (acute) angle.
// -ve cosine (less than 0.0f) means 90 degrees or sharper like the sides
// of a rectangular prism so this is a good value.
#define ACUTE -0.0001f

// Two vectors that have an angle between them with a cosine less than this
// value are judged to be approximately the inverse of each other, for the
// purposes of determining whether a normal has been inverted or not
#define INVERSE -0.20f

// Only try to correct normals if this many neighbour faces contribute to
// the assessment.  2 or 3 are good values.  4 or greater is the same as
// defining DO_NORMAL_CORRECT false
#define AVG_CNT 2

// make a QVector3D from a Lib3dsVector - by a hard cast.  Its required
// that QVector3D behave like a POD type - float[3] - for all of the
// OpenGL stuff
static inline QVector3D &l2v(Lib3dsVector &vec)
{
    return reinterpret_cast<QVector3D&>(vec);
}

// is this a null 3ds vector - same as QVector3D::isNull()
static inline bool qIsNull(const Lib3dsVector &vec)
{
    return (qIsNull(vec[0]) && qIsNull(vec[1]) && qIsNull(vec[2]));
}

static inline bool qFskCompare(const Lib3dsVector &a, const Lib3dsVector &b)
{
    return (qFskCompare(a[0], b[0]) && qFskCompare(a[1], b[1]) && qFskCompare(a[2], b[2]));
}

// find the normalized plane vector, that is a unit vector perpendicular
// to the plane of the face.  this is the same thing as the default normal
static inline void planeVec(Lib3dsFace *face, Lib3dsPoint *pointList, Lib3dsVector result)
{
    Lib3dsVector &l3a = pointList[face->points[0]].pos;
    Lib3dsVector &l3b = pointList[face->points[1]].pos;
    Lib3dsVector &l3c = pointList[face->points[2]].pos;
    lib3ds_vector_normal(result, l3a, l3b, l3c);
}

struct ModulateRecord
{
    bool disabled;
    bool keyFresh;
    int facesProcessed;
    int numModulated;
    Lib3dsDword altKey;
    Lib3dsDword key;
};

QGL3dsMesh::QGL3dsMesh(Lib3dsMesh *mesh, QGLMaterialCollection *materials)
    : QGLBuilder(materials)
    , m_mesh(mesh)
    , m_texFlip(false)
    , m_hasZeroSmoothing(false)
    , m_faceMap(0)
{
}

QGL3dsMesh::~QGL3dsMesh()
{
    delete[] m_faceMap;
}

void QGL3dsMesh::processNodeForMaterial(int matIx, QGLSceneNode *node)
{
    QGLSceneNode *s = sceneNode();
    QString baseName = s->objectName();
    node->setMaterialIndex(matIx);
    node->setObjectName(baseName + QLatin1String("::") +
                        ((matIx == -1)
                         ? QLatin1String("No_Material")
                             : s->palette()->materialName(matIx)));
    checkTextures(matIx);
    generateVertices();
    s->palette()->markMaterialAsUsed(matIx);
}

void QGL3dsMesh::initAdjacencyMap()
{
    Lib3dsFace *face;
    m_faceMap = new FacePtr[m_mesh->faces * 3];
    qMemSet(m_faceMap, 0, sizeof(m_faceMap));
    for (Lib3dsDword f = 0; f < m_mesh->faces; ++f)
    {
        face = &m_mesh->faceL[f];
        planeVec(face, m_mesh->pointL, face->normal);
        m_mesh->faceL[f].user.p = &m_faceMap[f*3];
    }
}

void QGL3dsMesh::initialize()
{
    QGLSceneNode *s = sceneNode();
    s->setObjectName(QString::fromLatin1(m_mesh->name));
    if ((m_options & QGL::ForceSmooth) && (m_options & QGL::ForceFaceted))
    {
        if (m_options & QGL::ShowWarnings)
            qWarning("Both smooth and faceted forced on for %s: forcing smooth\n",
                     m_mesh->name);
        m_options &= ~QGL::ForceFaceted;
    }
    initAdjacencyMap();
    if (m_options & (QGL::CorrectNormals | QGL::CorrectAcute))
        modulateMesh();
    analyzeMesh();

    if (m_smoothingGroups == 0)
    {
        bool forceSmooth = (m_options & QGL::ForceSmooth);
        if (!forceSmooth && m_mesh->faces > FACETED_THRESHOLD)
        {
            if (m_options & QGL::ShowWarnings)
                qWarning("Mesh %s has %d faces (threshold is %d):"
                        "forcing smooth render", m_mesh->name, m_mesh->faces,
                        FACETED_THRESHOLD);
            forceSmooth = true;
        }
        if (forceSmooth)
        {
            for (Lib3dsDword f = 0; f < m_mesh->faces; ++f)
                m_mesh->faceL[f].smoothing = 1;
            analyzeMesh();
        }
    }

    bool mixedTexturedAndPlain = m_plainMaterials.count() > 0 &&
                                 m_textureMaterials.count() > 0;

    s->setLocalTransform(meshMatrix());

    // start a new section and node
    newSection(m_smoothingGroups ? QGL::Smooth : QGL::Faceted);
    QGLSceneNode *node = currentNode();

    // process all the plain materials first, then textured to avoid effect swapping
    QList<int> matList = m_plainMaterials.toList();
    if (mixedTexturedAndPlain)
    {
        node->setEffect(QGL::LitMaterial);
        node->setObjectName(s->objectName() + QLatin1String("::Materials"));
        //qDebug() << ">>> mixed:" << node;
        pushNode();
        //qDebug() << "   pushed - current now:" << currentNode();
    }
    else
    {
        s->setEffect(m_textureMaterials.count() > 0 ? QGL::LitModulateTexture2D : QGL::LitMaterial);
    }
    //qDebug() << "processing:" << matList.count() << "materials";
    while (matList.count() > 0)
    {
        int matIx = matList.takeFirst();
        processNodeForMaterial(matIx, node);
        if (matList.count() > 0)
            node = newNode();
    }
    matList = m_textureMaterials.toList();
    if (mixedTexturedAndPlain)
    {
        //qDebug() << "<<< mixed:" << currentNode();
        popNode();
        //qDebug() << "   popped - current now:" << currentNode();
        node = currentNode();
        node->setEffect(QGL::LitModulateTexture2D);
        node->setObjectName(s->objectName() + QLatin1String("::Textures"));
    }
    while (matList.count() > 0)
    {
        int matIx = matList.takeFirst();
        processNodeForMaterial(matIx, node);
        if (matList.count() > 0)
            node = newNode();
    }
}

// Build a linked list, in a QArray: the first N*2 entries correspond
// to the N vertices: for each n'th vertex, n*2 is the face number,
// n*2+1is the index of the next entry for that vertex, or -1 if there
// is no next entry.
//
// While on the job find the smoothing keys and store in allKeys.
//
// And zero out the user data pointer for each face, and calculate the
// smoothing group count.
QArray<int> QGL3dsMesh::mapFacesToVerts(Lib3dsDword *allKeys)
{
    Lib3dsFace *face;
    QArray<int> vlist(m_mesh->points * 2, -1);
    int nx = 2 * m_mesh->points;
    m_smoothingGroupCount = 0;
    for (Lib3dsDword f = 0; f < m_mesh->faces; ++f)
    {
        face = &m_mesh->faceL[f];
        if (face->smoothing)
        {
            if (!(face->smoothing & *allKeys))
            {
                *allKeys |= face->smoothing;
                m_smoothingGroupCount += 1;
            }
        }
        for (int i = 0; i < 3; ++i)
        {
            int v = face->points[i];
            int ptr = v*2;
            int prv = -1;
            if (vlist[ptr] != -1)
            {
                prv = ptr;
                while (vlist[ptr+1] != -1)
                {
                    ptr = vlist[ptr+1];
                    prv = ptr;
                }
                ptr = nx;
                vlist.extend(2);
                nx += 2;
            }
            vlist[ptr] = f;
            vlist[ptr+1] = -1;
            if (prv != -1)
                vlist[prv+1] = ptr;
        }
    }
    return vlist;
}

void QGL3dsMesh::addToAdjacencyMap(Lib3dsFace *face, Lib3dsFace *neighbour)
{
    Q_ASSERT(face);
    Q_ASSERT(face->user.p);
    Q_ASSERT(neighbour);
    FacePtr *h = static_cast<FacePtr*>(face->user.p);
    while (*h != 0)
        ++h;
    *h = neighbour;
}

// add to each face a linked list of its <= 3 neighbouring faces
// and also ensure each face knows is own normal.  Rather than doing
// lots of small allocs, just assume every face has 3 neighbours
// and allocate the list nodes in one big lump - the m_faceMap.
// use the user data pointer in the lib3ds face struct to point
// to the first node in the list.
void QGL3dsMesh::buildAdjacencyMap(const QArray<int> &vlist)
{
    Lib3dsFace *face;
    Lib3dsFace *nbr;
    for (Lib3dsDword f = 0; f < m_mesh->faces; ++f)
    {
        // for each edge (where an edge is a pair of verts) find ones
        // adjacent, ie both verts in edge have same face (which is
        // not this face).
        face = &m_mesh->faceL[f];
        for (int i = 0; i < 3; ++i)
        {
            Q_ASSERT(face->points[i] < m_mesh->points);
            int vi = 2 * face->points[i];
            int neighbouri = -1;
            while (true)
            {
                neighbouri = vlist[vi];
                if (neighbouri != (int)f)
                {
                    Q_ASSERT(face->points[(i + 1) % 3] < m_mesh->points);
                    int vj = 2 * face->points[(i + 1) % 3];
                    int neighbourj = -1;
                    while (true)
                    {
                        neighbourj = vlist[vj];
                        if (neighbourj == neighbouri)
                        {
                            nbr = &m_mesh->faceL[neighbouri];
                            // only add as neighbour if at least one smoothing group
                            // is shared - still have to test again below for each group
                            if (nbr->smoothing & face->smoothing)
                                addToAdjacencyMap(face, nbr);
                            break;
                        }
                        if (vlist[vj+1] == -1)
                            break;
                        vj = vlist[vj+1];
                    }
                }
                if (vlist[vi+1] == -1)
                    break;
                else
                    vi = vlist[vi+1];
            }
        }
    }
}

static inline void incOrWarn(ModulateRecord *mod)
{
    if (mod->altKey != 0x80000000)
    {
        mod->altKey <<= 1;
    }
    else
    {
        qWarning("Overflowed smoothing keys - modulation disabled.");
        mod->disabled = true;
    }
}

static inline void nextUnusedKey(ModulateRecord *mod, const Lib3dsDword &allKeys)
{
    if (!mod->disabled)
    {
        if (mod->altKey != 0)
            incOrWarn(mod);
        else
        {
            mod->altKey = 1;  // initialize
        }
        while ((mod->altKey & allKeys) && !mod->disabled)
        {
            incOrWarn(mod);
        }
        mod->keyFresh = true;
    }
}

static int numCorrected = 0;
static int numWindingCorrected = 0;

static inline void doNormalCorrect(Lib3dsFace *face)
{
    QGL3dsMesh::FacePtr *n = reinterpret_cast<QGL3dsMesh::FacePtr*>(face->user.p);
    int acnt = 0;
    Lib3dsVector avgn = { 0 };
    for (; *n; ++n)
    {
        if ((*n)->smoothing & face->smoothing)
        {
            lib3ds_vector_add(avgn, avgn, (*n)->normal);
            ++acnt;
        }
    }
    if (acnt >= AVG_CNT)
    {
        lib3ds_vector_normalize(avgn);
        float na = lib3ds_vector_dot(face->normal, avgn);
        if (na < INVERSE)
        {
            //qWarning("corrected: %p - %d, %d, %d", face,
            //        face->points[0], face->points[1], face->points[2]);
            //lib3ds_vector_dump(face->normal);
            lib3ds_vector_neg(face->normal);
            numWindingCorrected++;
            qSwap(face->points[1], face->points[2]);
        }
    }
}

static inline void modFace(Lib3dsFace *face, ModulateRecord *mod)
{
    ++mod->numModulated;
    face->smoothing &= ~mod->key;    // remove old key
    face->smoothing |= mod->altKey;  // add in new key
    ::strncpy(face->material, "bright-red", 60);  //debug
}

static inline void doModulate(Lib3dsFace *face, ModulateRecord *mod)
{
    QGL3dsMesh::FacePtr *n = reinterpret_cast<QGL3dsMesh::FacePtr*>(face->user.p);
    ++mod->facesProcessed;
    for ( ; *n; ++n)
    {
        QGL3dsMesh::FacePtr neighbour = *n;
        if (neighbour->smoothing & face->smoothing)
        {
            if (lib3ds_vector_dot(face->normal, neighbour->normal) < ACUTE)
            {
                qWarning("Modulated due to ACUTE\n");
                modFace(neighbour, mod);
                mod->keyFresh = false;
            }
        }
    }
}

bool operator<(const QVector3D &a, const QVector3D &b)
{
    if (qFskCompare(a.x(), b.x()))
    {
        if (qFskCompare(a.y(), b.y()))
        {
            if (qFskCompare(a.z(), b.z()))
            {
                return false; // they're equal a is not less than b
            }
            else
            {
                return a.z() < b.z();
            }
        }
        else
        {
            return a.y() < b.y();
        }
    }
    else
    {
        return a.x() < b.x();
    }
}

// Go thru each smoothing group - we don't care about the zero group since
// they're already faceted.  In each group process all connected faces
// starting the queue off with a seed face (the first face in the group).
// Set the alternate group to be the first unused smoothing group key.
//
// To process a face: add the face to the processed set; for each adjacent
// face if its in this same smoothing group, add it to the queue.
//
// Modulate smoothing mode: if an adjacent face is at an acute angle to this
// face, move it to the alternate group.  Note that the alternate groups are
// not further iterated since they are not in "each smoothing group"
// (the allKeys variable).
//
// When the queue is empty, go to the next smoothing group in the mesh.

// Normal repair mode:  if a face normal is flipped in error, ie a mistake
// in model building (typically points specified in the wrong order)
// this can be detected and repaired.  The normal is flipped if the inverse
// of the normal is at an acute angle to the average of neighbour normals.
// If detected the normals is corrected and the winding is also checked to
// see if it needs to be corrected also.
void QGL3dsMesh::modulateMesh()
{
    if (!(m_options & (QGL::CorrectNormals | QGL::CorrectAcute)))
        return;
    FacePtr face;
    Lib3dsDword allKeys = 0;
    QArray<int> vlist = mapFacesToVerts(&allKeys);
    buildAdjacencyMap(vlist);
    QSet<FacePtr> processed;
    ModulateRecord mod;
    qMemSet(&mod, 0, sizeof(struct ModulateRecord));
    mod.key = 1;
    while ((mod.key < allKeys) && !mod.disabled && mod.key)
    {
        if (!(allKeys & mod.key))
        {
            ++mod.key;
            continue;
        }
        QArray<FacePtr> queue;
        Lib3dsDword fptr = 0;
        int head = 0;
        while (true)
        {
            if (head >= queue.size())   // seed another island of faces
            {
                if (!mod.keyFresh && head > 0)  // if not first time thru, did we use last key?
                    allKeys = allKeys | mod.altKey;
                FacePtr qf = 0;
                for (  ; fptr < m_mesh->faces; ++fptr)
                {
                    qf = &m_mesh->faceL[fptr];
                    if ((mod.key & qf->smoothing) && !processed.contains(qf))
                        break;
                }
                if (fptr == m_mesh->faces)
                    break;
                queue.append(qf);
                if (!mod.keyFresh)
                {
                    nextUnusedKey(&mod, allKeys);
                    if (mod.disabled)
                        break;
                }
            }
            face = queue.at(head++);
            processed.insert(face);
            doNormalCorrect(face);
            doModulate(face, &mod);
            FacePtr *n = reinterpret_cast<FacePtr*>(face->user.p);
            for ( ; *n; ++n)
                if ((mod.key & (*n)->smoothing) && !processed.contains(*n))
                    queue.append(*n);
        }
    }
    if ((m_options & QGL::CorrectNormals) && (m_options & QGL::ShowWarnings))
        qDebug() << "CorrectNormals mode:" << numCorrected << "normals corrected.";
    if ((m_options & QGL::CorrectAcute) && (m_options & QGL::ShowWarnings))
        qDebug() << "CorrectAcute mode:" << mod.numModulated << "normals corrected";
}

int QGL3dsMesh::cachedMaterialLookup(const char *material)
{
    static bool initialized = false;
    static int lastLookup = -1;
    static char lastName[512];
    if (!initialized)
    {
        ::memset(lastName, 0, 512);
        initialized = true;
    }
    if (qstrncmp(lastName, material, 510) != 0)
    {
        lastLookup = sceneNode()->palette()->indexOf(QString::fromLatin1(material));
        qstrncpy(lastName, material, 510);
    }
    return lastLookup;
}

/*!
    \internal
    Find material indexes and smoothing groups used in this mesh.  If any
    face has no material assigned then a -1 index will be listed.
    Also figures out how complex the mesh is, by finding the count of
    smoothing groups for the material with the greatest number of groups.
    If the mesh is faceted (no smoothing) but has greater than FACETED_THRESHOLD
    faces then smoothing is forced on and the mesh rescanned.

*/
void QGL3dsMesh::analyzeMesh()
{
    QGLMaterialCollection *pal = sceneNode()->palette();
    Lib3dsFace *face;
    Lib3dsDword allKeys = 0;
    m_smoothingGroupCount = 0;
    m_hasZeroSmoothing = false;
    m_plainMaterials.clear();
    m_textureMaterials.clear();
    for (Lib3dsDword f = 0; f < m_mesh->faces; ++f)
    {
        face = &m_mesh->faceL[f];
        int matIx = cachedMaterialLookup(face->material);
#ifndef QT_NO_DEBUG_STREAM
        if (matIx == -1 && strlen(face->material) != 0 && (m_options & QGL::ShowWarnings))
            qDebug("Bad .3ds file: no material %s! (Referenced in mesh %s)\n",
                     face->material, m_mesh->name);
#endif
        if (face->smoothing)
        {
            if ((face->smoothing & allKeys) != face->smoothing)
            {
                Lib3dsDword key = 1;
                while (key)
                {
                    if ((key & face->smoothing) && !(allKeys & key))
                    {
                        allKeys = allKeys | key;
                        m_smoothingGroupCount += 1;
                    }
                    key <<= 1;
                }
            }
        }
        else
        {
            m_hasZeroSmoothing = true;
        }
        if (!m_plainMaterials.contains(matIx) && !m_textureMaterials.contains(matIx))
        {
            QGLMaterial *mat = pal->material(matIx);
            if (mat && mat->texture())
                m_textureMaterials.insert(matIx);
            else
                m_plainMaterials.insert(matIx);
        }
    }
    m_smoothingGroups = allKeys;
}

/*!
    \internal
    Check the meshes textures and update the record of whether this mesh
    has textures or not.  In debug mode issue a warning if the textures are
    corrupt (number of texels and vertices not equal).
*/
void QGL3dsMesh::checkTextures(int material)
{
    QGLMaterial *mat = sceneNode()->palette()->material(material);
    QGLTexture2D *tex = (mat ? mat->texture() : 0);
    m_hasTextures = false;
    if (tex)
    {
        m_hasTextures = true;
#ifndef QT_NO_DEBUG_STREAM
        if (m_mesh->points != m_mesh->texels)
            qWarning("Mesh %s has unequal number of texels (%d) and vertices (%d)",
                     m_mesh->name, m_mesh->texels, m_mesh->points);
#endif
        // all texture coordinates from 3ds have to be flipped because
        // 3ds uses the correct coordinate system, whilst qt uses
        // upside-down coordinates
        m_texFlip = (tex->bindOptions() & QGLTexture2D::InvertedYBindOption) == 0;
    }
}

/*!
    \internal
    Returns any local transformation matrix for the mesh.
*/
QMatrix4x4 QGL3dsMesh::meshMatrix() const
{
    Lib3dsMatrix &m = m_mesh->matrix;  // typedef for float[4][4]
    QMatrix4x4 mat;
    for (int col = 0; col < 4; ++col)
        for (int row = 0; row < 4; ++row) {
            float e = m[col][row];
            if (qFuzzyIsNull(e))
                mat(row, col) = 0.0f;
            else
                mat(row, col) = e;
        }
    mat.optimize();  // setup to use optimizations
    if (mat.isIdentity())
        return mat;
    // The reverse transform is what we apply to model-view in order
    // to draw the underlying geometry
    bool invertible = true;
    mat = mat.inverted(&invertible);
    if (invertible)
         return mat;
    if (m_options & QGL::ShowWarnings)
        qWarning("Could not invert matrix for mesh %s", m_mesh->name);
    return QMatrix4x4();
}

/*!
    \internal
    Generate the vertices for the faces based on their smoothing keys and
    the current nodes material.
*/
void QGL3dsMesh::generateVertices()
{
    int matIx = currentNode()->materialIndex();
    int keyCount = m_smoothingGroupCount;
    if (m_hasZeroSmoothing)
        ++keyCount;
    QString baseName = currentNode()->objectName();
    Lib3dsDword key = 0;
    while (key <= m_smoothingGroups)
    {
        if ((key & m_smoothingGroups) || ((key == 0) && m_hasZeroSmoothing))
        {
            if (key == 0)
                currentSection()->setSmoothing(QGL::Faceted);
            keyCount -= 1;
            currentNode()->setMaterialIndex(matIx);
            currentNode()->setObjectName(baseName + QLatin1String("::") + QString::number(key));
            QGeometryData tri;
            int cur = 0;
            for (Lib3dsDword f = 0; f < m_mesh->faces; ++f)
            {
                Lib3dsFace *face = &m_mesh->faceL[f];
                int faceMat = cachedMaterialLookup(face->material);
                if (faceMat == matIx &&
                    ((key & face->smoothing) || (key == 0 && face->smoothing == 0)))
                {
                    QVector3D norm = l2v(face->normal);
                    for (int i = 0; i < 3; ++i)
                    {
                        int a = face->points[i];
                        Lib3dsVector &l3a = m_mesh->pointL[a].pos;
                        tri.appendVertex(l2v(l3a));
                        tri.appendNormal(norm);
                        if (m_hasTextures)
                        {
                            Lib3dsTexel &t0 = m_mesh->texelL[a];
                            tri.appendTexCoord(QVector2D(t0[0], m_texFlip ? 1.0f - t0[1] : t0[1]));
                        }
                        if (m_options & QGL::NativeIndices)
                            currentSection()->appendSmooth(tri.logicalVertexAt(cur++), a);
                    }
                }
            }
            if (m_options & QGL::NativeIndices)
                currentNode()->setCount(cur);
            else
                addTriangles(tri);
            if (keyCount > 0)
                newSection(QGL::Smooth);
            else
                break;
        }
        if (key == 0)
            key = 1;
        else
            key <<= 1;
    }
}