![](/img/trans.png)
[英]Understanding compilation order, compilation errors, and luabind due to base class undefined
[英]OpenGL Shader Compilation Errors: unexpected $undefined at token "<undefined>"
我看到了這個問題,它確實闡明了一些問題。 盡管如此,我似乎無法弄清楚我是如何“不正確地”加載我的着色器的,因為這之前已經執行過,而最近沒有對着色器加載代碼進行任何更改,所以我認為這些錯誤一定來自我的繪制調用。
盡管如此,為了簡潔起見,我仍將發布着色器文件、用於繪制我嘗試渲染的圓的繪制函數以及作為字符串加載到着色器文件中的代碼。
基本上我需要知道的是為什么我會收到這些錯誤以及它們到底有什么問題?
(來自調試輸出)
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;
}
通用顏色文件
#version 330
out vec4 outputColor;
void main()
{
outputColor = vec4(1.0f, 0, 0, 1.0f);
}
位置.vert
#version 330
layout(location = 0) in vec4 position;
void main()
{
gl_Position = position;
}
更新
由於我的着色器綁定/編譯代碼是被請求的,我想我最好只發布我的整個着色器處理程序以及引擎類。
-引擎- ShaderHandler 。
更新
以下是解析的着色器字符串( 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);
}
};
該錯誤消息表示着色器編譯器在着色器的第一行上看到垃圾字符(除了可打印的ASCII字符,空格,制表符或換行符之外的其他字符)。 這很可能意味着您傳遞給glShaderSource
的字符串是垃圾 - 可能是一個懸掛指針,曾經指向您的着色器代碼,但由於某些東西被破壞而不再存在。
編輯
我從你的鏈接看到你的代碼看起來像:
s.Source = shader.toStdString().c_str();
這將設置s.Source
指向臨時std::string
對象的內部緩沖區,該對象將在此行之后不久銷毀,使s.Source
成為懸空指針...
我的猜測:我已經看到你的代碼中的glLoadMatrix()調用,這基本上意味着你正在使用過時的(3.1之前的)GL API,並且沒有正確初始化GL3.1核心配置文件上下文。
這導致您的上下文不支持GLSL1.50 +和“location”屬性(因此來自着色器編譯器的錯誤)的情況。
嘗試更改GL的初始化,然后檢查glBindAttribLocation調用。 避免使用glLoadMatrix的東西 - 改為使用着色器制服。
查看opengl.org的網站: http : //www.opengl.org/wiki/Tutorial :_OpenGL_3.1_The_First_Triangle_(C%2B%2B/Win),了解GL 3.1上下文創建示例。 它與GL2.0-略有不同
這可能與您當前的問題無關,但很快就會出現問題:
verts[ i ] = new float[ size ];
verts[ i ] = const_cast< float* >( glm::value_ptr( iVertices.next() ) );
第一行中分配的內存被泄露,而且,當您在delete
幾行之后調用delete
時,您將刪除new
給出的值但是已經轉換的值。 你是這個意思嗎 ?
我收到此錯誤是因為我剪切了,並從網站粘貼了一些着色器代碼。 我認為LF / CR的差異導致了這個問題。 使用手動輸入的相同代碼刪除粘貼的文本。
我在標准 C 中遇到了這個錯誤,因為忘記將NUL
( \\0
) 終止符放在malloc
'd 內存的末尾,我正在將字符讀入其中。
我懷疑 GL 尋找空終止字符以便在glLinkProgram()
(glew) 期間鏈接着色器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.