简体   繁体   English

OpenGL 着色器编译错误:意外的 $undefined 在令牌“<undefined> ”

[英]OpenGL Shader Compilation Errors: unexpected $undefined at token "<undefined>"

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位置.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 . -引擎- ShaderHandler

Update更新

Here are the shader strings parsed ( Info is a debug output):以下是解析的着色器字符串( Info是调试输出):

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);
}


 };

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. 该错误消息表示着色器编译器在着色器的第一行上看到垃圾字符(除了可打印的ASCII字符,空格,制表符或换行符之外的其他字符)。 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. 这很可能意味着您传递给glShaderSource的字符串是垃圾 - 可能是一个悬挂指针,曾经指向您的着色器代码,但由于某些东西被破坏而不再存在。

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... 这将设置s.Source指向临时std::string对象的内部缓冲区,该对象将在此行之后不久销毁,使s.Source成为悬空指针...

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. 我的猜测:我已经看到你的代码中的glLoadMatrix()调用,这基本上意味着你正在使用过时的(3.1之前的)GL API,并且没有正确初始化GL3.1核心配置文件上下文。

This leads to the situation where your context does not support the GLSL1.50+ and the "location" attributes (thus the error from shader compiler). 这导致您的上下文不支持GLSL1.50 +和“location”属性(因此来自着色器编译器的错误)的情况。

Try changing the initialization of GL and then check the glBindAttribLocation calls. 尝试更改GL的初始化,然后检查glBindAttribLocation调用。 Avoid using the glLoadMatrix stuff - use shader uniforms instead. 避免使用glLoadMatrix的东西 - 改为使用着色器制服。

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. 查看opengl.org的网站: http//www.opengl.org/wiki/Tutorial :_OpenGL_3.1_The_First_Triangle_(C%2B%2B/Win),了解GL 3.1上下文创建示例。 It is a little different from GL2.0- 它与GL2.0-略有不同

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. 第一行中分配的内存被泄露,而且,当您在delete几行之后调用delete时,您将删除new给出的值但是已经转换的值。 Is that what you mean ? 你是这个意思吗 ?

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. 我认为LF / CR的差异导致了这个问题。 Removing the pasted text with the same code manually typed in worked. 使用手动输入的相同代码删除粘贴的文本。

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.我在标准 C 中遇到了这个错误,因为忘记将NUL ( \\0 ) 终止符放在malloc 'd 内存的末尾,我正在将字符读入其中。

I suspect GL looks for the null termination character in order to link the shaders during glLinkProgram() (glew).我怀疑 GL 寻找空终止字符以便在glLinkProgram() (glew) 期间链接着色器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM