opengl - GLSL - Weird syntax error "<" -
i'm trying use shader keeps telling me error on both fragment , vertex shader:
error(#132) syntax error: "<" parse error
vertex shader
varying vec4 diffuse; varying vec4 ambient; varying vec3 normal; varying vec3 halfvector; void main() { normal = normalize(gl_normalmatrix * gl_normal); halfvector = gl_lightsource[0].halfvector.xyz; diffuse = gl_frontmaterial.diffuse * gl_lightsource[0].diffuse; ambient = gl_frontmaterial.ambient * gl_lightsource[0].ambient; ambient += gl_lightmodel.ambient * gl_frontmaterial.ambient; gl_position = ftransform(); }
fragment shader
varying vec4 diffuse,ambient; varying vec3 normal,halfvector; void main() { vec3 n,halfv,lightdir; float ndotl,ndothv; lightdir = vec3(gl_lightsource[0].position); vec4 color = ambient; n = normalize(normal); ndotl = max(dot(n,lightdir),0.0); if (ndotl > 0.0) { color += diffuse * ndotl; halfv = normalize(halfvector); ndothv = max(dot(n,halfv),0.0); color += gl_frontmaterial.specular * gl_lightsource[0].specular * pow(ndothv, gl_frontmaterial.shininess); } gl_fragcolor = color; }
code read shaders:
bool shader::load(string vertex , string fragment) { // these hold shader's text file data string vshader, fshader; // make sure user passed in vertex , fragment shader file if(!vertex.length() || !fragment.length()) return false; // if of our shader pointers set, let's free them first. if(vertexshader || fragmentshader || programobject) release(); // here pointer our vertex , fragment shaders vertexshader = glcreateshader(gl_vertex_shader); fragmentshader = glcreateshader(gl_fragment_shader); // load shaders respective files , store in string. vshader = loadtextfile(vertex.c_str()); fshader = loadtextfile(fragment.c_str()); if(vshader == "" || fshader == "") return false; // quick switch can double pointer below const char *vvshader = vshader.c_str(); const char *ffshader = fshader.c_str(); // assigns shader text file each shader pointer glshadersource(vertexshader, 1, &vvshader, null); glshadersource(fragmentshader, 1, &ffshader, null); // compile shader's code glcompileshader(vertexshader); glcompileshader(fragmentshader); printinfolog(vertexshader); printinfolog(fragmentshader); // next create program object represent our shaders programobject = glcreateprogram(); // attach each shader loaded our program object glattachshader(programobject, vertexshader); glattachshader(programobject, fragmentshader); // our last init function link our program object opengl gllinkprogram(programobject); printinfolog(programobject); // now, let's turn off our current shader. gluseprogram(0); return true; } string shader::loadtextfile(string file) { // open file passed in ifstream fin(file.c_str()); // make sure opened file correctly if(!fin) return ""; string line = ""; string text = ""; // go through , store each line in text file within "string" object while(getline(fin, line)) { text = text + "\n" + line; } // close our file fin.close(); // return text file's data return text; }
however, if try shader works fine! don't know why.
your error , lack of <
token in shader sources suggests glshadersource reading trailing garbage @ end of strings.
this sounds subtle problem known seasoned developers can stump newbies. glshadersource either expects 0 terminated strings , null pointer length array or you're passing array lengths strings don't need 0 terminated. technically std::string::c_str should give access 0 terminated string, seems in case doesn't.
anyway simple solution provide length array, glshadersource doesn't read trailing garbage:
// quick switch can double pointer below const char *vvshader = vshader.c_str(); const char *ffshader = fshader.c_str(); const glint lenvshader = vshader.length(); const glint lenfshader = fshader.length(); // assigns shader text file each shader pointer glshadersource(vertexshader, 1, &vvshader, &lenvshader); glshadersource(fragmentshader, 1, &ffshader, &lenfshader);
Comments
Post a Comment