CppMonster
CppMonster

Reputation: 1276

Error in Shader Source

I've got a problem with a shader program. It comunicates with messages:

Vertex info

(0) : error C0000: syntax error, unexpected $end at token

Fragment info

(0) : error C0000: syntax error, unexpected $end at token

It's my Shader Loading function. Probably in this function there is somelike error (?).

GLuint ConfigureShaders(string vsName, string fsName)
{
    // Utworz Shadery
    VS = glCreateShader(GL_VERTEX_SHADER);
    FS = glCreateShader(GL_FRAGMENT_SHADER);
    // Czytaj Shadery
    string LineBuffer;

    fstream VSFile(vsName.c_str(),ios::in);
    string VSSource;
    if(VSFile.is_open())
    {
        while(getline(VSFile,LineBuffer))
            VSSource.append("\n"+LineBuffer);
        VSFile.close();
    }

    fstream FSFile(fsName.c_str(),ios::in);
    string FSSource;
    if(FSFile.is_open())
    {
        LineBuffer.clear();
        while(getline(FSFile,LineBuffer))
            FSSource.append("\n"+LineBuffer);
        FSFile.close();
    }

    GLint result;
    // int infoLogLength;
    // Wprowadz, Kompiluj i Sprawdz Shadery
    char const* VSSrc = VSSource.c_str();
    glShaderSource(GL_VERTEX_SHADER,1,&VSSrc,NULL);
    glCompileShader(VS);
    glGetShaderiv(VS,GL_COMPILE_STATUS,&result);
    // glGetShaderiv(VS,GL_INFO_LOG_LENGTH,&infoLogLength);

  char VSErrMsg[256];
    glGetShaderInfoLog(VS,1024,NULL,VSErrMsg);
    cout<<VSErrMsg<<endl;

    char const* FSSrc = FSSource.c_str();
    glShaderSource(GL_FRAGMENT_SHADER,1,&FSSrc,NULL);
    glCompileShader(FS);
  glGetShaderiv(VS,GL_COMPILE_STATUS,&result);
    // glGetShaderiv(VS,GL_INFO_LOG_LENGTH,&infoLogLength);

  char FSErrMsg[256];
    glGetShaderInfoLog(FS,1024,NULL,FSErrMsg);
    cout<<FSErrMsg<<endl;

    // Polacz Shadery
    GLuint Shader = glCreateProgram();
    glAttachShader(Shader,VS);
    glAttachShader(Shader,FS);
    glLinkProgram(Shader);
    // Sprawdz Program
    glGetProgramiv(Shader,GL_LINK_STATUS,&result);
    // glGetProgramiv(Shader,GL_INFO_LOG_LENGTH,&infoLogLength);

    char PrgErrMsg[256];
    glGetProgramInfoLog(Shader,1024,NULL,PrgErrMsg);
    cout<<PrgErrMsg<<endl;

  // Kasuj Shadery
    glDeleteShader(VS);
    glDeleteShader(FS);
    return Shader;
}

Shader Sources are simple and seem to be good:

VS:

#version 400
layout(location=0) vec3 vertexPos;

void main()
{
  gl_Position.xyz = vertexPos;
  gl_Position.w = 1.0f;
}

FS:

#version 400

out vec3 color;

void main()
{
  color = vec3(0,0,1);
}

So error I have in that lines:

char const* VSSrc = VSSource.c_str();
glShaderSource(GL_VERTEX_SHADER,1,&VSSrc,NULL);
...
char const* FSSrc = FSSource.c_str();
glShaderSource(GL_FRAGMENT_SHADER,1,&FSSrc,NULL);

@datenwolf:

[EDIT:] I've got the errors:

char const* VSSrc = VSSource.c_str();
glShaderSource(VS,1,&VSSrc,NULL); // It Should be VS ID instead of GL_VERTEX_SHADER
...
char const* FSSrc = FSSource.c_str();
glShaderSource(FS,1,&FSSrc,NULL); // It Should be FS ID instead of GL_FRAGMENT_SHADER

Problem in routine was using GL_VERTEX_SHADER and GL_FRAGMENT_SHADER as VS and FS IDs, those were undefined in program.

Upvotes: 0

Views: 658

Answers (1)

datenwolf
datenwolf

Reputation: 162164

This is a typical error happening, if glShaderSource is called without length information and the strings are not properly null terminated. My recommendation is to pass both length information and add padding null bytes to the end of the string.

Upvotes: 4

Related Questions