我在 OpenGL 下编译顶点着色器时遇到了一些问题。我有一个非常标准的顶点着色器:
#version 330
layout(location=0) in vec4 in_Position;
layout(location=1) in vec4 in_Color;
out vec4 ex_Color;
void main(void)
{
gl_Position = in_Position;
ex_Color = in_Color;
}
我的着色器加载功能看起来像:
string temp = LoadFile(vShaderPath);
const char* vShaderString = temp.c_str();
const char* vShaderPathC = vShaderPath.c_str();
fprintf(stderr, "File: %s \nContents: %s\n", vShaderPathC, vShaderString);
temp = LoadFile(fShaderPath);
const char* fShaderString = temp.c_str();
vShaderHandle = glCreateShader(GL_VERTEX_SHADER);
fShaderHandle = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vShaderHandle, 1, &vShaderString, NULL);
glShaderSource(fShaderHandle, 1, &fShaderString, NULL);
GLint compiled;
glCompileShader(vShaderHandle);
glCompileShader(fShaderHandle);
glGetShaderiv(vShaderHandle, GL_COMPILE_STATUS, &compiled);
if(compiled == false)
{
fprintf(stderr, "ERROR: Vertex shader not compiled properly.\n");
GLint blen = 0;
GLsizei slen = 0;
glGetShaderiv(vShaderHandle, GL_INFO_LOG_LENGTH , &blen);
if (blen > 1)
{
GLchar* compiler_log = new GLchar[blen];
glGetInfoLogARB(vShaderHandle, blen, &slen, compiler_log);
fprintf(stderr, "compiler log:\n %s", compiler_log);
delete [] compiler_log;
}
}
但是当我运行我的程序时,我得到以下输出:
INFO: OpenGL Version: 3.3.0 NVIDIA 310.19
File: vShader.v.glsl
Contents: #version 330
layout(location=0) in vec4 in_Position;
layout(location=1) in vec4 in_Color;
void main(void)
{
gl_Position = in_Position;
ex_Color = in_Color;
}
ERROR: Vertex shader not compiled properly.
compiler log:
(0) : error C0000: syntax error, unexpected $end at token "<EOF>"
加载文件定义:
string ShaderEffect::LoadFile(string path)
{
ifstream in(path.c_str(), ios::in);
if(in.is_open())
{
string contents;
in.seekg(0, ios::end);
contents.resize(in.tellg());
in.seekg(0, ios::beg);
in.read(&contents[0], contents.size());
in.close();
return contents;
}
else
throw "Problem reading file!";
}
我知道 glsl 代码本身不是问题,因为我已将字符串硬编码为:
const char * testvShader = {
"#version 330\n"\
"layout(location=0) in vec4 in_Position;\n"\
"layout(location=1) in vec4 in_Color;\n"\
"out vec4 ex_Color;"
"void main()\n"\
"{\n"\
" gl_Position = in_Position;\n"\
" ex_Color = in_Color;\n"\
"}"};
当我将 &vShaderString 切换到 &testvShader 时,程序运行良好。但我不明白加载是如何成为问题的,因为片段着色器以相同的方式加载并编译和运行非常好,我在编译之前将文件打印到控制台,它看起来很好。我一头雾水,不知道是什么问题。
PS 我在 Fedora 上运行,如果这很重要的话。