VertexArrayObjectLoadStore.cc 2.38 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
10
#include <ACGL/Utils/FileHelpers.hh>
#include <ACGL/Utils/StringHelpers.hh>
11
12
13
14
15
16
17
18

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

namespace ACGL{
namespace OpenGL{

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

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

    bool saveCachedVAO = false;
    if(_caching)
31
    {
32
        // See whether a cached version of the file is available
33
34
        if(FileHelpers::fileExists(_filename + ".vao")
        && FileHelpers::getFileModificationTime(_filename + ".vao") > FileHelpers::getFileModificationTime(_filename))
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
        {
            // 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")
    {
51
        vao = loadVertexArrayObjectFromVAO(loadFileName);
52
53
54
    }
    else
    {
55
        // Generic load: Try to load the file as an ArrayBuffer and attach it to a VAO
56
        SharedArrayBuffer ab = loadArrayBuffer(loadFileName);
57
58
        if(ab)
        {
59
            vao = SharedVertexArrayObject(new VertexArrayObject);
60
61
            vao->attachAllAttributes(ab);
        }
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");
    }

71
72
73
#ifdef ACGL_OPENGL_DEBUGGER_SUPPORT
    glObjectLabel( GL_VERTEX_ARRAY, vao->getObjectName(), -1, _filename.c_str() );
#endif
74
    return vao;
75
76
77
78
}

}
}