ShaderObject.cpp
Default mainpageApplicationShaderObject.cpp
Description Overview Included files Included by Source
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "ShaderObject.hpp"

ShaderObject::ShaderObject()
{

}

ShaderObject::~ShaderObject()
{
    glDeleteObjectARB(shader);
}

void ShaderObject::loadShader(const char* fileName)
{
    // Check for compilation of the shader
    GLint compiled;

    // Load shader source code
    GLcharARB* source = getFileContents(fileName);

    // Load source code into shaders
    glShaderSourceARB(shader, 1, (const char **)&source, 0);

    // Delete the shader source code
    delete[] source;

    // Compile the shader and print out the log file
    glCompileShaderARB(shader);
    glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &compiled);
    
    assert(compiled && "The shader could not be compiled");
}

GLhandleARB& ShaderObject::getShader()
{
    return(shader);
}

char* ShaderObject::getFileContents(const char* fileName)
{
    int length;
    char* buffer;

    // Load the file
    ifstream input(fileName);

    // Check to see that the file is open
    if (!input.is_open())
        return(NULL);

    // Get length of file:
    input.seekg(0, ios::end);
    length = input.tellg();
    input.seekg(0, ios::beg);

    // Allocate memory:
    buffer = new char[length];

    // Read data as a block:
    input.getline(buffer, length, '\0');

    // Close the input file
    input.close();

    // Return the shader code
    return(buffer);
}