danijar
danijar

Reputation: 34175

Load Shaders using an Object Orientated Approach

My Application is going to be an OpenGL Game using OpenGL 4.2, GLFW and GLEW. The Problem is about loading GLSL shaders from file. I use a class named ShadersObject for all shader related tasks.

class ShadersObject
{
public:
    // ...
private:
    // ...
    void LoadFile(string Name, string Path);
    string FolderPath;
    Shaders ShaderList;
};

The class uses a map of ShaderObjects to store the shaders.

enum ShaderType{ Vertex = GL_VERTEX_SHADER,
                 Geometry = GL_GEOMETRY_SHADER,
                 Fragment = GL_FRAGMENT_SHADER };

struct ShaderObject
{
    const GLchar* File;
    ShaderType Type;
    GLuint Shader;
    // ...
};

typedef map<string, ShaderObject> Shaders;

The following method I should load the source code of the GLSL shader from a file to the ShaderObject in ShaderList. Therefore it uses the Path to the file and the Name of the shader as the string key in the ShaderList map is named.

void ShadersObject::LoadFile(string Name, string Path)
{
    string FilePath = FolderPath + Path;
    ifstream Stream(FilePath);

    if(Stream.is_open())
    {
        cout << "Loading Shader from " << FilePath << endl;

        string Output;
        Stream.seekg(0, ios::end);   
        Output.reserve(Stream.tellg());
        Stream.seekg(0, ios::beg);
        Output.assign((istreambuf_iterator<char>(Stream)),istreambuf_iterator<char>());

        // this line doesn't compile
        strcpy(ShaderList[Name].File, Output.c_str());
    }
    else cout << "Can not load Shader from " << FilePath << endl;
}

I another method I want to use the value from ShaderList[Name].File to load and compile the shaders using glShaderSource and glCompileShader. Where glShaderSource needs a pointer to a const GLchar* holding the source code. In my case this would be &ShaderList[Name].File.

Some time ago I used ShaderList[Name].File = Output.c_str(); instead of the line which doesn't compile now. The result was that only the pointer was saved in the ShaderList and after the method exited the source code was gone. That's why I tried using strcpy but this function doesn't accept const GLchar* as parameter type.

How can I save the read file into my class member?

Upvotes: 0

Views: 993

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473322

This is not a complex problem: store the string. Store Output in your ShadersObject class. When you need to call glShaderSource, then convert it into a const GLchar* with string::c_str.

Or better yet; don't store either of them; OpenGL will retain the shader string in the shader object. You can query the shader strings from OpenGL via the API. So there's really no point in your shader object storing what OpenGL already stores.

Upvotes: 3

Related Questions