OpenGL Shader Compilation Errors: unexpected $undefined at token "<undefined>"
Asked Answered
F

5

12

I saw this question and it really shedded some light. Despite this, I can't seem to figure out how I'm "improperly" loading my shader, because this has executed before without any recent changes to the shader loading code, so I assume these errors must be coming from my draw calls.

Despite this, I'll still post the shader files for the sake of brevity, the draw function used to draw the circle I'm trying to render, and the code which loads in the shader file as a string.

Basically what I need to know is why I'm getting these errors and what in the hell is wrong with them?

(From debug output)

ERROR { 
    OpenGL Says: 
     Vertex info
-----------
0(1) : error C0000: syntax error, unexpected $undefined at token "<undefined>"

Fragment info
-------------
0(1) : error C0000: syntax error, unexpected $undefined at token "<undefined>"

 };

Draw Code

    void Circle::draw( GLuint program )
    {
        const size_t centerMag = mCenter.length();

        glUseProgram( program );

        for( float t = 0; t < mCircumference; t += 0.1 )
        {
            float x = centerMag + glm::cos( 2 * M_PI * t );
            float y = centerMag + glm::sin( 2 * M_PI * t );

            mVertices->push_back( glm::vec4( x, y, 0, 1 ) );
        }

        QListIterator< glm::vec4 > iVertices( *mVertices );

        const size_t size = mVertices->size();

        float** verts = new float*[ size ];

        size_t i = 0;

        glEnableClientState( GL_VERTEX_ARRAY );

        while( iVertices.hasNext() )
        {
            verts[ i ] = new float[ size ];
            verts[ i ] = const_cast< float* >( glm::value_ptr( iVertices.next() ) );

            glVertexPointer( 4, GL_FLOAT, 0, verts[ i ] );

            glDrawArrays( GL_LINES, 0, 4 );

            ++i;
        }

        glDisableClientState( GL_VERTEX_ARRAY );

        for( unsigned iMem = 0; iMem < size; ++iMem )
        {
            delete[] verts[ iMem ];
            verts[ iMem ] = NULL;
        }

        delete[] verts;
        verts = NULL;
    }

FileUtility

    QString FileUtility::readShader( QString filepath )
    {
        std::ifstream in( filepath.toStdString().c_str() );
        std::stringstream shaderDat;

        shaderDat << in.rdbuf();

        QString shaderFile;

        shaderFile += shaderDat.str().c_str();

        in.close();

        return shaderFile;
    }

GenericColor.frag

#version 330

out vec4 outputColor;

void main()
{
    outputColor = vec4(1.0f, 0, 0, 1.0f);
}

Position.vert

#version 330

layout(location = 0) in vec4 position;

void main()
{
    gl_Position = position;
}

Update

Since my shader binding/compilation code was requested, I figured I may as well just post my entire shader handler, as well as the engine class.

-Engine -ShaderHandler.

Update

Here are the shader strings parsed (Info is a debug output):

Info { 
    Shader Source #version 330

in uniform mvp;

layout(location = 0) in vec4 position;

void main()
{
    gl_ModelViewProjectionMatrix = mvp;
    gl_Position = position;
}




 };



Info { 
    Shader Source #version 330

out vec4 outputColor;

void main()
{
    outputColor = vec4(1.0f, 0, 0, 1.0f);
}


 };
Feeler answered 4/6, 2012 at 6:53 Comment(7)
Could you also show the shader compilation/VA binding code ?Midwinter
Updated, I decided to pastebin the entire source file.Feeler
Any chance you could post the exact c_str's that are getting loaded to glShaderSource? Shader compilation errors shouldn't have anything to do with the rest of the program (your draw code there is likely irrelevant). Maybe could spot something that way.Settera
Yup, just updated. I am wondering if the problem is because I'm using Qt though for this...Feeler
Oh, Qt... The QGLWindow might not be the thing that support the GL3.1 core profile... Unfortunately. You'll have to hack a little to create a compatible context using pure WinAPI. You didn't tell about Qt, but this is rather a well-known issue with GL3Midwinter
I'm actually on Linux, but I figured I may as well just use this: opengl.org/wiki/Tutorial:_OpenGL_3.0_Context_Creation_(GLX)Feeler
Yes, the GLX is almost the same.Midwinter
S
23

That error message means that the shader compiler is seeing a garbage character (something other than a printable ASCII character, a space, a tab, or a newline) on the first line of the shader. Which most likely means that the string you're passing to glShaderSource is garbage -- probably a dangling pointer that once pointed at your shader code but no longer does due to something getting destructed.

edit

I see from your link you have code that looks like:

s.Source = shader.toStdString().c_str();

That will set s.Source pointing at the internal buffer of a temporary std::string object that will be destroyed shortly after this line, leaving s.Source a dangling pointer...

Seleta answered 4/6, 2012 at 22:50 Comment(3)
THANK YOU. God, sometimes I hate the fact that I came from a C# background.Feeler
Another case note: This happened with me too, but with missing \0 terminator at the end, so I'm adding this to your statement saying "on the first line of the shader".Deviationism
@Wappenull: A missing NUL will generally give you that error on the last line of the shader rather than the first. The number in parenthesis at the beginning of the error message is the line number (The number before the paren is the index in the array passed to glShaderSource)Seleta
M
2

My guess: I've seen the glLoadMatrix() calls in your code which basically means you're using an outdated (pre-3.1) GL API and do not initialize the GL3.1 Core Profile context correctly.

This leads to the situation where your context does not support the GLSL1.50+ and the "location" attributes (thus the error from shader compiler).

Try changing the initialization of GL and then check the glBindAttribLocation calls. Avoid using the glLoadMatrix stuff - use shader uniforms instead.

Look at opengl.org's site: http://www.opengl.org/wiki/Tutorial:_OpenGL_3.1_The_First_Triangle_(C%2B%2B/Win) for a GL 3.1 context creation sample. It is a little different from GL2.0-

Midwinter answered 4/6, 2012 at 7:30 Comment(0)
G
1

This is probably not related to your current problem, but will be one soon enough :

verts[ i ] = new float[ size ];
verts[ i ] = const_cast< float* >( glm::value_ptr( iVertices.next() ) );

The memory allocated in the first line is leaked, moreover, when you call delete a few lines after, you're deleting the value given by new but the casted one. Is that what you mean ?

Grimy answered 4/6, 2012 at 7:48 Comment(0)
F
1

I got this error because I cut, and paste some shader code from a website. I assume the difference in LF/CR was causing the problem. Removing the pasted text with the same code manually typed in worked.

Flugelhorn answered 10/12, 2014 at 18:27 Comment(0)
C
0

I was encountering this error in Standard C by forgetting to put the NUL(\0) terminator at the end of the malloc'd memory where I was reading the characters into.

I suspect GL looks for the null termination character in order to link the shaders during glLinkProgram()(glew).

If you don't pass the const GLint *length parameter to glLinkProgram() it assumes NUL termination. hence the error. Thanks to derhass for pointing this out.

Casmey answered 12/12, 2021 at 20:47 Comment(2)
"I suspect GL looks for the null termination character " Only if you tell it to. You either specify the lenght of each string part via the length parameter of glShaderSource, or you use negative lenghtes or NULL for the whole length array to tell itthat the strings are NUL-terminated.Referential
You're totally right! My brain completely glossed over the documentation for the length** param.Casmey

© 2022 - 2024 — McMap. All rights reserved.