Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
744 views
in Technique[技术] by (71.8m points)

opengl - GLSL - Weird syntax error "<"

I'm trying to use a shader but it keeps telling me this error on both fragment and 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 to read shaders:

bool Shader::load(string vertex , string fragment)
{
    // These will hold the shader's text file data
    string vshader, fshader;

    // Make sure the user passed in a vertex and fragment shader file
    if(!vertex.length() || !fragment.length()) return false;

    // If any of our shader pointers are set, let's free them first.
    if(VertexShader || FragmentShader || ProgramObject) Release();

    // Here we get a pointer to our vertex and fragment shaders
    VertexShader = glCreateShader(GL_VERTEX_SHADER);
    FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    // Now we load the shaders from the respective files and store it in a string.
    vshader = LoadTextFile(vertex.c_str());
    fshader = LoadTextFile(fragment.c_str());

    if(vshader == "" || fshader == "") return false;

    // Do a quick switch so we can do a double pointer below
    const char *vvshader = vshader.c_str();
    const char *ffshader = fshader.c_str();

    // Now this assigns the shader text file to each shader pointer
    glShaderSource(VertexShader, 1, &vvshader, NULL);
    glShaderSource(FragmentShader, 1, &ffshader, NULL);

    // Now we actually compile the shader's code
    glCompileShader(VertexShader);
    glCompileShader(FragmentShader);

    printInfoLog(VertexShader);
    printInfoLog(FragmentShader);

    // Next we create a program object to represent our shaders
    ProgramObject = glCreateProgram();

    // We attach each shader we just loaded to our program object
    glAttachShader(ProgramObject, VertexShader);
    glAttachShader(ProgramObject, FragmentShader);

    // Our last init function is to link our program object with OpenGL
    glLinkProgram(ProgramObject);

    printInfoLog(ProgramObject);

    // Now, let's turn off our current shader.
    glUseProgram(0);

    return true;

}


string Shader::LoadTextFile(string file)
{
    // Open the file passed in
    ifstream fin(file.c_str());

    // Make sure we opened the file correctly
    if(!fin) return "";

    string line = "";
    string text = "";

    // Go through and store each line in the text file within a "string" object
    while(getline(fin, line))
    {
        text = text + "
" + line;
    }

    // Close our file
    fin.close();

    // Return the text file's data
    return text;
}

However, if I try another shader is works just fine! I really don't know why.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your error and the lack of a < token in your shader sources suggests that glShaderSource is reading into trailing garbage at the end of the strings.

This sounds like a subtle problem known to the seasoned developers but that can stump the newbies. glShaderSource either expects zero terminated strings and a NULL pointer for the length array or you're passing an array with lengths so that the strings don't need to be 0 terminated. Technically std::string::c_str should give access to a zero terminated string, but it seems in your case it doesn't.

Anyway the simple solution is to provide a length array, so that glShaderSource doesn't read into trailing garbage:

// Do a quick switch so we can do a 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();

// Now this assigns the shader text file to each shader pointer
glShaderSource(VertexShader, 1, &vvshader, &lenvshader);
glShaderSource(FragmentShader, 1, &ffshader, &lenfshader);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...