我正在尝试从如下所示的文件中读取顶点和片段着色器
#version 330 core
in vec3 ourColor;
out vec4 color;
void main()
{
color = vec4(ourColor, 1.0f);
}
but when i'm compiling shader i get the error like 'bad syntax'.
从文件读取着色器代码的代码
const GLchar* readFromFile(const GLchar* pathToFile)
{
std::string content;
std::ifstream fileStream(pathToFile, std::ios::in);
if(!fileStream.is_open()) {
std::cerr << "Could not read file " << pathToFile << ". File does not exist." << std::endl;
return "";
}
std::string line = "";
while(!fileStream.eof()) {
std::getline(fileStream, line);
content.append(line + "\n");
}
fileStream.close();
std::cout << "'" << content << "'" << std::endl;
return content.c_str();
}
这里的问题是,字符串content
在函数末尾超出范围,从而删除整个内容。然后,您返回的是指向已释放的内存地址的指针。
const GLchar* readFromFile(const GLchar* pathToFile)
{
std::string content; //function local variable
....
return content.c_str();
} //content's memory is freed here
我在这里看到两种方法可以防止这种情况:返回字符串本身而不是指向其内存的指针,或者在堆上创建一个 GLchar* 数组并将内容复制到那里。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)