C++ OpenGL 着色器编译错误:在标记“<undefined>”处出现意外的 $undefined

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10877386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:34:38  来源:igfitidea点击:

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

c++openglerror-handlingcompilationglsl

提问by zeboidlund

I saw thisquestion and it really shedded some light. Despite this, I can't seem to figure out how I'm "improperly" loading my shader, because this hasexecuted 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 (Infois 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);
}


 };

回答by Chris Dodd

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 glShaderSourceis garbage -- probably a dangling pointer that once pointed at your shader code but no longer does due to something getting destructed.

该错误消息意味着着色器编译器在着色器的第一行看到垃圾字符(可打印的 ASCII 字符、空格、制表符或换行符以外的其他字符)。这很可能意味着您传递给的字符串glShaderSource是垃圾——可能是一个悬空指针,它曾经指向您的着色器代码,但由于某些东西被破坏而不再指向。

edit

编辑

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

我从您的链接中看到您的代码如下所示:

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

That will set s.Sourcepointing at the internal buffer of a temporary std::stringobject that will be destroyed shortly after this line, leaving s.Sourcea dangling pointer...

这将设置s.Source指向一个临时std::string对象的内部缓冲区,该对象将在此行之后不久被销毁,留下s.Source一个悬空指针......

回答by Viktor Latypov

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+ 和“位置”属性(因此是着色器编译器的错误)。

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

尝试更改 GL 的初始化,然后检查glBindAttribLocation调用。避免使用 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. It is a little different from GL2.0-

查看 opengl.org 的站点:http: //www.opengl.org/wiki/Tutorial:_OpenGL_3.1_The_First_Triangle_(C%2B%2B/Win ) 以获得 GL 3.1 上下文创建示例。它与GL2.0有点不同-

回答by rotoglup

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 deletea few lines after, you're deleting the value given by newbut the casted one. Is that what you mean ?

第一行分配的内存泄漏了,而且,当您delete在几行之后调用时,您正在删除由new但铸造的给定的值。你是这个意思吗 ?

回答by Archon 808

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.

我收到此错误是因为我从网站上剪切并粘贴了一些着色器代码。我认为 LF/CR 的差异导致了问题。使用手动输入的相同代码删除粘贴的文本。