VertexArrayObjectLoadStore.cc 2.25 KB
Newer Older
1
2
3
4
5
6
7
8
/***********************************************************************
 * Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
 * All rights reserved.                                                *
 * Distributed under the terms of the MIT License (see LICENSE.TXT).   *
 **********************************************************************/

#include <ACGL/OpenGL/Data/VertexArrayObjectLoadStore.hh>
#include <ACGL/OpenGL/Data/ArrayBufferLoadStore.hh>
9
#include <ACGL/Base/FileHelpers.hh>
10
11
12
13
14
15
16
17

using namespace ACGL;
using namespace ACGL::OpenGL;
using namespace ACGL::Utils;

namespace ACGL{
namespace OpenGL{

18
SharedVertexArrayObject loadVertexArrayObject(const std::string& _filename, bool _caching)
19
{
20
    // Lower case file ending:
21
    std::string fileEnding = Base::FileHelpers::getFileEnding(_filename);
22
    std::string loadFileName = _filename;
23

24
    // Never cache VAO files
25
    if(fileEnding == "vao")
26
27
28
29
        _caching = false;

    bool saveCachedVAO = false;
    if(_caching)
30
    {
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
        // See whether a cached version of the file is available
        if(ACGL::Base::FileHelpers::fileExists(_filename + ".vao")
        && ACGL::Base::FileHelpers::getFileModificationTime(_filename + ".vao") > ACGL::Base::FileHelpers::getFileModificationTime(_filename))
        {
            // If so, load the cached file instead
            loadFileName = _filename + ".vao";
            fileEnding = "vao";
        }
        else
        {
            // Otherwise, create a cached copy after loading
            saveCachedVAO = true;
        }
    }

    // Load the VAO
    SharedVertexArrayObject vao;
    if(fileEnding == "vao")
    {
50
        vao = loadVertexArrayObjectFromVAO(loadFileName);
51
52
53
    }
    else
    {
54
        // Generic load: Try to load the file as an ArrayBuffer and attach it to a VAO
55
        SharedArrayBuffer ab = loadArrayBuffer(loadFileName);
56
57
        if(ab)
        {
58
            vao = SharedVertexArrayObject(new VertexArrayObject);
59
60
            vao->attachAllAttributes(ab);
        }
61
    }
62
63
64
65
66
67
68
69
70

    // If necessary, store a cached copy to disk
    if(vao && saveCachedVAO)
    {
        ACGL::Utils::debug() << "Saving a cached copy of " << _filename << " ..." << std::endl;
        saveVertexArrayObjectToVAO(vao, _filename + ".vao");
    }

    return vao;
71
72
73
74
}

}
}