我的应用程序将成为一个使用OpenGL 4.2和GLFW的OpenGL 游戏GLEW。问题是关于从文件加载 GLSL 着色器。我使用一个ShadersObject为所有着色器相关任务命名的类。
class ShadersObject
{
public:
// ...
private:
// ...
void LoadFile(string Name, string Path);
string FolderPath;
Shaders ShaderList;
};
该类使用ShaderObjects 的映射来存储着色器。
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;
以下方法我应该将 GLSL 着色器的源代码从文件加载ShaderObject到ShaderList. 因此,它使用Path文件和着色器的 ,作为映射中Name的字符串键被命名。ShaderList
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;
}
我想使用另一种方法来使用 fromShaderList[Name].File加载和编译着色器glShaderSourceand glCompileShader。whereglShaderSource需要一个指向const GLchar*持有源代码的指针。就我而言,这将是&ShaderList[Name].File.
前段时间我使用ShaderList[Name].File = Output.c_str();了而不是现在无法编译的行。结果是只保存了指针,ShaderList方法退出后源代码消失了。这就是我尝试使用strcpy但此函数不接受const GLchar*作为参数类型的原因。
如何将读取的文件保存到我的班级成员中?