OpenGL 着色器编译错误:在令牌“<undefined"处出现意外的 $undefined [英] OpenGL Shader Compilation Errors: unexpected $undefined at token &quot;&lt;undefined&gt;&quot;

查看:35
本文介绍了OpenGL 着色器编译错误:在令牌“<undefined"处出现意外的 $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?

(来自调试输出)

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>"

 };

绘制代码

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

文件实用程序

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

更新

由于我的着色器绑定/编译代码是被请求的,我想我最好只发布我的整个着色器处理程序以及引擎类.

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.

-引擎-ShaderHandler.

更新

这里是解析的着色器字符串(Info 是调试输出):

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


 };

推荐答案

该错误消息意味着着色器编译器在着色器的第一行.这很可能意味着您传递给 glShaderSource 的字符串是垃圾——可能是一个悬空指针,它曾经指向您的着色器代码,但由于某些东西被破坏而不再指向.

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.

编辑

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

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

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

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

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...

这篇关于OpenGL 着色器编译错误:在令牌“<undefined"处出现意外的 $undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆