OpenGL着色器编译错误

OpenGL着色器编译错误

本文介绍了OpenGL着色器编译错误:意外$未定义在标记“<未定义>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到问题,它真的散发了一些光。尽管这样,我似乎不知道我是如何不当地加载我的着色器,因为这已经执行之前没有任何最近更改着色器加载代码,所以我认为这些错误必须来自我的绘制调用。



尽管如此,我仍然会发布着色器文件为了简洁,绘制函数用于绘制圆我尝试渲染,以及以一个字符串的形式加载着色器文件的代码。



基本上我需要知道的是为什么我得到这些错误,他们错了吗?



从调试输出

 错误{
OpenGL说:
顶点信息
-----------
0(1):错误C0000:语法错误,意外$ undefined在令牌<未定义>

碎片信息
-------------
0(1):错误C0000:语法错误,意外$ undefined在令牌< undefined>

};

绘制代码

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

glUseProgram(程序);

for(float t = 0; t {
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 

输出vec4 outputColor;

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

Position.vert 在vec4位置的
$ b

  #version 330 

布局(位置= 0)

void main()
{
gl_Position = position;
}

更新

因为我的shader绑定/编译代码被请求,我想我也可以发布我的整个着色器处理程序,以及引擎类。



-
-



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

  Info {
Shader Source #version 330

in uniform mvp;

布局(位置= 0)在vec4位置;

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




};



信息{
Shader Source #version 330

out vec4 outputColor;

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


};


解决方案

错误消息意味着着色器编译器看到乱码字符(除了可打印的ASCII字符,空格,制表符或换行符之外的其他字符)。这很可能意味着你传递给 glShaderSource 的字符串是垃圾 - 可能是一个悬空指针,一旦指向你的着色器代码,但不再是由于某些东西被破坏。


$ b


$ b

我从链接中看到你的代码看起来像:

  s.Source = shader.toStdString()。c_str 

这将设置 s.Source 临时 std :: string 对象的内部缓冲区,将在此行后立即销毁,留下 s.Source 悬挂指针...


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


 };
解决方案

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

这篇关于OpenGL着色器编译错误:意外$未定义在标记“&lt;未定义&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:08