当我的着色器文件的所有信息都被正确读取到字符串中并且语法正确时,OpenGL为什么会出现语法错误
Why does OpenGL Give a syntax error when all the info of my shader file is read properly into a string and is syntactically correct
我正在学习learnopengl.com上的教程,它们的着色器已直接放入const char*中。当我试图将相同的代码放入着色器文件中,稍后将其读取为const char*,并且所有代码都相同时,glGetShaderiv()会生成语法错误。
这是错误:错误:0:1:"语法错误:非法的扩展ASCII字符(0xdd)。
然而,当代码直接放入const char*中时,不会出现此错误
这是const char*代码与我的代码
const char*:
const GLchar* vertexShaderSource = "#version 330 coren"
"layout (location = 0) in vec3 position;n"
"void main()n"
"{n"
"gl_Position = vec4(position.x, position.y, position.z, 1.0);n"
"} ";
const GLchar* orangeFragmentShaderSource = "#version 330 coren"
"out vec4 color;n"
"void main()n"
"{n"
"color = vec4(1.0f, 0.5f, 0.2f, 1.0f);n"
"}n ";
const GLchar* yellowFragmentShaderSource = "#version 330 coren"
"out vec4 color;n"
"void main()n"
"{n"
"color = vec4(1.0f, 1.0f, 0.0f, 1.0f); // The color yellow n"
"}n ";
我的顶点文件:
#version 330 core
layout (location = 0) in vec3 position;
void main() {
gl_Position = vec4(position.x, position.y, position.z, 1.0);
}
我的黄色碎片着色器:
#version 430 core
out vec4 colour;
void main() {
colour = vec4(1.0f, 1.0f, 0.0f, 1.0f);
}
我的橙色碎片着色器:
#version 430 core
out vec4 colour;
void main() {
colour = vec4(1.0f, 1.0f, 0.0f, 1.0f);
}
此外,这是我用来将着色器转换为字符串的代码:
std::ifstream shaderFile(filePath);
std::stringstream tmp;
std::string fileContents = "";
std::string line = "";
while (std::getline(shaderFile, line)) fileContents += line + "n";
return fileContents + " ";
这是我试图从文件中读取的另一种方法
std::ifstream shaderFile(filePath);
if (!shaderFile.good()) {
std::cout << "File failed to load..." << filePath << std::endl;
std::system("PAUSE");
std::exit(1);
}
return std::string(std::istreambuf_iterator<char>(shaderFile), std::istreambuf_iterator<char>());
我的着色器编译代码:
tmp = convertShaders("VertexShader.vert");
const GLchar *vertexShaderSource = tmp.c_str();
std::cout << vertexShaderSource << std::endl;
tmp = convertShaders("FragmentShaderYellow.frag");
const GLchar *yellowFragmentShaderSource = tmp.c_str();
tmp = convertShaders("FragmentShaderOrange.frag");
const GLchar *orangeFragmentShaderSource = tmp.c_str();
glShaderSource(vertexShaderID, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShaderID);
glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &success);
glShaderSource(yellowFragmentShaderID, 1, &yellowFragmentShaderSource, NULL);
glCompileShader(yellowFragmentShaderID);
glGetShaderiv(yellowFragmentShaderID, GL_COMPILE_STATUS, &success);
glShaderSource(orangeFragmentShaderID, 1, &orangeFragmentShaderSource, NULL);
glCompileShader(orangeFragmentShaderID);
glGetShaderiv(orangeFragmentShaderID, GL_COMPILE_STATUS, &success);
我不知道为什么我的字符串不起作用。当我将字符串输出到控制台时,一切看起来都一样。有人能帮我吗?
当您调用tmp.c_str()时,您会收到一个指向tmp拥有的字符串的指针。
当您调用tmp=something_else;分配一个新的内存块来保存新字符串,并且指针(例如vertex ShaderSource)现在无效。它被称为悬空指针。
一个简单的解决方案是改变:
tmp = convertShaders("VertexShader.vert");
const GLchar *vertexShaderSource = tmp.c_str();
std::cout << vertexShaderSource << std::endl;
tmp = convertShaders("FragmentShaderYellow.frag");
const GLchar *yellowFragmentShaderSource = tmp.c_str();
tmp = convertShaders("FragmentShaderOrange.frag");
const GLchar *orangeFragmentShaderSource = tmp.c_str();
至:
tmp = convertShaders("VertexShader.vert");
const GLchar *vertexShaderSource = tmp.c_str();
std::cout << vertexShaderSource << std::endl;
std::string tmp2 = convertShaders("FragmentShaderYellow.frag");
const GLchar *yellowFragmentShaderSource = tmp2.c_str();
std::string tmp3 = convertShaders("FragmentShaderOrange.frag");
const GLchar *orangeFragmentShaderSource = tmp3.c_str();
通过使用3个不同的tmpstd::字符串,可以避免旧内容无效的问题。
相关文章:
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 为什么我会收到错误 C2143 语法错误:缺少"*"之前的';'?
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 我该怎么做才能摆脱使用命名空间时的语法错误?
- 如何摆脱错误c2061;语法错误 cout?
- 语法错误:D3DX10Core 中的标识符'TEXTMETRICA'
- 错误 C2146:语法错误:标识符"A1"之前缺少","
- main.cpp(11):错误 C2059:语法错误:"字符串"
- RedisGraph 语法错误在偏移量 8 靠近"创建"
- 如何修复此 FSM C++语法错误 C3867
- 函数错误 C2059:语法错误:'>'不起作用
- 为什么在main()之前断言会导致语法错误"error: expected ')' before numeric constant"?
- c++default-int中的语法错误
- "pip install openexr"上的语法错误:错误的编译器?
- 为什么我收到语法错误:在"*"之前缺少";"
- 错误 C2059:语法错误:从结构成员声明'constant'
- C++语法错误,编译器不会警告或 int v = func(&v) 出错;
- 我正在调试这个C++程序.编译器不再显示语法错误,但存在隐藏的逻辑错误