ShaderControlFile.cc 2.03 KB
Newer Older
1
2
3
4
5
/***********************************************************************
 * Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
 * All rights reserved.                                                *
 * Distributed under the terms of the MIT License (see LICENSE.TXT).   *
 **********************************************************************/
Robert Menzel's avatar
Robert Menzel committed
6

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

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

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

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

        if(extension == "vsh")
25
            mType = GL_VERTEX_SHADER;
26
27
28
29
        else if(extension == "tcsh")
            mType = GL_TESS_CONTROL_SHADER;
        else if(extension == "tesh")
            mType = GL_TESS_EVALUATION_SHADER;
30
31
        else if(extension == "gsh")
            mType = GL_GEOMETRY_SHADER;
Robert Menzel's avatar
Robert Menzel committed
32
        else if(extension == "fsh")
33
            mType = GL_FRAGMENT_SHADER;
Robert Menzel's avatar
Robert Menzel committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        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
49
50
51
52
53
54
55
56
57
58
59
60
61
    { // 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
62
63
64
65

    updateFileModificationTime();
    return true;
}