ShaderControlFile.cc 1.99 KB
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1
2
3
4
5
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

Robert Menzel's avatar
Robert Menzel committed
6
7
#include <ACGL/OpenGL/Controller/ShaderControlFile.hh>
#include <ACGL/Base/StringOperations.hh>
Robert Menzel's avatar
Robert Menzel committed
8

Robert Menzel's avatar
Robert Menzel committed
9
10
using namespace ACGL::Base;
using namespace ACGL::OpenGL;
Robert Menzel's avatar
Robert Menzel committed
11
12
13
14
15

SharedShader ShaderControlFile::create(void)
{
    updateFileModificationTime();

16
    if(mType == GL_INVALID_ENUM)
Robert Menzel's avatar
Robert Menzel committed
17
18
19
20
21
22
23
    {
        std::string filename;
        std::string extension;
        if(!StringOperations::splitFileExtension(mFilename, filename, extension))
            return SharedShader();

        if(extension == "vsh")
24
            mType = GL_VERTEX_SHADER;
25
26
27
28
        else if(extension == "tcsh")
            mType = GL_TESS_CONTROL_SHADER;
        else if(extension == "tesh")
            mType = GL_TESS_EVALUATION_SHADER;
29
30
        else if(extension == "gsh")
            mType = GL_GEOMETRY_SHADER;
Robert Menzel's avatar
Robert Menzel committed
31
        else if(extension == "fsh")
32
            mType = GL_FRAGMENT_SHADER;
Robert Menzel's avatar
Robert Menzel committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
        else
            return SharedShader();
    }

    SharedShader shader(new Shader(mType));
    if(shader->setFromFile(mFullFilePath))
        return shader;
    return SharedShader();
}

bool ShaderControlFile::update(SharedShader& shader)
{
    if(fileIsUpToDate())
        return false;

Robert Menzel's avatar
Robert Menzel committed
48
49
50
51
52
53
54
55
56
57
58
59
60
    { // try to compile the source in another shader, only proceed if that worked!
        Shader dummy( shader->getType() );
        if (!dummy.setFromFile(mFullFilePath)) {

            // we had a shader compile error, update the timestamp to prevent a second try
            // of compiling the exact same (non-working) shader again
            updateFileModificationTime();
            return false;
        }
    }

    // it worked, so load the source in "our" shader:
    shader->setFromFile(mFullFilePath);
Robert Menzel's avatar
Robert Menzel committed
61
62
63
64

    updateFileModificationTime();
    return true;
}