我正在尝试使用 GLFW/GLEW 添加着色器。
我收到一个错误,即着色器已加载,但它们没有有效的目标代码。
这是我用于加载着色器的代码:
class SHADER {
public:
void LoadShaders(const char *vertexFile, const char *fragmentFile);
char *vertexShader;
char *fragmentShader;
private:
int Load(const char *filename, char*&shaderSource);
fstream file;
};
int SHADER::Load(const char *filename, char *&shaderSource) {
file.open(filename, ios::in);
if(file.is_open()) {
file.tellg();
file.seekg(0,ios::end);
unsigned long len = file.tellg();
file.seekg(ios::beg);
if(len == 0 ) {
return -2;
} else {
shaderSource = new char[len + 1];
file.read(shaderSource,len);
file.close();
printf("%s\n",shaderSource);
}
} else {
return -1;
}
return 0;
}
void SHADER::LoadShaders(const char *vertexFile, const char *fragmentFile) {
int resultVertex = this->Load(vertexFile, vertexShader);
int resultFragment = this->Load(fragmentFile, fragmentShader);
if(resultVertex ==0 && resultFragment ==0) {
printf("Shaders loaded succesfully.\n");
}
if(resultVertex == -2) {
printf("VertexShader is empty!\n");
}
if(resultFragment == -2) {
printf("FragmentShader is empty!\n");
}
if(resultVertex == -1) {
printf("Unable to load VertexShader!\n");
}
if(resultFragment == -1) {
printf("Unable to load FragmentShader!\n");
}
}
这是初始化着色器的代码:
SHADER Shaders;
GLhandleARB vertexShader, fragmentShader, shaderProgram;
Shaders.LoadShaders("vertexShader.vert","fragmentShader.frag");
const char* vertTemp = Shaders.vertexShader;
vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
glShaderSourceARB(vertexShader, 1, &vertTemp, NULL);
glCompileShaderARB(vertexShader);
traceShaderInfoLog(vertexShader);
const char* fragTemp = Shaders.fragmentShader;
fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
glShaderSourceARB(fragmentShader, 1, &fragTemp, NULL);
glCompileShaderARB(fragmentShader);
traceShaderInfoLog(fragmentShader);
delete[] Shaders.vertexShader;
delete[] Shaders.fragmentShader;
shaderProgram = glCreateProgramObjectARB();
glAttachObjectARB(shaderProgram,vertexShader);
glAttachObjectARB(shaderProgram,fragmentShader);
glLinkProgramARB(shaderProgram);
traceProgramInfoLog(shaderProgram);
glUseProgramObjectARB(shaderProgram);
这是vertexShader.vert和fragmentShader.frag:
void main(){
gl_Position = ftransform();
}
void main(){
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
这是我打印日志时遇到的错误,我在脚本末尾得到这个非常奇怪的符号,每次我再次编译另一个符号时:
void main(){
//gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_Position = ftransform();
}┘
void main(){
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}♣
Shaders loaded succesfully.
Vertex shader was successfully compiled to run on hardware.
Fragment shader failed to compile with the following errors:
ERROR: 0:3: error(#132) Syntax error: '<' parse error
ERROR: error(#273) 1 compilation errors. No code generated
Fragment shader(s) were not successfully compiled before glLinkProgram() was called.
Link failed.