Problem:
Been struggling to get my code to load external shaders and it is not working. No matter how i rewrite the code and try again i get the same error every time.
The shaders compile but will not link, is there anything that i am doing wrong in this function? The error catching blocks work great btw.
Fragment shader(s) failed to link, vertex shader(s) failed to link.
ERROR: error(#280) Not all shaders have valid object code.
ERROR: error(#280) Not all shaders have valid object code.
I'm using freeglut, x64 windows 7 and mingw (and I am not using an IDE)
Usage:
In my mainloop I have GLuint programId = loadShader("vertex.shader", "frag.shader");
then I enable it with glUseProgram();
GLuint loadShader(const char * vertex_file_path, const char * fragment_file_path) {
GLuint vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
GLuint programId = glCreateProgram();
GLint status = GL_FALSE;
int infoLogLength;
std::string line = "";
//read in and compile
std::string vertexShaderCode = "";
std::ifstream vifs(vertex_file_path);
if(!vifs.is_open()) {
while(getline(vifs, line))
vertexShaderCode += line +"\n";
vifs.close();
}
const char * vertexStream = vertexShaderCode.c_str();
std::string fragmentShaderCode = "";
std::ifstream fifs(fragment_file_path);
if(!fifs.is_open()) {
while(getline(fifs, line))
fragmentShaderCode += line +"\n";
fifs.close();
}
const GLchar * fragmentStream = fragmentShaderCode.c_str();
glShaderSource(vertexShaderId, 1, &vertexStream, NULL);
glCompileShader(vertexShaderId);
glShaderSource(fragmentShaderId, 1, &fragmentStream, NULL);
glCompileShader(fragmentShaderId);
glGetShaderiv(vertexShaderId, GL_COMPILE_STATUS, &status);
glGetShaderiv(vertexShaderId, GL_INFO_LOG_LENGTH, &infoLogLength);
std::vector<char> vertexShaderErrorMessage(infoLogLength);
glGetShaderInfoLog(vertexShaderId, infoLogLength, NULL, &vertexShaderErrorMessage[0]);
fprintf(stdout, "%s\n", &vertexShaderErrorMessage[0]);
glGetShaderiv(fragmentShaderId, GL_COMPILE_STATUS, &status);
glGetShaderiv(fragmentShaderId, GL_INFO_LOG_LENGTH, &infoLogLength);
std::vector<char> fragmentShaderErrorMessage(infoLogLength);
glGetShaderInfoLog(fragmentShaderId, infoLogLength, NULL, &fragmentShaderErrorMessage[0]);
fprintf(stdout, "%s\n", &fragmentShaderErrorMessage[0]);
glAttachShader(programId, vertexShaderId);
glAttachShader(programId, fragmentShaderId);
glLinkProgram(programId);
glUseProgram(programId);
glGetProgramiv(programId, GL_LINK_STATUS, &status);
glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &infoLogLength);
std::vector<char> programErrorMessage(std::max(infoLogLength, int(1)) );
glGetProgramInfoLog(programId, infoLogLength, NULL, &programErrorMessage[0]);
fprintf(stdout, "%s\n", &programErrorMessage[0]);
glDeleteShader(vertexShaderId);
glDeleteShader(fragmentShaderId);
return programId;
}
[vertex.shader]
#version 400
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;
}
frag.shader
#version 400
in vec4 ex_Color;
out vec4 out_Color;
void main(void) {
out_Color = ex_Color;
}
You only read the files if they failed to open. Right now you're passing empty strings to glShaderSource
.
Change the is_open
test (better yet, check good()
instead of is_open()
), and things will probably work.
Change
std::ifstream vifs(vertex_file_path);
if(!vifs.is_open()) { // <<=== BACKWARDS!
while (getline(vifs, line))
vertexShaderCode += line + "\n";
vifs.close();
}
to
{
std::ifstream vifs(vertex_file_path);
while(getline(vifs, line))
vertexShaderCode += line +"\n";
}
Since you're trying to read the whole file, look at some better ways in this question.