2012-09-01 36 views
0

我見過其他類似的問題,我現在通常是由於着色器加載代碼不對,但我嘗試了很多不同的方法加載它們,我總是得到這個錯誤。這是我目前使用的加載代碼(頂點着色器,我做同樣的片段着色器):

GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER); 
std::string VertexShaderCode; 
std::ifstream VertexShaderStream(vertex_file_path, std::ios::in); 
if(VertexShaderStream.is_open()){ 
    std::string Line = ""; 
    while(getline(VertexShaderStream, Line)) 
     VertexShaderCode += "\n" + Line; 
    VertexShaderStream.close(); 
} 
GLint Result = GL_FALSE; 
int InfoLogLength; 


// Compile Vertex Shader 
printf("Compiling shader : %s\n", vertex_file_path); 
char const * VertexSourcePointer = VertexShaderCode.c_str(); 
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL); 
glCompileShader(VertexShaderID); 

// Check Vertex Shader 
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result); 
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); 
std::vector<char> VertexShaderErrorMessage(InfoLogLength); 
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]); 
fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]); 

我也試過這樣:

GLuint VertexShaderID = glCreateShader(GL_FRAGMENT_SHADER); 
    std::ifstream shaderFile(vertex_file_path); 
    //Error out here. 
    stringstream shaderData; 
    shaderData << shaderFile.rdbuf(); //Loads the entire string into a string stream. 
    shaderFile.close(); 
    const std::string &VertexShaderCode = shaderData.str(); //Get the string stream as a std::string. 
    GLint Result = GL_FALSE; 
    int InfoLogLength; 
    printf("Compiling shader : %s\n", vertex_file_path); 
    char const * VertexSourcePointer = VertexShaderCode.c_str(); 
    glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL); 
    glCompileShader(VertexShaderID); 

    // Check Vertex Shader 
    glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result); 
    glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); 
    std::vector<char> VertexShaderErrorMessage(InfoLogLength); 
    glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]); 
    fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]); 

這是鏈接代碼:

// Link the program 
fprintf(stdout, "Linking program\n"); 
GLuint ProgramID = glCreateProgram(); 
glAttachShader(ProgramID, VertexShaderID); 
glAttachShader(ProgramID, FragmentShaderID); 
glLinkProgram(ProgramID); 

// Check the program 
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result); 
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength); 
std::vector<char> ProgramErrorMessage(max(InfoLogLength, int(1))); 
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]); 
fprintf(stdout, "%s\n", &ProgramErrorMessage[0]); 

我也試圖通過(GLint *)VertexShaderCode.size()而不是NULL來glShaderSource()的長度參數,但我得到了同樣的錯誤。我得到這些錯誤都編譯和鏈接

萬一有幫助,我使用OpenGL 3.3,GLEW,GLFW時,Visual C++ 2010,和windows的時候,當7 64位

+0

如果您在程序結束時未指定右括號'}',則會發生這種情況:P – linello

回答

2

你是如何傳遞長度參數到glShaderSource。你執行的類型轉換會觸發你一個厚厚的警告。正確的做法是將size()的結果放入一個變量中。然後你將指針指向該變量glShaderSource。

+0

謝謝!我做錯了。但我現在糾正它現在保存到一個size_t變量,然後將該變量類型轉換爲(GLint *),並且我仍然得到相同的錯誤 – XaitormanX

+0

@XaitormanX:你爲什麼要強制轉換。你應該從中得到一個指針:'size_t sourcelength = ... size(); glShaderSource(...,1,&sourcestring,&sourcelength);' – datenwolf

+0

,因爲我得到這個錯誤:'不能將參數4從'size_t *'轉換爲'const GLint *'' – XaitormanX