glsl - Light per vertex shader -
i´m trying compile glsl shader looks has wrong, because fails. message error _log gaves me: vertex shader(s) not compiled before gllinkprogram()was called.link failed.
its light per vertex shader:
vec4 ambient() { vec4 ambient = vec4 (0.0); ambient = gl_frontmaterial.ambient * gl_lightsource[0].ambient; ambient += (gl_lightmodel.ambient * gl_frontmaterial.ambient); return ambient; } vec4 diffuse(vec3 normal) { vec3 diffuse = gl_lightsource[0].position * normal; float diff = max(dot(normal,diffuse),0.0); diffuse = gl_lightsource[0].diffuse * gl_frontmaterial.diffuse * diff; return diffuse; } vec4 specular(vec3 normal) { float hv = max(dot(normal, vec3(gl_lightsource[0].halfvector)), 0.0); float spec = pow(hv, gl_frontmaterial.shininess); return gl_lightsource[0].specular * gl_frontmaterial.specular * spec; } void main() { vec3 normal = normalize(gl_normalmatrix * gl_normal); vec4 ambient = ambient(); vec4 diffuse = diffuse(normal); vec4 specular = specular(normal); gl_frontcolor = gl_color*(ambient+difuse)+specular; gl_position = ftransform(); }
this shader class:
#include "shader.h" namespace topicos { #include "shader.h" shader::shader() { program = glcreateprogram(); } shader::~shader() { gldetachshader(program, vertexshader); gldetachshader(program, geometryshader); gldetachshader(program, fragmentshader); gldeleteshader(vertexshader); gldeleteshader(geometryshader); gldeleteshader(fragmentshader); gldeleteprogram(program); } void shader::determinarfontes(std::string vertex, std::string geometry, std::string fragment) { vertexfilename = vertex; geometryfilename = geometry; fragmentfilename = fragment; } void shader::ativarshader() { gllinkprogram(program); gluseprogram(program); programlog(program); shaderlog(program); std::cout<<_log; } void shader::desativarshader() { gluseprogram(0); } void shader::shaderlog(unsigned int obj) { int infologlength = 0; int charswritten = 0; char *infolog; glgetshaderiv(obj, gl_info_log_length, &infologlength); if (infologlength > 0) { infolog = new char[infologlength]; glgetshaderinfolog(obj, infologlength, &charswritten, infolog); std::string str(infolog); if (!str.empty()) { _log += "\n" + str; } else { _log += "done.\n"; } delete [] infolog; } } void shader::programlog(unsigned int obj) { int infologlength = 0; int charswritten = 0; char *infolog; glgetprogramiv(obj, gl_info_log_length, &infologlength); if (infologlength > 0) { infolog = new char[infologlength]; glgetprograminfolog(obj, infologlength, &charswritten, infolog); std::string str(infolog); if (!str.empty()) { _log += "\n" + str; } else { _log += "done.\n"; } delete [] infolog; } } std::string shader::loadshader(std::string shaderfile) { std::string texto = ""; std::ifstream obj; obj.open(shaderfile.c_str(), std::ifstream::in); // std::cout << " ..>> " << obj.is_open() << std::endl; std::string linha; while(std::getline(obj, linha)) { texto += linha + "\n"; } obj.close(); //std::cout<<"texto: " << texto<< std::endl; return texto; } void shader::makevertexshader(const std::string& vertexfilename) { /**this function creates vertex shader, compiles , attached program**/ const char *stringconvertidavertex = loadshader(vertexfilename).c_str(); //std::cout<<"vertex shader: " << loadshader(vertexfilename)<<std::endl; vertexshader = glcreateshader(gl_vertex_shader); glshadersource(vertexshader, 1, &stringconvertidavertex, null); glcompileshader(vertexshader); glattachshader(program, vertexshader); } void shader::makefragmentshader(const std::string& fragmentfilename) { /**this function creates fragment shader, compiles , attached program**/ const char *stringconvertidafragment = loadshader(fragmentfilename).c_str(); fragmentshader = glcreateshader(gl_fragment_shader); glshadersource(fragmentshader, 1, &stringconvertidafragment, null); glcompileshader(fragmentshader); glattachshader(program, fragmentshader); } void shader::makegeometryshader(const std::string& geometryfilename) { /**this function creates fragment shader, compiles , attached program**/ const char *stringconvertidageometry = loadshader(geometryfilename).c_str(); geometryshader = glcreateshader(gl_geometry_shader); glshadersource(geometryshader, 1, &stringconvertidageometry, null); glcompileshader(geometryshader); glattachshader(program, geometryshader); } }
any appreciated.
i think
gl_frontcolor = gl_color*(ambient+difuse)+specular;
should be
gl_frontcolor = gl_color*(ambient+diffuse)+specular;
to let driver display these errors don't have find them yourself, (it's in c, i'm not sure host language using)
// checks if compilation of shader successful. // on success, returns gl_true. on failure, prints // error message stderr , returns gl_false. glint check_shader_compilation_status(gluint shader) { glchar* info_log; glint r; glgetshaderiv(shader, gl_compile_status, &r); if (r == gl_false) { glgetshaderiv(shader, gl_info_log_length, &r); info_log = malloc(sizeof(glchar) * r); glgetshaderinfolog(shader, r, null, info_log); fprintf(stderr, "%s", info_log); free(info_log); return gl_false; } return gl_true;
Comments
Post a Comment