c++ - OpenGL shader(s) refusing to link -
problem:
been struggling code load external shaders , not working. no matter how rewrite code , try again same error every time.
the shaders compile not link, there doing wrong in function? error catching blocks work great btw.
fragment shader(s) failed link, vertex shader(s) failed link. error: error(#280) not shaders have valid object code. error: error(#280) not shaders have valid object code.
i'm using freeglut, x64 windows 7 , mingw (and not using ide)
usage:
in mainloop have gluint programid = loadshader("vertex.shader", "frag.shader");
enable 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 , 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 read files if failed open. right you're passing empty strings glshadersource
.
change is_open
test (better yet, check good()
instead of is_open()
), , things 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 read whole file, @ better ways in this question.
Comments
Post a Comment