2015-12-15 39 views
0

我一直沒有進入OpenGL很長一段時間,從來沒有用過OpenGLES。 對於我的項目我必須使用OpenGLES,由glGetString(GL_VERSION)打印的版本是3.0,但因爲我正在做一個教程拼貼是完全可能的,我混合代碼不能在這個版本(即這就是爲什麼#version標籤在着色器代碼上發表評論的原因,但它並沒有解決問題)。GLSL着色器編譯,但程序沒有鏈接

這是頂點着色器的代碼:

 const GLchar * vertex_shader_source = 
//    "#version 100\n                " 
//    "                   " 
       " attribute vec2 a_TexCoordinate; /* Per-vertex texture coordinate */  " 
       "         /* information we will pass in. */  " 
       " attribute vec2 coord2d;             " 
       "                   " 
       "                   " 
       " varying vec2 v_TexCoordinate; // This will be passed into the fragment " 
       "                   " 
       " void main(){                " 
       "                   " 
       "  gl_Position = vec4(coord2d, 0.0, 1.0);        " 
       "                   " 
       "  // Pass through the texture coordinate.        " 
       "  v_TexCoordinate = a_TexCoordinate;         " 
       " }                   "; 

     glShaderSource(vertex_shader_index, 1, &vertex_shader_source, nullptr); 
     glCompileShader(vertex_shader_index); 
     glGetShaderiv(vertex_shader_index, GL_COMPILE_STATUS, &compile_ok); 

而這正是片段着色器的代碼:

 const GLchar * fragment_shader_source = 
//    "#version 100\n              " 
//    "                 " 
       "uniform sampler2D u_Texture; // The input texture.    " 
       "                 " 
       "varying vec2 v_TexCoordinate; /* Interpolated texture */  " 
       "        /* coordinate per fragment */  " 
       "                 " 
       " void main(){              " 
       "                 " 
       "  // Output color = color of the texture at the specified UV " 
       "  gl_FragColor = texture2D(u_Texture, v_TexCoordinate);   " 
       " }                 "; 

     glShaderSource(fragment_shader_index, 1, &fragment_shader_source, nullptr); 
     glCompileShader(fragment_shader_index); 
     glGetShaderiv(fragment_shader_index, GL_COMPILE_STATUS, &compile_ok); 

這是程序的代碼:

m_program = glCreateProgram(); 
     glAttachShader(m_program, vertex_shader_index); 
     glAttachShader(m_program, fragment_shader_index); 
     glLinkProgram(m_program); 
     glGetProgramiv(m_program, GL_LINK_STATUS, &link_ok); 

變量link_ok爲假,而變量compile_ok均爲爲真。從程序的鏈接擴展的錯誤說:

(0)頂點信息:C3001沒有程序規定 (0)片段信息:C3001沒有程序規定

回答

4

您個人的着色器編譯就好了。您所獲得的特定錯誤代碼意味着在完整程序中不能找到主要功能。 有充分的理由 - 你評論他們。你的着色器字符串不包含任何新行,所以整個着色器在一行上。

例如:

const GLchar * var = "uniform value; // a value" 
        "main() {}    "; 

實際上就是

const GLchar * var = "uniform value; // a valuemain() {}"; 

任何評論與//註釋出該行的其餘部分做了 - 你的着色器,因此休息。刪除//註釋,用/ ** /替換它們或在每行的末尾添加\ n,它對我來說工作得很好。

此外,它可能是安全的null結束你的字符串\ 0。

+0

好的,也許這可以解決!現在由於「未定義的浮點變量精度」行125,片段不編譯,但着色器短於125行......你能解釋哪個浮點是指的錯誤? – nyarlathotep108

+2

@ nyarlathotep108如果您使用OpenGL ES,則必須指定浮點精度。在片段着色器的頂部添加像這樣的'precision mediump float;'。看看這個:http://stackoverflow.com/questions/13780609/what-does-precision-mediump-float-mean – Reigertje