Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8432257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:04:49+00:00 2026-06-10T06:04:49+00:00

My Application is going to be an OpenGL Game using OpenGL 4.2 , GLFW

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:04:51+00:00Added an answer on June 10, 2026 at 6:04 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I stop my application from going back when the user clicks on
I'm developing a web application that is going to be served from an OSGi
I'm using LWJGL and Slick framework to load Textures to my OpenGL-application. I'm using
Im developing a windows forms application using C# 4.0 and that application is going
I am writing an application in C++ using CGL/OpenGL, and need keyboard/mouse input. I
I was trying to use this library to output text on a OpenGL application
I have an application thats going to use Active Directory for Authentication. This will
I want to create a very small application using java to understand about the
I've got a C# application going that needs to send out an html email
In my application there are going to be lots of users, over 500. They

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.