2016-02-19 108 views
0

我已經開發了將Qt源代碼中的着色器源作爲const char *變量的Open GL代碼。這工作很好,編譯好。現在我想將着色器移動到他們自己的存儲爲qresources的文件中。當我使用QFile讀取着色器時,片段着色器編譯而不是頂點着色器。我似乎無法弄清楚爲什麼。我會很感激任何幫助 - 請參閱下面的代碼。Qt QFile和OpenGL ES 2.0頂點着色器意外的文件結尾

錯誤:

Vertex Shader failed to compile! 
Error in vertex shader compilation! 
Info log: Compile failed. 
ERROR: Unexpected end of source found 
ERROR: 1 compilation errors. No code generated. 


Fragment Shader successfully compiled! 
Linking was unsuccessful... 

工作編碼:

// Compile Shaders 
    const char* VertexShaderSource = "\ 
      attribute highp vec4 inVertex;\ 
      attribute mediump vec2 inTexCoord;\ 
      varying mediump vec2 TexCoord;\ 
      void main()\ 
      {\ 
       gl_Position = inVertex;\ 
       TexCoord = inTexCoord;\ 
      }"; 

    const char* FragmentShaderSource = "\ 
        #ifdef GL_IMG_texture_stream2\n \ 
        #extension GL_IMG_texture_stream2 : enable \n \ 
        #endif \n \ 
         varying mediump vec2 TexCoord; \ 
         uniform samplerStreamIMG sTexture; \ 
         uniform sampler2D table1; \ 
         uniform sampler2D table2; \ 
         uniform sampler2D table3; \ 
         uniform sampler2D palette; \ 
         void main(void) \ 
         { \ 
         highp vec4 texVal = textureStreamIMG(sTexture, TexCoord); \ 
         highp vec4 tb1Val = texture2D(table1, TexCoord); \ 
         highp vec4 tb2Val = texture2D(table2, TexCoord); \ 
         highp vec4 tb3Val = texture2D(table3, TexCoord); \ 
         highp float index = (texVal.g * 255.0) * 256.0 + texVal.b * 255.0; \ 
         highp float x = (mod(index,256.0))/256.0; \ 
         highp float y = (index/256.0)/256.0; \ 
         highp vec4 palValue = texture2D(palette, vec2(x, y)); \ 
         gl_FragColor = vec4(palValue.a,palValue.r,palValue.g,palValue.b); \ 
         }"; 

破碎的代碼:

​​

輸出:

Success opening Vertex Shader: true 
"attribute highp vec4 inVertex; 
attribute mediump vec2 inTexCoord; 
varying mediump vec2 TexCoord; 
void main() 
{ 
       gl_Position = inVertex; 
       TexCoord = inTexCoord; 
} 
" 
Success opening Fragment Shader: true 
"#ifdef GL_IMG_texture_stream2 
#extension GL_IMG_texture_stream2 : enable 
#endif 
varying mediump vec2 TexCoord; 
uniform samplerStreamIMG sTexture; 
uniform sampler2D table1; 
uniform sampler2D table2; 
uniform sampler2D table3; 
uniform sampler2D palette; 
void main(void) 
{ 
     highp vec4 texVal = textureStreamIMG(sTexture, TexCoord); 
     highp vec4 tb1Val = texture2D(table1, TexCoord); 
     highp vec4 tb2Val = texture2D(table2, TexCoord); 
     highp vec4 tb3Val = texture2D(table3, TexCoord); 
     highp float index = (texVal.g * 255.0) * 256.0 + texVal.b * 255.0; 
     highp float x = (mod(index,256.0))/256.0; 
     highp float y = (index/256.0)/256.0; 
     highp vec4 palValue = texture2D(palette, vec2(x, y)); 
     gl_FragColor = vec4(palValue.a,palValue.r,palValue.g,palValue.b); 
} 
" 
Vertex Shader failed to compile! 
Error in vertex shader compilation! 
Info log: Compile failed. 
ERROR: Unexpected end of source found 
ERROR: 1 compilation errors. No code generated. 


Fragment Shader successfully compiled! 
Linking was unsuccessful... 

回答

2

const char* FragmentShaderSource = fOut.toStdString().c_str();

這條線在這裏很容易觸發未定義的行爲 - 您正在從臨時文件(std::string返回的toStdString())中提取c_str(),該文件將在聲明結尾被銷燬......!


而是保持簡單。閱讀所有與該文件的內容:

const QByteArray fragmentShaderSource = fSh.readAll(); 

沒有使用循環,QTextStream,或其他奇怪的事情;然後通過

glShaderSource(fragmentShaderObject, 1, fragmentShaderSource.constData(), NULL); 
+0

將其內容上傳到着色器中感謝您的幫助。 – PhilBot

相關問題