2013-09-26 66 views
5

我想在OpenGL 3.2中編譯一個着色器程序,但我得到一個奇怪的鏈接錯誤。OpenGL編譯着色器被破壞

創建頂點和片段着色器編譯和連接它們之後,我嘗試將它們連接成一個程序,但我得到以下信息日誌錯誤:

ERROR: Compiled vertex shader was corrupt. 
ERROR: Compiled fragment shader was corrupt. 

我完全不知道這意味着什麼,只我可以在谷歌上找到的東西就是忽略它。但是,當我glUseProgram()它得到一個無效的操作,所以我不能忽略這個錯誤。

此外,我剛剛更新到XCode 5和非常相同的代碼/着色器源工作。不知道怎麼可以,雖然有關..

編輯:着色器源

頂點:

#version 150 

in vec3 position; 

uniform mat4 worldMatrix; 
uniform float time; 

out vec3 outPos; 
void main(){ 
    gl_Position = worldMatrix*vec4(position, 1.0); 
    outPos = position; 
} 

片段:

#version 150 

out vec4 outColor; 
uniform float time; 
uniform float red; 
uniform float green; 
uniform float blue; 

void main(){ 
    outColor=vec4(red, green, blue,1.0); 
} 
+0

你登錄編譯錯誤太多了,你可以顯示着色器的代碼? – Appleshell

+0

我完全忘了放置着色器代碼。它正在編輯中。沒有編譯錯誤報告。 – zync

+1

您在頂點着色器中有out outP,但在片段着色器中沒有對應的in參數。從着色器代碼的外觀你可以完全刪除這個變量 - 無論如何你都不會使用它。 – keltar

回答

5

得到它的工作。

起初我用其他編輯器(文本伴侶)重寫了着色器,然後它有時會起作用。然後我確定它已正確空終止,並且每次都有效。

也許在某種程度上有像Andon M. Coleman建議的非打印字符。

3

我有同樣的問題,並發現,如果您使用'std :: stringstream緩衝區'來讀取文件,儘可能多的代碼示例在web上使用,方法.str()。c_str()來獲取glShaderSource需要一個* ptr,指針被刪除,這意味着,你會得到隨機的鏈接器錯誤。下面是我周圍創建的工作...

int shaderFromFile(const std::string& filePath, GLenum shaderType) { 
    //open file 
std::ifstream f; 
f.open(filePath.c_str(), std::ios::in); 
if(!f.is_open()){ 
    throw std::runtime_error(std::string("Failed to open file: ") + filePath); 
} 


//read whole file into stringstream buffer 
std::stringstream buffer; 
buffer << f.rdbuf(); 
buffer << "\0"; 
f.close(); 

//需要複製,當通話結束後 的std :: string shaderCode = buffer.str()c_str指針被刪除()。

//create new shader 
int ShaderID = glCreateShader(shaderType); 

//set the source code 

    const GLchar* code = (const GLchar *) shaderCode.c_str(); 

    glShaderSource(ShaderID, 1, &code, NULL); 
    //compile 
    glCompileShader(ShaderID); 


    //throw exception if compile error occurred 
    GLint status; 
    glGetShaderiv(ShaderID, GL_COMPILE_STATUS, &status); 
    std::cout << "Status from compile:" << status << "\r\n"; 
    if (status == GL_FALSE) { 
     std::string msg("Compile failure in shader:\n"); 

     GLint infoLogLength; 
     glGetShaderiv(ShaderID, GL_INFO_LOG_LENGTH, &infoLogLength); 
     char* strInfoLog = new char[infoLogLength + 1]; 
     glGetShaderInfoLog(ShaderID, infoLogLength, NULL, strInfoLog); 
     msg += strInfoLog; 
     delete[] strInfoLog; 

     glDeleteShader(ShaderID); ShaderID = 0; 
     throw std::runtime_error(msg); 
    } 

return ShaderID; 

}