TextureControlFile.cc 2.78 KB
Newer Older
1
2
3
4
5
6
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

#include <ACGL/OpenGL/Controller/TextureControlFile.hh>
7
#include <ACGL/OpenGL/Controller/DataControlFileFactory.hh>
8
9
10
11
12
13
14
15

using namespace ACGL::OpenGL;

TextureControlFile::TextureControlFile(const std::string& _filename)
:   Resource::FileController<Texture>(_filename, Base::Settings::the()->getFullTexturePath()),
    mDataController(TextureDataControlFileFactory::the()->create(_filename)),
    mMinFilter(0),
    mMagFilter(0),
Robert Menzel's avatar
Robert Menzel committed
16
    mAnisotropicFilter(0.0f),
17
18
19
20
21
22
23
24
25
26
27
28
29
    mWrapS(0),
    mWrapT(0),
    mWrapR(0)
{
    if(!mDataController)
        ACGL::Utils::error() << "No valid texture controller to load the image. Perhaps there is no loader for that file extension: " << _filename << std::endl;
}

TextureControlFile::TextureControlFile(const char* _filename)
:   Resource::FileController<Texture>(std::string(_filename), Base::Settings::the()->getFullTexturePath()),
    mDataController(TextureDataControlFileFactory::the()->create(std::string(_filename))),
    mMinFilter(0),
    mMagFilter(0),
Robert Menzel's avatar
Robert Menzel committed
30
    mAnisotropicFilter(0.0f),
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
    mWrapS(0),
    mWrapT(0),
    mWrapR(0)
{
    if(!mDataController)
        ACGL::Utils::error() << "No valid texture controller to load the image. Perhaps there is no loader for that file extension: " << _filename << std::endl;
}

bool TextureControlFile::load(SharedTexture& texture)
{
    if(!mDataController)
        return false;

    SharedTextureData tempData = mDataController->create();

    if(!tempData)
        return false;

    texture->bind();
    texture->setImageData2D(
            tempData->getData(),
            tempData->getWidth(),
            tempData->getHeight(),
            tempData->getFormat(),
            tempData->getFormat(),
            tempData->getType());
    texture->generateMipmaps();
    return true;
}

SharedTexture TextureControlFile::create(void)
{
    updateFileModificationTime();

    SharedTexture texture(new Texture(GL_TEXTURE_2D));

    texture->bind();
    texture->setMinFilter(mMinFilter == 0 ? GL_LINEAR_MIPMAP_LINEAR : mMinFilter);
    texture->setMagFilter(mMagFilter == 0 ? GL_LINEAR : mMagFilter);
    if(mWrapS > 0) texture->setWrap(mWrapS, mWrapT, mWrapR);

Robert Menzel's avatar
Robert Menzel committed
72
    if(mAnisotropicFilter > 0.0f)
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
        texture->setAnisotropicFilter(mAnisotropicFilter);

    if(!load(texture))
        return SharedTexture();
    return texture;
}

bool TextureControlFile::update(SharedTexture& texture)
{
    if(fileIsUpToDate())
        return false;

    if(!load(texture))
        return false;

    updateFileModificationTime();
    return true;
}